diff options
author | Lukáš Lalinský <lalinsky@gmail.com> | 2008-12-31 11:08:29 +0100 |
---|---|---|
committer | Lukáš Lalinský <lalinsky@gmail.com> | 2008-12-31 11:08:29 +0100 |
commit | e40be8a9658095b5d7e87afdf4a95762885e7aab (patch) | |
tree | 0586c6a25e4f26c0f958ef25614fcc215ed74eef | |
parent | ca579d4487f431baf791ccb0c2635fb339cdfacf (diff) | |
download | dbmodel-e40be8a9658095b5d7e87afdf4a95762885e7aab.tar.gz dbmodel-e40be8a9658095b5d7e87afdf4a95762885e7aab.tar.bz2 |
Update line layouts in a singleshot timer
-rw-r--r-- | src/boxsidehub.cpp | 3 | ||||
-rw-r--r-- | src/connector.cpp | 7 | ||||
-rw-r--r-- | src/diagramdocument.cpp | 70 | ||||
-rw-r--r-- | src/diagramdocument.h | 10 | ||||
-rw-r--r-- | src/items/database/databaserelationship.cpp | 55 | ||||
-rw-r--r-- | src/items/database/databaserelationship.h | 2 | ||||
-rw-r--r-- | src/line.cpp | 2 | ||||
-rw-r--r-- | src/line.h | 2 |
8 files changed, 73 insertions, 78 deletions
diff --git a/src/boxsidehub.cpp b/src/boxsidehub.cpp index 35e763c..4dfb460 100644 --- a/src/boxsidehub.cpp +++ b/src/boxsidehub.cpp @@ -70,7 +70,6 @@ BoxSideHub::update() QHash<Side, QList<ConnectorRealPair> > sides; DiagramObject *item = owner(); QRectF rect = item->boundingRect().translated(item->pos()); - qDebug() << this; foreach (Connector *connector, connectors()) { Line *connection = connector->owner(); Connector *connector1 = connection->connector(0); @@ -100,10 +99,8 @@ BoxSideHub::update() side = Bottom; } } - qDebug() << angle << cornerAngle << side; sides[side].append(qMakePair(connector, angle)); } - qDebug() << sides; foreach (Side side, sides.keys()) { QPointF p, dp; QList<ConnectorRealPair> c = sides[side]; diff --git a/src/connector.cpp b/src/connector.cpp index 123971d..f693fde 100644 --- a/src/connector.cpp +++ b/src/connector.cpp @@ -15,6 +15,7 @@ // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "connector.h" +#include "diagramdocument.h" #include "hub.h" #include "line.h" @@ -40,7 +41,8 @@ Connector::setPos(const QPointF &pos) { if (m_pos != pos) { m_pos = pos; - m_owner->updatePositions(); + if (m_owner->document()) + m_owner->document()->updateLineLayout(m_owner); } } @@ -55,7 +57,8 @@ Connector::setAngle(qreal angle) { if (m_angle != angle) { m_angle = angle; -// m_owner->updatePositions(); + if (m_owner->document()) + m_owner->document()->updateLineLayout(m_owner); } } diff --git a/src/diagramdocument.cpp b/src/diagramdocument.cpp index 28a3eda..bab359b 100644 --- a/src/diagramdocument.cpp +++ b/src/diagramdocument.cpp @@ -24,10 +24,12 @@ #include "items/database/column.h" #include "commands.h" #include <QGraphicsItem> +#include <QTimer> #include <QDebug> #include <QFile> #include <QTextStream> #include <QStyle> +#include <QSet> using namespace std; @@ -47,6 +49,10 @@ public: bool printing; Notation notation; + QTimer *updateTimer; + QList<Line *> linesToAdd; + QSet<DiagramObject *> objectsToUpdate; + QSet<Line *> linesToUpdate; QMap<QString, int> counters; }; @@ -54,6 +60,10 @@ DiagramDocument::DiagramDocument(QObject *parent) : QGraphicsScene(parent), d(new DiagramDocumentPrivate), m_mode(DiagramDocument::Select), m_line(NULL) { m_undoStack = new QUndoStack(this); + d->updateTimer = new QTimer(); + d->updateTimer->setSingleShot(true); + d->updateTimer->setInterval(10); + connect(d->updateTimer, SIGNAL(timeout()), this, SLOT(_updateLines())); } DiagramDocument::Notation @@ -139,20 +149,50 @@ DiagramDocument::setMode(Mode mode) } void -DiagramDocument::updatePositions(DiagramObject *object) +DiagramDocument::updatePositions(DiagramObject *object, bool force) { - Connector *connector; - foreach (Line *connection, findConnections(object)) { - connector = connection->connector(0); - if (connector->isConnected()) - connector->hub()->update(); - connector = connection->connector(1); - if (connector->isConnected()) - connector->hub()->update(); - connection->updatePositions(); - } + d->objectsToUpdate.insert(object); + if (force) + d->updateTimer->start(0); + else + d->updateTimer->start(); } +void +DiagramDocument::updateLineLayout(Line *line) +{ + d->linesToUpdate.insert(line); +} + +void +DiagramDocument::_updateLines() +{ + QSet<DiagramObject *> objectsToUpdate(d->objectsToUpdate); + d->objectsToUpdate.clear(); + updateLines(objectsToUpdate); +} + +void +DiagramDocument::updateLines(QSet<DiagramObject *> objectsToUpdate) +{ + QSet<Hub *> hubsToUpdate; + foreach (DiagramObject *obj, objectsToUpdate) { + hubsToUpdate.insert(obj->hub()); + foreach (Connector *conn, obj->hub()->connectors()) { + hubsToUpdate.insert(conn->otherEnd()->hub()); + } + } + foreach (Hub *hub, hubsToUpdate) { + hub->update(); + } + foreach (Line *line, d->linesToUpdate) { + line->updateLayout(); + line->update(); // XXX why is this necessary? + } + d->linesToUpdate.clear(); +} + + template <class T> QList<T *> DiagramDocument::itemsByType() { @@ -195,7 +235,10 @@ DiagramDocument::itemMoved(DiagramItem *item) void DiagramDocument::itemHasMoved(DiagramItem *item) { - updatePositions(static_cast<DatabaseTable*>(item)); + DiagramObject *obj = static_cast<DiagramObject *>(item); + if (obj) { + updatePositions(obj); + } } void @@ -254,8 +297,7 @@ DiagramDocument::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) relation->createId(); relation->connector(0)->setHub(source->hub()); relation->connector(1)->setHub(target->hub()); - source->hub()->update(); - target->hub()->update(); + updateLines(QSet<DiagramObject *>() << source); undoStack()->push(new AddItemCommand(this, relation)); } delete m_line; diff --git a/src/diagramdocument.h b/src/diagramdocument.h index c0a63c0..af348e9 100644 --- a/src/diagramdocument.h +++ b/src/diagramdocument.h @@ -101,18 +101,24 @@ signals: friend class DatabaseTable; -protected slots: - void updatePositions(DiagramObject *object); +public slots: + void updatePositions(DiagramObject *object, bool force = false); + void updateLineLayout(Line *line); protected: void mousePressEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); +private slots: + void _updateLines(); + private: class DiagramDocumentPrivate; DiagramDocumentPrivate *const d; + void updateLines(QSet<DiagramObject *> objects); + bool m_trackingMoves; QMap<DiagramItem *, QPointF> m_movedItems; diff --git a/src/items/database/databaserelationship.cpp b/src/items/database/databaserelationship.cpp index e120786..c47375a 100644 --- a/src/items/database/databaserelationship.cpp +++ b/src/items/database/databaserelationship.cpp @@ -278,69 +278,18 @@ DatabaseRelationship::itemChange(GraphicsItemChange change, const QVariant &valu } void -DatabaseRelationship::updatePositions() -{ - updateLayout(); - update(); -} - -void DatabaseRelationship::updateLayout() { if (!scene()) return; - Connector *connector1 = connector(0); - Connector *connector2 = connector(1); - - DatabaseTable *source = childTable(); - DatabaseTable *target = parentTable(); - if (!source || !target) - return; - prepareGeometryChange(); - QRectF rect1 = source->boundingRect().translated(source->pos()); - QRectF rect2 = target->boundingRect().translated(target->pos()); + Connector *connector1 = connector(0); + Connector *connector2 = connector(1); bool haveLine = false; - Range<qreal> verticalRange1(rect1.top(), rect1.bottom()); - Range<qreal> verticalRange2(rect2.top(), rect2.bottom()); - Range<qreal> verticalIntersection = verticalRange1.intersected(verticalRange2); - - Range<qreal> horizontalRange1(rect1.left(), rect1.right()); - Range<qreal> horizontalRange2(rect2.left(), rect2.right()); - Range<qreal> horizontalIntersection = horizontalRange1.intersected(horizontalRange2); - - // Straight horizontal line - if (0 && !haveLine) { - if (!verticalIntersection.isNull() && horizontalIntersection.isNull()) { - qreal y = verticalIntersection.min() + verticalIntersection.length() / 2; - if (rect1.right() < rect2.left()) { - d->line = QPolygonF() << QPointF(rect1.right(), y) << QPointF(rect2.left(), y); - } - else { - d->line = QPolygonF() << QPointF(rect1.left(), y) << QPointF(rect2.right(), y); - } - haveLine = true; - } - } - - // Straight vertical line - if (0 && !haveLine) { - if (verticalIntersection.isNull() && !horizontalIntersection.isNull()) { - qreal x = horizontalIntersection.min() + horizontalIntersection.length() / 2; - if (rect1.bottom() < rect2.top()) { - d->line = QPolygonF() << QPointF(x, rect1.bottom()) << QPointF(x, rect2.top()); - } - else { - d->line = QPolygonF() << QPointF(x, rect1.top()) << QPointF(x, rect2.bottom()); - } - haveLine = true; - } - } - // Orthogonal line if (!haveLine) { QPointF p1 = connector1->pos(); diff --git a/src/items/database/databaserelationship.h b/src/items/database/databaserelationship.h index 5d90053..b11f93e 100644 --- a/src/items/database/databaserelationship.h +++ b/src/items/database/databaserelationship.h @@ -54,8 +54,6 @@ public: void loadFromXml(QDomElement element, DiagramDocument *document = 0); void saveToXml(QDomDocument doc, QDomElement element); - void updatePositions(); - //! Returns true if the foreign key on the child table is also the primary key bool isIdentifying() const; diff --git a/src/line.cpp b/src/line.cpp index 90baa07..21c1d7a 100644 --- a/src/line.cpp +++ b/src/line.cpp @@ -36,7 +36,7 @@ Line::connector(int index) const } void -Line::updatePositions() +Line::updateLayout() { } @@ -36,7 +36,7 @@ public: Connector *connector(int index) const; - virtual void updatePositions(); + virtual void updateLayout(); virtual void loadFromXml(QDomElement element, DiagramDocument *document = 0); void saveToXml(QDomDocument doc, QDomElement element); |