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