Deduplicate calls for obtaining model index

Change-Id: I799742a06ce592ab935ee0609d5930a7d26a44e1
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Orgad Shaneh
2019-12-21 22:55:46 +02:00
committed by Orgad Shaneh
parent 9264cebe24
commit 586c6dd92f
4 changed files with 23 additions and 17 deletions

View File

@@ -443,7 +443,7 @@ QModelIndex TreeModel::indexOf(const MElement *element) const
QMT_CHECK(false); QMT_CHECK(false);
return QModelIndex(); return QModelIndex();
} }
QModelIndex parentIndex = indexFromItem(item); const QModelIndex parentIndex = indexFromItem(item);
int row = parentObject->children().indexOf(object); int row = parentObject->children().indexOf(object);
return QStandardItemModel::index(row, 0, parentIndex); return QStandardItemModel::index(row, 0, parentIndex);
} else if (auto relation = dynamic_cast<const MRelation *>(element)) { } else if (auto relation = dynamic_cast<const MRelation *>(element)) {
@@ -454,7 +454,7 @@ QModelIndex TreeModel::indexOf(const MElement *element) const
QMT_CHECK(false); QMT_CHECK(false);
return QModelIndex(); return QModelIndex();
} }
QModelIndex parentIndex = indexFromItem(item); const QModelIndex parentIndex = indexFromItem(item);
int row = owner->children().size() + owner->relations().indexOf(relation); int row = owner->children().size() + owner->relations().indexOf(relation);
return QStandardItemModel::index(row, 0, parentIndex); return QStandardItemModel::index(row, 0, parentIndex);
} }
@@ -519,7 +519,7 @@ void TreeModel::onEndUpdateObject(int row, const MObject *parent)
parentIndex = indexFromItem(parentItem); parentIndex = indexFromItem(parentItem);
} }
// reflect updated element in standard item // reflect updated element in standard item
QModelIndex elementIndex = this->QStandardItemModel::index(row, 0, parentIndex); const QModelIndex elementIndex = this->QStandardItemModel::index(row, 0, parentIndex);
MElement *element = TreeModel::element(elementIndex); MElement *element = TreeModel::element(elementIndex);
if (element) { if (element) {
auto object = dynamic_cast<MObject *>(element); auto object = dynamic_cast<MObject *>(element);
@@ -531,7 +531,7 @@ void TreeModel::onEndUpdateObject(int row, const MObject *parent)
} }
} }
m_busyState = NotBusy; m_busyState = NotBusy;
emit dataChanged(QStandardItemModel::index(row, 0, parentIndex), QStandardItemModel::index(row, 0, parentIndex)); emit dataChanged(elementIndex, elementIndex);
} }
void TreeModel::onBeginInsertObject(int row, const MObject *parent) void TreeModel::onBeginInsertObject(int row, const MObject *parent)
@@ -616,11 +616,11 @@ void TreeModel::onEndUpdateRelation(int row, const MObject *parent)
QMT_CHECK(m_objectToItemMap.contains(parent)); QMT_CHECK(m_objectToItemMap.contains(parent));
ModelItem *parentItem = m_objectToItemMap.value(parent); ModelItem *parentItem = m_objectToItemMap.value(parent);
QMT_ASSERT(parentItem, return); QMT_ASSERT(parentItem, return);
QModelIndex parentIndex = indexFromItem(parentItem); const QModelIndex parentIndex = indexFromItem(parentItem);
// reflect updated relation in standard item // reflect updated relation in standard item
row += parent->children().size(); row += parent->children().size();
QModelIndex elementIndex = QStandardItemModel::index(row, 0, parentIndex); const QModelIndex elementIndex = QStandardItemModel::index(row, 0, parentIndex);
MElement *element = TreeModel::element(elementIndex); MElement *element = TreeModel::element(elementIndex);
if (element) { if (element) {
auto relation = dynamic_cast<MRelation *>(element); auto relation = dynamic_cast<MRelation *>(element);
@@ -632,7 +632,7 @@ void TreeModel::onEndUpdateRelation(int row, const MObject *parent)
} }
} }
m_busyState = NotBusy; m_busyState = NotBusy;
emit dataChanged(QStandardItemModel::index(row, 0, parentIndex), QStandardItemModel::index(row, 0, parentIndex)); emit dataChanged(elementIndex, elementIndex);
} }
void TreeModel::onBeginInsertRelation(int row, const MObject *parent) void TreeModel::onBeginInsertRelation(int row, const MObject *parent)
@@ -707,10 +707,10 @@ void TreeModel::onRelationEndChanged(MRelation *relation, MObject *endObject)
QMT_CHECK(m_objectToItemMap.contains(parent)); QMT_CHECK(m_objectToItemMap.contains(parent));
ModelItem *parentItem = m_objectToItemMap.value(parent); ModelItem *parentItem = m_objectToItemMap.value(parent);
QMT_ASSERT(parentItem, return); QMT_ASSERT(parentItem, return);
QModelIndex parentIndex = indexFromItem(parentItem); const QModelIndex parentIndex = indexFromItem(parentItem);
int row = parent->children().size() + relation->owner()->relations().indexOf(relation); int row = parent->children().size() + relation->owner()->relations().indexOf(relation);
QModelIndex elementIndex = QStandardItemModel::index(row, 0, parentIndex); const QModelIndex elementIndex = QStandardItemModel::index(row, 0, parentIndex);
QMT_CHECK(elementIndex.isValid()); QMT_CHECK(elementIndex.isValid());
auto item = dynamic_cast<ModelItem *>(itemFromIndex(elementIndex)); auto item = dynamic_cast<ModelItem *>(itemFromIndex(elementIndex));
@@ -720,7 +720,7 @@ void TreeModel::onRelationEndChanged(MRelation *relation, MObject *endObject)
if (item->text() != label) if (item->text() != label)
item->setText(label); item->setText(label);
emit dataChanged(QStandardItemModel::index(row, 0, parentIndex), QStandardItemModel::index(row, 0, parentIndex)); emit dataChanged(elementIndex, elementIndex);
} }
void TreeModel::onModelDataChanged(const QModelIndex &topleft, const QModelIndex &bottomright) void TreeModel::onModelDataChanged(const QModelIndex &topleft, const QModelIndex &bottomright)

