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 #include <QItemEditorFactory>
26 #include <QLineEdit>
27 #include <QTextEdit>
28 #include <QTimer>
29 #include <QTreeView>
30 #include <QVBoxLayout>
31 
33 
34 
35 using namespace armarx;
36 
37 TreeBox::TreeBox(QStandardItemModel* model, bool hideChildren, QWidget* parent) :
38  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
58 TreeBox::eventFilter(QObject* object, QEvent* event)
59 {
60  if (event->type() == QEvent::MouseButtonPress && object == view()->viewport())
61  {
62  QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
63  QModelIndex index = view()->indexAt(mouseEvent->pos());
64 
65  if (!view()->visualRect(index).contains(mouseEvent->pos()))
66  {
67  skipNextHide = true;
68  }
69 
70  // else if(index.row() == 0 && index.parent() == qobject_cast<QStandardItemModel*>(model())->invisibleRootItem()->index() )
71  // {
72  // view()->edit(index);
73  // skipNextHide = true;
74  // }
75  }
76  else if ((event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) &&
77  object == view()->viewport())
78  {
79 
80  QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
81 
82  if (keyEvent->key() != Qt::Key_Return && keyEvent->key() != Qt::Key_Enter)
83  {
84  // skipping all keyboard entries for the search but enter
85  skipNextHide = true;
86  }
87  }
88 
89  return false;
90 }
91 
92 void
94 {
95  QComboBox::showPopup();
96 }
97 
98 void
100 {
101  if (skipNextHide)
102  {
103  skipNextHide = false;
104  }
105  else
106  {
107  if (treeView->isVisible())
108  {
109  QTimer::singleShot(
110  20,
111  this,
112  SLOT(
113  delayedIndexChanging())); // delay needed because, qt sends later an event that overwrites this
114  }
115 
116  QComboBox::hidePopup();
117  // TODO: better solution needed
118 
119  // emit itemSelected(view()->currentIndex());
120  }
121 }
122 
123 void
124 TreeBox::applyFilter(QString searchString)
125 {
126  StateItemModel* stateModel = qobject_cast<StateItemModel*>(model());
127 
128  if (!stateModel)
129  {
130  throw exceptions::local::eNullPointerException("model is null");
131  }
132 
133  QStandardItem* root = stateModel->invisibleRootItem();
134 
135  if (searchString == FilterableTreeView::DefaultFilterStr)
136  {
137  return;
138  }
139 
140  applyFilter(root, searchString);
141  treeView->setRowHidden(0, root->index(), false);
142 
143  if (searchString.length() > 0)
144  {
145  treeView->expandAll();
146  }
147 }
148 
149 void
150 TreeBox::delayedIndexChanging()
151 {
152  emit itemSelected(view()->currentIndex());
153 }
154 
155 bool
156 TreeBox::applyFilter(const QStandardItem* parent, QString searchString)
157 {
158  bool show = false;
159 
160  for (int row = 0; row < parent->rowCount(); ++row)
161  {
162  QStandardItem* child = parent->child(row);
163 
164  if (!child)
165  {
166  continue;
167  }
168 
169  bool showThisRow = applyFilter(child, searchString);
170  QRegExp rex(searchString, Qt::CaseInsensitive, QRegExp::Wildcard);
171 
172  if (child->data(Qt::DisplayRole).toString().contains(rex))
173  {
174 
175  showThisRow = true;
176  }
177 
178  if (showThisRow)
179  {
180  show = showThisRow;
181  }
182 
183  treeView->setRowHidden(row, parent->index(), !showThisRow);
184  }
185 
186  return show;
187 }
TreeBox::hidePopup
void hidePopup() override
Definition: TreeBox.cpp:99
index
uint8_t index
Definition: EtherCATFrame.h:59
TreeBox::applyFilter
void applyFilter(QString searchString)
Definition: TreeBox.cpp:124
armarx::StateItemModel
Definition: StateItemModel.h:47
FilterableTreeView.h
armarx::exceptions::local::eNullPointerException
Definition: Exception.h:49
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:563
TreeBox::TreeBox
TreeBox(QStandardItemModel *model, bool hideChildren=true, QWidget *parent=0)
Definition: TreeBox.cpp:37
FilterableTreeView::DefaultFilterStr
static const QString DefaultFilterStr
Definition: FilterableTreeView.h:52
TreeBox::showPopup
void showPopup() override
Definition: TreeBox.cpp:93
TreeBox::eventFilter
bool eventFilter(QObject *object, QEvent *event) override
Definition: TreeBox.cpp:58
TreeBox.h
TreeBox::itemSelected
void itemSelected(QModelIndex index)
FilterableTreeView
The FilterableTreeView class.
Definition: FilterableTreeView.h:40
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27