TreeModel.hh 7.51 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
33
34
35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36
37
38
39
40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42
43
44
45
46
47
48
49

#ifndef TREEMODEL_H
#define TREEMODEL_H

#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>

Dirk Wilden's avatar
Dirk Wilden committed
50
#include "TreeItem.hh"
Jan Möbius's avatar
 
Jan Möbius committed
51
52
53
54
55

class TreeModel : public QAbstractItemModel
{
    Q_OBJECT

Dirk Wilden's avatar
Dirk Wilden committed
56
signals:
Dirk Wilden's avatar
Dirk Wilden committed
57
   // the connected TreeView changed data
Matthias Möller's avatar
Matthias Möller committed
58
   void dataChangedInside(int _id, int _column,const QVariant& _value);
Dirk Wilden's avatar
Dirk Wilden committed
59
60
   // an object was moved via dragNdrop
   void moveBaseObject(int _id, int _newParentId);
Dirk Wilden's avatar
Dirk Wilden committed
61

Jan Möbius's avatar
 
Jan Möbius committed
62
public:
Dirk Wilden's avatar
Dirk Wilden committed
63
64

    /// Constructor
Jan Möbius's avatar
 
Jan Möbius committed
65
    TreeModel(QObject *_parent = 0);
Dirk Wilden's avatar
Dirk Wilden committed
66
67

    /// Destructor
Jan Möbius's avatar
 
Jan Möbius committed
68
69
    ~TreeModel();

Dirk Wilden's avatar
Dirk Wilden committed
70
71
72
73
74
75
76
//===========================================================================
/** @name inherited from QAbstractItemModel
  * @{ */
//===========================================================================

public:

Jan Möbius's avatar
 
Jan Möbius committed
77
    /// Get the data of the corresponding entry
Matthias Möller's avatar
Matthias Möller committed
78
    QVariant data(const QModelIndex &_index,int _role) const;
Dirk Wilden's avatar
Dirk Wilden committed
79

Jan Möbius's avatar
 
Jan Möbius committed
80
    /// return the types of the corresponding entry
81
    Qt::ItemFlags flags(const QModelIndex &_index) const;
Dirk Wilden's avatar
Dirk Wilden committed
82

Jan Möbius's avatar
 
Jan Möbius committed
83
    /// return the header data of the model
Matthias Möller's avatar
Matthias Möller committed
84
85
86
    QVariant headerData(int _section,
                        Qt::Orientation _orientation,
                        int _role = Qt::DisplayRole) const;
Dirk Wilden's avatar
Dirk Wilden committed
87
88

    /// Get the ModelIndex at given row,column
Matthias Möller's avatar
Matthias Möller committed
89
90
    QModelIndex index(int _row, int _column,
                      const QModelIndex &_parent = QModelIndex()) const;
Dirk Wilden's avatar
Dirk Wilden committed
91
92

    /// Get the parent ModelIndex
Matthias Möller's avatar
Matthias Möller committed
93
    QModelIndex parent(const QModelIndex &_index) const;
Dirk Wilden's avatar
Dirk Wilden committed
94
95

    /// get the number of rows
Matthias Möller's avatar
Matthias Möller committed
96
    int rowCount(const QModelIndex &_parent = QModelIndex()) const;
Dirk Wilden's avatar
Dirk Wilden committed
97

Jan Möbius's avatar
Jan Möbius committed
98
99
100
101
102
103
    /** \brief Return the number of columns
     *
     * @param  _parent unused
     * @return return always 4
     */
    int columnCount(const QModelIndex &_parent = QModelIndex()) const;
Dirk Wilden's avatar
Dirk Wilden committed
104

Jan Möbius's avatar
Jan Möbius committed
105
106
    /** \brief Set Data at 'index' to 'value'
     *
Matthias Möller's avatar
Matthias Möller committed
107
108
109
     * @param  _index a ModelIndex defining the positin in the model
     * @param  _value the new value
     * @param  _role unused
Jan Möbius's avatar
Jan Möbius committed
110
111
     * @return return if the data was set successfully
     */
Matthias Möller's avatar
Matthias Möller committed
112
    bool setData(const QModelIndex &_index, const QVariant &_value , int _role);
Dirk Wilden's avatar
Dirk Wilden committed
113

Dirk Wilden's avatar
Dirk Wilden committed
114
115
116
/** @} */

//===========================================================================
Dirk Wilden's avatar
Dirk Wilden committed
117
/** @name Internal DataStructure (the TreeItem Tree)
Dirk Wilden's avatar
Dirk Wilden committed
118
119
120
121
  * @{ */
//===========================================================================

public:
122

Dirk Wilden's avatar
Dirk Wilden committed
123
124
    /// Return the ModelIndex corresponding to a given TreeItem and Column
    QModelIndex getModelIndex(TreeItem* _object, int _column );
Dirk Wilden's avatar
Dirk Wilden committed
125

Jan Möbius's avatar
 
Jan Möbius committed
126
    /// Check if the given item is the root item
Dirk Wilden's avatar
Dirk Wilden committed
127
    bool isRoot(TreeItem* _item);
Dirk Wilden's avatar
Dirk Wilden committed
128
129

