From dffb276e8e1204aef906f7c38df2d23dc22cf0e5 Mon Sep 17 00:00:00 2001 From: Tobias Hunger <tobias.hunger@nokia.com> Date: Mon, 19 Apr 2010 18:43:17 +0200 Subject: [PATCH] Update model code * Be more paranoid when checking parameters * Use QAbstractTableModel instead of ItemModel, remove methods no longer needed * Update to newer interfaces for reseting the model * rename index() method which shadows the index from QAbstractTableModel Reviewed-by: dt --- .../projectexplorer/environmenteditmodel.cpp | 52 +++++++------------ .../projectexplorer/environmenteditmodel.h | 16 +++--- 2 files changed, 25 insertions(+), 43 deletions(-) diff --git a/src/plugins/projectexplorer/environmenteditmodel.cpp b/src/plugins/projectexplorer/environmenteditmodel.cpp index 33190ec9f47..72e727e87e3 100644 --- a/src/plugins/projectexplorer/environmenteditmodel.cpp +++ b/src/plugins/projectexplorer/environmenteditmodel.cpp @@ -59,6 +59,8 @@ void EnvironmentModel::updateResultEnvironment() { m_resultEnvironment = m_baseEnvironment; m_resultEnvironment.modify(m_items); + // Add removed variables again and mark them as "<UNSET>" so + // that the user can actually see those removals: foreach (const EnvironmentItem &item, m_items) { if (item.unset) { m_resultEnvironment.set(item.name, tr("<UNSET>")); @@ -68,9 +70,10 @@ void EnvironmentModel::updateResultEnvironment() void EnvironmentModel::setBaseEnvironment(const ProjectExplorer::Environment &env) { + beginResetModel(); m_baseEnvironment = env; updateResultEnvironment(); - reset(); + endResetModel(); } int EnvironmentModel::rowCount(const QModelIndex &parent) const @@ -82,25 +85,23 @@ int EnvironmentModel::rowCount(const QModelIndex &parent) const } int EnvironmentModel::columnCount(const QModelIndex &parent) const { - Q_UNUSED(parent) + if (parent.isValid()) + return 0; + return 2; } bool EnvironmentModel::changes(const QString &name) const { - foreach (const EnvironmentItem &item, m_items) - if (item.name == name) - return true; - return false; + return findInChanges(name) >= 0; } QVariant EnvironmentModel::data(const QModelIndex &index, int role) const { - if ((role == Qt::DisplayRole || role == Qt::EditRole) && index.isValid()) { - if (index.row() >= m_resultEnvironment.size()) { - return QVariant(); - } + if (!index.isValid()) + return QVariant(); + if ((role == Qt::DisplayRole || role == Qt::EditRole)) { if (index.column() == 0) { return m_resultEnvironment.key(m_resultEnvironment.constBegin() + index.row()); } else if (index.column() == 1) { @@ -131,14 +132,6 @@ Qt::ItemFlags EnvironmentModel::flags(const QModelIndex &index) const return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled; } -bool EnvironmentModel::hasChildren(const QModelIndex &index) const -{ - if (!index.isValid()) - return true; - else - return false; -} - QVariant EnvironmentModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Vertical || role != Qt::DisplayRole) @@ -146,19 +139,6 @@ QVariant EnvironmentModel::headerData(int section, Qt::Orientation orientation, return section == 0 ? tr("Variable") : tr("Value"); } -QModelIndex EnvironmentModel::index(int row, int column, const QModelIndex &parent) const -{ - if (!parent.isValid()) - return createIndex(row, column, 0); - return QModelIndex(); -} - -QModelIndex EnvironmentModel::parent(const QModelIndex &index) const -{ - Q_UNUSED(index) - return QModelIndex(); -} - /// ***************** /// Utility functions /// ***************** @@ -178,7 +158,7 @@ int EnvironmentModel::findInChangesInsertPosition(const QString &name) const return m_items.size(); } -QModelIndex EnvironmentModel::index(const QString &name) +QModelIndex EnvironmentModel::variableToIndex(const QString &name) const { int row = findInResult(name); if (row == -1) @@ -208,6 +188,9 @@ int EnvironmentModel::findInResultInsertPosition(const QString &name) const bool EnvironmentModel::setData(const QModelIndex &index, const QVariant &value, int role) { + if (!index.isValid()) + return false; + if (role == Qt::EditRole && index.isValid()) { // ignore changes to already set values: if (data(index, role) == value) @@ -357,9 +340,10 @@ QList<EnvironmentItem> EnvironmentModel::userChanges() const void EnvironmentModel::setUserChanges(QList<EnvironmentItem> list) { + beginResetModel(); m_items = list; updateResultEnvironment(); - emit reset(); + endResetModel(); } //// @@ -453,7 +437,7 @@ EnvironmentWidget::~EnvironmentWidget() void EnvironmentWidget::renamedVariable(const QString &name) { - QModelIndex idx = m_model->index(name); + QModelIndex idx = m_model->variableToIndex(name); m_environmentTreeView->setCurrentIndex(idx); m_environmentTreeView->setFocus(); } diff --git a/src/plugins/projectexplorer/environmenteditmodel.h b/src/plugins/projectexplorer/environmenteditmodel.h index 3200bdfd26f..0560cb66f3f 100644 --- a/src/plugins/projectexplorer/environmenteditmodel.h +++ b/src/plugins/projectexplorer/environmenteditmodel.h @@ -33,7 +33,7 @@ #include "environment.h" #include <QtCore/QString> -#include <QtCore/QAbstractItemModel> +#include <QtCore/QAbstractTableModel> #include <QtGui/QWidget> QT_BEGIN_NAMESPACE @@ -47,32 +47,30 @@ class DetailsWidget; namespace ProjectExplorer { -class EnvironmentModel : public QAbstractItemModel +class EnvironmentModel : public QAbstractTableModel { Q_OBJECT public: EnvironmentModel(); ~EnvironmentModel(); - void setBaseEnvironment(const ProjectExplorer::Environment &env); + int rowCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); Qt::ItemFlags flags(const QModelIndex &index) const; - bool hasChildren(const QModelIndex &index) const; - QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; - QModelIndex parent(const QModelIndex &index) const; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + QModelIndex addVariable(); QModelIndex addVariable(const EnvironmentItem &item); void removeVariable(const QString &name); void unset(const QString &name); bool isUnset(const QString &name); bool isInBaseEnvironment(const QString &name); - QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; QString indexToVariable(const QModelIndex &index) const; - QModelIndex index(const QString &name); + QModelIndex variableToIndex(const QString &name) const; bool changes(const QString &key) const; + void setBaseEnvironment(const ProjectExplorer::Environment &env); QList<EnvironmentItem> userChanges() const; void setUserChanges(QList<EnvironmentItem> list); -- GitLab