summaryrefslogtreecommitdiffstats
path: root/src/qgraphicslistview.h
blob: bf3aa17cf9fcc4a22c2dad1119ce6c97c1a0f449 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/****************************************************************************
**
** 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 QTGRAPHICSLISTVIEW_H
#define QTGRAPHICSLISTVIEW_H

#include "qitemviewsglobal.h"

#include <QtGui/qgraphicswidget.h>

QT_BEGIN_HEADER

QT_BEGIN_NAMESPACE

//QT_MODULE(Gui)

class QSizeF;
class QPointF;
class QtListController;
class QtListModelInterface;
class QtListSelectionManager;
class QtListSelectionChange;
class QStyleOptionViewItemV4;

class QtGraphicsListViewItemPrivate;
class QtGraphicsListViewPrivate;
class QtGraphicsListView;

class Q_ITEMVIEWSNG_EXPORT QtGraphicsListViewItem : public QGraphicsWidget
{
    Q_OBJECT
public:
    QtGraphicsListViewItem(int index, QtGraphicsListView *view);
    virtual ~QtGraphicsListViewItem();

    int index() const;
    void setIndex(int index);

    QRectF boundingRect() const;

    virtual QSizeF sizeHint(Qt::SizeHint which = Qt::PreferredSize, const QSizeF &constraint = QSizeF()) const;
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
    virtual void itemChanged(const QList<int> &roles = QList<int>());
    virtual void initStyleOption(QStyleOptionViewItemV4 *option) const;

    QHash<int, QVariant> data(const QList<int> &roles = QList<int>()) const;
    QtGraphicsListView *view() const;

protected:
    QtGraphicsListViewItemPrivate *d_ptr;

private:
    Q_DISABLE_COPY(QtGraphicsListViewItem)
    Q_DECLARE_PRIVATE(QtGraphicsListViewItem)
};

class Q_ITEMVIEWSNG_EXPORT QtGraphicsListViewItemCreatorBase
{
public:
    virtual ~QtGraphicsListViewItemCreatorBase();
    virtual QGraphicsObject *create(int index, QtGraphicsListView *view) = 0;
    virtual QGraphicsObject *reassign(int index, QGraphicsObject *item, QtGraphicsListView *view) = 0;
    virtual void update(int index, QGraphicsObject *item, const QList<int> &roles);
    virtual void recycle(QGraphicsObject *item);
};

template <class T>
class QtGraphicsListViewItemCreator : public QtGraphicsListViewItemCreatorBase
{
public:
    ~QtGraphicsListViewItemCreator();
    QGraphicsObject *create(int index, QtGraphicsListView *view);
    QGraphicsObject *reassign(int index, QGraphicsObject *item, QtGraphicsListView *view);
    void update(int index, QGraphicsObject *item, const QList<int> &roles);
};

template <class T>
QtGraphicsListViewItemCreator<T>::~QtGraphicsListViewItemCreator()
{}

template <class T>
inline QGraphicsObject *QtGraphicsListViewItemCreator<T>::create(int index, QtGraphicsListView *view)
{ return new T(index, view); }

template <class T>
inline QGraphicsObject *QtGraphicsListViewItemCreator<T>::reassign(int index, QGraphicsObject *item, QtGraphicsListView *view)
{
	Q_UNUSED(view);
	static_cast<T*>(item)->setIndex(index);
	return item;
}

template <class T>
inline void QtGraphicsListViewItemCreator<T>::update(int index, QGraphicsObject *item, const QList<int> &roles)
{
	Q_UNUSED(index);
	static_cast<T*>(item)->itemChanged(roles);
}

class Q_ITEMVIEWSNG_EXPORT QtGraphicsListView : public QGraphicsWidget
{
    Q_OBJECT
    Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation NOTIFY orientationChanged)
    Q_PROPERTY(Qt::TextElideMode textElideMode READ textElideMode WRITE setTextElideMode)
    Q_PROPERTY(int firstIndex READ firstIndex WRITE setFirstIndex NOTIFY firstIndexChanged)
    Q_PROPERTY(qreal offset READ offset WRITE setOffset NOTIFY offsetChanged)
    Q_PROPERTY(QGraphicsObject* highlight READ highlight WRITE setHighlight)

public:
    QtGraphicsListView(Qt::Orientation orientation = Qt::Vertical, QGraphicsWidget *parent = 0, Qt::WindowFlags wFlags = 0);
    virtual ~QtGraphicsListView();

    QtListController *controller() const;
    QtListModelInterface *model() const;
    QtListSelectionManager *selectionManager() const;

    Qt::Orientation orientation() const;
    void setOrientation(Qt::Orientation orientation);

    Qt::TextElideMode textElideMode() const;
    void setTextElideMode(Qt::TextElideMode mode);

    virtual int itemAt(const QPointF &position) const;
    virtual QRectF itemGeometry(int index) const;
    virtual void doLayout();

    int firstIndex() const;
    qreal offset() const;

    QGraphicsObject *highlight() const;
    void setHighlight(QGraphicsObject *highlight);

    virtual int maximumFirstIndex() const;
    virtual qreal maximumOffset() const;

    virtual void initStyleOption(QStyleOptionViewItemV4 *option) const;

    QtGraphicsListViewItemCreatorBase *itemCreator() const;
    void setItemCreator(QtGraphicsListViewItemCreatorBase *creator);

    QGraphicsObject *itemForIndex(int index) const;

    qreal offsetToEnsureIndexIsVisible(int index) const;
    int firstIndexToEnsureIndexIsVisible(int index) const;

public Q_SLOTS:
    void setFirstIndex(int index);
    void setOffset(qreal offset);
    void updateLayout();

Q_SIGNALS:
    void orientationChanged(Qt::Orientation orientation);
    void firstIndexChanged(int index);
    void offsetChanged(qreal offset);

protected:
    QtGraphicsListView(QtGraphicsListViewPrivate &, Qt::Orientation orientation, QGraphicsWidget *parent, Qt::WindowFlags wFlags = 0);

    // ### making them virtual allows the user to hook up to any signals they need
    virtual void setController(QtListController *control);
    virtual void setModel(QtListModelInterface *model);
    virtual void setSelectionManager(QtListSelectionManager *selectionManager);

    virtual bool event(QEvent *event);
    virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);

    QtGraphicsListViewPrivate *d_ptr;

private:
    friend class QtPrinterListView;
    friend class QtListController;
    friend class QtGraphicsListViewItem;
    Q_DECLARE_PRIVATE(QtGraphicsListView)
    Q_DISABLE_COPY(QtGraphicsListView)
    Q_PRIVATE_SLOT(d_func(), void _q_controllerDestroyed())
    Q_PRIVATE_SLOT(d_func(), void _q_modelDestroyed())
    Q_PRIVATE_SLOT(d_func(), void _q_selectionsDestroyed())
    Q_PRIVATE_SLOT(d_func(), void _q_itemsChanged(int index, int count, const QList<int> &roles))
    Q_PRIVATE_SLOT(d_func(), void _q_itemsInserted(int index, int count))
    Q_PRIVATE_SLOT(d_func(), void _q_itemsRemoved(int index, int count))
    Q_PRIVATE_SLOT(d_func(), void _q_selectionsChanged(const QtListSelectionChange &change))
    Q_PRIVATE_SLOT(d_func(), void _q_currentChanged(int current, int previous))
};

QT_END_NAMESPACE

QT_END_HEADER

#endif//QTGRAPHICSLISTVIEW_H