TreeBox.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX::
17 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
18 * @date 2012
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "TreeBox.h"
24 
25 
26 #include <QVBoxLayout>
27 #include <QTreeView>
28 #include <QLineEdit>
29 #include <QTextEdit>
30 #include <QItemEditorFactory>
31 #include <QTimer>
32 
34 
35 
36 using namespace armarx;
37 
38 TreeBox::TreeBox(QStandardItemModel* model, bool hideChildren, QWidget* parent) : QComboBox(parent), skipNextHide(false) //Widget creation
39 {
40  setModel(model);
41  treeView = new FilterableTreeView(this, hideChildren);
42  treeView->setHeaderHidden(true);
43  treeView->setModel(model);
44  treeView->setMinimumHeight(300);
45  treeView->setMinimumWidth(250);
46  treeView->setAnimated(true);
47  treeView->setSortingEnabled(true);
48 
49  setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
50  setMinimumContentsLength(10);
51  setView(treeView);
52 
53 
54  view()->viewport()->installEventFilter(this);
55 }
56 
57 bool TreeBox::eventFilter(QObject* object, QEvent* event)
58 {
59  if (event->type() == QEvent::MouseButtonPress && object == view()->viewport())
60  {
61  QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
62  QModelIndex index = view()->indexAt(mouseEvent->pos());
63 
64  if (!view()->visualRect(index).contains(mouseEvent->pos()))
65  {
66  skipNextHide = true;
67  }
68 
69  // else if(index.row() == 0 && index.parent() == qobject_cast<QStandardItemModel*>(model())->invisibleRootItem()->index() )
70  // {
71  // view()->edit(index);
72  // skipNextHide = true;
73  // }
74  }
75  else if ((event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
76  && object == view()->viewport())
77  {
78 
79  QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
80 
81  if (keyEvent->key() != Qt::Key_Return && keyEvent->key() != Qt::Key_Enter)
82  {
83  // skipping all keyboard entries for the search but enter
84  skipNextHide = true;
85  }
86 
87  }
88 
89  return false;
90 }
91 
93 {
94  QComboBox::showPopup();
95 }
96 
98 {
99  if (skipNextHide)
100  {
101  skipNextHide = false;
102  }
103  else
104  {
105  if (treeView->isVisible())
106  {
107  QTimer::singleShot(20, this, SLOT(delayedIndexChanging())); // delay needed because, qt sends later an event that overwrites this
108  }
109 
110  QComboBox::hidePopup();
111  // TODO: better solution needed
112 
113  // emit itemSelected(view()->currentIndex());
114  }
115 
116 
117 }
118 
119 void TreeBox::applyFilter(QString searchString)
120 {
121  StateItemModel* stateModel = qobject_cast<StateItemModel*>(model());
122 
123  if (!stateModel)
124  {
125  throw exceptions::local::eNullPointerException("model is null");
126  }
127 
128  QStandardItem* root = stateModel->invisibleRootItem();
129 
130  if (searchString == FilterableTreeView::DefaultFilterStr)
131  {
132  return;
133  }
134 
135  applyFilter(root, searchString);
136  treeView->setRowHidden(0, root->index(), false);
137 
138  if (searchString.length() > 0)
139  {
140  treeView->expandAll();
141  }
142 
143 }
144 
145 
146 void TreeBox::delayedIndexChanging()
147 {
148  emit itemSelected(view()->currentIndex());
149 }
150 
151 
152 
153 
154 
155 bool TreeBox::applyFilter(const QStandardItem* parent, QString searchString)
156 {
157  bool show = false;
158 
159  for (int row = 0; row < parent->rowCount(); ++row)
160  {
161  QStandardItem* child = parent->child(row);
162 
163  if (!child)
164  {
165  continue;
166  }
167 
168  bool showThisRow = applyFilter(child, searchString);
169  QRegExp rex(searchString, Qt::CaseInsensitive, QRegExp::Wildcard);
170 
171  if (child->data(Qt::DisplayRole).toString().contains(rex))
172  {
173 
174  showThisRow = true;
175  }
176 
177  if (showThisRow)
178  {
179  show = showThisRow;
180  }
181 
182  treeView->setRowHidden(row, parent->index(), !showThisRow);
183 
184  }
185 
186  return show;
187 
188 }
189 
190 
191 
192 
193 
194 
TreeBox::hidePopup
void hidePopup() override
Definition: TreeBox.cpp:97
index
uint8_t index
Definition: EtherCATFrame.h:59
TreeBox::applyFilter
void applyFilter(QString searchString)
Definition: TreeBox.cpp:119
armarx::StateItemModel
Definition: StateItemModel.h:50
FilterableTreeView.h
armarx::exceptions::local::eNullPointerException
Definition: Exception.h:42
armarx::armem::contains
bool contains(const MemoryID &general, const MemoryID &specific)
Indicates whether general is "less specific" than, or equal to, specific, i.e.
Definition: MemoryID.cpp:558
TreeBox::TreeBox
TreeBox(QStandardItemModel *model, bool hideChildren=true, QWidget *parent=0)
Definition: TreeBox.cpp:38
FilterableTreeView::DefaultFilterStr
static const QString DefaultFilterStr
Definition: FilterableTreeView.h:48
TreeBox::showPopup
void showPopup() override
Definition: TreeBox.cpp:92
TreeBox::eventFilter
bool eventFilter(QObject *object, QEvent *event) override
Definition: TreeBox.cpp:57
TreeBox.h
TreeBox::itemSelected
void itemSelected(QModelIndex index)
FilterableTreeView
The FilterableTreeView class.
Definition: FilterableTreeView.h:39
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28