    /// Get the name of a given object
Dirk Wilden's avatar
Dirk Wilden committed
130
131
132
    bool getObjectName(TreeItem* _object , QString& _name);

    /// Get the TreeItem corresponding to a given ModelIndex
133
    TreeItem *getItem(const QModelIndex &_index) const;
Dirk Wilden's avatar
Dirk Wilden committed
134

135
136
137
    /// Get the TreeItem corresponding to a given OpenFlipper ObjectID
    TreeItem *getItem(const int _id) const;

Dirk Wilden's avatar
Dirk Wilden committed
138
    /// Get the name of a TreeItem corresponding to a given ModelIndex
139
    QString itemName(const QModelIndex &_index) const;
Dirk Wilden's avatar
Dirk Wilden committed
140
141

    /// Get the id of a TreeItem corresponding to a given ModelIndex
142
    int itemId(const QModelIndex &_index) const;
Dirk Wilden's avatar
Dirk Wilden committed
143

144

Dirk Wilden's avatar
Dirk Wilden committed
145
    /// The object with the given id has been changed. Check if model also has to be changed
146
    void objectChanged(int _id);
Dirk Wilden's avatar
Dirk Wilden committed
147

Dirk Wilden's avatar
Dirk Wilden committed
148
149
150
151
    /// The object with the given id has been added. add it to the internal tree
    void objectAdded(BaseObject* _object);

    /// The object with the given id has been deleted. delete it from the internal tree
152
    void objectDeleted(int _id);
Dirk Wilden's avatar
Dirk Wilden committed
153

Dirk Wilden's avatar
Dirk Wilden committed
154
155
    /// move the item to a new parent
    void moveItem(TreeItem* _item, TreeItem* _parent );
156

Dirk Wilden's avatar
Dirk Wilden committed
157
158
private:

Jan Möbius's avatar
Jan Möbius committed
159
    /// Root item of the tree
Dirk Wilden's avatar
Dirk Wilden committed
160
161
    TreeItem* rootItem_;

Dirk Wilden's avatar
Dirk Wilden committed
162
/** @} */
Dirk Wilden's avatar
Dirk Wilden committed
163
164
165
166
167
168

//===========================================================================
/** @name Drag and Drop
  * @{ */
//===========================================================================

Dirk Wilden's avatar
Dirk Wilden committed
169
170
public:

Dirk Wilden's avatar
Dirk Wilden committed
171
    /// supported drag & Drop actions
172
173
    Qt::DropActions supportedDropActions() const;

Dirk Wilden's avatar
Dirk Wilden committed
174
    /// stores the mimeType for Drag & Drop
175
176
    QStringList mimeTypes() const;

Dirk Wilden's avatar
Dirk Wilden committed
177
    /// get the mimeData for a given ModelIndex
178
179
    QMimeData* mimeData(const QModelIndexList& indexes) const;

Jan Möbius's avatar
Jan Möbius committed
180
181
182
183
184
185
186
187
188
    /** \brief This is called when mimeData is dropped
     *
     * @param data   The dropped data
     * @param action The definition of the dropAction which occurred
     * @param row    Unused
     * @param column Unused
     * @param parent Parent under which the drop occurred
     * @return returns if the drop was successful
     */
Jan Möbius's avatar
Jan Möbius committed
189
    bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
Dirk Wilden's avatar
Dirk Wilden committed
190
191
192

/** @} */

Jan Möbius's avatar
 
Jan Möbius committed
193
194
195
196
};

#endif