summaryrefslogtreecommitdiffstats
path: root/src/qtreedefaultmodel.h
blob: ae913fe7f624517e7b4e7a8cce631119d11b527f (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/****************************************************************************
**
** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the Itemviews NG project on Trolltech Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#ifndef QTTREEDefaultModel_H
#define QTTREEDefaultModel_H

#include "qtreemodelinterface.h"
#include <QtGui/qicon.h>
#include <QtGui/qfont.h>
#include <QtGui/qbrush.h>
#include <QtCore/qstring.h>
#include <QtCore/qiodevice.h>

QT_BEGIN_HEADER

QT_BEGIN_NAMESPACE

//QT_MODULE(Gui)

struct QtTreeDefaultItemPrivate;

class Q_ITEMVIEWSNG_EXPORT QtTreeDefaultItem
{
public:
    QtTreeDefaultItem(QtTreeDefaultItem *parent = 0);
    QtTreeDefaultItem(const QString &text, QtTreeDefaultItem *parent = 0);
    virtual ~QtTreeDefaultItem();

    // ### should these be part of the public API ?
    QtTreeDefaultItem *parent() const;
    QtTreeDefaultItem *firstChild() const;
    QtTreeDefaultItem *next() const;
    QtTreeDefaultItem *previous() const;

    int columnCount() const;

    const QList<QtTreeDefaultItem*> &children() const;
    void insertChild(int i, QtTreeDefaultItem *child);
    void appendChild(QtTreeDefaultItem *child);
    QtTreeDefaultItem *takeChild(int i);
    void removeChild(QtTreeDefaultItem *child);
    
#ifndef QT_NO_DATASTREAM
    virtual void read(QDataStream &in);
    virtual void write(QDataStream &out) const;
#endif

    // data functions
    virtual QHash<int,QVariant> data(int column, const QList<int> &roles) const;
    virtual QVariant data(int column, int role) const;
    virtual void setData(const QVariant &data, int column, int role); // ### is the order of args ok ?

    // ### we should add
    // Qt::TextRole == Qt::DisplayRole
    // Qt::IconRole == Qt::DecorationRole
    
    inline QString text(int column) const { return qvariant_cast<QString>(data(column, Qt::DisplayRole)); }
    inline void setText(int column, const QString &text) { setData(text, column, Qt::DisplayRole); }

    inline QIcon icon(int column) const { return qvariant_cast<QIcon>(data(column, Qt::DecorationRole)); }
    inline void setIcon(int column, const QIcon &icon) { setData(icon, column, Qt::DecorationRole); }

    inline QString statusTip(int column) const { return qvariant_cast<QString>(data(column, Qt::StatusTipRole)); }
    inline void setStatusTip(int column, const QString &tip) { setData(tip, column, Qt::StatusTipRole); }
#ifndef QT_NO_TOOLTIP
    inline QString toolTip(int column) const { return qvariant_cast<QString>(data(column, Qt::ToolTipRole)); }
    inline void setToolTip(int column, const QString &tip) { setData(tip, column, Qt::ToolTipRole); }
#endif
#ifndef QT_NO_WHATSTHIS
    inline QString whatsThis(int column) const { return qvariant_cast<QString>(data(column, Qt::WhatsThisRole)); }
    inline void setWhatsThis(int column, const QString &what) { setData(what, column, Qt::WhatsThisRole); }
#endif
    inline QFont font(int column) const { return qvariant_cast<QFont>(data(column, Qt::FontRole)); }
    inline void setFont(int column, const QFont &font) { setData(font, column, Qt::FontRole); }

    inline int textAlignment(int column) const { return qvariant_cast<int>(data(column, Qt::TextAlignmentRole)); }
    inline void setTextAlignment(int column, int alignment) { setData(alignment, column, Qt::TextAlignmentRole); }

    inline QBrush background(int column) const { return qvariant_cast<QBrush>(data(column, Qt::BackgroundRole)); }
    inline void setBackground(int column, const QBrush &brush) { setData(brush, column, Qt::BackgroundRole); }

    inline QBrush foreground(int column) const { return qvariant_cast<QBrush>(data(column, Qt::ForegroundRole)); }
    inline void setForeground(int column, const QBrush &brush) { setData(brush, column, Qt::ForegroundRole); }

    inline Qt::CheckState checkState(int column) const { return static_cast<Qt::CheckState>(data(column, Qt::CheckStateRole).toInt()); }
    inline void setCheckState(int column, Qt::CheckState state) { setData(state, column, Qt::CheckStateRole); }

    inline QSizeF sizeHint(int column) const { return qvariant_cast<QSizeF>(data(column, Qt::SizeHintRole)); }
    inline void setSizeHint(int column, const QSizeF &size) { setData(size, column, Qt::SizeHintRole); }

private:
    QtTreeDefaultItemPrivate *d_ptr;
    friend class QtTreeDefaultModel;
};

class QtTreeDefaultModelPrivate;

class Q_ITEMVIEWSNG_EXPORT QtTreeDefaultModel : public QtTreeModelInterface<QtTreeDefaultItem*>
{
    Q_OBJECT
public:
    explicit QtTreeDefaultModel(QObject *parent = 0);
    virtual ~QtTreeDefaultModel();

    QtTreeDefaultModel::iterator itemIterator(QtTreeDefaultItem *item); // ###
    QtTreeDefaultItem *rootItem() const;

protected:
    // model interface
    QtTreeDefaultItem *firstChild(QtTreeDefaultItem *item) const;
    QtTreeDefaultItem *nextSibling(QtTreeDefaultItem *item) const;
    QtTreeDefaultItem *previousSibling(QtTreeDefaultItem *item) const;
    QtTreeDefaultItem *parentItem(QtTreeDefaultItem *item) const;
    QHash<int,QVariant> data(QtTreeDefaultItem *item, int column, const QList<int> &roles) const;
    bool setData(const QVariant &data, QtTreeDefaultItem *item, int column, int role);
    bool isValid(QtTreeDefaultItem *item) const;
    int columnCount(QtTreeDefaultItem *item) const;

    // optimization
    bool hasChildren(QtTreeDefaultItem *item) const;
    bool hasNextSibling(QtTreeDefaultItem *item) const;
    bool hasPreviousSibling(QtTreeDefaultItem *item) const;
    bool hasParent(QtTreeDefaultItem *item) const;

    // other
    void itemInserted(QtTreeDefaultItem *item);
    void itemRemoved(QtTreeDefaultItem *item);
    void itemChanged(QtTreeDefaultItem *item, const QList<int> &roles);

protected:
    QtTreeDefaultModelPrivate *d_ptr;
    
private:
    friend class QtTreeDefaultItem;
    Q_DECLARE_PRIVATE(QtTreeDefaultModel)
    Q_DISABLE_COPY(QtTreeDefaultModel)
};

QT_END_NAMESPACE

QT_END_HEADER

#endif//QTTREEDefaultModel_H