Skip to content
Snippets Groups Projects
Commit dffb276e authored by Tobias Hunger's avatar Tobias Hunger
Browse files

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
parent 76738c21
No related branches found
No related tags found
No related merge requests found
...@@ -59,6 +59,8 @@ void EnvironmentModel::updateResultEnvironment() ...@@ -59,6 +59,8 @@ void EnvironmentModel::updateResultEnvironment()
{ {
m_resultEnvironment = m_baseEnvironment; m_resultEnvironment = m_baseEnvironment;
m_resultEnvironment.modify(m_items); 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) { foreach (const EnvironmentItem &item, m_items) {
if (item.unset) { if (item.unset) {
m_resultEnvironment.set(item.name, tr("<UNSET>")); m_resultEnvironment.set(item.name, tr("<UNSET>"));
...@@ -68,9 +70,10 @@ void EnvironmentModel::updateResultEnvironment() ...@@ -68,9 +70,10 @@ void EnvironmentModel::updateResultEnvironment()
void EnvironmentModel::setBaseEnvironment(const ProjectExplorer::Environment &env) void EnvironmentModel::setBaseEnvironment(const ProjectExplorer::Environment &env)
{ {
beginResetModel();
m_baseEnvironment = env; m_baseEnvironment = env;
updateResultEnvironment(); updateResultEnvironment();
reset(); endResetModel();
} }
int EnvironmentModel::rowCount(const QModelIndex &parent) const int EnvironmentModel::rowCount(const QModelIndex &parent) const
...@@ -82,25 +85,23 @@ 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 int EnvironmentModel::columnCount(const QModelIndex &parent) const
{ {
Q_UNUSED(parent) if (parent.isValid())
return 0;
return 2; return 2;
} }
bool EnvironmentModel::changes(const QString &name) const bool EnvironmentModel::changes(const QString &name) const
{ {
foreach (const EnvironmentItem &item, m_items) return findInChanges(name) >= 0;
if (item.name == name)
return true;
return false;
} }
QVariant EnvironmentModel::data(const QModelIndex &index, int role) const QVariant EnvironmentModel::data(const QModelIndex &index, int role) const
{ {
if ((role == Qt::DisplayRole || role == Qt::EditRole) && index.isValid()) { if (!index.isValid())
if (index.row() >= m_resultEnvironment.size()) { return QVariant();
return QVariant();
}
if ((role == Qt::DisplayRole || role == Qt::EditRole)) {
if (index.column() == 0) { if (index.column() == 0) {
return m_resultEnvironment.key(m_resultEnvironment.constBegin() + index.row()); return m_resultEnvironment.key(m_resultEnvironment.constBegin() + index.row());
} else if (index.column() == 1) { } else if (index.column() == 1) {
...@@ -131,14 +132,6 @@ Qt::ItemFlags EnvironmentModel::flags(const QModelIndex &index) const ...@@ -131,14 +132,6 @@ Qt::ItemFlags EnvironmentModel::flags(const QModelIndex &index) const
return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled; 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 QVariant EnvironmentModel::headerData(int section, Qt::Orientation orientation, int role) const
{ {
if (orientation == Qt::Vertical || role != Qt::DisplayRole) if (orientation == Qt::Vertical || role != Qt::DisplayRole)
...@@ -146,19 +139,6 @@ QVariant EnvironmentModel::headerData(int section, Qt::Orientation orientation, ...@@ -146,19 +139,6 @@ QVariant EnvironmentModel::headerData(int section, Qt::Orientation orientation,
return section == 0 ? tr("Variable") : tr("Value"); 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 /// Utility functions
/// ***************** /// *****************
...@@ -178,7 +158,7 @@ int EnvironmentModel::findInChangesInsertPosition(const QString &name) const ...@@ -178,7 +158,7 @@ int EnvironmentModel::findInChangesInsertPosition(const QString &name) const
return m_items.size(); return m_items.size();
} }
QModelIndex EnvironmentModel::index(const QString &name) QModelIndex EnvironmentModel::variableToIndex(const QString &name) const
{ {
int row = findInResult(name); int row = findInResult(name);
if (row == -1) if (row == -1)
...@@ -208,6 +188,9 @@ int EnvironmentModel::findInResultInsertPosition(const QString &name) const ...@@ -208,6 +188,9 @@ int EnvironmentModel::findInResultInsertPosition(const QString &name) const
bool EnvironmentModel::setData(const QModelIndex &index, const QVariant &value, int role) bool EnvironmentModel::setData(const QModelIndex &index, const QVariant &value, int role)
{ {
if (!index.isValid())
return false;
if (role == Qt::EditRole && index.isValid()) { if (role == Qt::EditRole && index.isValid()) {
// ignore changes to already set values: // ignore changes to already set values:
if (data(index, role) == value) if (data(index, role) == value)
...@@ -357,9 +340,10 @@ QList<EnvironmentItem> EnvironmentModel::userChanges() const ...@@ -357,9 +340,10 @@ QList<EnvironmentItem> EnvironmentModel::userChanges() const
void EnvironmentModel::setUserChanges(QList<EnvironmentItem> list) void EnvironmentModel::setUserChanges(QList<EnvironmentItem> list)
{ {
beginResetModel();
m_items = list; m_items = list;
updateResultEnvironment(); updateResultEnvironment();
emit reset(); endResetModel();
} }
//// ////
...@@ -453,7 +437,7 @@ EnvironmentWidget::~EnvironmentWidget() ...@@ -453,7 +437,7 @@ EnvironmentWidget::~EnvironmentWidget()
void EnvironmentWidget::renamedVariable(const QString &name) void EnvironmentWidget::renamedVariable(const QString &name)
{ {
QModelIndex idx = m_model->index(name); QModelIndex idx = m_model->variableToIndex(name);
m_environmentTreeView->setCurrentIndex(idx); m_environmentTreeView->setCurrentIndex(idx);
m_environmentTreeView->setFocus(); m_environmentTreeView->setFocus();
} }
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
#include "environment.h" #include "environment.h"
#include <QtCore/QString> #include <QtCore/QString>
#include <QtCore/QAbstractItemModel> #include <QtCore/QAbstractTableModel>
#include <QtGui/QWidget> #include <QtGui/QWidget>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
...@@ -47,32 +47,30 @@ class DetailsWidget; ...@@ -47,32 +47,30 @@ class DetailsWidget;
namespace ProjectExplorer { namespace ProjectExplorer {
class EnvironmentModel : public QAbstractItemModel class EnvironmentModel : public QAbstractTableModel
{ {
Q_OBJECT Q_OBJECT
public: public:
EnvironmentModel(); EnvironmentModel();
~EnvironmentModel(); ~EnvironmentModel();
void setBaseEnvironment(const ProjectExplorer::Environment &env);
int rowCount(const QModelIndex &parent) const; int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
Qt::ItemFlags flags(const QModelIndex &index) const; Qt::ItemFlags flags(const QModelIndex &index) const;
bool hasChildren(const QModelIndex &index) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
QModelIndex addVariable(); QModelIndex addVariable();
QModelIndex addVariable(const EnvironmentItem &item); QModelIndex addVariable(const EnvironmentItem &item);
void removeVariable(const QString &name); void removeVariable(const QString &name);
void unset(const QString &name); void unset(const QString &name);
bool isUnset(const QString &name); bool isUnset(const QString &name);
bool isInBaseEnvironment(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; QString indexToVariable(const QModelIndex &index) const;
QModelIndex index(const QString &name); QModelIndex variableToIndex(const QString &name) const;
bool changes(const QString &key) const; bool changes(const QString &key) const;
void setBaseEnvironment(const ProjectExplorer::Environment &env);
QList<EnvironmentItem> userChanges() const; QList<EnvironmentItem> userChanges() const;
void setUserChanges(QList<EnvironmentItem> list); void setUserChanges(QList<EnvironmentItem> list);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment