From 3b49b6a0d4c4d2ee94faeff207da10c452f70b07 Mon Sep 17 00:00:00 2001 From: Lukáš Lalinský Date: Thu, 11 Dec 2008 12:34:38 +0100 Subject: Support for generic diagram item properties editors + add relationship props editor UI --- src/diagramdocument.cpp | 15 ++- src/diagramdocument.h | 2 + src/diagramitem.cpp | 6 + src/diagramitem.h | 3 + src/diagramitemfactory.h | 62 ++++++++- src/diagramitemproperties.cpp | 61 +++++++++ src/diagramitemproperties.h | 45 +++++++ src/items/database/columnlistview.cpp | 3 + src/items/database/database.pri | 9 +- src/items/database/databaserelationship.cpp | 7 + src/items/database/databaserelationship.h | 2 + .../database/databaserelationshipproperties.cpp | 60 +++++++++ .../database/databaserelationshipproperties.h | 43 ++++++ src/items/database/databasetable.cpp | 7 + src/items/database/databasetable.h | 2 + src/items/database/databasetableproperties.cpp | 149 +++++++++++++++++++++ src/items/database/databasetableproperties.h | 47 +++++++ src/items/database/tableproperties.cpp | 97 -------------- src/items/database/tableproperties.h | 48 ------- src/items/database/tableproperties.ui | 137 ------------------- src/main.cpp | 1 + src/mainwindow.cpp | 90 ++++++++++--- src/mainwindow.h | 6 +- src/src.pro | 2 + src/utils/factory.h | 26 +++- src/utils/singelton.h | 2 + 26 files changed, 610 insertions(+), 322 deletions(-) create mode 100644 src/diagramitemproperties.cpp create mode 100644 src/diagramitemproperties.h create mode 100644 src/items/database/databaserelationshipproperties.cpp create mode 100644 src/items/database/databaserelationshipproperties.h create mode 100644 src/items/database/databasetableproperties.cpp create mode 100644 src/items/database/databasetableproperties.h delete mode 100644 src/items/database/tableproperties.cpp delete mode 100644 src/items/database/tableproperties.h delete mode 100644 src/items/database/tableproperties.ui (limited to 'src') diff --git a/src/diagramdocument.cpp b/src/diagramdocument.cpp index 1f34d9f..9df8b75 100644 --- a/src/diagramdocument.cpp +++ b/src/diagramdocument.cpp @@ -138,6 +138,19 @@ DiagramDocument::itemsByType() return result; } +QList +DiagramDocument::selectedItems() +{ + QList result; + foreach(QGraphicsItem *item, QGraphicsScene::selectedItems()) { + DiagramItem *typedItem = dynamic_cast(item); + if (typedItem) { + result.append(typedItem); + } + } + return result; +} + void DiagramDocument::itemMoved(DiagramItem *item) { @@ -230,7 +243,7 @@ DiagramDocument::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) DatabaseTable * DiagramDocument::selectedTable() { - QList items = selectedItems(); + QList items = selectedItems(); if (items.size() != 1) return NULL; return qgraphicsitem_cast(items[0]); diff --git a/src/diagramdocument.h b/src/diagramdocument.h index 350879c..ffb38ac 100644 --- a/src/diagramdocument.h +++ b/src/diagramdocument.h @@ -53,6 +53,8 @@ public: DatabaseTable *selectedTable(); void deleteSelectedItems(); + QList selectedItems(); + void save(const QString &fileName); void load(const QString &fileName); diff --git a/src/diagramitem.cpp b/src/diagramitem.cpp index 1d75039..5b5133e 100644 --- a/src/diagramitem.cpp +++ b/src/diagramitem.cpp @@ -109,3 +109,9 @@ DiagramItem::fromMimeData(const QMimeData *mimeData) item->createId(); return item; } + +DiagramItemProperties * +DiagramItem::createPropertiesEditor(QWidget *parent) +{ + return NULL; +} diff --git a/src/diagramitem.h b/src/diagramitem.h index 3444f8d..76b8a0e 100644 --- a/src/diagramitem.h +++ b/src/diagramitem.h @@ -23,6 +23,7 @@ #include class QMimeData; class DiagramDocument; +class DiagramItemProperties; class DiagramItem : public QObject, public QGraphicsItem { @@ -51,6 +52,8 @@ public: virtual QMimeData *toMimeData(); static DiagramItem *fromMimeData(const QMimeData *mimeData); + static DiagramItemProperties *createPropertiesEditor(QWidget *parent = 0); + public: QUuid m_id; }; diff --git a/src/diagramitemfactory.h b/src/diagramitemfactory.h index 274957e..5874541 100644 --- a/src/diagramitemfactory.h +++ b/src/diagramitemfactory.h @@ -17,15 +17,69 @@ #ifndef DIAGRAMITEMFACTORY_H #define DIAGRAMITEMFACTORY_H -#include "utils/factory.h" - +#include +#include +#include "utils/singelton.h" class DiagramItem; +class DiagramItemProperties; -class DiagramItemFactory : public Factory +class DiagramItemFactory : public Singelton { -private: + + class CreatorBase + { + public: + virtual DiagramItem *create() = 0; + virtual DiagramItemProperties *createPropertiesEditor(QWidget *parent = 0) = 0; + }; + + template + class Creator : public CreatorBase + { + public: + DiagramItem *create() + { return new ItemType(); } + DiagramItemProperties *createPropertiesEditor(QWidget *parent = 0) + { return ItemType::createPropertiesEditor(parent); } + }; + +public: + + static DiagramItem *create(const QString &key) + { + DiagramItemFactory *inst = DiagramItemFactory::instance(); + if (!inst->m_map.contains(key)) + return NULL; + return inst->m_map[key]->create(); + } + + static DiagramItemProperties *createPropertiesEditor(const QString &key, QWidget *parent = 0) + { + DiagramItemFactory *inst = DiagramItemFactory::instance(); + if (!inst->m_map.contains(key)) + return NULL; + return inst->m_map[key]->createPropertiesEditor(parent); + } + + static QStringList keys() + { + DiagramItemFactory *inst = DiagramItemFactory::instance(); + return inst->m_map.keys(); + } + +protected: + + template void add() + { + m_map[Type::staticTypeName()] = new Creator(); + } + + QMap m_map; + DiagramItemFactory(); friend class Singelton; }; +#define FACTORY_ADD_TYPE(ST) add(); + #endif diff --git a/src/diagramitemproperties.cpp b/src/diagramitemproperties.cpp new file mode 100644 index 0000000..9568d5a --- /dev/null +++ b/src/diagramitemproperties.cpp @@ -0,0 +1,61 @@ +// Copyright (C) 2008 Lukas Lalinsky +// +// This program 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. +// +// This program 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. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#include "diagramitemproperties.h" + +class DiagramItemProperties::DiagramItemPropertiesPrivate +{ +public: + DiagramItemPropertiesPrivate() + : currentItem(0) + {} + DiagramItem *currentItem; +}; + +DiagramItemProperties::DiagramItemProperties(QWidget *parent) + : QTabWidget(parent), d(new DiagramItemPropertiesPrivate) +{ +} + +DiagramItemProperties::~DiagramItemProperties() +{ + delete d; +} + +int +DiagramItemProperties::addPage(const QString &label, QWidget *page) +{ + return addTab(page, label); +} + +DiagramItem * +DiagramItemProperties::currentItem() const +{ + return d->currentItem; +} + +void +DiagramItemProperties::setCurrentItem(DiagramItem *item) +{ + DiagramItem *oldItem = d->currentItem; + d->currentItem = item; + switchCurrentItem(oldItem, item); +} + +void +DiagramItemProperties::switchCurrentItem(DiagramItem *oldItem, DiagramItem *newItem) +{ +} diff --git a/src/diagramitemproperties.h b/src/diagramitemproperties.h new file mode 100644 index 0000000..038c75f --- /dev/null +++ b/src/diagramitemproperties.h @@ -0,0 +1,45 @@ +// Copyright (C) 2008 Lukas Lalinsky +// +// This program 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. +// +// This program 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. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#ifndef DIAGRAMITEMPROPERTIES_H +#define DIAGRAMITEMPROPERTIES_H + +#include +class DiagramItem; + +class DiagramItemProperties : public QTabWidget +{ + Q_OBJECT + +public: + DiagramItemProperties(QWidget *parent = 0); + ~DiagramItemProperties(); + + DiagramItem *currentItem() const; + +public slots: + void setCurrentItem(DiagramItem *item); + +protected: + int addPage(const QString &label, QWidget *page); + virtual void switchCurrentItem(DiagramItem *oldItem, DiagramItem *newItem); + +private: + class DiagramItemPropertiesPrivate; + DiagramItemPropertiesPrivate *d; +}; + +#endif diff --git a/src/items/database/columnlistview.cpp b/src/items/database/columnlistview.cpp index 4edb4ee..cbfb4b9 100644 --- a/src/items/database/columnlistview.cpp +++ b/src/items/database/columnlistview.cpp @@ -23,6 +23,9 @@ ColumnListView::ColumnListView(QWidget *parent) : QTreeView(parent) { + setRootIsDecorated(false); + setItemsExpandable(false); + setExpandsOnDoubleClick(false); setModel(new ColumnListModel(this)); } diff --git a/src/items/database/database.pri b/src/items/database/database.pri index 53d4f6f..9a7ebe9 100644 --- a/src/items/database/database.pri +++ b/src/items/database/database.pri @@ -7,8 +7,9 @@ SOURCES += \ columnlistmodel.cpp \ columnlistview.cpp \ databasetable.cpp \ + databasetableproperties.cpp \ databaserelationship.cpp \ - tableproperties.cpp + databaserelationshipproperties.cpp HEADERS += \ databasecommands.h \ @@ -17,8 +18,6 @@ HEADERS += \ columnlistmodel.h \ columnlistview.h \ databasetable.h \ + databasetableproperties.h \ databaserelationship.h \ - tableproperties.h - -FORMS += \ - tableproperties.ui + databaserelationshipproperties.h diff --git a/src/items/database/databaserelationship.cpp b/src/items/database/databaserelationship.cpp index 8eb0e2f..5adda58 100644 --- a/src/items/database/databaserelationship.cpp +++ b/src/items/database/databaserelationship.cpp @@ -19,6 +19,7 @@ #include "diagramdocument.h" #include "databasetable.h" #include "databaserelationship.h" +#include "databaserelationshipproperties.h" #include "range.h" DatabaseRelationship::DatabaseRelationship(DiagramItem *parent) @@ -249,3 +250,9 @@ DatabaseRelationship::saveToXml(QDomDocument doc, QDomElement element) { DiagramConnection::saveToXml(doc, element); } + +DiagramItemProperties * +DatabaseRelationship::createPropertiesEditor(QWidget *parent) +{ + return new DatabaseRelationshipProperties(parent); +} diff --git a/src/items/database/databaserelationship.h b/src/items/database/databaserelationship.h index e9e26d0..0447d3b 100644 --- a/src/items/database/databaserelationship.h +++ b/src/items/database/databaserelationship.h @@ -47,6 +47,8 @@ public: void updatePositions(); + static DiagramItemProperties *createPropertiesEditor(QWidget *parent = 0); + protected slots: void updateLayout(); diff --git a/src/items/database/databaserelationshipproperties.cpp b/src/items/database/databaserelationshipproperties.cpp new file mode 100644 index 0000000..cb3866d --- /dev/null +++ b/src/items/database/databaserelationshipproperties.cpp @@ -0,0 +1,60 @@ +// Copyright (C) 2008 Lukas Lalinsky +// +// This program 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. +// +// This program 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. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#include +#include +#include +#include +#include "databaserelationshipproperties.h" + +class DatabaseRelationshipProperties::PrivateData +{ +public: + PrivateData() + {} + + QWidget *nameEdit; + QCheckBox *generateNameCheckBox; +}; + +DatabaseRelationshipProperties::DatabaseRelationshipProperties(QWidget *parent) + : DiagramItemProperties(parent), d(new PrivateData) +{ + addPage(tr("&Relationship"), createRelationshipPage()); +} + +QWidget * +DatabaseRelationshipProperties::createRelationshipPage() +{ + QWidget *page = new QWidget(this); + QGridLayout *layout = new QGridLayout(page); + + d->nameEdit = new QLineEdit(page); + layout->addWidget(new QLabel(tr("Name:"), page), 0, 0); + layout->addWidget(d->nameEdit, 0, 1); + d->generateNameCheckBox = new QCheckBox(tr("Generated"), page); + connect(d->generateNameCheckBox, SIGNAL(toggled(bool)), d->nameEdit, SLOT(setDisabled(bool))); + d->generateNameCheckBox->setChecked(true); + layout->addWidget(d->generateNameCheckBox, 0, 2); + layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding), 1, 0, 1, 3); + + return page; +} + +void +DatabaseRelationshipProperties::switchCurrentItem(DiagramItem *oldItem, DiagramItem *newItem) +{ +} diff --git a/src/items/database/databaserelationshipproperties.h b/src/items/database/databaserelationshipproperties.h new file mode 100644 index 0000000..2cf8f29 --- /dev/null +++ b/src/items/database/databaserelationshipproperties.h @@ -0,0 +1,43 @@ +// Copyright (C) 2008 Lukas Lalinsky +// +// This program 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. +// +// This program 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. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#ifndef DATABASERELATIONSHIPPROPERTIES_H +#define DATABASERELATIONSHIPPROPERTIES_H + +#include "diagramitemproperties.h" +class DatabaseRelationship; + +class DatabaseRelationshipProperties : public DiagramItemProperties +{ + Q_OBJECT + +public: + DatabaseRelationshipProperties(QWidget *parent = 0); + DatabaseRelationship *currentRelationship(); + +protected: + void switchCurrentItem(DiagramItem *oldItem, DiagramItem *newItem); + +protected slots: + +private: + class PrivateData; + PrivateData *d; + + QWidget *createRelationshipPage(); +}; + +#endif diff --git a/src/items/database/databasetable.cpp b/src/items/database/databasetable.cpp index 059284f..5c80629 100644 --- a/src/items/database/databasetable.cpp +++ b/src/items/database/databasetable.cpp @@ -18,6 +18,7 @@ #include #include #include "databasetable.h" +#include "databasetableproperties.h" #include "diagramdocument.h" #include "column.h" #include "columnlist.h" @@ -220,3 +221,9 @@ DatabaseTable::saveToXml(QDomDocument doc, QDomElement element) appendStringElement(doc, columnElement, "notes", column->notes()); } } + +DiagramItemProperties * +DatabaseTable::createPropertiesEditor(QWidget *parent) +{ + return new DatabaseTableProperties(parent); +} diff --git a/src/items/database/databasetable.h b/src/items/database/databasetable.h index 4402586..da03a1a 100644 --- a/src/items/database/databasetable.h +++ b/src/items/database/databasetable.h @@ -57,6 +57,8 @@ public: // QMimeData *toMimeData(); + static DiagramItemProperties *createPropertiesEditor(QWidget *parent = 0); + signals: void propertyChanged(const QString &name, const QVariant &value); diff --git a/src/items/database/databasetableproperties.cpp b/src/items/database/databasetableproperties.cpp new file mode 100644 index 0000000..4c7b138 --- /dev/null +++ b/src/items/database/databasetableproperties.cpp @@ -0,0 +1,149 @@ +// Copyright (C) 2008 Lukas Lalinsky +// +// This program 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. +// +// This program 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. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#include +#include +#include +#include +#include "commands.h" +#include "columnlist.h" +#include "columnlistview.h" +#include "databasetable.h" +#include "databasetableproperties.h" +#include "diagramdocument.h" + +class DatabaseTableProperties::PrivateData +{ +public: + PrivateData() + {} + + QLineEdit *nameEdit; + ColumnListView *columnListView; + QPushButton *addColumnButton; + QPushButton *removeColumnButton; + QPushButton *moveColumnUpButton; + QPushButton *moveColumnDownButton; +}; + +DatabaseTableProperties::DatabaseTableProperties(QWidget *parent) + : DiagramItemProperties(parent), d(new PrivateData) +{ + addPage(tr("&Table"), createTablePage()); + addPage(tr("&Columns"), createColumnsPage()); +} + +DatabaseTable * +DatabaseTableProperties::currentTable() +{ + return static_cast(currentItem()); +} + +QWidget * +DatabaseTableProperties::createTablePage() +{ + QWidget *page = new QWidget(this); + QGridLayout *layout = new QGridLayout(page); + + d->nameEdit = new QLineEdit(page); + connect(d->nameEdit, SIGNAL(textEdited(const QString &)), SLOT(setTableName(const QString &))); + layout->addWidget(new QLabel(tr("Name:"), page), 0, 0); + layout->addWidget(d->nameEdit, 0, 1); + layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding), 1, 0, 1, 2); + + return page; +} + +QWidget * +DatabaseTableProperties::createColumnsPage() +{ + QWidget *page = new QWidget(this); + QGridLayout *layout = new QGridLayout(page); + + d->columnListView = new ColumnListView(page); + layout->addWidget(d->columnListView, 0, 0, 5, 1); + + connect(d->columnListView->selectionModel(), + SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), + SLOT(updateColumnSelection())); + + d->addColumnButton = new QPushButton(tr("&Add"), page); + d->removeColumnButton = new QPushButton(tr("&Remove"), page); + d->moveColumnUpButton = new QPushButton(tr("Move &Up"), page); + d->moveColumnDownButton = new QPushButton(tr("Move &Down"), page); + layout->addWidget(d->addColumnButton, 0, 1); + layout->addWidget(d->removeColumnButton, 1, 1); + layout->addWidget(d->moveColumnUpButton, 2, 1); + layout->addWidget(d->moveColumnDownButton, 3, 1); + layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding), 4, 1, 1, 1); + + connect(d->addColumnButton, SIGNAL(clicked()), d->columnListView, SLOT(addColumn())); + connect(d->removeColumnButton, SIGNAL(clicked()), d->columnListView, SLOT(removeColumn())); + connect(d->moveColumnUpButton, SIGNAL(clicked()), d->columnListView, SLOT(moveColumnUp())); + connect(d->moveColumnDownButton, SIGNAL(clicked()), d->columnListView, SLOT(moveColumnDown())); + + return page; +} + +void +DatabaseTableProperties::switchCurrentItem(DiagramItem *oldItem, DiagramItem *) +{ + if (oldItem) + disconnect(oldItem, 0, this, 0); + DatabaseTable *table = currentTable(); + if (table) { + d->nameEdit->setText(table->name()); + d->columnListView->setColumnList(table->columnList()); + connect(table, SIGNAL(propertyChanged(const QString &, const QVariant &)), SLOT(updateProperty(const QString &, const QVariant &))); + } + else { + d->nameEdit->clear(); + d->columnListView->setColumnList(NULL); + } + updateColumnSelection(); +} + +void +DatabaseTableProperties::updateProperty(const QString &name, const QVariant &value) +{ + if (name == "name") { + d->nameEdit->setText(value.toString()); + } +} + +void +DatabaseTableProperties::setTableName(const QString &name) +{ + DatabaseTable *table = currentTable(); + table->document()->undoStack()->push(new SetObjectPropertyCommand(table, "name", name)); +} + +void +DatabaseTableProperties::updateColumnSelection() +{ + QList columns = d->columnListView->selectedColumns(); + if (columns.isEmpty()) { + d->removeColumnButton->setEnabled(false); + d->moveColumnUpButton->setEnabled(false); + d->moveColumnDownButton->setEnabled(false); + } + else { + int index = columns[0]; + d->removeColumnButton->setEnabled(true); + d->moveColumnUpButton->setEnabled(index > 0); + d->moveColumnDownButton->setEnabled(index + 1 < currentTable()->columnList()->columnCount()); + } +} diff --git a/src/items/database/databasetableproperties.h b/src/items/database/databasetableproperties.h new file mode 100644 index 0000000..cd7fa14 --- /dev/null +++ b/src/items/database/databasetableproperties.h @@ -0,0 +1,47 @@ +// Copyright (C) 2008 Lukas Lalinsky +// +// This program 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. +// +// This program 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. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#ifndef DATABASETABLEPROPERTIES_H +#define DATABASETABLEPROPERTIES_H + +#include "diagramitemproperties.h" +class DatabaseTable; + +class DatabaseTableProperties : public DiagramItemProperties +{ + Q_OBJECT + +public: + DatabaseTableProperties(QWidget *parent = 0); + DatabaseTable *currentTable(); + +protected: + void switchCurrentItem(DiagramItem *oldItem, DiagramItem *newItem); + +protected slots: + void updateProperty(const QString &name, const QVariant &value); + void updateColumnSelection(); + void setTableName(const QString &name); + +private: + class PrivateData; + PrivateData *d; + + QWidget *createTablePage(); + QWidget *createColumnsPage(); +}; + +#endif diff --git a/src/items/database/tableproperties.cpp b/src/items/database/tableproperties.cpp deleted file mode 100644 index 11b3fc4..0000000 --- a/src/items/database/tableproperties.cpp +++ /dev/null @@ -1,97 +0,0 @@ -// Copyright (C) 2008 Lukas Lalinsky -// -// This program 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. -// -// This program 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. -// -// You should have received a copy of the GNU General Public License along -// with this program; if not, write to the Free Software Foundation, Inc., -// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -#include "tableproperties.h" -#include "databasetable.h" -#include "mainwindow.h" -#include "commands.h" -#include "column.h" -#include -#include - -TableProperties::TableProperties(MainWindow *window, QWidget *parent) - : QWidget(parent), m_window(window), m_table(0) -{ - ui.setupUi(this); - setTable(0); - connect(ui.nameEdit, - SIGNAL(textEdited(const QString &)), - SLOT(setSelectedTableName(const QString &))); - connect(ui.addColumnButton, SIGNAL(clicked()), ui.columnsWidget, SLOT(addColumn())); - connect(ui.removeColumnButton, SIGNAL(clicked()), ui.columnsWidget, SLOT(removeColumn())); - connect(ui.moveColumnUpButton, SIGNAL(clicked()), ui.columnsWidget, SLOT(moveColumnUp())); - connect(ui.moveColumnDownButton, SIGNAL(clicked()), ui.columnsWidget, SLOT(moveColumnDown())); - connect(ui.columnsWidget->selectionModel(), - SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), - SLOT(updateColumnSelection())); -} - -void -TableProperties::setSelectedTableName(const QString &name) -{ - if (m_table) { - m_window->currentUndoStack()->push(new SetObjectPropertyCommand(m_table, "name", name)); - } -} - -void -TableProperties::setTable(DatabaseTable *table) -{ - // Disconnect all connections from the previous table - if (m_table) { - disconnect(m_table, 0, this, 0); - } - - m_table = NULL; - if (table == NULL) { - setEnabled(false); - ui.nameEdit->clear(); - ui.columnsWidget->setColumnList(0); - } - else { - setEnabled(true); - ui.nameEdit->setText(table->name()); - ui.columnsWidget->setColumnList(table->columnList()); - connect(table, SIGNAL(propertyChanged(const QString &, const QVariant &)), SLOT(updateProperty(const QString &, const QVariant &))); - } - m_table = table; - updateColumnSelection(); -} - -void -TableProperties::updateProperty(const QString &name, const QVariant &value) -{ - if (name == "name") { - ui.nameEdit->setText(value.toString()); - } -} - -void -TableProperties::updateColumnSelection() -{ - QList columns = ui.columnsWidget->selectedColumns(); - if (columns.isEmpty()) { - ui.removeColumnButton->setEnabled(false); - ui.moveColumnUpButton->setEnabled(false); - ui.moveColumnDownButton->setEnabled(false); - } - else { - int index = columns[0]; - ui.removeColumnButton->setEnabled(true); - ui.moveColumnUpButton->setEnabled(index > 0); - ui.moveColumnDownButton->setEnabled(index + 1 < m_table->columnList()->columnCount()); - } -} diff --git a/src/items/database/tableproperties.h b/src/items/database/tableproperties.h deleted file mode 100644 index 6ad51a5..0000000 --- a/src/items/database/tableproperties.h +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (C) 2008 Lukas Lalinsky -// -// This program 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. -// -// This program 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. -// -// You should have received a copy of the GNU General Public License along -// with this program; if not, write to the Free Software Foundation, Inc., -// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -#ifndef TABLEPROPERTIES_H -#define TABLEPROPERTIES_H - -#include -#include -#include "ui_tableproperties.h" - -class DatabaseTable; -class MainWindow; - -class TableProperties : public QWidget -{ - Q_OBJECT - -public: - TableProperties(MainWindow *window, QWidget *parent = 0); - - DatabaseTable *table() { return m_table; } - void setTable(DatabaseTable *table); - -protected slots: - void setSelectedTableName(const QString &name); - void updateColumnSelection(); - void updateProperty(const QString &name, const QVariant &value); - -private: - MainWindow *m_window; - DatabaseTable *m_table; - Ui_TableProperties ui; -}; - -#endif diff --git a/src/items/database/tableproperties.ui b/src/items/database/tableproperties.ui deleted file mode 100644 index 0ad531a..0000000 --- a/src/items/database/tableproperties.ui +++ /dev/null @@ -1,137 +0,0 @@ - - TableProperties - - - - 0 - 0 - 471 - 192 - - - - - 0 - - - - - 0 - - - - &Definition - - - - - - Name: - - - - - - - - - - Qt::Vertical - - - - 158 - 113 - - - - - - - - - &Columns - - - - - - false - - - false - - - false - - - - - - - Add new column - - - &Add - - - - - - - Remove selected column - - - &Remove - - - - - - - Move selected column up - - - Move &Up - - - - - - - Move selected column down - - - Move &Down - - - - - - - Qt::Vertical - - - - 17 - 21 - - - - - - - - - - - - - ColumnListView - QTreeView -
items/database/columnlistview.h
-
-
- - -
diff --git a/src/main.cpp b/src/main.cpp index 0b7ece9..37fabe6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,7 @@ // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include +#include #include #include #include "mainwindow.h" diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 0a8a160..52cf5e6 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -15,6 +15,7 @@ // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include +#include #include #include #include @@ -28,20 +29,37 @@ #include #include #include -#include "mainwindow.h" -#include "items/database/databasetable.h" +#include +#include +#include "diagramitem.h" +#include "diagramitemfactory.h" +#include "diagramitemproperties.h" #include "commands.h" #include "utils/iconprovider.h" +#include "mainwindow.h" using namespace std; +class MainWindow::MainWindowPrivate +{ +public: + MainWindowPrivate() + {} + + QDockWidget *itemPropsDock; + QStackedWidget *propertyEditorsStack; + QMap propertyEditorsIndexes; +}; + MainWindow::MainWindow() - : QMainWindow(), m_model(NULL) + : QMainWindow(), d(new MainWindowPrivate), m_model(NULL) { m_undoGroup = new QUndoGroup(this); - setupActions(); setupUi(); + setupActions(); + setupToolBar(); + setupMenuBar(); newModel(); QIcon icon; @@ -64,6 +82,7 @@ MainWindow::~MainWindow() if (m_model) { m_undoGroup->removeStack(m_model->undoStack()); } + delete d; } void @@ -129,20 +148,25 @@ MainWindow::saveWindowState() void MainWindow::setupUi() { - setupToolBar(); - setupMenuBar(); - - m_properties = new TableProperties(this, this); + d->itemPropsDock = new QDockWidget(tr("&Properties"), this); + d->itemPropsDock->setObjectName("itemPropsDock"); + d->itemPropsDock->setFeatures( + QDockWidget::AllDockWidgetFeatures | + QDockWidget::DockWidgetVerticalTitleBar); + addDockWidget(Qt::BottomDockWidgetArea, d->itemPropsDock); + + d->propertyEditorsStack = new QStackedWidget(d->itemPropsDock); + d->propertyEditorsStack->addWidget(new QWidget(this)); + foreach (QString name, DiagramItemFactory::keys()) { + QWidget *editor = DiagramItemFactory::createPropertiesEditor(name, this); + if (editor) + d->propertyEditorsIndexes[name] = d->propertyEditorsStack->addWidget(editor); + } + d->itemPropsDock->setWidget(d->propertyEditorsStack); m_view = new DiagramView(this); - m_splitter = new QSplitter(Qt::Vertical, this); - setCentralWidget(m_splitter); - m_splitter->addWidget(m_view); - m_splitter->addWidget(m_properties); - - m_splitter->setStretchFactor(0, 6); - m_splitter->setStretchFactor(1, 1); + setCentralWidget(m_view); } void @@ -347,6 +371,8 @@ MainWindow::setupMenuBar() menu->addAction(m_actionDelete); QMenu *viewMenu = menuBar()->addMenu(tr("&View")); + viewMenu->addAction(d->itemPropsDock->toggleViewAction()); + viewMenu->addSeparator(); viewMenu->addAction(m_actionShowGrid); menu = menuBar()->addMenu(tr("&Help")); @@ -493,9 +519,29 @@ MainWindow::newModel(DiagramDocument *newModel) void MainWindow::updateSelection() { - DatabaseTable *table = m_model->selectedTable(); - m_properties->setTable(table); - if (table) { + QList items = m_model->selectedItems(); + + // Update the property editor + bool enablePropertiesEditor = false; + DiagramItemProperties *previousEditor = qobject_cast(d->propertyEditorsStack->currentWidget()); + if (previousEditor) + previousEditor->setCurrentItem(NULL); + if (items.size() == 1) { + DiagramItem *item = items.first(); + QString itemTypeName = item->typeName(); + if (d->propertyEditorsIndexes.contains(itemTypeName)) { + int index = d->propertyEditorsIndexes.value(itemTypeName); + d->propertyEditorsStack->setCurrentIndex(index); + static_cast(d->propertyEditorsStack->currentWidget())->setCurrentItem(item); + enablePropertiesEditor = true; + } + } + if (!enablePropertiesEditor) + d->propertyEditorsStack->setCurrentIndex(0); + d->propertyEditorsStack->setEnabled(enablePropertiesEditor); + + // Update edit actions + if (items.size() > 0) { m_actionCut->setEnabled(true); m_actionCopy->setEnabled(true); m_actionDelete->setEnabled(true); @@ -597,16 +643,16 @@ MainWindow::updateRecentFileActions() void MainWindow::cut() { - DatabaseTable *table = m_model->selectedTable(); +/* DatabaseTable *table = m_model->selectedTable(); QApplication::clipboard()->setMimeData(table->toMimeData()); - m_model->deleteSelectedItems(); + m_model->deleteSelectedItems();*/ } void MainWindow::copy() { - DatabaseTable *table = m_model->selectedTable(); - QApplication::clipboard()->setMimeData(table->toMimeData()); +/* DatabaseTable *table = m_model->selectedTable(); + QApplication::clipboard()->setMimeData(table->toMimeData());*/ } void diff --git a/src/mainwindow.h b/src/mainwindow.h index cc52934..d1d3e2a 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -25,7 +25,6 @@ #include #include "diagramview.h" #include "diagramdocument.h" -#include "items/database/tableproperties.h" class MainWindow: public QMainWindow { @@ -88,9 +87,10 @@ protected: void restoreWindowState(); private: + class MainWindowPrivate; + MainWindowPrivate *const d; + QUndoGroup *m_undoGroup; - QSplitter *m_splitter; - TableProperties *m_properties; DiagramView *m_view; DiagramDocument *m_model; diff --git a/src/src.pro b/src/src.pro index d637a89..22d66de 100644 --- a/src/src.pro +++ b/src/src.pro @@ -10,6 +10,7 @@ SOURCES = \ diagramconnection.cpp \ diagramitem.cpp \ diagramitemfactory.cpp \ + diagramitemproperties.cpp \ diagramobject.cpp \ commands.cpp HEADERS = \ @@ -19,6 +20,7 @@ HEADERS = \ diagramconnection.h \ diagramitem.h \ diagramitemfactory.h \ + diagramitemproperties.h \ diagramobject.h \ range.h \ commands.h diff --git a/src/utils/factory.h b/src/utils/factory.h index 0ed4b96..e9c86a5 100644 --- a/src/utils/factory.h +++ b/src/utils/factory.h @@ -21,7 +21,24 @@ #include #include "singelton.h" -template +template +class FactoryHelperBase +{ +public: + virtual TI *create() = 0; +}; + +template +class FactoryHelper : public FactoryHelperBase +{ +public: + TI *create() + { + return new ST(); + } +}; + +template class Factory : public Singelton { public: @@ -30,7 +47,7 @@ public: T *inst = T::instance(); if (!inst->m_map.contains(key)) return NULL; - return inst->m_map[key](); + return inst->m_map[key].create(); } static QStringList keys() { @@ -38,10 +55,9 @@ public: return inst->m_map.keys(); } protected: - template void add() { m_map[ST::staticTypeName()] = &(createObject); } - template static TI *createObject() { return new ST(); } + template void add() { m_map[ST::staticTypeName()] = Helper(); } typedef TI *(*CreateFunc)(); - QMap m_map; + QMap m_map; }; #define FACTORY_ADD_TYPE(ST) add(); diff --git a/src/utils/singelton.h b/src/utils/singelton.h index 743c5ba..d791e51 100644 --- a/src/utils/singelton.h +++ b/src/utils/singelton.h @@ -27,6 +27,8 @@ public: _instance = new T(); return _instance; } +protected: + Singelton() {} }; #endif -- cgit v1.2.3-54-g00ecf