diff options
author | Lukáš Lalinský <lalinsky@gmail.com> | 2008-12-06 11:36:39 +0100 |
---|---|---|
committer | Lukáš Lalinský <lalinsky@gmail.com> | 2008-12-06 11:36:39 +0100 |
commit | 54bf61f97071a93619a4a03028f8b71e4fbd6225 (patch) | |
tree | 87d1b156b5fcb9be2e655f3a16ec3227f43cf7ee /columnlistmodel.cpp | |
parent | d3a08d8f3b6302133452a7434f4d05a899830dfa (diff) | |
download | dbmodel-54bf61f97071a93619a4a03028f8b71e4fbd6225.tar.xz |
Covert column operations to the undo/command framework
Diffstat (limited to 'columnlistmodel.cpp')
-rw-r--r-- | columnlistmodel.cpp | 80 |
1 files changed, 45 insertions, 35 deletions
diff --git a/columnlistmodel.cpp b/columnlistmodel.cpp index f2c8a99..b0ff343 100644 --- a/columnlistmodel.cpp +++ b/columnlistmodel.cpp @@ -20,18 +20,29 @@ #include "databasetable.h" #include "commands.h" #include "column.h" +#include "columnlist.h" #define COLUMN_COUNT 5 ColumnListModel::ColumnListModel(QObject *parent) - : QAbstractTableModel(parent), m_table(0) + : QAbstractTableModel(parent), m_columnList(0) { } void -ColumnListModel::setTable(DatabaseTable *table) +ColumnListModel::setColumnList(ColumnList *columnList) { - m_table = table; + if (m_columnList) { + disconnect(m_columnList, 0, this, 0); + } + m_columnList = columnList; + if (m_columnList) { + connect(m_columnList, SIGNAL(columnAboutToBeInserted(int)), this, SLOT(_columnAboutToBeInserted(int))); + connect(m_columnList, SIGNAL(columnInserted(int)), this, SLOT(_columnInserted())); + connect(m_columnList, SIGNAL(columnAboutToBeRemoved(int)), this, SLOT(_columnAboutToBeRemoved(int))); + connect(m_columnList, SIGNAL(columnRemoved(int)), this, SLOT(_columnRemoved())); + connect(m_columnList, SIGNAL(columnChanged(int)), this, SLOT(_columnChanged(int))); + } reset(); } @@ -40,7 +51,7 @@ ColumnListModel::rowCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; - return m_table ? m_table->columnCount() : 0; + return m_columnList ? m_columnList->columnCount() : 0; } int @@ -54,9 +65,9 @@ ColumnListModel::columnCount(const QModelIndex &parent) const QVariant ColumnListModel::data(const QModelIndex &index, int role) const { - if (m_table) { + if (m_columnList) { Q_ASSERT(index.isValid()); - Column *column = m_table->column(index.row()); + Column *column = m_columnList->column(index.row()); if (role == Qt::DisplayRole || role == Qt::EditRole) { if (index.column() == 0) { return column->name(); @@ -83,23 +94,23 @@ ColumnListModel::data(const QModelIndex &index, int role) const bool ColumnListModel::setData(const QModelIndex &index, const QVariant &value, int role) { - if (m_table) { + if (m_columnList) { Q_ASSERT(index.isValid()); - Column *column = m_table->column(index.row()); + Column *column = m_columnList->column(index.row()); if (role == Qt::DisplayRole || role == Qt::EditRole) { QString text = value.toString(); if (index.column() == 0) { - column->table()->model()->undoStack()->push( + m_columnList->table()->model()->undoStack()->push( new ChangeColumnPropertyCommand(column, Column::NameProperty, text)); goto OK; } if (index.column() == 1) { - column->table()->model()->undoStack()->push( + m_columnList->table()->model()->undoStack()->push( new ChangeColumnPropertyCommand(column, Column::DataTypeProperty, text)); goto OK; } if (index.column() == 4) { - column->table()->model()->undoStack()->push( + m_columnList->table()->model()->undoStack()->push( new ChangeColumnPropertyCommand(column, Column::NotesProperty, text)); goto OK; } @@ -107,16 +118,16 @@ ColumnListModel::setData(const QModelIndex &index, const QVariant &value, int ro if (role == Qt::CheckStateRole) { bool checked = value.toInt() == Qt::Checked ? true : false; if (index.column() == 2) { - column->table()->model()->undoStack()->push( + m_columnList->table()->model()->undoStack()->push( new ChangeColumnPropertyCommand(column, Column::RequiredProperty, checked)); goto OK; } if (index.column() == 3) { - column->table()->model()->undoStack()->push( + m_columnList->table()->model()->undoStack()->push( new ChangeColumnPropertyCommand(column, Column::PrimaryKeyProperty, checked)); if (!column->isRequired()) { // TODO is there a simple way to group this with the previous command? - column->table()->model()->undoStack()->push( + m_columnList->table()->model()->undoStack()->push( new ChangeColumnPropertyCommand(column, Column::RequiredProperty, true)); } goto OK; @@ -161,38 +172,37 @@ ColumnListModel::headerData(int section, Qt::Orientation orientation, int role) } QModelIndex -ColumnListModel::addColumn() +ColumnListModel::indexFromRow(int i) const +{ + return createIndex(i, 0); +} + +void +ColumnListModel::_columnAboutToBeInserted(int index) +{ + beginInsertRows(QModelIndex(), index, index); +} + +void +ColumnListModel::_columnInserted() { - Q_ASSERT(m_table); - int row = m_table->columnCount(); - beginInsertRows(QModelIndex(), row, row); - m_table->addColumn(); endInsertRows(); - return createIndex(row, 0); } void -ColumnListModel::removeColumn(const QModelIndex &index) +ColumnListModel::_columnAboutToBeRemoved(int index) { - Q_ASSERT(m_table); - int row = index.row(); - beginRemoveRows(QModelIndex(), row, row); - m_table->removeColumn(row); - endRemoveRows(); + beginRemoveRows(QModelIndex(), index, index); } void -ColumnListModel::swapColumns(int i1, int i2) +ColumnListModel::_columnRemoved() { - Q_ASSERT(m_table); - int j1 = qMin(i1, i2); - int j2 = qMax(i1, i2); - m_table->swapColumns(j1, j2); - emit dataChanged(createIndex(j1, 0), createIndex(j2, COLUMN_COUNT)); + endRemoveRows(); } -QModelIndex -ColumnListModel::indexFromRow(int i) const +void +ColumnListModel::_columnChanged(int index) { - return createIndex(i, 0); + emit dataChanged(createIndex(index, 0), createIndex(index, COLUMN_COUNT)); } |