TreeModel.hh 4.68 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius committed
1
2
3
4
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  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.
// 
//  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 Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision$
//   $Author$
//   $Date$
//
//=============================================================================




#ifndef TREEMODEL_H
#define TREEMODEL_H

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

#include <OpenFlipper/common/Types.hh>

class TreeItem;

class TreeModel : public QAbstractItemModel
{
    Q_OBJECT

Dirk Wilden's avatar
Dirk Wilden committed
50
signals:
Dirk Wilden's avatar
Dirk Wilden committed
51
   void dataChangedInside(BaseObject* _obj, int _column);
Dirk Wilden's avatar
Dirk Wilden committed
52

Jan Möbius's avatar
 
Jan Möbius committed
53
public:
Dirk Wilden's avatar
Dirk Wilden committed
54
55

    /// Constructor
Jan Möbius's avatar
 
Jan Möbius committed
56
    TreeModel(QObject *_parent = 0);
Dirk Wilden's avatar
Dirk Wilden committed
57
58

    /// Destructor
Jan Möbius's avatar
 
Jan Möbius committed
59
60
    ~TreeModel();

Dirk Wilden's avatar
Dirk Wilden committed
61
62
63
64
65
66
67
//===========================================================================
/** @name inherited from QAbstractItemModel
  * @{ */
//===========================================================================

public:

Jan Möbius's avatar
 
Jan Möbius committed
68
69
    /// Get the data of the corresponding entry
    QVariant data(const QModelIndex &index, int role) const;
Dirk Wilden's avatar
Dirk Wilden committed
70

Jan Möbius's avatar
 
Jan Möbius committed
71
72
    /// return the types of the corresponding entry
    Qt::ItemFlags flags(const QModelIndex &index) const;
Dirk Wilden's avatar
Dirk Wilden committed
73

Jan Möbius's avatar
 
Jan Möbius committed
74
75
76
77
    /// return the header data of the model
    QVariant headerData(int section, 
                        Qt::Orientation orientation,
                        int role = Qt::DisplayRole) const;
Dirk Wilden's avatar
Dirk Wilden committed
78
79

    /// Get the ModelIndex at given row,column
Jan Möbius's avatar
 
Jan Möbius committed
80
81
    QModelIndex index(int row, int column,
                      const QModelIndex &parent = QModelIndex()) const;
Dirk Wilden's avatar
Dirk Wilden committed
82
83

    /// Get the parent ModelIndex
Jan Möbius's avatar
 
Jan Möbius committed
84
    QModelIndex parent(const QModelIndex &index) const;
Dirk Wilden's avatar
Dirk Wilden committed
85
86

    /// get the number of rows
Jan Möbius's avatar
 
Jan Möbius committed
87
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
Dirk Wilden's avatar
Dirk Wilden committed
88
89

    /// get the number of columns
Jan Möbius's avatar
 
Jan Möbius committed
90
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
Dirk Wilden's avatar
Dirk Wilden committed
91

Jan Möbius's avatar
 
Jan Möbius committed
92
93
    /// Set the data at the given index
    bool setData(const QModelIndex &index, const QVariant &value , int role);
Dirk Wilden's avatar
Dirk Wilden committed
94

Dirk Wilden's avatar
Dirk Wilden committed
95
96
97
98
99
100
101
102
/** @} */

//===========================================================================
/** @name Internal DataStructure (the BaseObject Tree)
  * @{ */
//===========================================================================

public:
103

Dirk Wilden's avatar
Dirk Wilden committed
104
    /// Return the ModelIndex corresponding to a given BaseObject and Column
105
    QModelIndex getModelIndex(BaseObject* _object, int _column );
Dirk Wilden's avatar
Dirk Wilden committed
106

Jan Möbius's avatar
 
Jan Möbius committed
107
    /// Check if the given item is the root item
Dirk Wilden's avatar
Dirk Wilden committed
108
109
110
    bool isRoot(BaseObject* _item);

    /// Get the name of a given object
Jan Möbius's avatar
 
Jan Möbius committed
111
    bool getObjectName(BaseObject* _object , QString& _name);
112

Dirk Wilden's avatar
Dirk Wilden committed
113
114
    /// The object with the given id has been changed. Check if model also has to be changed
    void objectChanged(int id_);
Dirk Wilden's avatar
Dirk Wilden committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

    /// Get the BaseObject corresponding to a given ModelIndex
    BaseObject *getItem(const QModelIndex &index) const;

private:

    /// Rootitem of the tree
    BaseObject* rootItem_;
    
    /** Mapping of the group ids to their parent
     * Use this to check if a group with the given id exists and which item 
     * represents this group
     */
    std::map< int, TreeItem* > map_;

Dirk Wilden's avatar
Dirk Wilden committed
130
131
132
    
    void propagateUpwards(BaseObject* _obj,   int _column );
    void propagateDownwards(BaseObject* _obj, int _column );
Dirk Wilden's avatar
Dirk Wilden committed
133
134

/** @} */
Dirk Wilden's avatar
Dirk Wilden committed
135
136
137
138
139
140

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

Dirk Wilden's avatar
Dirk Wilden committed
141
142
public:

Dirk Wilden's avatar
Dirk Wilden committed
143
    /// supported drag & Drop actions
144
145
    Qt::DropActions supportedDropActions() const;

Dirk Wilden's avatar
Dirk Wilden committed
146
    /// stores the mimeType for Drag & Drop
147
148
    QStringList mimeTypes() const;

Dirk Wilden's avatar
Dirk Wilden committed
149
    /// get the mimeData for a given ModelIndex
150
151
    QMimeData* mimeData(const QModelIndexList& indexes) const;

Dirk Wilden's avatar
Dirk Wilden committed
152
    /// Called when mimeData is dropped somewhere
153
154
    bool dropMimeData(const QMimeData *data,
     Qt::DropAction action, int row, int column, const QModelIndex &parent);
Dirk Wilden's avatar
Dirk Wilden committed
155
156
157

/** @} */

Jan Möbius's avatar
 
Jan Möbius committed
158
159
160
161
};

#endif