summaryrefslogtreecommitdiff
path: root/src/modelutils.cpp
diff options
context:
space:
mode:
authorMikaël Capelle <capelle.mikael@gmail.com>2020-12-31 21:02:04 +0100
committerMikaël Capelle <capelle.mikael@gmail.com>2021-01-02 15:38:18 +0100
commitf2d8469ed6cd0fafc22097cdba2ee8f325f00513 (patch)
treecd61150c0187a93f1b68602a26d1d501c4d67ed7 /src/modelutils.cpp
parent8dffe65032c498a218079f941d88af6053b28d7f (diff)
Start moving stuff from MainWindow to PluginListView.
Diffstat (limited to 'src/modelutils.cpp')
-rw-r--r--src/modelutils.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/modelutils.cpp b/src/modelutils.cpp
new file mode 100644
index 00000000..43aa6b99
--- /dev/null
+++ b/src/modelutils.cpp
@@ -0,0 +1,58 @@
+#include "modelutils.h"
+
+#include <QAbstractProxyModel>
+
+QModelIndex indexModelToView(const QModelIndex& index, const QAbstractItemView* view)
+{
+ // we need to stack the proxy
+ std::vector<QAbstractProxyModel*> proxies;
+ {
+ auto* currentModel = view->model();
+ while (auto* proxy = qobject_cast<QAbstractProxyModel*>(currentModel)) {
+ proxies.push_back(proxy);
+ currentModel = proxy->sourceModel();
+ }
+ }
+
+ if (proxies.empty() || proxies.back()->sourceModel() != index.model()) {
+ return QModelIndex();
+ }
+
+ auto qindex = index;
+ for (auto rit = proxies.rbegin(); rit != proxies.rend(); ++rit) {
+ qindex = (*rit)->mapFromSource(qindex);
+ }
+
+ return qindex;
+}
+
+QModelIndexList indexModelToView(const QModelIndexList& index, const QAbstractItemView* view)
+{
+ QModelIndexList result;
+ for (auto& idx : index) {
+ result.append(indexModelToView(idx, view));
+ }
+ return result;
+}
+
+QModelIndex indexViewToModel(const QModelIndex& index, const QAbstractItemModel* model)
+{
+ if (index.model() == model) {
+ return index;
+ }
+ else if (auto* proxy = qobject_cast<const QAbstractProxyModel*>(index.model())) {
+ return indexViewToModel(proxy->mapToSource(index), model);
+ }
+ else {
+ return QModelIndex();
+ }
+}
+
+QModelIndexList indexViewToModel(const QModelIndexList& index, const QAbstractItemModel* model)
+{
+ QModelIndexList result;
+ for (auto& idx : index) {
+ result.append(indexViewToModel(idx, model));
+ }
+ return result;
+}