View File

@@ -75,6 +75,7 @@ void ProjectConfigurationModel::displayNameChanged()
if (oldPos < 0) if (oldPos < 0)
return; return;
QModelIndex itemIndex;
if (oldPos >= 1 && isOrderedBefore(m_projectConfigurations.at(oldPos), m_projectConfigurations.at(oldPos - 1))) { if (oldPos >= 1 && isOrderedBefore(m_projectConfigurations.at(oldPos), m_projectConfigurations.at(oldPos - 1))) {
// We need to move up // We need to move up
int newPos = oldPos - 1; int newPos = oldPos - 1;
@@ -88,7 +89,7 @@ void ProjectConfigurationModel::displayNameChanged()
m_projectConfigurations.removeAt(oldPos + 1); m_projectConfigurations.removeAt(oldPos + 1);
endMoveRows(); endMoveRows();
// Not only did we move, we also changed... // Not only did we move, we also changed...
emit dataChanged(index(newPos, 0), index(newPos,0)); itemIndex = index(newPos, 0);
} else if (oldPos < m_projectConfigurations.size() - 1 } else if (oldPos < m_projectConfigurations.size() - 1
&& isOrderedBefore(m_projectConfigurations.at(oldPos + 1), m_projectConfigurations.at(oldPos))) { && isOrderedBefore(m_projectConfigurations.at(oldPos + 1), m_projectConfigurations.at(oldPos))) {
// We need to move down // We need to move down
@@ -103,10 +104,11 @@ void ProjectConfigurationModel::displayNameChanged()
endMoveRows(); endMoveRows();
// We need to subtract one since removing at the old place moves the newIndex down // We need to subtract one since removing at the old place moves the newIndex down
emit dataChanged(index(newPos - 1, 0), index(newPos - 1, 0)); itemIndex = index(newPos - 1, 0);
} else { } else {
emit dataChanged(index(oldPos, 0), index(oldPos, 0)); itemIndex = index(oldPos, 0);
} }
emit dataChanged(itemIndex, itemIndex);
} }
QVariant ProjectConfigurationModel::data(const QModelIndex &index, int role) const QVariant ProjectConfigurationModel::data(const QModelIndex &index, int role) const

View File

@@ -148,7 +148,8 @@ void TaskModel::updateTaskFileName(unsigned int id, const QString &fileName)
QTC_ASSERT(i != -1, return); QTC_ASSERT(i != -1, return);
if (m_tasks.at(i).taskId == id) { if (m_tasks.at(i).taskId == id) {
m_tasks[i].file = Utils::FilePath::fromString(fileName); m_tasks[i].file = Utils::FilePath::fromString(fileName);
emit dataChanged(index(i, 0), index(i, 0)); const QModelIndex itemIndex = index(i, 0);
emit dataChanged(itemIndex, itemIndex);
} }
} }
@@ -158,7 +159,8 @@ void TaskModel::updateTaskLineNumber(unsigned int id, int line)
QTC_ASSERT(i != -1, return); QTC_ASSERT(i != -1, return);
if (m_tasks.at(i).taskId == id) { if (m_tasks.at(i).taskId == id) {
m_tasks[i].movedLine = line; m_tasks[i].movedLine = line;
emit dataChanged(index(i, 0), index(i, 0)); const QModelIndex itemIndex = index(i, 0);
emit dataChanged(itemIndex, itemIndex);
} }
} }

View File

@@ -70,8 +70,10 @@ void SerialDeviceModel::disablePort(const QString &portName)
return info.portName() == portName; return info.portName() == portName;
}); });
if (i >= 0) if (i >= 0) {
emit dataChanged(index(i), index(i), {Qt::DisplayRole}); const QModelIndex itemIndex = index(i);
emit dataChanged(itemIndex, itemIndex, {Qt::DisplayRole});
}
} }
void SerialDeviceModel::enablePort(const QString &portName) void SerialDeviceModel::enablePort(const QString &portName)