TreeModel.hh 4.66 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
50
//=============================================================================
//
//                               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

public:
Dirk Wilden's avatar
Dirk Wilden committed
51
52

    /// Constructor
Jan Möbius's avatar
 
Jan Möbius committed
53
    TreeModel(QObject *_parent = 0);
Dirk Wilden's avatar
Dirk Wilden committed
54
55

    /// Destructor
Jan Möbius's avatar
 
Jan Möbius committed
56
57
    ~TreeModel();

Dirk Wilden's avatar
Dirk Wilden committed
58
59
60
61
62
63
64
//===========================================================================
/** @name inherited from QAbstractItemModel
  * @{ */
//===========================================================================

public:

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

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

Jan Möbius's avatar
 
Jan Möbius committed
71
72
73
74
    /// 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
75
76

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

    /// Get the parent ModelIndex
Jan Möbius's avatar
 
Jan Möbius committed
81
    QModelIndex parent(const QModelIndex &index) const;
Dirk Wilden's avatar
Dirk Wilden committed
82
83

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

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

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

Dirk Wilden's avatar
Dirk Wilden committed
92
93
94
95
96
97
98
99
/** @} */

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

public:
100

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

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

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

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

    /// 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_;

    /// Recursively update source,target selection up to the root of the tree
    void updateSourceSelection(BaseObject* _obj );
    void updateTargetSelection(BaseObject* _obj );

/** @} */
Dirk Wilden's avatar
Dirk Wilden committed
132
133
134
135
136
137

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

Dirk Wilden's avatar
Dirk Wilden committed
138
139
public:

Dirk Wilden's avatar
Dirk Wilden committed
140
    /// supported drag & Drop actions
141
142
    Qt::DropActions supportedDropActions() const;

Dirk Wilden's avatar
Dirk Wilden committed
143
    /// stores the mimeType for Drag & Drop
144
145
    QStringList mimeTypes() const;

Dirk Wilden's avatar
Dirk Wilden committed
146
    /// get the mimeData for a given ModelIndex
147
148
    QMimeData* mimeData(const QModelIndexList& indexes) const;

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

/** @} */

Jan Möbius's avatar
 
Jan Möbius committed
155
156
157
158
};

#endif