summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukáš Lalinský <lalinsky@gmail.com>2009-01-01 08:57:48 +0100
committerLukáš Lalinský <lalinsky@gmail.com>2009-01-01 08:57:48 +0100
commita02fbb306009ab27ab91867d0736e133d4d42b45 (patch)
tree260dc67e6d999e3561668e104b69fab35aa7a6fe
parente40be8a9658095b5d7e87afdf4a95762885e7aab (diff)
downloaddbmodel-a02fbb306009ab27ab91867d0736e133d4d42b45.tar.gz
dbmodel-a02fbb306009ab27ab91867d0736e133d4d42b45.tar.bz2
Fix adding and removing of all items
-rw-r--r--src/commands.cpp95
-rw-r--r--src/commands.h38
-rw-r--r--src/connector.cpp19
-rw-r--r--src/connector.h3
-rw-r--r--src/diagramdocument.cpp74
-rw-r--r--src/diagramdocument.h3
-rw-r--r--src/domutils.h2
-rw-r--r--src/hub.cpp7
-rw-r--r--src/items/database/databasetable.cpp1
-rw-r--r--src/line.h1
-rw-r--r--src/mainwindow.cpp5
11 files changed, 182 insertions, 66 deletions
diff --git a/src/commands.cpp b/src/commands.cpp
index 70f0f94..e61f0a7 100644
--- a/src/commands.cpp
+++ b/src/commands.cpp
@@ -17,9 +17,11 @@
#include <QDebug>
#include "commands.h"
#include "line.h"
+#include "hub.h"
#include "diagramdocument.h"
#include "diagramitem.h"
#include "diagramobject.h"
+#include "connector.h"
SetObjectPropertyCommand::SetObjectPropertyCommand(QObject *object, const char *name, const QVariant &value, QUndoCommand *parent)
: QUndoCommand(parent), m_object(object), m_name(name), m_newValue(value)
@@ -76,87 +78,120 @@ MoveItemCommand::undo()
}
-AddItemCommand::AddItemCommand(DiagramDocument *document, DiagramItem *item, QUndoCommand *parent)
+AddObjectCommand::AddObjectCommand(DiagramDocument *document, DiagramObject *item, QUndoCommand *parent)
: QUndoCommand(parent), m_document(document), m_item(item), m_owner(true)
{
}
-AddItemCommand::~AddItemCommand()
+AddObjectCommand::~AddObjectCommand()
{
if (m_owner)
delete m_item;
}
void
-AddItemCommand::redo()
+AddObjectCommand::redo()
{
m_document->addItem(m_item);
m_owner = false;
}
void
-AddItemCommand::undo()
+AddObjectCommand::undo()
{
m_document->removeItem(m_item);
m_owner = true;
}
-
-RemoveItemCommand::RemoveItemCommand(DiagramDocument *document, DiagramItem *item, QUndoCommand *parent)
- : QUndoCommand(parent), m_document(document), m_item(item), m_owner(false)
+RemoveObjectCommand::RemoveObjectCommand(DiagramDocument *document, DiagramObject *object, QUndoCommand *parent)
+ : QUndoCommand(parent), m_document(document), m_object(object), m_owner(false)
{
+ foreach (Line *line, document->findConnections(object)) {
+ new RemoveLineCommand(document, line, this);
+ }
}
-RemoveItemCommand::~RemoveItemCommand()
+RemoveObjectCommand::~RemoveObjectCommand()
{
- if (m_owner)
- delete m_item;
+ if (m_owner) {
+ delete m_object;
+ }
}
void
-RemoveItemCommand::redo()
+RemoveObjectCommand::redo()
{
- m_document->removeItem(m_item);
+ QUndoCommand::redo();
+ m_document->removeItem(m_object);
m_owner = true;
}
void
-RemoveItemCommand::undo()
+RemoveObjectCommand::undo()
{
- m_document->addItem(m_item);
+ m_document->addItem(m_object);
m_owner = false;
+ QUndoCommand::undo();
}
-
-RemoveObjectCommand::RemoveObjectCommand(DiagramDocument *document, DiagramObject *object, QUndoCommand *parent)
- : QUndoCommand(parent), m_document(document), m_object(object), m_owner(false)
+RemoveLineCommand::RemoveLineCommand(DiagramDocument *document, Line *line, QUndoCommand *parent)
+ : QUndoCommand(parent), m_document(document), m_line(line), m_owner(false)
{
- m_connections = document->findConnections(object);
}
-RemoveObjectCommand::~RemoveObjectCommand()
+RemoveLineCommand::~RemoveLineCommand()
{
if (m_owner) {
- qDeleteAll(m_connections);
- delete m_object;
+ delete m_line;
}
}
void
-RemoveObjectCommand::redo()
+RemoveLineCommand::redo()
{
- foreach (Line *connection, m_connections)
- m_document->removeItem(connection);
- m_document->removeItem(m_object);
+ m_line->connector(0)->removeFromHub();
+ m_line->connector(1)->removeFromHub();
+ m_document->removeItemLater(m_line);
m_owner = true;
}
void
-RemoveObjectCommand::undo()
+RemoveLineCommand::undo()
{
- m_document->addItem(m_object);
- foreach (Line *connection, m_connections) {
- m_document->addItem(connection);
- }
+ m_line->connector(0)->addToHub();
+ m_line->connector(1)->addToHub();
+ m_document->addItemLater(m_line);
m_owner = false;
}
+
+AddLineCommand::AddLineCommand(DiagramDocument *document, Line *line, QUndoCommand *parent)
+ : QUndoCommand(parent), m_document(document), m_line(line), m_owner(true)
+{
+ m_line->connector(0)->removeFromHub();
+ m_line->connector(1)->removeFromHub();
+}
+
+AddLineCommand::~AddLineCommand()
+{
+ if (m_owner) {
+ delete m_line;
+ }
+}
+
+void
+AddLineCommand::redo()
+{
+ m_line->connector(0)->addToHub();
+ m_line->connector(1)->addToHub();
+ m_document->addItemLater(m_line);
+ m_owner = false;
+}
+
+void
+AddLineCommand::undo()
+{
+ m_line->connector(0)->removeFromHub();
+ m_line->connector(1)->removeFromHub();
+ m_document->removeItemLater(m_line);
+ m_owner = true;
+}
diff --git a/src/commands.h b/src/commands.h
index 76dbbf5..dbdfe05 100644
--- a/src/commands.h
+++ b/src/commands.h
@@ -59,43 +59,55 @@ private:
QPointF m_oldPos, m_newPos;
};
-class AddItemCommand : public QUndoCommand
+class AddObjectCommand : public QUndoCommand
{
public:
- AddItemCommand(DiagramDocument *document, DiagramItem *item, QUndoCommand *parent = 0);
- ~AddItemCommand();
+ AddObjectCommand(DiagramDocument *document, DiagramObject *item, QUndoCommand *parent = 0);
+ ~AddObjectCommand();
void undo();
void redo();
private:
DiagramDocument *m_document;
- DiagramItem *m_item;
+ DiagramObject *m_item;
bool m_owner;
};
-class RemoveItemCommand : public QUndoCommand
+class RemoveObjectCommand : public QUndoCommand
{
public:
- RemoveItemCommand(DiagramDocument *document, DiagramItem *item, QUndoCommand *parent = 0);
- ~RemoveItemCommand();
+ RemoveObjectCommand(DiagramDocument *document, DiagramObject *object, QUndoCommand *parent = 0);
+ ~RemoveObjectCommand();
void undo();
void redo();
protected:
DiagramDocument *m_document;
- DiagramItem *m_item;
+ DiagramObject *m_object;
bool m_owner;
};
-class RemoveObjectCommand : public QUndoCommand
+class RemoveLineCommand : public QUndoCommand
{
public:
- RemoveObjectCommand(DiagramDocument *document, DiagramObject *object, QUndoCommand *parent = 0);
- ~RemoveObjectCommand();
+ RemoveLineCommand(DiagramDocument *document, Line *line, QUndoCommand *parent = 0);
+ ~RemoveLineCommand();
void undo();
void redo();
protected:
DiagramDocument *m_document;
- DiagramObject *m_object;
- QList<Line *> m_connections;
+ Line *m_line;
+ bool m_owner;
+};
+
+class AddLineCommand : public QUndoCommand
+{
+public:
+ AddLineCommand(DiagramDocument *document, Line *line, QUndoCommand *parent = 0);
+ ~AddLineCommand();
+ void undo();
+ void redo();
+private:
+ DiagramDocument *m_document;
+ Line *m_line;
bool m_owner;
};
diff --git a/src/connector.cpp b/src/connector.cpp
index f693fde..bbd7580 100644
--- a/src/connector.cpp
+++ b/src/connector.cpp
@@ -71,10 +71,9 @@ Connector::hub() const
void
Connector::setHub(Hub *hub)
{
- if (hub)
- hub->removeConnector(this);
+ removeFromHub();
m_hub = hub;
- m_hub->addConnector(this);
+ addToHub();
}
bool
@@ -102,3 +101,17 @@ Connector::otherEnd() const
return conn->connector(0);
}
}
+
+void
+Connector::addToHub()
+{
+ if (m_hub)
+ m_hub->addConnector(this);
+}
+
+void
+Connector::removeFromHub()
+{
+ if (m_hub)
+ m_hub->removeConnector(this);
+}
diff --git a/src/connector.h b/src/connector.h
index 0d2ede2..66e2b1a 100644
--- a/src/connector.h
+++ b/src/connector.h
@@ -42,6 +42,9 @@ public:
Hub *hub() const;
void setHub(Hub *hub);
+ void addToHub();
+ void removeFromHub();
+
private:
QPointF m_pos;
qreal m_angle;
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<Line *> linesToAdd;
- QSet<DiagramObject *> objectsToUpdate;
+ QList<DiagramItem *> itemsToShow;
+ QList<DiagramItem *> itemsToRemove;
QSet<Line *> linesToUpdate;
+ QSet<DiagramObject *> objectsToUpdate;
QMap<QString, int> counters;
};
@@ -78,8 +81,10 @@ DiagramDocument::setNotation(Notation notation)
if (d->notation != notation) {
d->notation = notation;
// FIXME
- foreach (DatabaseRelationship *connection, itemsByType<DatabaseRelationship>())
- connection->updateLayout();
+ foreach (Line *line, itemsByType<Line>()) {
+ updateLineLayout(line);
+ qDebug() << (void *)line;
+ }
update();
}
}
@@ -167,9 +172,11 @@ DiagramDocument::updateLineLayout(Line *line)
void
DiagramDocument::_updateLines()
{
+ d->updateTimerIsRunning = true;
QSet<DiagramObject *> objectsToUpdate(d->objectsToUpdate);
d->objectsToUpdate.clear();
updateLines(objectsToUpdate);
+ d->updateTimerIsRunning = false;
}
void
@@ -187,11 +194,39 @@ DiagramDocument::updateLines(QSet<DiagramObject *> 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 <class T> QList<T *>
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<DatabaseTable *>(itemAt(m_line->line().p1()));
DatabaseTable *target = qgraphicsitem_cast<DatabaseTable *>(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<DiagramObject *>() << 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<DatabaseTable *>(item);
- if (table) {
- undoStack()->push(new RemoveObjectCommand(this, table));
+ foreach (DiagramItem *item, selectedItems()) {
+ DiagramObject *obj = qobject_cast<DiagramObject *>(item);
+ if (obj) {
+ undoStack()->push(new RemoveObjectCommand(this, obj));
+ }
+ else {
+ Line *line = qobject_cast<Line *>(item);
+ if (line) {
+ undoStack()->push(new RemoveLineCommand(this, line));
+ }
}
}
}
diff --git a/src/diagramdocument.h b/src/diagramdocument.h
index af348e9..9347281 100644
--- a/src/diagramdocument.h
+++ b/src/diagramdocument.h
@@ -96,6 +96,9 @@ public:
bool isPrinting() const;
void setPrinting(bool printing);
+ void addItemLater(DiagramItem *item);
+ void removeItemLater(DiagramItem *item);
+
signals:
void modeChanged(DiagramDocument::Mode mode);
diff --git a/src/domutils.h b/src/domutils.h
index 1fe430d..4b7c0ed 100644
--- a/src/domutils.h
+++ b/src/domutils.h
@@ -104,7 +104,7 @@ inline void
appendFloatElement(QDomDocument &doc, QDomElement &parent, const QString &name, qreal value)
{
QDomElement element = doc.createElement(name);
- element.appendChild(doc.createTextNode(QString::number(value)));
+ element.appendChild(doc.createTextNode(QString::number(double(value))));
parent.appendChild(element);
}
diff --git a/src/hub.cpp b/src/hub.cpp
index aec14a6..52ca0e9 100644
--- a/src/hub.cpp
+++ b/src/hub.cpp
@@ -16,6 +16,7 @@
#include "hub.h"
#include "diagramobject.h"
+#include "diagramdocument.h"
Hub::Hub(DiagramObject *owner)
: m_owner(owner)
@@ -32,12 +33,18 @@ void
Hub::addConnector(Connector *connector)
{
m_connectors.insert(connector);
+ if (m_owner->document()) {
+ m_owner->document()->updatePositions(m_owner);
+ }
}
void
Hub::removeConnector(Connector *connector)
{
m_connectors.remove(connector);
+ if (m_owner->document()) {
+ m_owner->document()->updatePositions(m_owner);
+ }
}
QSet<Connector *>
diff --git a/src/items/database/databasetable.cpp b/src/items/database/databasetable.cpp
index 129f66d..a1f83c1 100644
--- a/src/items/database/databasetable.cpp
+++ b/src/items/database/databasetable.cpp
@@ -148,6 +148,7 @@ DatabaseTable::updateLayout()
return;
prepareGeometryChange();
+ document()->updatePositions(this);
QFont font = scene()->font();
QFontMetricsF fontMetrics(font);
diff --git a/src/line.h b/src/line.h
index 7684f84..b564274 100644
--- a/src/line.h
+++ b/src/line.h
@@ -18,6 +18,7 @@
#define DIAGRAMCONNECTION_H
#include "diagramitem.h"
+#include "diagramdocument.h"
class Connector;
class DiagramObject;
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index 2fe39f8..e7ae7c9 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -33,6 +33,7 @@
#include <QUndoView>
#include <QStackedWidget>
#include "diagramitem.h"
+#include "diagramobject.h"
#include "diagramitemfactory.h"
#include "diagramitemproperties.h"
#include "commands.h"
@@ -702,9 +703,9 @@ MainWindow::paste()
{
const QMimeData *mimeData = QApplication::clipboard()->mimeData(QClipboard::Clipboard);
if (mimeData) {
- DiagramItem *item = DiagramItem::fromMimeData(mimeData);
+ DiagramObject *item = dynamic_cast<DiagramObject *>(DiagramItem::fromMimeData(mimeData));
if (item) {
- m_model->undoStack()->push(new AddItemCommand(m_model, item));
+ m_model->undoStack()->push(new AddObjectCommand(m_model, item));
}
}
}