filterabletreemodelsortfilterproxymodel.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore::core
19 * @author Cedric Seehausen (usdnr at kit dot edu)
20 * @date 2016
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25
27
28#include <QDate>
29#include <QModelIndexList>
30#include <QString>
31
33
34#include "treeitem.h"
35
37 QSortFilterProxyModel(0)
38{
39 qRegisterMetaType<ScenarioItem*>();
40 qRegisterMetaType<ApplicationDatabaseItem*>();
41 qRegisterMetaType<OpenScenarioItem*>();
42 qRegisterMetaType<SettingsItem*>();
43}
44
45QVariant
46FilterableTreeModelSortFilterProxyModel::data(const QModelIndex& proxy_index, int role) const
47{
48 switch (role)
49 {
55 {
56 QModelIndex source_index = mapToSource(proxy_index);
57 return sourceModel()->data(source_index, role);
58 }
60 return QVariant::fromValue(
61 reinterpret_cast<ScenarioItem*>(proxy_index.internalPointer()));
63 return QVariant::fromValue(
64 reinterpret_cast<ApplicationDatabaseItem*>(proxy_index.internalPointer()));
66 return QVariant::fromValue(
67 reinterpret_cast<OpenScenarioItem*>(proxy_index.internalPointer()));
69 return QVariant::fromValue(
70 reinterpret_cast<SettingsItem*>(proxy_index.internalPointer()));
71
72 default:
73 return QSortFilterProxyModel::data(proxy_index, role);
74 }
75}
76
77bool
79 const QModelIndex& sourceParent) const
80{
81 // Without a filter every row is accepted anyway. Checking that first matters a lot: the search
82 // below walks the complete subtree of the row twice, and with dynamic sorting and filtering
83 // enabled it is re-run for every dataChanged() the status refresh emits.
84 if (filterRegExp().isEmpty())
85 {
86 return true;
87 }
88
89 QModelIndexList childrenColumn1;
90
91 childrenColumn1 << sourceModel()->index(sourceRow, 0, sourceParent);
92
93 //descend through the generations.
94 for (int i = 0; i < childrenColumn1.size(); ++i)
95 {
96 for (int j = 0; j < sourceModel()->rowCount(childrenColumn1[i]); ++j)
97 {
98 childrenColumn1 << childrenColumn1[i].child(j, 0);
99 }
100 }
101
102 bool result = false;
103 for (int i = 0; i < childrenColumn1.size(); ++i)
104 {
105 if (sourceModel()->data(childrenColumn1[i]).toString().contains(filterRegExp()))
106 {
107 result = true;
108 break;
109 }
110 }
111
112 if (sourceModel()->columnCount() > 1)
113 {
114 QModelIndexList childrenColumn2;
115
116 childrenColumn2 << sourceModel()->index(sourceRow, 1, sourceParent);
117
118 //descend through the generations.
119 for (int i = 0; i < childrenColumn2.size(); ++i)
120 {
121 for (int j = 0; j < sourceModel()->rowCount(childrenColumn2[i]); ++j)
122 {
123 childrenColumn2 << childrenColumn2[i].child(j, 1);
124 }
125 }
126
127 for (int i = 0; i < childrenColumn2.size(); ++i)
128 {
129 if (sourceModel()->data(childrenColumn2[i]).toString().contains(filterRegExp()))
130 {
131 result = true;
132 break;
133 }
134 }
135 }
136
137 return result;
138}
uint8_t data[1]
This class represents an item in the ApplicationDatabaseView.
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
Returns whether the filter accepts a row.
FilterableTreeModelSortFilterProxyModel()
Constructor that registers the used meta-types.
QVariant data(const QModelIndex &proxy_index, int role) const override
Returns the data stored at the specified location, based on the given role.
Item in the TreeItemView.
TreeItem representing data contained in a Scenario or an Application.
@ APPLICATIONITEMSOURCE
Definition treeitem.h:42
@ SETTINGSITEMPROXY
Definition treeitem.h:40
@ SCENARIOITEMSOURCE
Definition treeitem.h:37
@ SCENARIOITEMPROXY
Definition treeitem.h:38
@ OPENSCENARIOITEMSOURCE
Definition treeitem.h:43
@ APPLICATIONITEMPROXY
Definition treeitem.h:41
@ SCENARIOITEMPENDINGINTENT
ScenarioManager::Data_Structure::Intent of the row, as an int. See ScenarioItem::update().
Definition treeitem.h:46
@ OPENSCENARIOITEMPROXY
Definition treeitem.h:44
@ SETTINGSITEMSOURCE
Definition treeitem.h:39