diff options
author | Lukáš Lalinský <lalinsky@gmail.com> | 2008-11-24 23:16:51 +0100 |
---|---|---|
committer | Lukáš Lalinský <lalinsky@gmail.com> | 2008-11-24 23:16:51 +0100 |
commit | c976b19ca29551587a649505920a8b07ec247b5b (patch) | |
tree | eca0c637a8045ab31f06087a7f6dfdfb4d81f7a7 /tableproperties.cpp | |
parent | 4b28f29b0dc2dbb24b6e056a715a780712b27a82 (diff) | |
download | dbmodel-c976b19ca29551587a649505920a8b07ec247b5b.tar.xz |
Implement column list as a standalone QTreeView-based widget
Diffstat (limited to 'tableproperties.cpp')
-rw-r--r-- | tableproperties.cpp | 135 |
1 files changed, 14 insertions, 121 deletions
diff --git a/tableproperties.cpp b/tableproperties.cpp index e44e172..ecd0966 100644 --- a/tableproperties.cpp +++ b/tableproperties.cpp @@ -17,24 +17,23 @@ #include "tableproperties.h" #include "databasetable.h" #include "column.h" +#include <QCheckBox> #include <QDebug> TableProperties::TableProperties(QWidget *parent) : QWidget(parent) { ui.setupUi(this); - setTable(0); connect(ui.nameEdit, SIGNAL(textEdited(const QString &)), SLOT(setSelectedTableName(const QString &))); - connect(ui.columnsWidget, - SIGNAL(itemChanged(QTreeWidgetItem *, int)), - SLOT(setSelectedTableColumnName(QTreeWidgetItem *, int))); - connect(ui.addColumnButton, SIGNAL(clicked()), SLOT(addColumn())); - connect(ui.removeColumnButton, SIGNAL(clicked()), SLOT(removeColumn())); - connect(ui.moveColumnUpButton, SIGNAL(clicked()), SLOT(moveColumnUp())); - connect(ui.moveColumnDownButton, SIGNAL(clicked()), SLOT(moveColumnDown())); - connect(ui.columnsWidget, SIGNAL(itemSelectionChanged()), SLOT(updateColumnSelection())); + 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 @@ -46,142 +45,36 @@ TableProperties::setSelectedTableName(const QString &name) } void -setColumnItem(QTreeWidgetItem *item, Column *column) -{ - item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled| Qt::ItemIsEditable); - item->setText(0, column->name()); - item->setText(1, column->dataType()); - item->setText(2, column->isRequired() ? "Y" : "N"); - item->setText(3, column->isPrimaryKey() ? "Y" : "N"); - item->setText(4, column->notes()); -} - -void -TableProperties::setSelectedTableColumnName(QTreeWidgetItem *item, int column) -{ - if (m_table) { - QString text = item->text(column); - int index = ui.columnsWidget->indexOfTopLevelItem(item); - if (column == 0) { - m_table->column(index)->setName(text); - } - else if (column == 1) { - m_table->column(index)->setDataType(text); - } - else if (column == 2) { - m_table->column(index)->setRequired(text.toLower() == "y"); - setColumnItem(item, m_table->column(index)); - } - else if (column == 3) { - m_table->column(index)->setPrimaryKey(text.toLower() == "y"); - setColumnItem(item, m_table->column(index)); - } - else if (column == 4) { - m_table->column(index)->setNotes(text); - } - } -} - -void TableProperties::setTable(DatabaseTable *table) { m_table = NULL; if (table == NULL) { setEnabled(false); ui.nameEdit->clear(); - ui.columnsWidget->clear(); + ui.columnsWidget->setTable(table); } else { setEnabled(true); ui.nameEdit->setText(table->name()); - ui.columnsWidget->clear(); - foreach (Column *column, table->columns()) { - QTreeWidgetItem *item = new QTreeWidgetItem(ui.columnsWidget); - setColumnItem(item, column); - } + ui.columnsWidget->setTable(table); } m_table = table; updateColumnSelection(); } void -TableProperties::addColumn() -{ - if (!m_table) - return; - - QString name = QString("column%0").arg(m_table->columnCount() + 1); - Column *column = m_table->addColumn(name); - QTreeWidgetItem *item = new QTreeWidgetItem(ui.columnsWidget); - setColumnItem(item, column); - ui.columnsWidget->setCurrentItem(item, 0); - ui.columnsWidget->editItem(item, 0); -} - -void -TableProperties::removeColumn() -{ - if (!m_table) - return; - - QList<QTreeWidgetItem *> items = ui.columnsWidget->selectedItems(); - foreach (QTreeWidgetItem *item, items) { - int index = ui.columnsWidget->indexOfTopLevelItem(item); - ui.columnsWidget->takeTopLevelItem(index); - m_table->removeColumn(index); - } -} - -void -TableProperties::moveColumnUp() -{ - if (m_table) { - QList<QTreeWidgetItem *> items = ui.columnsWidget->selectedItems(); - if (items.size() == 1) { - QTreeWidgetItem *item = items[0]; - int index = ui.columnsWidget->indexOfTopLevelItem(item); - if (index > 0) { - ui.columnsWidget->takeTopLevelItem(index); - ui.columnsWidget->insertTopLevelItem(index - 1, item); - ui.columnsWidget->setCurrentItem(item); - m_table->swapColumns(index, index - 1); - } - } - } -} - -void -TableProperties::moveColumnDown() -{ - if (m_table) { - QList<QTreeWidgetItem *> items = ui.columnsWidget->selectedItems(); - if (items.size() == 1) { - QTreeWidgetItem *item = items[0]; - int index = ui.columnsWidget->indexOfTopLevelItem(item); - if (index + 1 < ui.columnsWidget->topLevelItemCount()) { - ui.columnsWidget->takeTopLevelItem(index); - ui.columnsWidget->insertTopLevelItem(index + 1, item); - ui.columnsWidget->setCurrentItem(item); - m_table->swapColumns(index, index + 1); - } - } - } -} - -void TableProperties::updateColumnSelection() { - QList<QTreeWidgetItem *> items = ui.columnsWidget->selectedItems(); - if (items.isEmpty()) { + QList<int> columns = ui.columnsWidget->selectedColumns(); + if (columns.isEmpty()) { ui.removeColumnButton->setEnabled(false); ui.moveColumnUpButton->setEnabled(false); ui.moveColumnDownButton->setEnabled(false); } else { - QTreeWidgetItem *item = items[0]; - int index = ui.columnsWidget->indexOfTopLevelItem(item); + int index = columns[0]; ui.removeColumnButton->setEnabled(true); ui.moveColumnUpButton->setEnabled(index > 0); - ui.moveColumnDownButton->setEnabled(index + 1 < ui.columnsWidget->topLevelItemCount()); + ui.moveColumnDownButton->setEnabled(index + 1 < m_table->columnCount()); } } |