qelectrotech-source-mirror/sources/qtextorientationwidget.cpp

375 lines
11 KiB
C++
Raw Normal View History

/*
2024-03-29 10:09:48 +01:00
Copyright 2006-2024 The QElectroTech Team
This file is part of QElectroTech.
2020-10-03 15:45:43 +02:00
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
2020-10-03 15:45:43 +02:00
QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2020-10-03 15:45:43 +02:00
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "qtextorientationwidget.h"
#include <algorithm>
2020-10-03 15:45:43 +02:00
#include <QMouseEvent>
/**
Constructeur
Par defaut, ce widget met en valeur les angles multiples de 45 degres
et presente un texte oriente a 0 degre, avec la police par defaut de
2020-10-03 15:45:43 +02:00
l'application. Le texte affiche est
@param parent Widget parent
*/
QTextOrientationWidget::QTextOrientationWidget(QWidget *parent) :
QWidget(parent),
squares_interval_(45.0),
current_orientation_(0.0),
display_text_(true),
must_highlight_angle_(false),
read_only_(false)
{
// chaines par defaut
text_size_hash_.insert(tr("Ex.", "Short example string"), -1);
text_size_hash_.insert(tr("Example", "Longer example string"), -1);
2020-10-03 15:45:43 +02:00
// definit la politique de gestion de la taille de ce widget :
// on prefere la sizeHint()
QSizePolicy size_policy(QSizePolicy::Minimum, QSizePolicy::Minimum);
// on souhaite conserver le rapport entre sa hauteur et sa largeur
size_policy.setHeightForWidth(true);
setSizePolicy(size_policy);
2020-10-03 15:45:43 +02:00
// suivi de la souris : permet de recevoir les evenements relatifs aux
// mouvement de la souris sans que l'utilisateur n'ait a cliquer
setMouseTracking(true);
}
/**
Destructeur
*/
2020-09-07 22:03:40 +02:00
QTextOrientationWidget::~QTextOrientationWidget()
{
}
/**
@param angle la nouvelle orientation / le nouvel angle selectionne(e)
0 degre correspond a un texte horizontal, de gauche a droite
90 degres correspondent a un texte vertical de haut en bas
*/
void QTextOrientationWidget::setOrientation(const double &angle) {
current_orientation_ = angle;
update();
}
/**
@return l'orientation / l'angle actuellement selectionne(e)
0 degre correspond a un texte horizontal, de gauche a droite
90 degres correspondent a un texte vertical de haut en bas
*/
2020-09-07 22:03:40 +02:00
double QTextOrientationWidget::orientation() const
{
return(current_orientation_);
}
/**
Definit la police de caracteres a utiliser pour le texte affiche
@param font Une police de caracteres
*/
void QTextOrientationWidget::setFont(const QFont &font) {
text_font_ = font;
2020-10-03 15:45:43 +02:00
// invalide le cache contenant les longueurs des textes a disposition
foreach(QString text, text_size_hash_.keys()) {
text_size_hash_[text] = -1;
}
}
/**
@return la police utilisee pour le texte affiche
*/
2020-09-07 22:03:40 +02:00
QFont QTextOrientationWidget::font() const
{
return(text_font_);
}
/**
@param display_text true pour afficher un texte, false sinon
*/
void QTextOrientationWidget::setDisplayText(bool display_text) {
display_text_ = display_text;
}
/**
@return la police utilisee pour le texte affiche
*/
2020-09-07 22:03:40 +02:00
bool QTextOrientationWidget::textDisplayed() const
{
return(display_text_);
}
/**
@param texts_list Une liste de chaines de caracteres utilisables par le
widget afin d'afficher un texte en guise d'exemple. Le widget choisit la
chaine la plus appropriee en fonction de sa taille.
Note : la liste fournie ne doit pas etre vide. Utilisez setDisplayText si
vous ne voulez plus afficher de texte.
*/
void QTextOrientationWidget::setUsableTexts(const QStringList &texts_list) {
if (texts_list.isEmpty()) return;
2020-10-03 15:45:43 +02:00
// on oublie les anciennes chaines
foreach(QString text, text_size_hash_.keys()) {
// il faut oublier les anciennes chaines
if (!texts_list.contains(text)) {
text_size_hash_.remove(text);
}
}
2020-10-03 15:45:43 +02:00
// on ajoute les nouvelles, sans les calculer (on met -1 en guise de longueur)
foreach(QString text, texts_list) {
if (!text_size_hash_.contains(text)) {
text_size_hash_[text] = -1;
}
}
}
/**
@return la liste des chaines dont le widget dispose pour afficher un texte
*/
2020-09-07 22:03:40 +02:00
QStringList QTextOrientationWidget::usableTexts() const
{
return(text_size_hash_.keys());
}
/**
@return true si le widget est en mode "lecture seule", false sinon
*/
2020-09-07 22:03:40 +02:00
bool QTextOrientationWidget::isReadOnly() const
{
return(read_only_);
}
/**
@param ro true pour passer le widget en mode "lecture seule", false sinon
*/
void QTextOrientationWidget::setReadOnly(bool ro) {
read_only_ = ro;
}
/**
@return la taille recommandee pour ce widget
*/
2020-09-07 22:03:40 +02:00
QSize QTextOrientationWidget::sizeHint() const
{
return(QSize(50, 50));
}
/**
@param w une largeur donnee
@return la hauteur preferee pour une largeur donnee
Pour ce widget : retourne la largeur fournie afin de maintenir le widget carre
*/
2020-09-07 22:03:40 +02:00
int QTextOrientationWidget::heightForWidth(int w) const
{
return(w);
}
/**
Effectue le rendu du widget
@param event Evenement decrivant la demande de rendu du widget
*/
void QTextOrientationWidget::paintEvent(QPaintEvent *event) {
Q_UNUSED(event);
2020-10-03 15:45:43 +02:00
// rectangle de travail avec son centre et son rayon
QRect drawing_rectangle(QPoint(0, 0), size());
drawing_rectangle.adjust(5, 5, -5, -5);
2020-10-03 15:45:43 +02:00
QPointF drawing_rectangle_center(drawing_rectangle.center());
qreal drawing_rectangle_radius = drawing_rectangle.width() / 2.0;
2020-10-03 15:45:43 +02:00
QPainter p;
p.begin(this);
2020-10-03 15:45:43 +02:00
p.setRenderHint(QPainter::Antialiasing, true);
p.setRenderHint(QPainter::TextAntialiasing, true);
2020-10-03 15:45:43 +02:00
// cercle gris a fond jaune
p.setPen(QPen(QBrush(QColor("#9FA8A8")), 2.0));
p.setBrush(QBrush(QColor("#ffffaa")));
p.drawEllipse(drawing_rectangle);
2020-10-03 15:45:43 +02:00
// ligne rouge indiquant l'angle actuel
p.setPen(QPen(QBrush(Qt::red), 1.0));
p.translate(drawing_rectangle_center);
p.rotate(current_orientation_);
p.drawLine(QLineF(QPointF(), QPointF(drawing_rectangle_radius, 0.0)));
2020-10-03 15:45:43 +02:00
// texte optionnel
if (display_text_) {
// determine le texte a afficher
QString chosen_text = getMostUsableStringForRadius(drawing_rectangle_radius);
if (!chosen_text.isEmpty()) {
p.resetTransform();
p.setPen(Qt::black);
p.setFont(text_font_);
p.translate(drawing_rectangle_center);
p.rotate(current_orientation_);
p.drawText(QPoint(), chosen_text);
}
}
2020-10-03 15:45:43 +02:00
// carres verts a fond vert
qreal squares_size = size().width() / 15.0;
qreal square_offset = - squares_size / 2.0;
QRectF square_qrect = QRect(square_offset, square_offset, squares_size, squares_size);
p.setPen(Qt::NoPen);
p.setBrush(QBrush(QColor("#248A34")));
for (double drawing_angle = 0.0 ; drawing_angle < 360.0 ; drawing_angle += squares_interval_) {
if (must_highlight_angle_ && highlight_angle_ == drawing_angle && underMouse()) {
p.setBrush(QBrush(QColor("#43FF5F")));
}
p.resetTransform();
p.translate(drawing_rectangle_center);
p.rotate(drawing_angle);
p.translate(drawing_rectangle_radius - 1.0, 0.0);
p.rotate(-45.0);
p.drawRect(square_qrect);
if (must_highlight_angle_ && highlight_angle_ == drawing_angle) {
p.setBrush(QBrush(QColor("#248A34")));
}
}
2020-10-03 15:45:43 +02:00
p.end();
}
/**
Gere les mouvements de la souris sur ce widget
@param event Evenement decrivant le mouvement de la souris
*/
void QTextOrientationWidget::mouseMoveEvent(QMouseEvent *event) {
if (read_only_) return;
2020-10-03 15:45:43 +02:00
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) // ### Qt 6: remove
Merge Qt5 branch sources folder to trunk -Cette ligne, et les suivantes ci-dessous, seront ignorées-- M sources/aboutqet.cpp M sources/bordertitleblock.cpp M sources/conductorproperties.h M sources/configdialog.cpp M sources/configpages.cpp M sources/configpages.h M sources/createdxf.h M sources/diagram.cpp M sources/diagram.h M sources/diagramcommands.cpp M sources/diagramcommands.h M sources/diagramprintdialog.cpp M sources/diagramprintdialog.h M sources/diagramschooser.cpp M sources/diagramschooser.h M sources/diagramview.cpp M sources/diagramview.h M sources/dvevent/dveventaddimage.cpp M sources/dvevent/dveventaddshape.cpp M sources/editor/arceditor.cpp M sources/editor/arceditor.h M sources/editor/editorcommands.cpp M sources/editor/editorcommands.h M sources/editor/elementitemeditor.h M sources/editor/elementprimitivedecorator.cpp M sources/editor/elementscene.cpp M sources/editor/elementscene.h M sources/editor/elementview.cpp M sources/editor/ellipseeditor.cpp M sources/editor/ellipseeditor.h M sources/editor/esevent/eseventaddtext.cpp M sources/editor/esevent/eseventaddtextfield.cpp M sources/editor/esevent/eseventinterface.cpp M sources/editor/graphicspart/customelementpart.h M sources/editor/graphicspart/parttext.cpp M sources/editor/graphicspart/parttext.h M sources/editor/graphicspart/parttextfield.cpp M sources/editor/graphicspart/parttextfield.h M sources/editor/lineeditor.cpp M sources/editor/lineeditor.h M sources/editor/polygoneditor.cpp M sources/editor/qetelementeditor.cpp M sources/editor/qetelementeditor.h M sources/editor/rectangleeditor.cpp M sources/editor/rectangleeditor.h M sources/editor/styleeditor.cpp M sources/editor/styleeditor.h M sources/editor/terminaleditor.cpp M sources/editor/terminaleditor.h M sources/editor/texteditor.cpp M sources/editor/texteditor.h M sources/editor/textfieldeditor.cpp M sources/editor/textfieldeditor.h M sources/editor/ui/elementpropertieseditorwidget.cpp M sources/elementdefinition.cpp M sources/elementdeleter.cpp M sources/elementdeleter.h M sources/elementdialog.cpp M sources/elementscategorieslist.h M sources/elementscategorieswidget.cpp M sources/elementscategorieswidget.h M sources/elementscategory.cpp M sources/elementscategorydeleter.cpp M sources/elementscategorydeleter.h M sources/elementscategoryeditor.cpp M sources/elementscategoryeditor.h M sources/elementscollection.cpp M sources/elementscollectioncache.cpp M sources/elementspanel.cpp M sources/elementspanel.h M sources/elementspanelwidget.cpp M sources/elementspanelwidget.h M sources/elementtextsmover.h M sources/exportdialog.cpp M sources/exportdialog.h M sources/exportproperties.cpp M sources/exportpropertieswidget.cpp M sources/exportpropertieswidget.h M sources/genericpanel.cpp M sources/integrationmoveelementshandler.cpp M sources/integrationmoveelementshandler.h M sources/interactivemoveelementshandler.cpp M sources/nameslistwidget.cpp M sources/nameslistwidget.h M sources/newelementwizard.cpp M sources/newelementwizard.h M sources/nomenclature.cpp M sources/nomenclature.h M sources/projectconfigpages.cpp M sources/projectview.cpp M sources/projectview.h M sources/qet.cpp M sources/qetapp.cpp M sources/qetapp.h M sources/qetdiagrameditor.cpp M sources/qetdiagrameditor.h M sources/qetgraphicsitem/conductor.cpp M sources/qetgraphicsitem/conductortextitem.cpp M sources/qetgraphicsitem/customelement.cpp M sources/qetgraphicsitem/diagramimageitem.cpp M sources/qetgraphicsitem/diagramtextitem.cpp M sources/qetgraphicsitem/diagramtextitem.h M sources/qetgraphicsitem/element.cpp M sources/qetgraphicsitem/ghostelement.cpp M sources/qetgraphicsitem/qetshapeitem.cpp M sources/qetgraphicsitem/terminal.cpp M sources/qetgraphicsitem/terminal.h M sources/qeticons.cpp M sources/qeticons.h M sources/qetmainwindow.cpp M sources/qetmessagebox.cpp M sources/qetmessagebox.h M sources/qetprintpreviewdialog.cpp M sources/qetprintpreviewdialog.h M sources/qetproject.cpp M sources/qetsingleapplication.cpp M sources/qettabbar.h M sources/qfilenameedit.cpp M sources/qtextorientationspinboxwidget.cpp M sources/qtextorientationspinboxwidget.h M sources/qtextorientationwidget.cpp M sources/qtextorientationwidget.h M sources/richtext/richtexteditor.cpp M sources/richtext/richtexteditor_p.h M sources/richtext/ui_addlinkdialog.h M sources/titleblock/dimensionwidget.h M sources/titleblock/gridlayoutanimation.h M sources/titleblock/helpercell.h M sources/titleblock/integrationmovetemplateshandler.cpp M sources/titleblock/integrationmovetemplateshandler.h M sources/titleblock/qettemplateeditor.cpp M sources/titleblock/qettemplateeditor.h M sources/titleblock/templatecellsset.h M sources/titleblock/templatecellwidget.cpp M sources/titleblock/templatecellwidget.h M sources/titleblock/templatecommands.cpp M sources/titleblock/templatedeleter.cpp M sources/titleblock/templatedeleter.h M sources/titleblock/templatelocationchooser.cpp M sources/titleblock/templatelocationchooser.h M sources/titleblock/templatelocationsaver.cpp M sources/titleblock/templatelocationsaver.h M sources/titleblock/templatelogomanager.cpp M sources/titleblock/templatelogomanager.h M sources/titleblock/templateview.cpp M sources/titleblock/templatevisualcell.h M sources/titleblockcell.cpp M sources/titleblocktemplate.cpp M sources/treecoloranimation.h M sources/ui/conductorpropertieswidget.cpp M sources/ui/diagrampropertiesdialog.cpp M sources/ui/diagramselection.cpp M sources/ui/dialogautonum.cpp M sources/ui/dialogwaiting.cpp M sources/ui/elementpropertieswidget.cpp M sources/ui/elementselectorwidget.cpp M sources/ui/linksingleelementwidget.cpp M sources/ui/masterpropertieswidget.cpp M sources/ui/potentialtextsdialog.cpp M sources/ui/projectpropertiesdialog.cpp M sources/ui/selectautonumw.cpp M sources/ui/titleblockpropertieswidget.cpp M sources/ui/xrefpropertieswidget.cpp M sources/undocommand/changeelementinformationcommand.cpp git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3783 bfdf4180-ca20-0410-9c96-a3a8aa849046
2015-03-02 20:14:56 +00:00
bool drawn_angle_hovered = positionIsASquare(event -> localPos(), &highlight_angle_);
2020-10-03 15:45:43 +02:00
#else
#if TODO_LIST
#pragma message("@TODO remove code for QT 6 or later")
#endif
bool drawn_angle_hovered = positionIsASquare(event -> position(), &highlight_angle_);
#endif
if (must_highlight_angle_ != drawn_angle_hovered) {
must_highlight_angle_ = drawn_angle_hovered;
update();
}
}
/**
Gere les relachements de la souris sur ce widget
@param event Evenement decrivant le relachement de la souris
*/
void QTextOrientationWidget::mouseReleaseEvent(QMouseEvent *event) {
if (read_only_) return;
2020-10-03 15:45:43 +02:00
double clicked_angle;
2020-10-03 15:45:43 +02:00
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) // ### Qt 6: remove
Merge Qt5 branch sources folder to trunk -Cette ligne, et les suivantes ci-dessous, seront ignorées-- M sources/aboutqet.cpp M sources/bordertitleblock.cpp M sources/conductorproperties.h M sources/configdialog.cpp M sources/configpages.cpp M sources/configpages.h M sources/createdxf.h M sources/diagram.cpp M sources/diagram.h M sources/diagramcommands.cpp M sources/diagramcommands.h M sources/diagramprintdialog.cpp M sources/diagramprintdialog.h M sources/diagramschooser.cpp M sources/diagramschooser.h M sources/diagramview.cpp M sources/diagramview.h M sources/dvevent/dveventaddimage.cpp M sources/dvevent/dveventaddshape.cpp M sources/editor/arceditor.cpp M sources/editor/arceditor.h M sources/editor/editorcommands.cpp M sources/editor/editorcommands.h M sources/editor/elementitemeditor.h M sources/editor/elementprimitivedecorator.cpp M sources/editor/elementscene.cpp M sources/editor/elementscene.h M sources/editor/elementview.cpp M sources/editor/ellipseeditor.cpp M sources/editor/ellipseeditor.h M sources/editor/esevent/eseventaddtext.cpp M sources/editor/esevent/eseventaddtextfield.cpp M sources/editor/esevent/eseventinterface.cpp M sources/editor/graphicspart/customelementpart.h M sources/editor/graphicspart/parttext.cpp M sources/editor/graphicspart/parttext.h M sources/editor/graphicspart/parttextfield.cpp M sources/editor/graphicspart/parttextfield.h M sources/editor/lineeditor.cpp M sources/editor/lineeditor.h M sources/editor/polygoneditor.cpp M sources/editor/qetelementeditor.cpp M sources/editor/qetelementeditor.h M sources/editor/rectangleeditor.cpp M sources/editor/rectangleeditor.h M sources/editor/styleeditor.cpp M sources/editor/styleeditor.h M sources/editor/terminaleditor.cpp M sources/editor/terminaleditor.h M sources/editor/texteditor.cpp M sources/editor/texteditor.h M sources/editor/textfieldeditor.cpp M sources/editor/textfieldeditor.h M sources/editor/ui/elementpropertieseditorwidget.cpp M sources/elementdefinition.cpp M sources/elementdeleter.cpp M sources/elementdeleter.h M sources/elementdialog.cpp M sources/elementscategorieslist.h M sources/elementscategorieswidget.cpp M sources/elementscategorieswidget.h M sources/elementscategory.cpp M sources/elementscategorydeleter.cpp M sources/elementscategorydeleter.h M sources/elementscategoryeditor.cpp M sources/elementscategoryeditor.h M sources/elementscollection.cpp M sources/elementscollectioncache.cpp M sources/elementspanel.cpp M sources/elementspanel.h M sources/elementspanelwidget.cpp M sources/elementspanelwidget.h M sources/elementtextsmover.h M sources/exportdialog.cpp M sources/exportdialog.h M sources/exportproperties.cpp M sources/exportpropertieswidget.cpp M sources/exportpropertieswidget.h M sources/genericpanel.cpp M sources/integrationmoveelementshandler.cpp M sources/integrationmoveelementshandler.h M sources/interactivemoveelementshandler.cpp M sources/nameslistwidget.cpp M sources/nameslistwidget.h M sources/newelementwizard.cpp M sources/newelementwizard.h M sources/nomenclature.cpp M sources/nomenclature.h M sources/projectconfigpages.cpp M sources/projectview.cpp M sources/projectview.h M sources/qet.cpp M sources/qetapp.cpp M sources/qetapp.h M sources/qetdiagrameditor.cpp M sources/qetdiagrameditor.h M sources/qetgraphicsitem/conductor.cpp M sources/qetgraphicsitem/conductortextitem.cpp M sources/qetgraphicsitem/customelement.cpp M sources/qetgraphicsitem/diagramimageitem.cpp M sources/qetgraphicsitem/diagramtextitem.cpp M sources/qetgraphicsitem/diagramtextitem.h M sources/qetgraphicsitem/element.cpp M sources/qetgraphicsitem/ghostelement.cpp M sources/qetgraphicsitem/qetshapeitem.cpp M sources/qetgraphicsitem/terminal.cpp M sources/qetgraphicsitem/terminal.h M sources/qeticons.cpp M sources/qeticons.h M sources/qetmainwindow.cpp M sources/qetmessagebox.cpp M sources/qetmessagebox.h M sources/qetprintpreviewdialog.cpp M sources/qetprintpreviewdialog.h M sources/qetproject.cpp M sources/qetsingleapplication.cpp M sources/qettabbar.h M sources/qfilenameedit.cpp M sources/qtextorientationspinboxwidget.cpp M sources/qtextorientationspinboxwidget.h M sources/qtextorientationwidget.cpp M sources/qtextorientationwidget.h M sources/richtext/richtexteditor.cpp M sources/richtext/richtexteditor_p.h M sources/richtext/ui_addlinkdialog.h M sources/titleblock/dimensionwidget.h M sources/titleblock/gridlayoutanimation.h M sources/titleblock/helpercell.h M sources/titleblock/integrationmovetemplateshandler.cpp M sources/titleblock/integrationmovetemplateshandler.h M sources/titleblock/qettemplateeditor.cpp M sources/titleblock/qettemplateeditor.h M sources/titleblock/templatecellsset.h M sources/titleblock/templatecellwidget.cpp M sources/titleblock/templatecellwidget.h M sources/titleblock/templatecommands.cpp M sources/titleblock/templatedeleter.cpp M sources/titleblock/templatedeleter.h M sources/titleblock/templatelocationchooser.cpp M sources/titleblock/templatelocationchooser.h M sources/titleblock/templatelocationsaver.cpp M sources/titleblock/templatelocationsaver.h M sources/titleblock/templatelogomanager.cpp M sources/titleblock/templatelogomanager.h M sources/titleblock/templateview.cpp M sources/titleblock/templatevisualcell.h M sources/titleblockcell.cpp M sources/titleblocktemplate.cpp M sources/treecoloranimation.h M sources/ui/conductorpropertieswidget.cpp M sources/ui/diagrampropertiesdialog.cpp M sources/ui/diagramselection.cpp M sources/ui/dialogautonum.cpp M sources/ui/dialogwaiting.cpp M sources/ui/elementpropertieswidget.cpp M sources/ui/elementselectorwidget.cpp M sources/ui/linksingleelementwidget.cpp M sources/ui/masterpropertieswidget.cpp M sources/ui/potentialtextsdialog.cpp M sources/ui/projectpropertiesdialog.cpp M sources/ui/selectautonumw.cpp M sources/ui/titleblockpropertieswidget.cpp M sources/ui/xrefpropertieswidget.cpp M sources/undocommand/changeelementinformationcommand.cpp git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3783 bfdf4180-ca20-0410-9c96-a3a8aa849046
2015-03-02 20:14:56 +00:00
bool drawn_angle_clicked = positionIsASquare(event -> localPos(), &clicked_angle);
2020-10-03 15:45:43 +02:00
#else
#if TODO_LIST
#pragma message("@TODO remove code for QT 6 or later")
#endif
bool drawn_angle_clicked = positionIsASquare(event -> position(), &clicked_angle);
#endif
if (drawn_angle_clicked) {
setOrientation(clicked_angle);
emit(orientationChanged(clicked_angle));
must_highlight_angle_ = false;
update();
}
}
/**
@param radius Rayon du cercle qui limitera le rendu du texte
@return la chaine la plus appropriee en fonction de la taille du widget.
*/
QString QTextOrientationWidget::getMostUsableStringForRadius(const qreal &radius) {
// s'assure que l'on connait la longueur de chaque texte a disposition
generateTextSizeHash();
2020-10-03 15:45:43 +02:00
// recupere les longueurs a disposition
QList<qreal> available_lengths = text_size_hash_.values();
// trie les longueurs par ordre croissant
std::sort(available_lengths.begin(), available_lengths.end());
// recherche la position ou l'on insererait le rayon
QList<qreal>::const_iterator i = std::upper_bound(available_lengths.begin(),
available_lengths.end(),
radius);
2020-10-03 15:45:43 +02:00
// la valeur precedent cette position est donc celle qui nous interesse
if (i == available_lengths.begin()) {
// nous sommes au debut de la liste - nous ne pouvons donc pas afficher de chaine
return(QString());
} else {
-- i;
qreal final_length = *i;
QString final_string = text_size_hash_.keys(final_length).first();
return(final_string);
}
}
/**
S'assure que le hash associant les textes utilisables a leur taille soit
correctement rempli.
*/
2020-09-07 22:03:40 +02:00
void QTextOrientationWidget::generateTextSizeHash()
{
QFontMetrics font_metrics(text_font_);
foreach(QString text, text_size_hash_.keys()) {
if (text_size_hash_[text] == -1) {
text_size_hash_[text] = font_metrics.boundingRect(text).width();
}
}
}
/**
Determine si une position donnee correspond a un des carres representant un
angle pertinent.
@param pos Position donnee
@param angle_value_ptr Si different de 0, le double pointe par ce parametre
vaudra l'angle pertinent concerne
*/
bool QTextOrientationWidget::positionIsASquare(const QPointF &pos, double *angle_value_ptr) {
// rectangle de travail avec son centre et son rayon
QRect drawing_rectangle(QPoint(0, 0), size());
drawing_rectangle.adjust(5, 5, -5, -5);
2020-10-03 15:45:43 +02:00
QPointF drawing_rectangle_center(drawing_rectangle.center());
qreal drawing_rectangle_radius = drawing_rectangle.width() / 2.0;
2020-10-03 15:45:43 +02:00
qreal squares_size = size().width() / 15.0;
qreal square_offset = - squares_size / 2.0;
QRectF square_qrect = QRect(square_offset, square_offset, squares_size, squares_size);
2020-10-03 15:45:43 +02:00
for (double drawing_angle = 0.0 ; drawing_angle < 360.0 ; drawing_angle += squares_interval_) {
QTransform transform = QTransform()
.translate(drawing_rectangle_center.x(), drawing_rectangle_center.y())
.rotate(drawing_angle)
.translate(drawing_rectangle_radius - 1.0, 0.0)
.rotate(-45.0);
2020-10-03 15:45:43 +02:00
QRectF mapped_rectangle = transform.mapRect(square_qrect);
if (mapped_rectangle.contains(pos)) {
if (angle_value_ptr) *angle_value_ptr = drawing_angle;
return(true);
}
}
2020-10-03 15:45:43 +02:00
return(false);
}