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

Update EnvironmentModel::setData

Reviewed-by: dt
parent b5d08242
No related branches found
No related tags found
No related merge requests found
...@@ -190,56 +190,57 @@ int EnvironmentModel::findInResultInsertPosition(const QString &name) const ...@@ -190,56 +190,57 @@ 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()) if (!index.isValid() || role != Qt::EditRole)
return false; return false;
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) return true;
return true;
if (index.column() == 0) { const QString oldName = data(this->index(index.row(), 0, QModelIndex())).toString();
//fail if a variable with the same name already exists const QString oldValue = data(this->index(index.row(), 1, QModelIndex())).toString();
#ifdef Q_OS_WIN int changesPos = findInChanges(oldName);
const QString &newName = value.toString().toUpper();
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 #else
const QString &newName = value.toString(); const QString &newName = value.toString();
#endif #endif
// Does the new name exist already?
if (findInChanges(newName) != -1) if (m_resultEnvironment.hasKey(newName))
return false; return false;
EnvironmentItem old("", ""); EnvironmentItem newVariable(newName, oldValue);
int pos = findInChanges(indexToVariable(index));
if (pos != -1) { if (changesPos != -1)
old = m_items.at(pos); 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
if (stringValue == m_baseEnvironment.value(oldName)) {
// ... and now went back to the base value
m_items.removeAt(changesPos);
} else { } else {
old.name = m_resultEnvironment.key(m_resultEnvironment.constBegin() + index.row()); // ... and changed it again
old.value = m_resultEnvironment.value(m_resultEnvironment.constBegin() + index.row()); m_items[changesPos].value = stringValue;
old.unset = false; m_items[changesPos].unset = false;
} }
} else {
if (changes(old.name)) // Add a new change item:
resetVariable(old.name); m_items.append(EnvironmentItem(oldName, stringValue));
old.name = newName;
addVariable(old);
emit renamedVariable(newName);
return true;
} else if (index.column() == 1) {
const QString &name = indexToVariable(index);
int pos = findInChanges(name);
if (pos != -1) {
m_items[pos].value = value.toString();
m_items[pos].unset = false;
updateResultEnvironment();
emit dataChanged(index, index);
emit userChangesChanged();
return true;
}
// not found in m_items, so add it as a new variable
addVariable(EnvironmentItem(name, value.toString()));
return true;
} }
updateResultEnvironment();
emit dataChanged(index, index);
emit userChangesChanged();
return true;
} }
return false; return false;
} }
...@@ -446,8 +447,8 @@ EnvironmentWidget::~EnvironmentWidget() ...@@ -446,8 +447,8 @@ EnvironmentWidget::~EnvironmentWidget()
void EnvironmentWidget::renamedVariable(const QString &name) void EnvironmentWidget::renamedVariable(const QString &name)
{ {
QModelIndex idx = m_model->variableToIndex(name); QModelIndex index = m_model->variableToIndex(name);
m_environmentTreeView->setCurrentIndex(idx); m_environmentTreeView->setCurrentIndex(index);
m_environmentTreeView->setFocus(); m_environmentTreeView->setFocus();
} }
......
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