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 "treeitem.h"
30 #include <QDate>
31 #include <QString>
32 #include <QModelIndexList>
33 
35 {
36  qRegisterMetaType<ScenarioItem*>();
37  qRegisterMetaType<ApplicationDatabaseItem*>();
38  qRegisterMetaType<OpenScenarioItem*>();
39  qRegisterMetaType<SettingsItem*>();
40 }
41 
42 QVariant FilterableTreeModelSortFilterProxyModel::data(const QModelIndex& proxy_index, int role) const
43 {
44  switch (role)
45  {
46  case SCENARIOITEMSOURCE:
49  case SETTINGSITEMSOURCE:
50  {
51  QModelIndex source_index = mapToSource(proxy_index);
52  return sourceModel()->data(source_index, role);
53  }
54  case SCENARIOITEMPROXY:
55  return QVariant::fromValue(reinterpret_cast<ScenarioItem*>(proxy_index.internalPointer()));
57  return QVariant::fromValue(reinterpret_cast<ApplicationDatabaseItem*>(proxy_index.internalPointer()));
59  return QVariant::fromValue(reinterpret_cast<OpenScenarioItem*>(proxy_index.internalPointer()));
60  case SETTINGSITEMPROXY:
61  return QVariant::fromValue(reinterpret_cast<SettingsItem*>(proxy_index.internalPointer()));
62 
63  default:
64  return QSortFilterProxyModel::data(proxy_index, role);
65  }
66 }
67 
68 bool FilterableTreeModelSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
69 {
70  QModelIndexList childrenColumn1;
71 
72  childrenColumn1 << sourceModel()->index(sourceRow, 0, sourceParent);
73 
74  //descend through the generations.
75  for (int i = 0; i < childrenColumn1.size(); ++i)
76  {
77  for (int j = 0; j < sourceModel()->rowCount(childrenColumn1[i]); ++j)
78  {
79  childrenColumn1 << childrenColumn1[i].child(j, 0);
80  }
81  }
82 
83  bool result = false;
84  for (int i = 0; i < childrenColumn1.size(); ++i)
85  {
86  if (sourceModel()->data(childrenColumn1[i]).toString().contains(filterRegExp()))
87  {
88  result = true;
89  break;
90  }
91  }
92 
93  if (sourceModel()->columnCount() > 1)
94  {
95  QModelIndexList childrenColumn2;
96 
97  childrenColumn2 << sourceModel()->index(sourceRow, 1, sourceParent);
98 
99  //descend through the generations.
100  for (int i = 0; i < childrenColumn2.size(); ++i)
101  {
102  for (int j = 0; j < sourceModel()->rowCount(childrenColumn2[i]); ++j)
103  {
104  childrenColumn2 << childrenColumn2[i].child(j, 1);
105  }
106  }
107 
108  for (int i = 0; i < childrenColumn2.size(); ++i)
109  {
110  if (sourceModel()->data(childrenColumn2[i]).toString().contains(filterRegExp()))
111  {
112  result = true;
113  break;
114  }
115  }
116  }
117 
118  return result;
119 }
ScenarioItem
TreeItem representing data contained in a Scenario or an Application.
Definition: scenarioitem.h:38
treeitem.h
FilterableTreeModelSortFilterProxyModel::filterAcceptsRow
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
Returns whether the filter accepts a row.
Definition: filterabletreemodelsortfilterproxymodel.cpp:68
filterabletreemodelsortfilterproxymodel.h
SCENARIOITEMSOURCE
@ SCENARIOITEMSOURCE
Definition: treeitem.h:36
OPENSCENARIOITEMSOURCE
@ OPENSCENARIOITEMSOURCE
Definition: treeitem.h:42
SETTINGSITEMSOURCE
@ SETTINGSITEMSOURCE
Definition: treeitem.h:38
FilterableTreeModelSortFilterProxyModel::FilterableTreeModelSortFilterProxyModel
FilterableTreeModelSortFilterProxyModel()
Constructor that registers the used meta-types.
Definition: filterabletreemodelsortfilterproxymodel.cpp:34
ApplicationDatabaseItem
This class represents an item in the ApplicationDatabaseView.
Definition: applicationdatabaseitem.h:34
APPLICATIONITEMSOURCE
@ APPLICATIONITEMSOURCE
Definition: treeitem.h:41
SETTINGSITEMPROXY
@ SETTINGSITEMPROXY
Definition: treeitem.h:39
APPLICATIONITEMPROXY
@ APPLICATIONITEMPROXY
Definition: treeitem.h:40
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
OPENSCENARIOITEMPROXY
@ OPENSCENARIOITEMPROXY
Definition: treeitem.h:43
SCENARIOITEMPROXY
@ SCENARIOITEMPROXY
Definition: treeitem.h:37
FilterableTreeModelSortFilterProxyModel::data
QVariant data(const QModelIndex &proxy_index, int role) const override
Returns the data stored at the specified location, based on the given role.
Definition: filterabletreemodelsortfilterproxymodel.cpp:42
Logging.h
SettingsItem
Definition: settingsitem.h:31
OpenScenarioItem
Item in the TreeItemView. Every item represents a Scenario.
Definition: openscenarioitem.h:36