summaryrefslogtreecommitdiff
path: root/plugins/autotest/testtreemodel.cpp
diff options
context:
space:
mode:
authorChristian Stenger <christian.stenger@digia.com>2014-11-11 17:30:34 +0100
committerChristian Stenger <christian.stenger@theqtcompany.com>2014-12-04 13:52:16 +0100
commit9a644d1257199a58cbb208c79c33831458d4aa01 (patch)
treea23d760226baa5b601f9e17bf58d3322db4d78db /plugins/autotest/testtreemodel.cpp
parent0357b0e98bf049ddb2e365a6ddcace67c2f0e836 (diff)
downloadqt-creator-9a644d1257199a58cbb208c79c33831458d4aa01.tar.gz
Support sorting of test tree
Additionally preparation of filtering has been done.
Diffstat (limited to 'plugins/autotest/testtreemodel.cpp')
-rw-r--r--plugins/autotest/testtreemodel.cpp112
1 files changed, 105 insertions, 7 deletions
diff --git a/plugins/autotest/testtreemodel.cpp b/plugins/autotest/testtreemodel.cpp
index 38a38c51f9..93d9d160ef 100644
--- a/plugins/autotest/testtreemodel.cpp
+++ b/plugins/autotest/testtreemodel.cpp
@@ -16,6 +16,7 @@
**
****************************************************************************/
+#include "autotestconstants.h"
#include "testcodeparser.h"
#include "testtreeitem.h"
#include "testtreemodel.h"
@@ -152,7 +153,7 @@ QVariant TestTreeModel::data(const QModelIndex &index, int role) const
return QString(item->name() + tr(" (none)"));
} else {
if (item->name().isEmpty())
- return tr("<unnamed>");
+ return tr(Constants::UNNAMED_QUICKTESTS);
return item->name();
}
@@ -535,19 +536,24 @@ void TestTreeModel::removeAllQuickTests()
emit testTreeModelChanged();
}
-void TestTreeModel::removeUnnamedQuickTest(const QString &filePath)
+bool TestTreeModel::removeUnnamedQuickTests(const QString &filePath)
{
TestTreeItem *unnamedQT = unnamedQuickTests();
if (!unnamedQT)
- return;
+ return false;
+ bool removed = false;
const QModelIndex unnamedQTIndex = index(1, 0).child(unnamedQT->row(), 0);
for (int childRow = unnamedQT->childCount() - 1; childRow >= 0; --childRow) {
const TestTreeItem *child = unnamedQT->child(childRow);
if (filePath == child->filePath())
- removeRow(childRow, unnamedQTIndex);
+ removed |= removeRow(childRow, unnamedQTIndex);
}
+
+ if (unnamedQT->childCount() == 0)
+ removeRow(unnamedQT->row(), unnamedQTIndex.parent());
emit testTreeModelChanged();
+ return removed;
}
void TestTreeModel::modifyTestSubtree(QModelIndex &toBeModifiedIndex, TestTreeItem *newItem)
@@ -566,6 +572,7 @@ void TestTreeModel::modifyTestSubtree(QModelIndex &toBeModifiedIndex, TestTreeIt
const int newChildCount = newItem->childCount();
// for keeping the CheckState on modifications
+ // TODO might still fail for duplicate entries (e.g. unnamed Quick Tests)
QHash<QString, Qt::CheckState> originalItems;
for (int row = 0; row < childCount; ++row) {
const TestTreeItem *child = toBeModifiedItem->child(row);
@@ -594,9 +601,8 @@ void TestTreeModel::modifyTestSubtree(QModelIndex &toBeModifiedIndex, TestTreeIt
if (childCount < newChildCount) { // add aditional items
for (int row = childCount; row < newChildCount; ++row) {
TestTreeItem *newChild = newItem->child(row);
- TestTreeItem *toBeAdded = new TestTreeItem(newChild->name(), newChild->filePath(),
- newChild->type(), toBeModifiedItem);
- toBeAdded->setLine(newChild->line());
+ TestTreeItem *toBeAdded = new TestTreeItem(*newChild);
+ toBeAdded->setParent(toBeModifiedItem);
beginInsertRows(toBeModifiedIndex, row, row);
toBeModifiedItem->appendChild(toBeAdded);
endInsertRows();
@@ -628,5 +634,97 @@ void TestTreeModel::modifyTestSubtree(QModelIndex &toBeModifiedIndex, TestTreeIt
emit testTreeModelChanged();
}
+/***************************** Sort/Filter Model **********************************/
+
+TestTreeSortFilterModel::TestTreeSortFilterModel(TestTreeModel *sourceModel, QObject *parent)
+ : QSortFilterProxyModel(parent),
+ m_sourceModel(sourceModel),
+ m_sortMode(Alphabetically),
+ m_filterMode(Basic)
+{
+ setSourceModel(sourceModel);
+}
+
+void TestTreeSortFilterModel::setSortMode(SortMode sortMode)
+{
+ m_sortMode = sortMode;
+ invalidate();
+}
+
+void TestTreeSortFilterModel::setFilterMode(FilterMode filterMode)
+{
+ m_filterMode = filterMode;
+ invalidateFilter();
+}
+
+void TestTreeSortFilterModel::toggleFilter(FilterMode filterMode)
+{
+ m_filterMode = toFilterMode(m_filterMode ^ filterMode);
+ invalidateFilter();
+}
+
+TestTreeSortFilterModel::FilterMode TestTreeSortFilterModel::toFilterMode(int f)
+{
+ switch (f) {
+ case TestTreeSortFilterModel::ShowInitAndCleanup:
+ return TestTreeSortFilterModel::ShowInitAndCleanup;
+ case TestTreeSortFilterModel::ShowTestData:
+ return TestTreeSortFilterModel::ShowTestData;
+ case TestTreeSortFilterModel::ShowAll:
+ return TestTreeSortFilterModel::ShowAll;
+ default:
+ return TestTreeSortFilterModel::Basic;
+ }
+}
+
+bool TestTreeSortFilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
+{
+ // root items keep the intended order: 1st Auto Tests, 2nd Quick Tests
+ const TestTreeItem *leftItem = static_cast<TestTreeItem *>(left.internalPointer());
+ if (leftItem->type() == TestTreeItem::ROOT)
+ return left.row() > right.row();
+
+ const QString leftVal = m_sourceModel->data(left).toString();
+ const QString rightVal = m_sourceModel->data(right).toString();
+
+ // unnamed Quick Tests will always be listed first
+ if (leftVal == tr(Constants::UNNAMED_QUICKTESTS))
+ return false;
+ if (rightVal == tr(Constants::UNNAMED_QUICKTESTS))
+ return true;
+
+ switch (m_sortMode) {
+ case Alphabetically:
+ return leftVal > rightVal;
+ case Naturally: {
+ const TextEditor::TextEditorWidget::Link leftLink =
+ m_sourceModel->data(left, LinkRole).value<TextEditor::TextEditorWidget::Link>();
+ const TextEditor::TextEditorWidget::Link rightLink =
+ m_sourceModel->data(right, LinkRole).value<TextEditor::TextEditorWidget::Link>();
+
+ if (leftLink.targetFileName == rightLink.targetFileName) {
+ return leftLink.targetLine == rightLink.targetLine
+ ? leftLink.targetColumn > rightLink.targetColumn
+ : leftLink.targetLine > rightLink.targetLine;
+ } else {
+ return leftLink.targetFileName > rightLink.targetFileName;
+ }
+ }
+ default:
+ return true;
+ }
+}
+
+bool TestTreeSortFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
+{
+ // TODO add filtering capabilities
+ QModelIndex index = m_sourceModel->index(sourceRow, 0,sourceParent);
+ if (!index.isValid())
+ return false;
+
+
+ return true;
+}
+
} // namespace Internal
} // namespace Autotest