MIRA
PropertyEditor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_PROPERTYEDITOR_H_
48 #define _MIRA_PROPERTYEDITOR_H_
49 
50 #include <QVariant>
51 #include <QModelIndex>
52 #include <QIcon>
53 #include <QColor>
54 #include <QWidget>
55 #include <QLineEdit>
56 #include <QEvent>
57 #include <QMutex>
58 #include <QWaitCondition>
59 
61 #include <utils/EnumToFlags.h>
62 
63 #include <widgets/TreeViewFilter.h>
64 
65 class QToolButton;
66 
67 namespace mira {
68 
70 
71 class PropertyItemEditor;
72 
73 class PropertyEditor : public QWidget, public PropertyNodeListener
74 {
75  Q_OBJECT
76 
77 public:
78  PropertyEditor(QWidget *parent = 0);
79  virtual ~PropertyEditor();
80 
81 public:
82 
83  class Delegate;
84  typedef boost::shared_ptr<Delegate> DelegatePtr;
85 
86 public:
87 
90 
91 public slots:
92 
97  void clear();
98 
100  void addProperty(PropertyNode* property, QVariant user = QVariant());
101 
103  void removeProperty(PropertyNode* property);
104 
106  void moveUpProperty(PropertyNode* property);
107 
109  void moveDownProperty(PropertyNode* property);
110 
111  QVariant getUser(PropertyNode* property);
112 
113 public:
114 
119  void setHideSingleRootNode(bool hide = true);
120 
122  bool getHideSingleRootNode() const;
123 
124 public:
125 
126  void installFilterShortCut(QWidget* widget, const QString& shortcut = "Ctrl+F");
127 
128  bool getAutoHideFilterBar() const;
129 
130 public slots:
131 
132  void setAutoHideFilterBar(bool hide = true);
133  void showFilter();
134  void hideFilter();
135 
136 public:
137 
139  void setColor(const PropertyNode* property, const QColor& color);
140 
141  QColor getColor(const PropertyNode* property) const;
142 
143 public:
144 
145  std::string getText(const PropertyNode* property) const;
146  QIcon getIcon(const PropertyNode* property) const;
147  QColor getBackgroundColor(const PropertyNode* property) const;
148  QWidget* createEditor(PropertyNode* property, QWidget* parent);
149  std::string getName(const PropertyNode* property);
150 
151 protected:
152 
153  void timerEvent(QTimerEvent*);
154  void customEvent(QEvent*);
155 
156 private slots:
157 
158  void onDestroyedEditorDelegate(QObject*);
159 
160 public:
161 
162  const PropertyNode* getCurrentEditedProperty() const { return mCurrentEditedProperty; }
163 
164 public:
165 
168  virtual void beginAddChildren(const PropertyNode* node, int index, int count);
169  virtual void endAddChildren();
170 
171  virtual void beginRemoveChildren(const PropertyNode* node, int index, int count);
172  virtual void endRemoveChildren();
173 
174  virtual void beginMoveChildren(const PropertyNode* node, int index, int count, int destination);
175  virtual void endMoveChildren();
176 
178 private:
179 
180  struct AddRemoveChildEvent : public QEvent
181  {
182  static const QEvent::Type typeID;
183 
184  enum EventSubType {
185  BEGIN_ADD,
186  END_ADD,
187  BEGIN_REMOVE,
188  END_REMOVE,
189  BEGIN_MOVE,
190  END_MOVE
191  };
192 
193  AddRemoveChildEvent(EventSubType iSubtype, const PropertyNode* iNode = NULL,
194  int iIndex = 0, int iCount = 1,
195  bool iFirst = false, int iDestination = -1)
196  : QEvent(typeID), subtype(iSubtype), node(iNode),
197  index(iIndex), count(iCount), first(iFirst), destination(iDestination) {}
198 
199  EventSubType subtype;
200  const PropertyNode* node;
201  int index;
202  int count;
203  bool first;
204  int destination;
205  };
206 
207 public:
208 
209  const static QColor lightBlue;
210  const static QColor lightRed;
211  const static QColor lightGreen;
212  const static QColor lightYellow;
213  const static QColor lightPurple;
214 
215 private:
216 
217  class DelegateRegistry;
218  class Model;
219  class TreeView;
221 
222  //friend class DelegateRegistry;
223  friend class Model;
224  friend class TreeView;
225  friend class ItemDelegate;
226  friend class PropertyItemEditor;
227 
228  TreeView* mTreeView;
229  Model* mModel;
230  ItemDelegate* mItemDelegate;
231 
232  TreeViewFilter* mFilter;
233 
234  std::map<const PropertyNode*, QColor> mColorMap;
235 
236  int mUpdateTimerId;
237  bool mHideSingleRootNode;
238 
239  PropertyNode* mCurrentEditedProperty;
240 
241  QMutex mPropertyNodeListenerMutex;
242  QWaitCondition mPropertyNodeListenerCondition;
243  QList<QPersistentModelIndex> mPopulatingIndex;
244 };
245 
247 
259 {
261 public:
262 
264  typedef std::list<SupportedType> SupportedTypes;
265 
267  {
268  enum Flags {
270  TEXT = 0x01,
271 
273  ICON = 0x02,
274 
276  COLOR = 0x04,
277 
279  EDITOR = 0x08,
280 
282  DIALOG = 0x10,
283 
285  NAME = 0x20,
286  };
288 
289 
290  SupportedType(const Typename& iType, Flags iFlags) :
291  type(iType), flags(iFlags) {}
292 
293  Typename type; // the typename of the type
294 
296 
297  operator SupportedTypes()
298  {
299  SupportedTypes list;
300  list.push_back(*this);
301  return list;
302  }
303 
305  list.push_back(type);
306  return list;
307  }
308  };
309 
310  template <typename Type>
312  return SupportedType(typeName<Type>(), flags);
313  }
314 
315  virtual SupportedTypes supportedTypes() const = 0;
316 
320  virtual std::string getText(const PropertyNode* property) { return std::string(); }
321 
322  virtual QIcon getIcon(const PropertyNode* property) { return QIcon(); }
323 
324  virtual QColor getBackgroundColor(const PropertyNode* property) { return QColor(); }
325 
326  virtual QWidget* createEditor(PropertyNode* property, QWidget* parent) { return NULL; }
327 
328  virtual bool execDialog(PropertyNode* property, QWidget* parent) { return false; }
329 
330  virtual std::string getName(const PropertyNode* property) { return property->name(); }
331 };
332 
334 
335 class PropertyItemEditor : public QWidget
336 {
337  Q_OBJECT
338 public:
339 
340  PropertyItemEditor(PropertyEditor* editor, QWidget* parent,
341  PropertyNode* property,
342  PropertyEditor::DelegatePtr editorDelegate,
343  PropertyEditor::DelegatePtr dialogDelegate);
344 
345  bool eventFilter(QObject *obj, QEvent *e);
346 
347 private slots:
348 
349  void buttonClicked();
350 
351 private:
352 
353  PropertyEditor* mEditor;
354  PropertyNode* mProperty;
355  QToolButton* mButton;
356  PropertyEditor::DelegatePtr mDialogDelegate;
357 
358 };
359 
361 
362 }
363 
364 #endif
friend class ItemDelegate
Definition: PropertyEditor.h:225
virtual QWidget * createEditor(PropertyNode *property, QWidget *parent)
Definition: PropertyEditor.h:326
Abstract base class for all derived property node classes.
Definition: PropertyNode.h:202
set if the Delegate provides at least a text for a label
Definition: PropertyEditor.h:270
static const QColor lightGreen
Definition: PropertyEditor.h:211
static const QColor lightPurple
Definition: PropertyEditor.h:213
void moveUpProperty(PropertyNode *property)
Moves the property node up in the editor&#39;s property node list.
virtual std::string getText(const PropertyNode *property)
Returns text for a text label.
Definition: PropertyEditor.h:320
PropertyNode * currentProperty()
Returns the current selected property node, or NULL if nothing is selected.
QIcon getIcon(const PropertyNode *property) const
set if the Delegate can provide an editor for the data
Definition: PropertyEditor.h:279
boost::shared_ptr< Delegate > DelegatePtr
Definition: PropertyEditor.h:83
Declaration and implementation of the property node hierarchy.
Macros for generating logical operators for using enum values as flags.
friend class TreeView
Definition: PropertyEditor.h:224
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
QColor getBackgroundColor(const PropertyNode *property) const
std::string Typename
Definition: Typename.h:60
void timerEvent(QTimerEvent *)
static SupportedType makeSupportedType(SupportedType::Flags flags)
Definition: PropertyEditor.h:311
set if the Delegate provides an background color
Definition: PropertyEditor.h:276
Definition: PropertyNode.h:430
virtual std::string getName(const PropertyNode *property)
Definition: PropertyEditor.h:330
set if the Delegate provides an icon
Definition: PropertyEditor.h:273
std::list< SupportedType > SupportedTypes
Definition: PropertyEditor.h:263
void clear()
Removes all property nodes that were set and therefore clears the whole editor content (without delet...
virtual void endAddChildren()
bool getHideSingleRootNode() const
Returns the set mode, whether a single root node should be hidden or not.
void setHideSingleRootNode(bool hide=true)
If hiding of the single root node is enabled, a single top level node will not be shown explicitly...
PropertyItemEditor(PropertyEditor *editor, QWidget *parent, PropertyNode *property, PropertyEditor::DelegatePtr editorDelegate, PropertyEditor::DelegatePtr dialogDelegate)
#define MIRA_ABSTRACT_OBJECT(classIdentifier)
Use this MACRO instead of MIRA_OBJECT to declare the class as abstract.
Definition: FactoryMacros.h:235
Definition: PropertyEditor.h:266
virtual SupportedTypes supportedTypes() const =0
Typename type
Definition: PropertyEditor.h:293
virtual void beginMoveChildren(const PropertyNode *node, int index, int count, int destination)
The object class acts as a generic base class for classes which should be used with the classFactory...
Definition: Object.h:144
bool eventFilter(QObject *obj, QEvent *e)
QVariant getUser(PropertyNode *property)
set if the Delegate can provide a dialog for editing the data
Definition: PropertyEditor.h:282
Filter widget containing an line edit that filters the elements of an assigned TreeView.
void setAutoHideFilterBar(bool hide=true)
virtual void endMoveChildren()
PropertyEditor(QWidget *parent=0)
virtual QIcon getIcon(const PropertyNode *property)
Definition: PropertyEditor.h:322
virtual void beginAddChildren(const PropertyNode *node, int index, int count)
virtual QColor getBackgroundColor(const PropertyNode *property)
Definition: PropertyEditor.h:324
void removeProperty(PropertyNode *property)
Remove the specified property node from the editor.
Definition: PropertyEditor.h:335
virtual void endRemoveChildren()
void setColor(const PropertyNode *property, const QColor &color)
Sets the background color for the specified property.
const PropertyNode * getCurrentEditedProperty() const
Definition: PropertyEditor.h:162
bool getAutoHideFilterBar() const
friend class Model
Definition: PropertyEditor.h:220
SupportedType(const Typename &iType, Flags iFlags)
Definition: PropertyEditor.h:290
Flags flags
Definition: PropertyEditor.h:295
std::string getName(const PropertyNode *property)
virtual void beginRemoveChildren(const PropertyNode *node, int index, int count)
QWidget * createEditor(PropertyNode *property, QWidget *parent)
void installFilterShortCut(QWidget *widget, const QString &shortcut="Ctrl+F")
static const QColor lightBlue
Definition: PropertyEditor.h:209
Definition: PropertyEditor.h:73
static const QColor lightRed
Definition: PropertyEditor.h:210
std::string getText(const PropertyNode *property) const
void customEvent(QEvent *)
set if the Delegate provides a (dynamic) name
Definition: PropertyEditor.h:285
virtual ~PropertyEditor()
Flags
Definition: PropertyEditor.h:268
Provides display and editing facilities for property items in the editor.
Definition: PropertyEditor.h:258
static const QColor lightYellow
Definition: PropertyEditor.h:212
void moveDownProperty(PropertyNode *property)
Moves the property node down in the editor&#39;s property node list.
void addProperty(PropertyNode *property, QVariant user=QVariant())
Adds the specified property node as top level node to the editor.
QColor getColor(const PropertyNode *property) const
friend SupportedTypes operator+(SupportedTypes list, const SupportedType &type)
Definition: PropertyEditor.h:304
Definition: TreeViewFilter.h:62
virtual bool execDialog(PropertyNode *property, QWidget *parent)
Definition: PropertyEditor.h:328