From a02fbb306009ab27ab91867d0736e133d4d42b45 Mon Sep 17 00:00:00 2001 From: Lukáš Lalinský Date: Thu, 1 Jan 2009 08:57:48 +0100 Subject: Fix adding and removing of all items --- src/diagramdocument.cpp | 74 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 17 deletions(-) (limited to 'src/diagramdocument.cpp') diff --git a/src/diagramdocument.cpp b/src/diagramdocument.cpp index bab359b..3c73df6 100644 --- a/src/diagramdocument.cpp +++ b/src/diagramdocument.cpp @@ -41,7 +41,8 @@ public: gridVisible(true), gridPen(QColor(185, 185, 185), 0), printing(false), - notation(Relational) + notation(Relational), + updateTimerIsRunning(false) {} int gridSize; bool gridVisible; @@ -49,10 +50,12 @@ public: bool printing; Notation notation; + bool updateTimerIsRunning; QTimer *updateTimer; - QList linesToAdd; - QSet objectsToUpdate; + QList itemsToShow; + QList itemsToRemove; QSet linesToUpdate; + QSet objectsToUpdate; QMap counters; }; @@ -78,8 +81,10 @@ DiagramDocument::setNotation(Notation notation) if (d->notation != notation) { d->notation = notation; // FIXME - foreach (DatabaseRelationship *connection, itemsByType()) - connection->updateLayout(); + foreach (Line *line, itemsByType()) { + updateLineLayout(line); + qDebug() << (void *)line; + } update(); } } @@ -167,9 +172,11 @@ DiagramDocument::updateLineLayout(Line *line) void DiagramDocument::_updateLines() { + d->updateTimerIsRunning = true; QSet objectsToUpdate(d->objectsToUpdate); d->objectsToUpdate.clear(); updateLines(objectsToUpdate); + d->updateTimerIsRunning = false; } void @@ -187,11 +194,39 @@ DiagramDocument::updateLines(QSet objectsToUpdate) } foreach (Line *line, d->linesToUpdate) { line->updateLayout(); - line->update(); // XXX why is this necessary? + line->update(); } d->linesToUpdate.clear(); + + foreach (DiagramItem *item, d->itemsToShow) { + item->show(); + } + d->itemsToShow.clear(); + + foreach (DiagramItem *item, d->itemsToRemove) { + removeItem(item); + } + d->itemsToRemove.clear(); +} + +void +DiagramDocument::addItemLater(DiagramItem *item) +{ + Q_ASSERT(d->updateTimerIsRunning == false); + item->hide(); + addItem(item); + d->itemsToShow.append(item); + d->updateTimer->start(0); } +void +DiagramDocument::removeItemLater(DiagramItem *item) +{ + Q_ASSERT(d->updateTimerIsRunning == false); + item->hide(); + d->itemsToRemove.append(item); + d->updateTimer->start(0); +} template QList DiagramDocument::itemsByType() @@ -251,7 +286,7 @@ DiagramDocument::mousePressEvent(QGraphicsSceneMouseEvent *event) table->createId(); table->setInitialName(1 + d->counters[table->typeName()]++); table->setPos(event->scenePos()); - undoStack()->push(new AddItemCommand(this, table)); + undoStack()->push(new AddObjectCommand(this, table)); clearSelection(); table->setSelected(true); setMode(Select); @@ -293,12 +328,11 @@ DiagramDocument::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) DatabaseTable *source = qgraphicsitem_cast(itemAt(m_line->line().p1())); DatabaseTable *target = qgraphicsitem_cast(itemAt(m_line->line().p2())); if (source && target && source != target) { - DatabaseRelationship *relation = new DatabaseRelationship(); - relation->createId(); - relation->connector(0)->setHub(source->hub()); - relation->connector(1)->setHub(target->hub()); - updateLines(QSet() << source); - undoStack()->push(new AddItemCommand(this, relation)); + Line *line = new DatabaseRelationship(); + line->createId(); + line->connector(0)->setHub(source->hub()); + line->connector(1)->setHub(target->hub()); + undoStack()->push(new AddLineCommand(this, line)); } delete m_line; m_line = NULL; @@ -328,10 +362,16 @@ DiagramDocument::selectedTable() void DiagramDocument::deleteSelectedItems() { - foreach (QGraphicsItem *item, selectedItems()) { - DatabaseTable *table = qgraphicsitem_cast(item); - if (table) { - undoStack()->push(new RemoveObjectCommand(this, table)); + foreach (DiagramItem *item, selectedItems()) { + DiagramObject *obj = qobject_cast(item); + if (obj) { + undoStack()->push(new RemoveObjectCommand(this, obj)); + } + else { + Line *line = qobject_cast(item); + if (line) { + undoStack()->push(new RemoveLineCommand(this, line)); + } } } } -- cgit v1.2.3-54-g00ecf