ObserverWidget.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
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "ObserverWidget.h"
25 #include "../ObserverGuiPlugin.h"
26 
27 #include <QList>
28 #include <QMenu>
29 #include <QAction>
30 #include <QItemSelectionModel>
31 
32 #include "../ObserverWidgetController.h"
34 #include <ArmarXGui/gui-plugins/ObserverPropertiesPlugin/ui_FilterPropertiesDialog.h>
39 
40 #include <boost/regex.hpp>
41 
42 namespace armarx
43 {
45  QWidget(0),
46  model(NULL)
47  {
48  this->controller = controller;
49  qRegisterMetaType<DatafieldFilterBasePtr>("DatafieldFilterBasePtr");
50  ui.setupUi(this);
51  ui.propertiesView->setLayout(new QVBoxLayout());
52  ui.observerTreeView->setSortingEnabled(true);
53  //ui.observerTreeView->setHideChildren(false);
54 
55  propertiesWidget = 0;
56 
57  updateTimer = new QTimer(this);
58 
59  connect(ui.observerTreeView, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(treeView_contextMenu(const QPoint&)));
60 
61  QList<int> sizes;
62  sizes.append(400);
63  sizes.append(800);
64 
65  ui.splitter->setSizes(sizes);
66 
67  autoUpdateAction = new QAction("Auto update", ui.observerTreeView);
68 
69  filterExpansionTimer.setSingleShot(true);
70  connect(&filterExpansionTimer, SIGNAL(timeout()), this, SLOT(delayedFilterExpansion()));
71  }
72 
74  {
75  model = new ObserverItemModel(controller->getIceManager(), controller->getVariantInfo());
76  proxyModel = new InfixFilterModel(this);
77  proxyModel->setSourceModel(model);
78  ui.observerTreeView->setModel(proxyModel);
79  proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
80  connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateObservers()));
81  QItemSelectionModel* selectionModel = ui.observerTreeView->selectionModel();
82  connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(treeView_selected(const QItemSelection&, const QItemSelection&)));
83  // updateObservers();
84 
85  connect(ui.lineEditSearch, SIGNAL(textChanged(QString)), proxyModel, SLOT(setFilterFixedString(QString)));
86  connect(ui.lineEditSearch, SIGNAL(textChanged(QString)), this, SLOT(expandFilterSelection(QString)), Qt::QueuedConnection);
87  }
88 
90  {
91  if (model)
92  {
93  model->updateObservers();
94  }
95 
96  auto selection = ui.observerTreeView->selectionModel()->selectedIndexes();
97 
98  if (selection.size() > 0)
99  {
100  if (propertiesWidget)
101  {
102  ui.propertiesView->layout()->removeWidget(propertiesWidget);
103  propertiesWidget->deleteLater();
104  propertiesWidget = NULL;
105  }
106  auto tempWidgetPtr = model->getPropertiesWidget(proxyModel->mapToSource(selection.at(0)), ui.propertiesView);
107  if (tempWidgetPtr)
108  {
109  propertiesWidget = tempWidgetPtr;
110  ui.propertiesView->layout()->addWidget(propertiesWidget);
111  propertiesWidget->show();
112  }
113  }
114  }
115 
116  void ObserverWidget::treeView_selected(const QItemSelection& selected, const QItemSelection& deselected)
117  {
118  if (selected.indexes().size() == 0)
119  {
120  return;
121  }
122 
123  if (propertiesWidget)
124  {
125  ui.propertiesView->layout()->removeWidget(propertiesWidget);
126  propertiesWidget->deleteLater();
127  propertiesWidget = NULL;
128  }
129 
130  QModelIndexList list = selected.indexes();
131  propertiesWidget = model->getPropertiesWidget(proxyModel->mapToSource(list.at(0)), ui.propertiesView);
132  ui.propertiesView->layout()->addWidget(propertiesWidget);
133  propertiesWidget->show();
134  }
135 
137  {
138  if (checked)
139  {
140  updateTimer->start(500);
141  }
142  else
143  {
144  updateTimer->stop();
145  }
146  }
147 
149  {
150  QAction* action = qobject_cast<QAction*>(sender());
151  if (action)
152  {
153  // auto widget = qobject_cast<DataFieldPropertiesWidget*>(propertiesWidget);
154  DatafieldFilterBasePtr filter;
155  if (!action->data().isNull())
156  {
157  auto filterName = action->data().toString().toStdString();
158  auto valFac = controller->getIceManager()->getCommunicator()->getValueFactoryManager()->find(filterName);
159  if (!valFac)
160  {
161  ARMARX_WARNING << "Could not find obj factory of type " << filterName;
162  return;
163  }
164 
165  filter = DatafieldFilterBasePtr::dynamicCast(valFac->create(filterName));
166  if (!filter)
167  {
168  ARMARX_WARNING << "Could not create filter of type " << valFac;
169  return;
170  }
171  }
172  else
173  {
174  ARMARX_ERROR << "Filter " << action->text().toStdString() << " is unknown";
175  return;
176  }
177  QDialog d;
178  Ui::FilterPropertiesDialog dSetup;
179  dSetup.setupUi(&d);
180  dSetup.tableWidget->setSortingEnabled(false);
181  StringFloatDictionary props = filter->getProperties();
182  ARMARX_INFO << VAROUT(props);
183  dSetup.tableWidget->setRowCount(props.size());
184  dSetup.tableWidget->setColumnCount(2);
185  int i = 0;
186  for (auto& p : props)
187  {
188  auto keyItem = new QTableWidgetItem(p.first.c_str());
189  keyItem->setFlags(Qt::ItemIsEnabled);
190  dSetup.tableWidget->setItem(i, 0, keyItem);
191  auto valueItem = new QTableWidgetItem(QString::number(p.second));
192  dSetup.tableWidget->setItem(i, 1, valueItem);
193  i++;
194  }
195  dSetup.groupBox->setTitle(action->text() + " properties");
196  if (d.exec() == QDialog::Accepted)
197  {
198  props.clear();
199  for (int i = 0; i < dSetup.tableWidget->rowCount(); ++i)
200  {
201  bool ok;
202  float value = dSetup.tableWidget->item(i, 1)->text().toFloat(&ok);
203  if (ok)
204  {
205  props[dSetup.tableWidget->item(i, 0)->text().toStdString()] = value;
206  }
207  }
208  ARMARX_INFO << VAROUT(props);
209  filter->setProperties(props);
210  emit createFilterClicked(selectedFilterId,
211  filter);
212  }
213 
214  }
215  selectedFilterId.clear();
216  }
217 
219  {
220  if (propertiesWidget)
221  {
222  ui.propertiesView->layout()->removeWidget(propertiesWidget);
223  propertiesWidget->deleteLater();
224  propertiesWidget = NULL;
225  }
226  }
227 
228  void ObserverWidget::treeView_contextMenu(const QPoint& point)
229  {
230  QPoint globalPos = ui.observerTreeView->viewport()->mapToGlobal(point);
231  QMenu treeViewMenu;
232 
233  // update action
234  QAction* updateAction = new QAction("Update", ui.observerTreeView);
235 
236  if (autoUpdateAction->isChecked())
237  {
238  updateAction->setEnabled(false);
239  }
240 
241  connect(updateAction, SIGNAL(triggered()), this, SLOT(updateObservers()));
242  treeViewMenu.addAction(updateAction);
243 
244  // // auto update action
245  // autoUpdateAction->setCheckable(true);
246  // connect(autoUpdateAction, SIGNAL(toggled(bool)), this, SLOT(toggleAutoUpdate(bool)));
247  // treeViewMenu.addAction(autoUpdateAction);
248 
249  DataFieldPropertiesWidget* widget = qobject_cast<DataFieldPropertiesWidget*>(propertiesWidget);
250  if (widget)
251  {
252  auto typeId = widget->getVariant()->getType();
253  selectedFilterId = widget->getDatafieldIdentifier()->getIdentifierStr();
254  QMenu* filters = treeViewMenu.addMenu("Filters");
255  std::map<std::string, bool> filterNames;
257  {
258  if (!facContainer)
259  {
260  continue;
261  }
262  for (auto pair : facContainer->getFactories())
263  {
264 
265  Ice::ValueFactoryPtr fac = pair.second;
266  if (!fac)
267  {
268  continue;
269  }
270  try
271  {
272  auto obj = fac->create(pair.first);
273  if (!obj)
274  {
275  continue;
276  }
277  // auto ids = obj->ice_ids();
278  // auto it = std::find(ids.begin(), ids.end(), "::armarx::DatafieldFilterBase");
279  DatafieldFilterBasePtr filter = DatafieldFilterBasePtr::dynamicCast(obj);
280  if (filter)
281  {
282  auto supportedTypes = filter->getSupportedTypes();
283  auto typeIt = std::find(supportedTypes.begin(), supportedTypes.end(), typeId);
284  filterNames.insert(std::make_pair(pair.first, typeIt != supportedTypes.end()));
285  }
286  }
287  catch (...)
288  {
289 
290  }
291  }
292  }
293  for (auto filter : filterNames)
294  {
295  const boost::regex e("::armarx::([a-zA-Z0-9_]+)Base");
296  boost::match_results<std::string::const_iterator> what;
297  boost::regex_search(filter.first, what, e);
298  QAction* a;
299  if (what.size() < 2)
300  {
301  a = filters->addAction(filter.first.c_str(), this, SLOT(filterSelected()));
302 
303  }
304  else
305  {
306  std::string filteredName = what[1];
307  a = filters->addAction(filteredName.c_str(), this, SLOT(filterSelected()));
308 
309  }
310  a->setEnabled(filter.second);
311  if (!filter.second)
312  {
313  a->setToolTip("This filter does not support this type");
314  }
315  a->setData(filter.first.c_str());
316  }
317  }
318 
319  // show menu
320  QAction* selectedItem = treeViewMenu.exec(globalPos);
321 
322 
323 
324  if (selectedItem)
325  {
326  // something was chosen, do stuff
327  }
328  else
329  {
330  // nothing was chosen
331  }
332  }
333 
334  void ObserverWidget::expandFilterSelection(QString filterStr)
335  {
336  //ARMARX_INFO_S << VAROUT(filterStr);
337  if (filterStr.length() == 0)
338  {
339  ui.observerTreeView->collapseAll();
340  // ui.monitoredManagersTree->expandToDepth(1);
341  }
342  else
343  {
344  filterExpansionTimer.start(500);
345  }
346  }
347 
348  void ObserverWidget::delayedFilterExpansion()
349  {
350  InfixFilterModel::ExpandFilterResults(ui.observerTreeView);
351  }
352 }
armarx::ObserverWidget::createNewModel
void createNewModel()
Definition: ObserverWidget.cpp:73
armarx::FactoryCollectionBase::GetPreregistratedFactories
static const std::vector< FactoryCollectionBasePtr > & GetPreregistratedFactories()
Definition: FactoryCollectionBase.h:73
GaussianFilter.h
armarx::DataFieldPropertiesWidget::getVariant
VariantPtr getVariant()
Definition: DataFieldPropertiesWidget.cpp:74
list
list(APPEND SOURCES ${QT_RESOURCES}) set(COMPONENT_LIBS ArmarXGui ArmarXCoreObservers ArmarXCoreEigen3Variants PlotterController $
Definition: CMakeLists.txt:49
armarx::ObserverWidget::updateObservers
void updateObservers()
Definition: ObserverWidget.cpp:89
armarx::ObserverItemModel::getPropertiesWidget
QWidget * getPropertiesWidget(const QModelIndex &index, QWidget *parent)
Definition: ObserverItemModel.cpp:76
ButterworthFilter.h
armarx::ObserverWidget::treeView_contextMenu
void treeView_contextMenu(const QPoint &point)
Definition: ObserverWidget.cpp:228
armarx::ObserverWidget::treeView_selected
void treeView_selected(const QItemSelection &selected, const QItemSelection &deselected)
Definition: ObserverWidget.cpp:116
armarx::ObserverWidget::createFilterClicked
void createFilterClicked(const std::string &datafieldStr, DatafieldFilterBasePtr filter)
visionx::imrecman::ok
@ ok
Definition: ImageRecordingManagerInterface.ice:46
DataFieldPropertiesWidget.h
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
controller
Definition: AddOperation.h:39
ObserverWidget.h
armarx::InfixFilterModel::ExpandFilterResults
static void ExpandFilterResults(QTreeView *treeView)
Expands the treeview that all items that match the filterstring are expanded and directly visible.
Definition: InfixFilterModel.cpp:63
armarx::ObserverItemModel::updateObservers
void updateObservers()
Definition: ObserverItemModel.cpp:228
armarx::DataFieldPropertiesWidget::getDatafieldIdentifier
DataFieldIdentifierPtr getDatafieldIdentifier() const
Definition: DataFieldPropertiesWidget.cpp:69
AverageFilter.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
IceUtil::Handle< FactoryCollectionBase >
MedianFilter.h
armarx::ObserverWidget::filterSelected
void filterSelected()
Definition: ObserverWidget.cpp:148
armarx::ObserverItemModel
Definition: ObserverItemModel.h:64
armarx::InfixFilterModel
This proxy model reimplements the filterAcceptsRow function with a new behavior: All elements that fi...
Definition: InfixFilterModel.h:41
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::ObserverWidget::ObserverWidget
ObserverWidget(ObserverWidgetController *controller)
Definition: ObserverWidget.cpp:44
armarx::DataFieldPropertiesWidget
Definition: DataFieldPropertiesWidget.h:41
armarx::ObserverWidget::toggleAutoUpdate
void toggleAutoUpdate(bool checked)
Definition: ObserverWidget.cpp:136
armarx::ObserverWidgetController
Definition: ObserverWidgetController.h:57
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::ObserverWidget::clearDetailedView
void clearDetailedView()
Definition: ObserverWidget.cpp:218