2011-01-13 11:41:45 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2011-01-13 11:41:45 +01:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** This file may be used under the terms of the GNU Lesser General Public
|
|
|
|
|
** License version 2.1 as published by the Free Software Foundation and
|
|
|
|
|
** appearing in the file LICENSE.LGPL included in the packaging of this file.
|
|
|
|
|
** Please review the following information to ensure the GNU Lesser General
|
|
|
|
|
** Public License version 2.1 requirements will be met:
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2011-01-13 11:41:45 +01:00
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2011-01-13 11:41:45 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2011-01-13 11:41:45 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2011-01-13 11:41:45 +01:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "environmentmodel.h"
|
|
|
|
|
|
|
|
|
|
#include <utils/environment.h>
|
|
|
|
|
|
|
|
|
|
#include <QtGui/QFont>
|
|
|
|
|
|
|
|
|
|
namespace Utils {
|
|
|
|
|
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
class EnvironmentModelPrivate
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
void 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 Utils::EnvironmentItem &item, m_items) {
|
|
|
|
|
if (item.unset) {
|
|
|
|
|
m_resultEnvironment.set(item.name, EnvironmentModel::tr("<UNSET>"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int findInChanges(const QString &name) const
|
|
|
|
|
{
|
|
|
|
|
for (int i=0; i<m_items.size(); ++i)
|
|
|
|
|
if (m_items.at(i).name == name)
|
|
|
|
|
return i;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int findInResultInsertPosition(const QString &name) const
|
|
|
|
|
{
|
|
|
|
|
Utils::Environment::const_iterator it;
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (it = m_resultEnvironment.constBegin(); it != m_resultEnvironment.constEnd(); ++it, ++i)
|
|
|
|
|
if (m_resultEnvironment.key(it) > name)
|
|
|
|
|
return i;
|
|
|
|
|
return m_resultEnvironment.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int findInResult(const QString &name) const
|
|
|
|
|
{
|
|
|
|
|
Utils::Environment::const_iterator it;
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (it = m_resultEnvironment.constBegin(); it != m_resultEnvironment.constEnd(); ++it, ++i)
|
|
|
|
|
if (m_resultEnvironment.key(it) == name)
|
|
|
|
|
return i;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::Environment m_baseEnvironment;
|
|
|
|
|
Utils::Environment m_resultEnvironment;
|
|
|
|
|
QList<Utils::EnvironmentItem> m_items;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
|
|
|
|
|
EnvironmentModel::EnvironmentModel(QObject *parent) :
|
|
|
|
|
QAbstractTableModel(parent),
|
2011-09-07 14:26:11 +02:00
|
|
|
d(new Internal::EnvironmentModelPrivate)
|
2011-01-13 11:41:45 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
EnvironmentModel::~EnvironmentModel()
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
delete d;
|
2011-01-13 11:41:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString EnvironmentModel::indexToVariable(const QModelIndex &index) const
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_resultEnvironment.key(d->m_resultEnvironment.constBegin() + index.row());
|
2011-01-13 11:41:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EnvironmentModel::setBaseEnvironment(const Utils::Environment &env)
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
if (d->m_baseEnvironment == env)
|
2011-01-13 11:41:45 +01:00
|
|
|
return;
|
|
|
|
|
beginResetModel();
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_baseEnvironment = env;
|
|
|
|
|
d->updateResultEnvironment();
|
2011-01-13 11:41:45 +01:00
|
|
|
endResetModel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EnvironmentModel::rowCount(const QModelIndex &parent) const
|
|
|
|
|
{
|
|
|
|
|
if (parent.isValid())
|
|
|
|
|
return 0;
|
|
|
|
|
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_resultEnvironment.size();
|
2011-01-13 11:41:45 +01:00
|
|
|
}
|
|
|
|
|
int EnvironmentModel::columnCount(const QModelIndex &parent) const
|
|
|
|
|
{
|
|
|
|
|
if (parent.isValid())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool EnvironmentModel::changes(const QString &name) const
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->findInChanges(name) >= 0;
|
2011-01-13 11:41:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant EnvironmentModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
{
|
|
|
|
|
if (!index.isValid())
|
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::ToolTipRole) {
|
|
|
|
|
if (index.column() == 0) {
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_resultEnvironment.key(d->m_resultEnvironment.constBegin() + index.row());
|
2011-01-13 11:41:45 +01:00
|
|
|
} else if (index.column() == 1) {
|
|
|
|
|
// Do not return "<UNSET>" when editing a previously unset variable:
|
|
|
|
|
if (role == Qt::EditRole) {
|
2011-09-07 14:26:11 +02:00
|
|
|
int pos = d->findInChanges(indexToVariable(index));
|
2011-01-13 11:41:45 +01:00
|
|
|
if (pos >= 0)
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_items.at(pos).value;
|
2011-01-13 11:41:45 +01:00
|
|
|
}
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_resultEnvironment.value(d->m_resultEnvironment.constBegin() + index.row());
|
2011-01-13 11:41:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (role == Qt::FontRole) {
|
2011-09-15 13:42:38 +02:00
|
|
|
// check whether this environment variable exists in d->m_items
|
2011-09-07 14:26:11 +02:00
|
|
|
if (changes(d->m_resultEnvironment.key(d->m_resultEnvironment.constBegin() + index.row()))) {
|
2011-01-13 11:41:45 +01:00
|
|
|
QFont f;
|
|
|
|
|
f.setBold(true);
|
|
|
|
|
return QVariant(f);
|
|
|
|
|
}
|
|
|
|
|
return QFont();
|
|
|
|
|
}
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Qt::ItemFlags EnvironmentModel::flags(const QModelIndex &index) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(index)
|
|
|
|
|
return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant EnvironmentModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
|
{
|
|
|
|
|
if (orientation == Qt::Vertical || role != Qt::DisplayRole)
|
|
|
|
|
return QVariant();
|
|
|
|
|
return section == 0 ? tr("Variable") : tr("Value");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// *****************
|
|
|
|
|
/// Utility functions
|
|
|
|
|
/// *****************
|
|
|
|
|
QModelIndex EnvironmentModel::variableToIndex(const QString &name) const
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
int row = d->findInResult(name);
|
2011-01-13 11:41:45 +01:00
|
|
|
if (row == -1)
|
|
|
|
|
return QModelIndex();
|
|
|
|
|
return index(row, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool EnvironmentModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
|
{
|
|
|
|
|
if (!index.isValid() || role != Qt::EditRole)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// ignore changes to already set values:
|
|
|
|
|
if (data(index, role) == value)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
const QString oldName = data(this->index(index.row(), 0, QModelIndex())).toString();
|
|
|
|
|
const QString oldValue = data(this->index(index.row(), 1, QModelIndex())).toString();
|
2011-09-07 14:26:11 +02:00
|
|
|
int changesPos = d->findInChanges(oldName);
|
2011-01-13 11:41:45 +01:00
|
|
|
|
|
|
|
|
if (index.column() == 0) {
|
|
|
|
|
//fail if a variable with the same name already exists
|
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
|
const QString &newName = value.toString().toUpper();
|
|
|
|
|
#else
|
|
|
|
|
const QString &newName = value.toString();
|
|
|
|
|
#endif
|
|
|
|
|
// Does the new name exist already?
|
2011-12-06 15:23:46 +01:00
|
|
|
if (d->m_resultEnvironment.hasKey(newName) || newName.isEmpty())
|
2011-01-13 11:41:45 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
Utils::EnvironmentItem newVariable(newName, oldValue);
|
|
|
|
|
|
|
|
|
|
if (changesPos != -1)
|
|
|
|
|
resetVariable(oldName); // restore the original base variable again
|
|
|
|
|
|
|
|
|
|
QModelIndex newIndex = addVariable(newVariable); // add the new variable
|
|
|
|
|
emit focusIndex(newIndex.sibling(newIndex.row(), 1)); // hint to focus on the value
|
|
|
|
|
return true;
|
|
|
|
|
} else if (index.column() == 1) {
|
|
|
|
|
// We are changing an existing value:
|
|
|
|
|
const QString stringValue = value.toString();
|
|
|
|
|
if (changesPos != -1) {
|
|
|
|
|
// We have already changed this value
|
2011-12-06 15:23:46 +01:00
|
|
|
if (d->m_baseEnvironment.hasKey(oldName) && stringValue == d->m_baseEnvironment.value(oldName)) {
|
2011-01-13 11:41:45 +01:00
|
|
|
// ... and now went back to the base value
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_items.removeAt(changesPos);
|
2011-01-13 11:41:45 +01:00
|
|
|
} else {
|
|
|
|
|
// ... and changed it again
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_items[changesPos].value = stringValue;
|
|
|
|
|
d->m_items[changesPos].unset = false;
|
2011-01-13 11:41:45 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Add a new change item:
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_items.append(Utils::EnvironmentItem(oldName, stringValue));
|
2011-01-13 11:41:45 +01:00
|
|
|
}
|
2011-09-07 14:26:11 +02:00
|
|
|
d->updateResultEnvironment();
|
2011-01-13 11:41:45 +01:00
|
|
|
emit dataChanged(index, index);
|
|
|
|
|
emit userChangesChanged();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QModelIndex EnvironmentModel::addVariable()
|
|
|
|
|
{
|
|
|
|
|
//: Name when inserting a new variable
|
|
|
|
|
return addVariable(Utils::EnvironmentItem(tr("<VARIABLE>"),
|
|
|
|
|
//: Value when inserting a new variable
|
|
|
|
|
tr("<VALUE>")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QModelIndex EnvironmentModel::addVariable(const Utils::EnvironmentItem &item)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// Return existing index if the name is already in the result set:
|
2011-09-07 14:26:11 +02:00
|
|
|
int pos = d->findInResult(item.name);
|
|
|
|
|
if (pos >= 0 && pos < d->m_resultEnvironment.size())
|
2011-01-13 11:41:45 +01:00
|
|
|
return index(pos, 0, QModelIndex());
|
|
|
|
|
|
2011-09-07 14:26:11 +02:00
|
|
|
int insertPos = d->findInResultInsertPosition(item.name);
|
|
|
|
|
int changePos = d->findInChanges(item.name);
|
|
|
|
|
if (d->m_baseEnvironment.hasKey(item.name)) {
|
2011-01-13 11:41:45 +01:00
|
|
|
// We previously unset this!
|
|
|
|
|
Q_ASSERT(changePos >= 0);
|
|
|
|
|
// Do not insert a line here as we listed the variable as <UNSET> before!
|
2011-09-07 14:26:11 +02:00
|
|
|
Q_ASSERT(d->m_items.at(changePos).name == item.name);
|
|
|
|
|
Q_ASSERT(d->m_items.at(changePos).unset);
|
|
|
|
|
Q_ASSERT(d->m_items.at(changePos).value.isEmpty());
|
|
|
|
|
d->m_items[changePos] = item;
|
2011-01-13 11:41:45 +01:00
|
|
|
emit dataChanged(index(insertPos, 0, QModelIndex()), index(insertPos, 1, QModelIndex()));
|
|
|
|
|
} else {
|
|
|
|
|
// We add something that is not in the base environment
|
|
|
|
|
// Insert a new line!
|
|
|
|
|
beginInsertRows(QModelIndex(), insertPos, insertPos);
|
|
|
|
|
Q_ASSERT(changePos < 0);
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_items.append(item);
|
|
|
|
|
d->updateResultEnvironment();
|
2011-01-13 11:41:45 +01:00
|
|
|
endInsertRows();
|
|
|
|
|
}
|
|
|
|
|
emit userChangesChanged();
|
|
|
|
|
return index(insertPos, 0, QModelIndex());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EnvironmentModel::resetVariable(const QString &name)
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
int rowInChanges = d->findInChanges(name);
|
2011-01-13 11:41:45 +01:00
|
|
|
if (rowInChanges < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2011-09-07 14:26:11 +02:00
|
|
|
int rowInResult = d->findInResult(name);
|
2011-01-13 11:41:45 +01:00
|
|
|
if (rowInResult < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2011-09-07 14:26:11 +02:00
|
|
|
if (d->m_baseEnvironment.hasKey(name)) {
|
|
|
|
|
d->m_items.removeAt(rowInChanges);
|
|
|
|
|
d->updateResultEnvironment();
|
2011-01-13 11:41:45 +01:00
|
|
|
emit dataChanged(index(rowInResult, 0, QModelIndex()), index(rowInResult, 1, QModelIndex()));
|
|
|
|
|
emit userChangesChanged();
|
|
|
|
|
} else {
|
|
|
|
|
// Remove the line completely!
|
|
|
|
|
beginRemoveRows(QModelIndex(), rowInResult, rowInResult);
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_items.removeAt(rowInChanges);
|
|
|
|
|
d->updateResultEnvironment();
|
2011-01-13 11:41:45 +01:00
|
|
|
endRemoveRows();
|
|
|
|
|
emit userChangesChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EnvironmentModel::unsetVariable(const QString &name)
|
|
|
|
|
{
|
|
|
|
|
// This does not change the number of rows as we will display a <UNSET>
|
|
|
|
|
// in place of the original variable!
|
2011-09-07 14:26:11 +02:00
|
|
|
int row = d->findInResult(name);
|
2011-01-13 11:41:45 +01:00
|
|
|
if (row < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2011-09-15 13:42:38 +02:00
|
|
|
// look in d->m_items for the variable
|
2011-09-07 14:26:11 +02:00
|
|
|
int pos = d->findInChanges(name);
|
2011-01-13 11:41:45 +01:00
|
|
|
if (pos != -1) {
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_items[pos].unset = true;
|
|
|
|
|
d->m_items[pos].value.clear();
|
|
|
|
|
d->updateResultEnvironment();
|
2011-01-13 11:41:45 +01:00
|
|
|
emit dataChanged(index(row, 0, QModelIndex()), index(row, 1, QModelIndex()));
|
|
|
|
|
emit userChangesChanged();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Utils::EnvironmentItem item(name, QString());
|
|
|
|
|
item.unset = true;
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_items.append(item);
|
|
|
|
|
d->updateResultEnvironment();
|
2011-01-13 11:41:45 +01:00
|
|
|
emit dataChanged(index(row, 0, QModelIndex()), index(row, 1, QModelIndex()));
|
|
|
|
|
emit userChangesChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool EnvironmentModel::canUnset(const QString &name)
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
int pos = d->findInChanges(name);
|
2011-01-13 11:41:45 +01:00
|
|
|
if (pos != -1)
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_items.at(pos).unset;
|
2011-01-13 11:41:45 +01:00
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool EnvironmentModel::canReset(const QString &name)
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_baseEnvironment.hasKey(name);
|
2011-01-13 11:41:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<Utils::EnvironmentItem> EnvironmentModel::userChanges() const
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_items;
|
2011-01-13 11:41:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EnvironmentModel::setUserChanges(QList<Utils::EnvironmentItem> list)
|
|
|
|
|
{
|
|
|
|
|
// We assume nobody is reordering the items here.
|
2011-09-07 14:26:11 +02:00
|
|
|
if (list == d->m_items)
|
2011-01-13 11:41:45 +01:00
|
|
|
return;
|
|
|
|
|
beginResetModel();
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_items = list;
|
|
|
|
|
d->updateResultEnvironment();
|
2011-01-13 11:41:45 +01:00
|
|
|
endResetModel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Utils
|