From b91b418c95e75bce5df67dc43a2c0611eddce1ec Mon Sep 17 00:00:00 2001 From: xavierqet Date: Wed, 10 Oct 2007 22:35:32 +0000 Subject: [PATCH] Correction d'un bug dans la modification des conducteurs apres un deplacement d'elements Implementation partielle de l'alignement des conducteurs sur la grille. Lors de la modification manuelle des conducteurs, les segments se fixent sur la grille. L'ancien comportement peut etre obtenu en maintenant la touche Shift enfoncee. Optimisation des fonctions "Selectionner tout" et "Deselectionner tout" Remplacement des #define par des attributs static const git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@168 bfdf4180-ca20-0410-9c96-a3a8aa849046 --- conductor.cpp | 7 ++++++- diagram.cpp | 40 ++++++++++++++++++++++------------------ diagram.h | 9 ++++++--- diagramview.cpp | 8 +++++--- editor/elementscene.cpp | 13 +++++++------ editor/elementscene.h | 4 ++++ editor/parttext.h | 4 ++-- editor/parttextfield.h | 4 ++-- element.cpp | 4 ++-- hotspoteditor.cpp | 2 +- terminal.cpp | 9 +++++---- terminal.h | 1 + 12 files changed, 63 insertions(+), 42 deletions(-) diff --git a/conductor.cpp b/conductor.cpp index d67a0e2ae..5f6dc27a2 100644 --- a/conductor.cpp +++ b/conductor.cpp @@ -499,7 +499,7 @@ void Conductor::mousePressEvent(QGraphicsSceneMouseEvent *e) { // clic gauche if (e -> buttons() & Qt::LeftButton) { // recupere les coordonnees du clic - press_point = mapFromScene(e -> pos()); + press_point = e -> pos(); /* parcourt les segments pour determiner si le clic a eu lieu @@ -541,6 +541,11 @@ void Conductor::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { qreal mouse_x = e -> pos().x(); qreal mouse_y = e -> pos().y(); + bool snap_conductors_to_grid = e -> modifiers() ^ Qt::ShiftModifier; + if (snap_conductors_to_grid) { + mouse_x = qRound(mouse_x / (Diagram::xGrid * 1.0)) * Diagram::xGrid; + mouse_y = qRound(mouse_y / (Diagram::yGrid * 1.0)) * Diagram::yGrid; + } if (moving_point) { // la modification par points revient bientot /* diff --git a/diagram.cpp b/diagram.cpp index 50f45facb..252cdabf2 100644 --- a/diagram.cpp +++ b/diagram.cpp @@ -6,6 +6,10 @@ #include "exportdialog.h" #include "diagramcommands.h" +const int Diagram::xGrid = 10; +const int Diagram::yGrid = 10; +const qreal Diagram::margin = 5.0; + /** Constructeur @param parent Le QObject parent du schema @@ -58,18 +62,18 @@ void Diagram::drawBackground(QPainter *p, const QRectF &r) { qreal limite_y = r.y() + r.height(); int g_x = (int)ceil(r.x()); - while (g_x % GRILLE_X) ++ g_x; + while (g_x % xGrid) ++ g_x; int g_y = (int)ceil(r.y()); - while (g_y % GRILLE_Y) ++ g_y; + while (g_y % yGrid) ++ g_y; - for (int gx = g_x ; gx < limite_x ; gx += GRILLE_X) { - for (int gy = g_y ; gy < limite_y ; gy += GRILLE_Y) { + for (int gx = g_x ; gx < limite_x ; gx += xGrid) { + for (int gy = g_y ; gy < limite_y ; gy += yGrid) { p -> drawPoint(gx, gy); } } } - if (use_border) border_and_inset.draw(p, MARGIN, MARGIN); + if (use_border) border_and_inset.draw(p, margin, margin); p -> restore(); } @@ -80,10 +84,10 @@ void Diagram::drawBackground(QPainter *p, const QRectF &r) { void Diagram::keyPressEvent(QKeyEvent *e) { QPointF movement; switch(e -> key()) { - case Qt::Key_Left: movement = QPointF(-GRILLE_X, 0.0); break; - case Qt::Key_Right: movement = QPointF(+GRILLE_X, 0.0); break; - case Qt::Key_Up: movement = QPointF(0.0, -GRILLE_Y); break; - case Qt::Key_Down: movement = QPointF(0.0, +GRILLE_Y); break; + case Qt::Key_Left: movement = QPointF(-xGrid, 0.0); break; + case Qt::Key_Right: movement = QPointF(+xGrid, 0.0); break; + case Qt::Key_Up: movement = QPointF(0.0, -yGrid); break; + case Qt::Key_Down: movement = QPointF(0.0, +yGrid); break; } if (!movement.isNull()) { QSet moved_elements = elementsToMove(); @@ -136,15 +140,15 @@ bool Diagram::toPaintDevice(QPaintDevice &pix, int width, int height, Qt::Aspect QRectF source_area; if (!use_border) { source_area = itemsBoundingRect(); - source_area.translate(-MARGIN, -MARGIN); - source_area.setWidth (source_area.width () + 2.0 * MARGIN); - source_area.setHeight(source_area.height() + 2.0 * MARGIN); + source_area.translate(-margin, -margin); + source_area.setWidth (source_area.width () + 2.0 * margin); + source_area.setHeight(source_area.height() + 2.0 * margin); } else { source_area = QRectF( 0.0, 0.0, - border_and_inset.borderWidth () + 2.0 * MARGIN, - border_and_inset.borderHeight() + 2.0 * MARGIN + border_and_inset.borderWidth () + 2.0 * margin, + border_and_inset.borderHeight() + 2.0 * margin ); } @@ -190,8 +194,8 @@ QSize Diagram::imageSize() const { image_height = border_and_inset.borderHeight(); } - image_width += 2.0 * MARGIN; - image_height += 2.0 * MARGIN; + image_width += 2.0 * margin; + image_height += 2.0 * margin; // renvoie la taille de la zone source return(QSizeF(image_width, image_height).toSize()); @@ -448,8 +452,8 @@ void Diagram::slot_checkSelectionEmptinessChange() { QRectF Diagram::border() const { return( QRectF( - MARGIN, - MARGIN, + margin, + margin, border_and_inset.borderWidth(), border_and_inset.borderHeight() ) diff --git a/diagram.h b/diagram.h index b37cb5535..9191b353f 100644 --- a/diagram.h +++ b/diagram.h @@ -1,8 +1,5 @@ #ifndef SCHEMA_H #define SCHEMA_H -#define GRILLE_X 10 -#define GRILLE_Y 10 -#define MARGIN 5.0 #include #include #include "qetdiagrameditor.h" @@ -32,6 +29,12 @@ class Diagram : public QGraphicsScene { enum BorderOptions { EmptyBorder, Inset, Columns }; BorderInset border_and_inset; QPointF current_movement; + /// taille de la grille en abscisse + static const int xGrid; + /// taille de la grille en ordonnee + static const int yGrid; + /// marge autour du schema + static const qreal margin; private: QGraphicsLineItem *conductor_setter; diff --git a/diagramview.cpp b/diagramview.cpp index 747f1db20..4337a0d9d 100644 --- a/diagramview.cpp +++ b/diagramview.cpp @@ -47,7 +47,9 @@ DiagramView::~DiagramView() { */ void DiagramView::selectAll() { if (scene -> items().isEmpty()) return; - foreach (QGraphicsItem *item, scene -> items()) item -> setSelected(true); + QPainterPath path; + path.addRect(scene -> itemsBoundingRect()); + scene -> setSelectionArea(path); } /** @@ -55,7 +57,7 @@ void DiagramView::selectAll() { */ void DiagramView::selectNothing() { if (scene -> items().isEmpty()) return; - foreach (QGraphicsItem *item, scene -> items()) item -> setSelected(false); + scene -> clearSelection(); } /** @@ -595,7 +597,7 @@ void DiagramView::adjustSceneRect() { QRectF elements_bounding_rect = scene -> itemsBoundingRect(); // rectangle contenant le cadre = colonnes + cartouche - QRectF border_bounding_rect = scene -> border().adjusted(-MARGIN, -MARGIN, MARGIN, MARGIN); + QRectF border_bounding_rect = scene -> border().adjusted(-Diagram::margin, -Diagram::margin, Diagram::margin, Diagram::margin); // ajuste la sceneRect QRectF new_scene_rect = elements_bounding_rect.united(border_bounding_rect); diff --git a/editor/elementscene.cpp b/editor/elementscene.cpp index 60b66e6ce..120c8c222 100644 --- a/editor/elementscene.cpp +++ b/editor/elementscene.cpp @@ -11,8 +11,9 @@ #include "partarc.h" #include "hotspoteditor.h" #include "editorcommands.h" -#define GRILLE_X 10 -#define GRILLE_Y 10 + +const int ElementScene::xGrid = 10; +const int ElementScene::yGrid = 10; ElementScene::ElementScene(QETElementEditor *editor, QObject *parent) : QGraphicsScene(parent), @@ -248,12 +249,12 @@ void ElementScene::drawBackground(QPainter *p, const QRectF &r) { qreal limite_y = r.y() + r.height(); int g_x = (int)ceil(r.x()); - while (g_x % GRILLE_X) ++ g_x; + while (g_x % xGrid) ++ g_x; int g_y = (int)ceil(r.y()); - while (g_y % GRILLE_Y) ++ g_y; + while (g_y % yGrid) ++ g_y; - for (int gx = g_x ; gx < limite_x ; gx += GRILLE_X) { - for (int gy = g_y ; gy < limite_y ; gy += GRILLE_Y) { + for (int gx = g_x ; gx < limite_x ; gx += xGrid) { + for (int gy = g_y ; gy < limite_y ; gy += yGrid) { p -> drawPoint(gx, gy); } } diff --git a/editor/elementscene.h b/editor/elementscene.h index 8a7eb55d0..be4737144 100644 --- a/editor/elementscene.h +++ b/editor/elementscene.h @@ -26,6 +26,10 @@ class ElementScene : public QGraphicsScene { ElementScene(const ElementScene &); // attributs + public: + static const int xGrid; + static const int yGrid; + private: /// longueur de l'element en dizaines de pixels uint _width; diff --git a/editor/parttext.h b/editor/parttext.h index aefe56366..6de0adf7b 100644 --- a/editor/parttext.h +++ b/editor/parttext.h @@ -1,5 +1,5 @@ -#ifndef PART_TEXT -#define PART_TEXT +#ifndef PART_TEXT_H +#define PART_TEXT_H #include #include "customelementpart.h" class TextEditor; diff --git a/editor/parttextfield.h b/editor/parttextfield.h index 7194fdf24..8ef8ced66 100644 --- a/editor/parttextfield.h +++ b/editor/parttextfield.h @@ -1,5 +1,5 @@ -#ifndef PART_TEXTFIELD -#define PART_TEXTFIELD +#ifndef PART_TEXTFIELD_H +#define PART_TEXTFIELD_H #include #include "customelementpart.h" class TextFieldEditor; diff --git a/element.cpp b/element.cpp index 0e1c7a23a..f1dcedb63 100644 --- a/element.cpp +++ b/element.cpp @@ -206,9 +206,9 @@ void Element::setPos(const QPointF &p) { // pas la peine de positionner sur la grille si l'element n'est pas sur un Diagram if (scene()) { // arrondit l'abscisse a 10 px pres - int p_x = qRound(p.x() / 10.0) * 10; + int p_x = qRound(p.x() / (Diagram::xGrid * 1.0)) * Diagram::xGrid; // arrondit l'ordonnee a 10 px pres - int p_y = qRound(p.y() / 10.0) * 10; + int p_y = qRound(p.y() / (Diagram::yGrid * 1.0)) * Diagram::yGrid; QGraphicsItem::setPos(p_x, p_y); } else QGraphicsItem::setPos(p); } diff --git a/hotspoteditor.cpp b/hotspoteditor.cpp index de8edf680..1d36d26b9 100644 --- a/hotspoteditor.cpp +++ b/hotspoteditor.cpp @@ -33,7 +33,7 @@ HotspotEditor::HotspotEditor(QWidget *parent) : diagram_view = new QGraphicsView(diagram_scene); diagram_view -> setMaximumSize( - static_cast((5 * diagram_scene -> border_and_inset.columnsWidth()) + (3 * MARGIN)), + static_cast((5 * diagram_scene -> border_and_inset.columnsWidth()) + (3 * Diagram::margin)), 300 ); diagram_view -> setTransformationAnchor(QGraphicsView::AnchorUnderMouse); diff --git a/terminal.cpp b/terminal.cpp index bb11fe016..f87fb085d 100644 --- a/terminal.cpp +++ b/terminal.cpp @@ -8,6 +8,7 @@ QColor Terminal::couleur_neutre = QColor(Qt::blue); QColor Terminal::couleur_autorise = QColor(Qt::darkGreen); QColor Terminal::couleur_prudence = QColor("#ff8000"); QColor Terminal::couleur_interdit = QColor(Qt::red); +const qreal Terminal::terminalSize = 4.0; /** Fonction privee pour initialiser la borne. @@ -25,11 +26,11 @@ void Terminal::initialise(QPointF pf, QET::Orientation o) { // calcul de la position du point d'amarrage a l'element amarrage_elmt = amarrage_conductor; switch(sens) { - case QET::North: amarrage_elmt += QPointF(0, TAILLE_BORNE); break; - case QET::East : amarrage_elmt += QPointF(-TAILLE_BORNE, 0); break; - case QET::West : amarrage_elmt += QPointF(TAILLE_BORNE, 0); break; + case QET::North: amarrage_elmt += QPointF(0, Terminal::terminalSize); break; + case QET::East : amarrage_elmt += QPointF(-Terminal::terminalSize, 0); break; + case QET::West : amarrage_elmt += QPointF(Terminal::terminalSize, 0); break; case QET::South: - default : amarrage_elmt += QPointF(0, -TAILLE_BORNE); + default : amarrage_elmt += QPointF(0, -Terminal::terminalSize); } // par defaut : pas de conducteur diff --git a/terminal.h b/terminal.h index 432810a43..6f6516396 100644 --- a/terminal.h +++ b/terminal.h @@ -61,6 +61,7 @@ class Terminal : public QGraphicsItem { // attributs public: enum { Type = UserType + 1002 }; + static const qreal terminalSize; // differentes couleurs statiques utilisables pour l'effet "hover" static QColor couleur_neutre;