TreeModel.hh 8.06 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
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
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
*                                                                            *
40 41 42
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
43 44 45 46 47
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
48
\*===========================================================================*/
49 50 51 52 53 54 55 56

#ifndef TREEMODEL_H
#define TREEMODEL_H

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

57
#include "TreeItem.hh"
58 59 60 61 62

class TreeModel : public QAbstractItemModel
{
    Q_OBJECT

Dirk Wilden's avatar
Dirk Wilden committed
63
signals:
64
   // the connected TreeView changed data
Matthias Möller's avatar
Matthias Möller committed
65
   void dataChangedInside(int _id, int _column,const QVariant& _value);
66 67
   // an object was moved via dragNdrop
   void moveBaseObject(int _id, int _newParentId);
Dirk Wilden's avatar
Dirk Wilden committed
68

69
public:
Dirk Wilden's avatar
Dirk Wilden committed
70 71

    /// Constructor
Jan Möbius's avatar
Jan Möbius committed
72
    explicit TreeModel(QObject *_parent = 0);
Dirk Wilden's avatar
Dirk Wilden committed
73 74

    /// Destructor
75 76
    ~TreeModel();

Dirk Wilden's avatar
Dirk Wilden committed
77 78 79 80 81 82 83
//===========================================================================
/** @name inherited from QAbstractItemModel
  * @{ */
//===========================================================================

public:

84
    /// Get the data of the corresponding entry
Matthias Möller's avatar
Matthias Möller committed
85
    QVariant data(const QModelIndex &_index,int _role) const;
Dirk Wilden's avatar
Dirk Wilden committed
86

87
    /// return the types of the corresponding entry
88
    Qt::ItemFlags flags(const QModelIndex &_index) const;
Dirk Wilden's avatar
Dirk Wilden committed
89

90
    /// return the header data of the model
Matthias Möller's avatar
Matthias Möller committed
91 92 93
    QVariant headerData(int _section,
                        Qt::Orientation _orientation,
                        int _role = Qt::DisplayRole) const;
Dirk Wilden's avatar
Dirk Wilden committed
94 95

    /// Get the ModelIndex at given row,column
Matthias Möller's avatar
Matthias Möller committed
96 97
    QModelIndex index(int _row, int _column,
                      const QModelIndex &_parent = QModelIndex()) const;
Dirk Wilden's avatar
Dirk Wilden committed
98 99

    /// Get the parent ModelIndex
Matthias Möller's avatar
Matthias Möller committed
100
    QModelIndex parent(const QModelIndex &_index) const;
Dirk Wilden's avatar
Dirk Wilden committed
101 102

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

105 106 107 108 109 110
    /** \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
111

112 113
    /** \brief Set Data at 'index' to 'value'
     *
Matthias Möller's avatar
Matthias Möller committed
114 115 116
     * @param  _index a ModelIndex defining the positin in the model
     * @param  _value the new value
     * @param  _role unused
117 118
     * @return return if the data was set successfully
     */
Matthias Möller's avatar
Matthias Möller committed
119
    bool setData(const QModelIndex &_index, const QVariant &_value , int _role);
Dirk Wilden's avatar
Dirk Wilden committed
120

Dirk Wilden's avatar
Dirk Wilden committed
121 122 123
/** @} */

//===========================================================================
124
/** @name Internal DataStructure (the TreeItem Tree)
Dirk Wilden's avatar
Dirk Wilden committed
125 126 127 128
  * @{ */
//===========================================================================

public:
129

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

133
    /// Check if the given item is the root item
134
    bool isRoot(TreeItem* _item);
Dirk Wilden's avatar
Dirk Wilden committed
135 136

    /// Get the name of a given object
137 138 139
    bool getObjectName(TreeItem* _object , QString& _name);

    /// Get the TreeItem corresponding to a given ModelIndex
140
    TreeItem *getItem(const QModelIndex &_index) const;
141

142 143 144
    /// Get the TreeItem corresponding to a given OpenFlipper ObjectID
    TreeItem *getItem(const int _id) const;

145
    /// Get the name of a TreeItem corresponding to a given ModelIndex
146
    QString itemName(const QModelIndex &_index) const;
147 148

    /// Get the id of a TreeItem corresponding to a given ModelIndex
149
    int itemId(const QModelIndex &_index) const;
150

151

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

155 156 157 158
    /// 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
159
    void objectDeleted(int _id);
Dirk Wilden's avatar
Dirk Wilden committed
160

161 162
    /// move the item to a new parent
    void moveItem(TreeItem* _item, TreeItem* _parent );
163

Dirk Wilden's avatar
Dirk Wilden committed
164 165
private:

166
    /// Root item of the tree
167 168
    TreeItem* rootItem_;

Dirk Wilden's avatar
Dirk Wilden committed
169
/** @} */
Dirk Wilden's avatar
Dirk Wilden committed
170 171 172 173 174 175

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

Dirk Wilden's avatar
Dirk Wilden committed
176 177
public:

Dirk Wilden's avatar
Dirk Wilden committed
178
    /// supported drag & Drop actions
179 180
    Qt::DropActions supportedDropActions() const;

Dirk Wilden's avatar
Dirk Wilden committed
181
    /// stores the mimeType for Drag & Drop
182 183
    QStringList mimeTypes() const;

Dirk Wilden's avatar
Dirk Wilden committed
184
    /// get the mimeData for a given ModelIndex
185 186
    QMimeData* mimeData(const QModelIndexList& indexes) const;

187 188 189 190 191 192 193 194 195
    /** \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
     */
196
    bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
Dirk Wilden's avatar
Dirk Wilden committed
197 198 199

/** @} */

200 201 202 203
};

#endif