ObjectExplorerModel.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-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 MemoryX::gui-plugins::SceneEditor
19  * @date 2015
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #include <QStandardItem>
25 #include <QDataStream>
26 #include <QByteArray>
27 #include <QtDesigner/abstractmetadatabase.h>
28 #include "ObjectExplorerModel.h"
29 
31  QSortFilterProxyModel(parent),
33  sModel(new gui::ObjectExplorerModel::ObjectExplorerModelPrivate(control, 0, 1, this))
34 {
35  setSourceModel(sModel);
36 
37  connect(this, SIGNAL(reload()), this, SLOT(onReload()));
38 }
39 
41 {
42  emit reload();
43 }
44 
45 void gui::ObjectExplorerModel::onReload()
46 {
47  sModel->onReload();
48 }
49 
51 {
52  setFilterCaseSensitivity(Qt::CaseInsensitive);
53  QSortFilterProxyModel::setFilterFixedString(searchPattern);
54 }
55 
56 std::pair<std::string, std::string> gui::ObjectExplorerModel::getItemInformation(const QModelIndex& index)
57 {
58  return sModel->getItemInformation(mapToSource(index));
59 }
60 
61 bool gui::ObjectExplorerModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
62 {
63  QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
64  return showThis(index);
65 }
66 
67 bool gui::ObjectExplorerModel::showThis(const QModelIndex& index) const
68 {
69  bool acceptRow = false;
70 
71  //Gives you the info for number of childs with a parent
72  if (sourceModel()->rowCount(index) > 0)
73  {
74  for (int nChild = 0; nChild < sourceModel()->rowCount(index); ++nChild)
75  {
76  QModelIndex childIndex = sourceModel()->index(nChild, 0, index);
77 
78  if (!childIndex.isValid())
79  {
80  break;
81  }
82 
83  acceptRow = showThis(childIndex);
84 
85  if (acceptRow)
86  {
87  break;
88  }
89  }
90  }
91  else
92  {
93  QModelIndex useIndex = sourceModel()->index(index.row(), 0, index.parent());
94  QString name = sourceModel()->data(useIndex, Qt::DisplayRole).toString();
95 
96  if (!name.contains(filterRegExp()) || name.isEmpty())
97  {
98  acceptRow = false;
99  }
100  else
101  {
102  acceptRow = true;
103  }
104  }
105 
106  return acceptRow;
107 }
108 
109 QPixmap gui::ObjectExplorerModel::getItemPixmap(const QModelIndex& index) const
110 {
111  return sModel->getItemPixmap(mapToSource(index));
112 }
113 
114 
115 /* methods for private class */
116 
117 gui::ObjectExplorerModel::ObjectExplorerModelPrivate::ObjectExplorerModelPrivate(const controller::ControllerPtr& control, int rows, int columns, QObject* parent) :
118  QStandardItemModel(rows, columns, parent),
119  control(control),
120  sizeHint(50, 50)
121 {
122 }
123 
124 void gui::ObjectExplorerModel::ObjectExplorerModelPrivate::onReload()
125 {
127  {
128  std::vector<std::string> collections = controller->getMemoryXController()->getPriorKnowlegdeController()->getCollectionNames();
129  this->clear();
130  this->setRowCount(collections.size());
131  std::vector<memoryx::ObjectClassPtr> objects;
132  std::string objectName;
133 
134  for (int row = 0; row < (int)collections.size(); ++row)
135  {
136  objects = controller->getMemoryXController()->getPriorKnowlegdeController()->getAllObjectClassesPtr(collections[row]);
137 
138  // check if collection is empty
139  // in this case the collection will not be added to the model
140  if (objects.size() < 1)
141  {
142  ARMARX_WARNING_S << "No objects found in collection " << collections.at(row);
143  continue;
144  }
145 
146  QStandardItem* item = new QStandardItem(QString::fromStdString(collections[row]));
147 
148  for (int i = 0; i < (int)objects.size(); ++i)
149  {
150  objectName = objects[i]->getName();
151 
152  if (controller->getMemoryXController()->getPriorKnowlegdeController()->getCoinVisualisation(objects[i], true) && controller->getMemoryXController()->getPriorKnowlegdeController()->getCoinVisualisation(objects[i], false))
153  {
154  QStandardItem* child = new QStandardItem(QString::fromStdString(objectName));
155  child->setSizeHint(sizeHint);
156  child->setIcon(this->getPreviewImageOfObject(controller, objects[i]));
157  child->setEditable(false);
158  item->setChild(i, child);
159  }
160  else
161  {
162  ARMARX_WARNING_S << "No model found for: " << objectName;
163  }
164  }
165 
166  item->setSizeHint(QSize(30, 30));
167  item->setDragEnabled(false);
168  this->appendRow(item);
169  }
170  }
171 }
172 
173 std::pair<std::string, std::string> gui::ObjectExplorerModel::ObjectExplorerModelPrivate::getItemInformation(const QModelIndex& index)
174 {
175  std::pair<std::string, std::string> toReturn;
176 
177  if (index.isValid())
178  {
180  QStandardItem* item = itemFromIndex(index);
181 
182  if (controller && item->parent())
183  {
184  toReturn.first = item->text().toStdString(); // objectClass
185  toReturn.second = item->parent()->text().toStdString(); // collection
186  }
187  }
188 
189 
190  return toReturn;
191 
192 }
193 
194 QPixmap gui::ObjectExplorerModel::ObjectExplorerModelPrivate::getItemPixmap(const QModelIndex& index) const
195 {
196  QPixmap pixmap;
197 
198  if (index.isValid())
199  {
200  QStandardItem* item = itemFromIndex(index);
201  pixmap = item->icon().pixmap(item->icon().actualSize(sizeHint));
202  }
203  else
204  {
205  QIcon i = QIcon(QPixmap::fromImage(QImage(":/icons/armarx-gui.png")));
206  pixmap = i.pixmap(i.actualSize(sizeHint));
207  }
208 
209  return pixmap;
210 }
211 
212 QIcon gui::ObjectExplorerModel::ObjectExplorerModelPrivate::getPreviewImageOfObject(const controller::ControllerPtr& controller, const memoryx::ObjectClassPtr& object)
213 {
214  //SoNode* visualisation = controller->getMemoryXController()->getPriorKnowlegdeController()->getCoinVisualisation(object, false);
215  QImage preview;
216  preview = QImage(":/icons/armarx-gui.png");
217 
218  /*if (visualisation)
219  {
220  preview = controller->getScene()->getPreviewGenerator()->createPreview(visualisation, 50, 50);
221  }
222  if (!visualisation || preview.isNull())
223  {
224  preview = QImage("/home/h2t/armarx/Gui/source/Gui/ArmarXGui/icons/armarx-gui.png", "PNG");
225  std::cout << "QImage does not exist for " + object->getName() << std::endl;
226  }*/
227 
228  return QIcon(QPixmap::fromImage(preview));
229 }
230 
231 QStringList gui::ObjectExplorerModel::ObjectExplorerModelPrivate::mimeTypes() const
232 {
233  QStringList types;
234  types << "application/vnd.text.list";
235  return types;
236 }
237 
238 QMimeData* gui::ObjectExplorerModel::ObjectExplorerModelPrivate::mimeData(const QModelIndexList& indexes) const
239 {
240  QMimeData* mimeData = new QMimeData();
241  QByteArray encodedData;
242 
243  QDataStream stream(&encodedData, QIODevice::WriteOnly);
244 
245  foreach (QModelIndex index, indexes)
246  {
247  if (index.isValid())
248  {
249  QStandardItem* item = itemFromIndex(index);
250 
251  if (item->parent())
252  {
253  stream << item->parent()->text();
254  stream << item->text();
255  }
256  }
257  }
258 
259  mimeData->setData("application/vnd.text.list", encodedData);
260  return mimeData;
261 }
index
uint8_t index
Definition: EtherCATFrame.h:59
gui::ObjectExplorerModel::getItemPixmap
QPixmap getItemPixmap(const QModelIndex &index) const
Returns the Icon of the class at index.
Definition: ObjectExplorerModel.cpp:109
IceInternal::Handle< ObjectClass >
controller
Definition: AddOperation.h:39
controller::ControllerPtr
std::shared_ptr< Controller > ControllerPtr
Definition: ClassDefinitions.h:41
gui::ObjectExplorerModel::reload
void reload()
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
gui::ObjectExplorerModel::setFilterFixedString
void setFilterFixedString(QString searchPattern)
Sets the string to filter all classes.
Definition: ObjectExplorerModel.cpp:50
gui::ObjectExplorerModel::getItemInformation
std::pair< std::string, std::string > getItemInformation(const QModelIndex &index)
Returns a tupel containing information about the class at the given index.
Definition: ObjectExplorerModel.cpp:56
gui::ObjectExplorerModel
This class provides a model for storing classes from memoryxcontroller::PriorKnowledgeController and ...
Definition: ObjectExplorerModel.h:44
control
This file is part of ArmarX.
ObjectExplorerModel.h
gui::ObjectExplorerModel::ObjectExplorerModel
ObjectExplorerModel(const controller::ControllerPtr &control, QObject *parent=0)
Creates a new instance of this class.
Definition: ObjectExplorerModel.cpp:30
gui::ObjectExplorerModel::onConnect
void onConnect()
Reloads the model.
Definition: ObjectExplorerModel.cpp:40