// 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 "columnlistmodel.h" #include "databasemodel.h" #include "databasetable.h" #include "commands.h" #include "column.h" #define COLUMN_COUNT 5 ColumnListModel::ColumnListModel(QObject *parent) : QAbstractTableModel(parent), m_table(0) { } void ColumnListModel::setTable(DatabaseTable *table) { m_table = table; reset(); } int ColumnListModel::rowCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; return m_table ? m_table->columnCount() : 0; } int ColumnListModel::columnCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; return COLUMN_COUNT; } QVariant ColumnListModel::data(const QModelIndex &index, int role) const { if (m_table) { Q_ASSERT(index.isValid()); Column *column = m_table->column(index.row()); if (role == Qt::DisplayRole || role == Qt::EditRole) { if (index.column() == 0) { return column->name(); } if (index.column() == 1) { return column->dataType(); } if (index.column() == 4) { return column->notes(); } } if (role == Qt::CheckStateRole) { if (index.column() == 2) { return column->isRequired() ? Qt::Checked : Qt::Unchecked; } if (index.column() == 3) { return column->isPrimaryKey() ? Qt::Checked : Qt::Unchecked; } } } return QVariant(); } bool ColumnListModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (m_table) { Q_ASSERT(index.isValid()); Column *column = m_table->column(index.row()); if (role == Qt::DisplayRole || role == Qt::EditRole) { QString text = value.toString(); if (index.column() == 0) { column->table()->model()->undoStack()->push( new ChangeColumnPropertyCommand(column, Column::NameProperty, text)); goto OK; } if (index.column() == 1) { column->table()->model()->undoStack()->push( new ChangeColumnPropertyCommand(column, Column::DataTypeProperty, text)); goto OK; } if (index.column() == 4) { column->table()->model()->undoStack()->push( new ChangeColumnPropertyCommand(column, Column::NotesProperty, text)); goto OK; } } if (role == Qt::CheckStateRole) { bool checked = value.toInt() == Qt::Checked ? true : false; if (index.column() == 2) { column->table()->model()->undoStack()->push( new ChangeColumnPropertyCommand(column, Column::RequiredProperty, checked)); goto OK; } if (index.column() == 3) { column->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( new ChangeColumnPropertyCommand(column, Column::RequiredProperty, true)); } goto OK; } } } return false; OK: emit dataChanged(createIndex(index.row(), 0), createIndex(index.row(), COLUMN_COUNT)); return true; } Qt::ItemFlags ColumnListModel::flags(const QModelIndex &index) const { Q_ASSERT(index.isValid()); if (index.column() == 0 || index.column() == 1 || index.column() == 4) { return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable; } if (index.column() == 2 || index.column() == 3) { return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable; } return Qt::ItemIsSelectable | Qt::ItemIsEnabled; } QVariant ColumnListModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { if (section == 0) return tr("Name"); if (section == 1) return tr("Data Type"); if (section == 2) return tr("Req'd"); if (section == 3) return tr("PK"); if (section == 4) return tr("Notes"); } return QVariant(); } QModelIndex ColumnListModel::addColumn() { 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) { Q_ASSERT(m_table); int row = index.row(); beginRemoveRows(QModelIndex(), row, row); m_table->removeColumn(row); endRemoveRows(); } void ColumnListModel::swapColumns(int i1, int i2) { 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)); } QModelIndex ColumnListModel::indexFromRow(int i) const { return createIndex(i, 0); }