Developer Documentation
TreeModel.hh
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #ifndef TREEMODEL_H
51 #define TREEMODEL_H
52 
53 #include <QAbstractItemModel>
54 #include <QModelIndex>
55 #include <QVariant>
56 
57 #include "TreeItem.hh"
58 
59 class TreeModel : public QAbstractItemModel
60 {
61  Q_OBJECT
62 
63 signals:
64  // the connected TreeView changed data
65  void dataChangedInside(int _id, int _column,const QVariant& _value);
66  // an object was moved via dragNdrop
67  void moveBaseObject(int _id, int _newParentId);
68 
69 public:
70 
72  TreeModel(QObject *_parent = 0);
73 
75  ~TreeModel();
76 
77 //===========================================================================
80 //===========================================================================
81 
82 public:
83 
85  QVariant data(const QModelIndex &_index,int _role) const;
86 
88  Qt::ItemFlags flags(const QModelIndex &_index) const;
89 
91  QVariant headerData(int _section,
92  Qt::Orientation _orientation,
93  int _role = Qt::DisplayRole) const;
94 
96  QModelIndex index(int _row, int _column,
97  const QModelIndex &_parent = QModelIndex()) const;
98 
100  QModelIndex parent(const QModelIndex &_index) const;
101 
103  int rowCount(const QModelIndex &_parent = QModelIndex()) const;
104 
110  int columnCount(const QModelIndex &_parent = QModelIndex()) const;
111 
119  bool setData(const QModelIndex &_index, const QVariant &_value , int _role);
120 
123 //===========================================================================
126 //===========================================================================
127 
128 public:
129 
131  QModelIndex getModelIndex(TreeItem* _object, int _column );
132 
134  bool isRoot(TreeItem* _item);
135 
137  bool getObjectName(TreeItem* _object , QString& _name);
138 
140  TreeItem *getItem(const QModelIndex &_index) const;
141 
143  TreeItem *getItem(const int _id) const;
144 
146  QString itemName(const QModelIndex &_index) const;
147 
149  int itemId(const QModelIndex &_index) const;
150 
151 
153  void objectChanged(int _id);
154 
156  void objectAdded(BaseObject* _object);
157 
159  void objectDeleted(int _id);
160 
162  void moveItem(TreeItem* _item, TreeItem* _parent );
163 
164 private:
165 
168 
171 //===========================================================================
174 //===========================================================================
175 
176 public:
177 
179  Qt::DropActions supportedDropActions() const;
180 
182  QStringList mimeTypes() const;
183 
185  QMimeData* mimeData(const QModelIndexList& indexes) const;
186 
196  bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
197 
200 };
201 
202 #endif
203 
int itemId(const QModelIndex &_index) const
Get the id of a TreeItem corresponding to a given ModelIndex.
Definition: TreeModel.cc:552
int columnCount(const QModelIndex &_parent=QModelIndex()) const
Return the number of columns.
Definition: TreeModel.cc:90
int rowCount(const QModelIndex &_parent=QModelIndex()) const
get the number of rows
Definition: TreeModel.cc:344
QModelIndex getModelIndex(TreeItem *_object, int _column)
Return the ModelIndex corresponding to a given TreeItem and Column.
Definition: TreeModel.cc:572
void objectChanged(int _id)
The object with the given id has been changed. Check if model also has to be changed.
Definition: TreeModel.cc:363
QString itemName(const QModelIndex &_index) const
Get the name of a TreeItem corresponding to a given ModelIndex.
Definition: TreeModel.cc:535
TreeModel(QObject *_parent=0)
Constructor.
Definition: TreeModel.cc:71
~TreeModel()
Destructor.
Definition: TreeModel.cc:82
Qt::ItemFlags flags(const QModelIndex &_index) const
return the types of the corresponding entry
Definition: TreeModel.cc:231
QStringList mimeTypes() const
stores the mimeType for Drag & Drop
Definition: TreeModel.cc:626
TreeItem * rootItem_
Root item of the tree.
Definition: TreeModel.hh:167
QVariant data(const QModelIndex &_index, int _role) const
Get the data of the corresponding entry.
Definition: TreeModel.cc:105
void objectAdded(BaseObject *_object)
The object with the given id has been added. add it to the internal tree.
Definition: TreeModel.cc:436
QModelIndex parent(const QModelIndex &_index) const
Get the parent ModelIndex.
Definition: TreeModel.cc:322
void moveItem(TreeItem *_item, TreeItem *_parent)
move the item to a new parent
Definition: TreeModel.cc:498
bool isRoot(TreeItem *_item)
Check if the given item is the root item.
Definition: TreeModel.cc:602
TreeItem * getItem(const QModelIndex &_index) const
Get the TreeItem corresponding to a given ModelIndex.
Definition: TreeModel.cc:518
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
This is called when mimeData is dropped.
Definition: TreeModel.cc:669
QModelIndex index(int _row, int _column, const QModelIndex &_parent=QModelIndex()) const
Get the ModelIndex at given row,column.
Definition: TreeModel.cc:295
bool getObjectName(TreeItem *_object, QString &_name)
Get the name of a given object.
void objectDeleted(int _id)
The object with the given id has been deleted. delete it from the internal tree.
Definition: TreeModel.cc:471
Qt::DropActions supportedDropActions() const
supported drag & Drop actions
Definition: TreeModel.cc:614
QVariant headerData(int _section, Qt::Orientation _orientation, int _role=Qt::DisplayRole) const
return the header data of the model
Definition: TreeModel.cc:268
bool setData(const QModelIndex &_index, const QVariant &_value, int _role)
Set Data at &#39;index&#39; to &#39;value&#39;.
Definition: TreeModel.cc:585
QMimeData * mimeData(const QModelIndexList &indexes) const
get the mimeData for a given ModelIndex
Definition: TreeModel.cc:641