AutoTest: Simplify code for (de)selecting all items

Checkstate handling has been redone, which allows to simplify
these actions.

Change-Id: Ice89d2e9a1d8c3e38520ff2519d06fc4e6d67df9
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Stenger
2018-01-26 08:02:27 +01:00
parent f3c8ab52fa
commit 4e81360e4a
2 changed files with 7 additions and 36 deletions

View File

@@ -45,6 +45,13 @@ TestTreeView::TestTreeView(QWidget *parent)
Core::ICore::addContextObject(m_context);
}
static void changeCheckStateAll(const Qt::CheckState checkState)
{
TestTreeModel *model = TestTreeModel::instance();
for (int row = 0, count = model->rowCount(); row < count; ++row)
model->setData(model->index(row, 0), checkState, Qt::CheckStateRole);
}
void TestTreeView::selectAll()
{
changeCheckStateAll(Qt::Checked);
@@ -55,40 +62,5 @@ void TestTreeView::deselectAll()
changeCheckStateAll(Qt::Unchecked);
}
// this avoids the re-evaluation of parent nodes when modifying the child nodes (setData())
void TestTreeView::changeCheckStateAll(const Qt::CheckState checkState)
{
const TestTreeModel *model = TestTreeModel::instance();
for (int rootRow = 0; rootRow < model->rowCount(rootIndex()); ++rootRow) {
QModelIndex currentRootIndex = model->index(rootRow, 0, rootIndex());
if (!currentRootIndex.isValid())
return;
int count = model->rowCount(currentRootIndex);
QModelIndex last;
for (int classesRow = 0; classesRow < count; ++classesRow) {
const QModelIndex classesIndex = model->index(classesRow, 0, currentRootIndex);
int funcCount = model->rowCount(classesIndex);
TestTreeItem *item = static_cast<TestTreeItem *>(classesIndex.internalPointer());
if (item) {
item->setData(0, checkState, Qt::CheckStateRole);
if (!item->childCount())
last = classesIndex;
}
for (int functionRow = 0; functionRow < funcCount; ++functionRow) {
last = model->index(functionRow, 0, classesIndex);
TestTreeItem *item = static_cast<TestTreeItem *>(last.internalPointer());
if (item)
item->setData(0, checkState, Qt::CheckStateRole);
}
}
if (count == 0) {
if (auto item = static_cast<TestTreeItem *>(currentRootIndex.internalPointer()))
item->setData(0, checkState, Qt::CheckStateRole);
}
emit dataChanged(currentRootIndex, last);
}
}
} // namespace Internal
} // namespace Autotest