diff --git a/demo/CMakeLists.txt b/demo/CMakeLists.txt index 3cb699c..7899c91 100644 --- a/demo/CMakeLists.txt +++ b/demo/CMakeLists.txt @@ -71,7 +71,7 @@ if (Qt5Charts_FOUND) endif() set(Demo_SRCS src/main.cpp src/Fibonacci.cpp src/Tree.cpp src/TimeSeries.cpp - src/Processes.cpp resource_file.qrc) + src/Processes.cpp src/SortedModel.cpp resource_file.qrc) add_executable(Demo ${Demo_SRCS}) add_dependencies(Demo rust_target) diff --git a/demo/demo.qml b/demo/demo.qml index eadfcef..c734a7d 100644 --- a/demo/demo.qml +++ b/demo/demo.qml @@ -93,46 +93,65 @@ ApplicationWindow { var first = sortedFileSystem.index(0, 0, root); treeView.expand(first); } + onSortIndicatorColumnChanged: sort() + onSortIndicatorOrderChanged: sort() + function sort() { + var role = getColumn(treeView.sortIndicatorColumn).role; + model.sortByRole(role, treeView.sortIndicatorOrder); + } } } Tab { title: "processes" - TreeView { - id: processView - model: processes - sortIndicatorVisible: true - alternatingRowColors: true - TableViewColumn { - title: "pid" - role: "pid" - } - TableViewColumn { - title: "name" - role: "name" - } - TableViewColumn { - title: "cpu" - role: "cpu" - } - onSortIndicatorColumnChanged: { - switch (processView.sortIndicatorColumn) { - case 0: model.sortRole = Qt.DisplayRole; break; + Item { + anchors.fill: parent + TextField { + id: processFilterInput + width: parent.width + placeholderText: "Filter processes" + onTextChanged: { + processes.filterRegExp + = new RegExp(processFilterInput.text); } } - onSortIndicatorOrderChanged: { - model.sort(Qt.DisplayRole, processView.sortIndicatorOrderChanged); - } - Component.onCompleted: { - var r = processView.rootIndex; - var a = processes.index(0, 0, r); - var b = processes.index(1, 0, r); - processView.expand(processView.rootIndex); - processView.expand(a); - processView.expand(b); - processes.rowsInserted.connect(function (index) { + TreeView { + width: parent.width + anchors.top: processFilterInput.bottom + anchors.bottom: parent.bottom + id: processView + model: processes + sortIndicatorVisible: true + alternatingRowColors: true + TableViewColumn { + title: "pid" + role: "pid" + } + TableViewColumn { + title: "name" + role: "name" + } + TableViewColumn { + title: "cpu" + role: "cpu" + } + onSortIndicatorColumnChanged: sort() + onSortIndicatorOrderChanged: sort() + function sort() { + var role = getColumn(processView.sortIndicatorColumn).role; + model.sortByRole(role, processView.sortIndicatorOrder); + } + Component.onCompleted: { + var r = processView.rootIndex; + var a = processes.index(0, 0, r); + var b = processes.index(1, 0, r); processView.expand(processView.rootIndex); - processView.expand(index); - }); + processView.expand(a); + processView.expand(b); + processes.rowsInserted.connect(function (index) { + processView.expand(processView.rootIndex); + processView.expand(index); + }); + } } } /* diff --git a/demo/src/SortedModel.cpp b/demo/src/SortedModel.cpp new file mode 100644 index 0000000..6d51da9 --- /dev/null +++ b/demo/src/SortedModel.cpp @@ -0,0 +1 @@ +#include "SortedModel.moc" diff --git a/demo/src/SortedModel.h b/demo/src/SortedModel.h new file mode 100644 index 0000000..5157aa0 --- /dev/null +++ b/demo/src/SortedModel.h @@ -0,0 +1,24 @@ +#ifndef SORTED_MODEL +#define SORTED_MODEL + +#include +#include + +class SortedModel : public QSortFilterProxyModel { +Q_OBJECT +public: + SortedModel() :QSortFilterProxyModel() {} +public slots: + void sortByRole(const QString& role, Qt::SortOrder order) { + QHashIterator i(roleNames()); + while (i.hasNext()) { + i.next(); + if (i.value() == role) { + setSortRole(i.key()); + } + } + sort(0, order); + } +}; + +#endif diff --git a/demo/src/main.cpp b/demo/src/main.cpp index c280adf..4cc9d16 100644 --- a/demo/src/main.cpp +++ b/demo/src/main.cpp @@ -2,6 +2,7 @@ #include "Fibonacci.h" #include "TimeSeries.h" #include "Processes.h" +#include "SortedModel.h" #ifdef QT_CHARTS_LIB #include @@ -43,9 +44,9 @@ struct Models { Fibonacci fibonacci; FibonacciList fibonacciList; Tree fileSystem; - QSortFilterProxyModel sortedFileSystem; + SortedModel sortedFileSystem; Processes processes; - QSortFilterProxyModel sortedProcesses; + SortedModel sortedProcesses; TimeSeries timeSeries; };