summaryrefslogtreecommitdiffstats
path: root/src/qlistwidgetng.cpp
blob: 08b03819ff92a58244f369fa6c30e6a0331f6513 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/****************************************************************************
**
** 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.
**
****************************************************************************/

#include "qlistwidgetng.h"
#include "qlistwidgetng_p.h"

#include "qlistdefaultmodel.h"
#include "qlistcontroller.h"
#include "qlistselectionmanager.h"
#include "qgraphicslistview.h"
#include "qgraphicsscrollbar.h"

#include <qgraphicsgridlayout.h>
#include <qdebug.h>

QT_BEGIN_NAMESPACE

QtListWidgetNGPrivate::QtListWidgetNGPrivate()
    : controller(0), scrollbar(0), q_ptr(0)
{
}

void QtListWidgetNGPrivate::_q_update(const QRegion &region)
{
    Q_UNUSED(region);
}

void QtListWidgetNGPrivate::_q_itemsInserted(int index, int count)
{
    Q_UNUSED(index);
    Q_UNUSED(count);
    _q_updateScrollbars(); // ### FIXME
}

void QtListWidgetNGPrivate::_q_itemsRemoved(int index, int count)
{
    Q_UNUSED(index);
    Q_UNUSED(count);
}

void QtListWidgetNGPrivate::_q_itemsMoved(int from, int to, int count)
{
    Q_UNUSED(from);
    Q_UNUSED(to);
    Q_UNUSED(count);
}

void QtListWidgetNGPrivate::_q_itemsChanged(int from, int count, const QList<int> &roles)
{
    Q_UNUSED(from);
    Q_UNUSED(count);
    Q_UNUSED(roles);
    _q_updateScrollbars(); // ### FIXME
}

void QtListWidgetNGPrivate::_q_showView()
{
    if (controller && controller->view()) {
        controller->view()->show();
        controller->view()->grabKeyboard();
    }
}

void QtListWidgetNGPrivate::_q_hideView()
{
    if (controller && controller->view()) {
        controller->view()->ungrabKeyboard();
        controller->view()->hide();
    }
}

void QtListWidgetNGPrivate::_q_controllerChanged(QtListController *current, QtListController *previous)
{
    Q_Q(QtListWidgetNG);
    if (previous) {
        QObject::disconnect(previous, SIGNAL(modelChanged(QtListModelInterface*,QtListModelInterface*)),
                            q, SLOT(_q_modelChanged(QtListModelInterface*,QtListModelInterface*)));
        QObject::disconnect(previous, SIGNAL(selectionManagerChanged(QtListSelectionManager*,QtListSelectionManager*)),
                            q, SLOT(_q_selectionManagerChanged(QtListSelectionManager*,QtListSelectionManager*)));
        QObject::disconnect(previous, SIGNAL(viewChanged(QtGraphicsListView*,QtGraphicsListView*)),
                            q, SLOT(_q_viewChanged(QtGraphicsListView*,QtGraphicsListView*)));

    }
    if (current) {
        QObject::connect(current, SIGNAL(modelChanged(QtListModelInterface*,QtListModelInterface*)),
                         q, SLOT(_q_modelChanged(QtListModelInterface*,QtListModelInterface*)));
        QObject::connect(current, SIGNAL(selectionManagerChanged(QtListSelectionManager*,QtListSelectionManager*)),
                         q, SLOT(_q_selectionManagerChanged(QtListSelectionManager*,QtListSelectionManager*)));
        QObject::connect(current, SIGNAL(viewChanged(QtGraphicsListView*,QtGraphicsListView*)),
                         q, SLOT(_q_viewChanged(QtGraphicsListView*,QtGraphicsListView*)));
        //QObject::connect(current, SIGNAL(scrollValueChanged(qreal)),
        //                 scrollbar, SLOT(setValue(qreal)));
         QObject::connect(scrollbar, SIGNAL(valueChanged(qreal)),
                          current, SLOT(setScrollValue(qreal)));
    }
    // everything changed
    _q_modelChanged(current ? current->model() : 0, previous ? previous->model() : 0);
    _q_selectionManagerChanged(current ? current->selectionManager() : 0, previous ? previous->selectionManager() : 0);
    _q_viewChanged(current ? current->view() : 0, previous ? previous->view() : 0);
}

void QtListWidgetNGPrivate::_q_modelChanged(QtListModelInterface *current, QtListModelInterface *previous)
{
    Q_Q(QtListWidgetNG);
    if (previous) {
        QObject::disconnect(previous, SIGNAL(itemsInserted(int,int)),
                            q, SLOT(_q_itemsInserted(int,int)));
        QObject::disconnect(previous, SIGNAL(itemsRemoved(int,int)),
                            q, SLOT(_q_itemsRemoved(int,int)));
        QObject::disconnect(previous, SIGNAL(itemsMoved(int,int,int)),
                            q, SLOT(_q_itemsMoved(int,int,int)));
    }
    if (current) {
        QObject::connect(current, SIGNAL(itemsInserted(int,int)),
                         q, SLOT(_q_itemsInserted(int,int)));
        QObject::connect(current, SIGNAL(itemsRemoved(int,int)),
                         q, SLOT(_q_itemsRemoved(int,int)));
        QObject::connect(current, SIGNAL(itemsMoved(int,int,int)),
                         q, SLOT(_q_itemsMoved(int,int,int)));
    }
}

void QtListWidgetNGPrivate::_q_selectionManagerChanged(QtListSelectionManager *current, QtListSelectionManager *previous)
{
    if (previous) {
        QObject::disconnect(previous, SIGNAL(currentChanged(int,int)),
                            controller->view(), SLOT(setFirstIndexToEnsureIndexIsVisible(int)));
    }
    if (current) {
        QObject::connect(current, SIGNAL(currentChanged(int,int)),
                         controller->view(), SLOT(setFirstIndexToEnsureIndexIsVisible(int)));
    }
}

void QtListWidgetNGPrivate::_q_viewChanged(QtGraphicsListView *current, QtGraphicsListView *previous)
{
    Q_Q(QtListWidgetNG);
    if (previous) {
        q->scene()->removeItem(previous);
    }
    scrollbar->setParentItem(current); // ### FIXME
    if (current) {
        q->scene()->addItem(current);
        current->grabKeyboard();
    }
    //_q_updateScrollbars();
}

void QtListWidgetNGPrivate::_q_updateScrollbars()
{
    Q_Q(QtListWidgetNG);
    if (!controller || !controller->view()) {
        scrollbar->hide();
        return;
    }

    QRect r = q->viewport()->geometry();
    q->scene()->setSceneRect(0, 0, r.width(), r.height());
    qreal w = scrollbar->boundingRect().width();
    scrollbar->setGeometry(QRect(r.width() - w, 0, w, r.height()));

    // first set the geometry to allow maximumVerticalScrollValue to use it
    controller->view()->setGeometry(QRect(0, 0, r.width(), r.height()));
    //controller->view()->doLayout();
    qreal maximum = 0;
    scrollbar->setPageStep(controller->pageStepValue(&maximum));
    scrollbar->setMaximum(maximum);

    if (scrollbar->minimum() >= scrollbar->maximum()) {
        scrollbar->hide();
    } else {
        scrollbar->show();
        controller->view()->setGeometry(QRect(0, 0, r.width() - w, r.height())); // ###
    }
}

/*!
  \internal
 */
void QtListWidgetNGPrivate::initialize()
{
    Q_Q(QtListWidgetNG);
    q->setScene(new QGraphicsScene(q));
    // scrollbar
    scrollbar = new QtGraphicsScrollBar(Qt::Vertical);
    // other parts
    if (!controller)
        controller = new QtListController(q);
    if (!controller->model())
        controller->setModel(new QtListDefaultModel(controller));
    if (!controller->selectionManager())
        controller->setSelectionManager(new QtListSelectionManager(controller));
    if (!controller->view()) {
        controller->setView(new QtGraphicsListView);
        controller->view()->setParent(controller);
    }
    _q_controllerChanged(controller, 0);
}

// QtListWidgetNG

/*!
  \class QtListWidgetNG
  \brief The QtListWidgetNG class provides an item-based list widget.

  QtListWidgetNG is a QWidget sub-class that uses default constructed
  QtListController, QtListDefaultModel and QtGraphicsListView instances
  to show a list of items.

  A QtListWidgetNG can be also be constructed with a custom controller,
  view and/or model.
*/

/*!
    Creates a new QtListWidgetNG with the given \a parent.
 */
QtListWidgetNG::QtListWidgetNG(QWidget *parent)
    : QGraphicsView(parent), d_ptr(new QtListWidgetNGPrivate)
{
    Q_D(QtListWidgetNG);
    d->q_ptr = this;
    d->initialize();
}

/*!
  Creates a new QtListWidgetNG with the given \a controller and \a parent.
 */
QtListWidgetNG::QtListWidgetNG(QtListController *controller, QWidget *parent)
    : QGraphicsView(parent), d_ptr(new QtListWidgetNGPrivate)
{
    Q_D(QtListWidgetNG);
    Q_ASSERT(controller);
    d->q_ptr = this;
    d->controller = controller;
    controller->setParent(this);
    d->initialize();
}

/*!
  Destructor.
 */
QtListWidgetNG::~QtListWidgetNG()
{
    delete d_ptr;
}

/*!
   Returns the controller used by the widget.

  \sa defaultModel()
 */
QtListController *QtListWidgetNG::controller() const
{
    Q_D(const QtListWidgetNG);
    return d->controller;
}

/*!
  Returns the default model.

  The QtListWidgetNG is created with a default item model to hold the data
  shown by this widget. If this list is created with a custom controller
  with a QtListDefaultModel, the pointer to the custom model is returned.
  Otherwise null is returned.

  \sa controller()
 */
QtListDefaultModel *QtListWidgetNG::defaultModel() const
{
    Q_D(const QtListWidgetNG);
    return qobject_cast<QtListDefaultModel*>(d->controller->model());
}

/*!
  \reimp
 */
void QtListWidgetNG::showEvent(QShowEvent *event)
{
    Q_D(QtListWidgetNG);
    d->_q_updateScrollbars();
    QGraphicsView::showEvent(event);
}

/*!
  \reimp
 */
void QtListWidgetNG::resizeEvent(QResizeEvent *event)
{
    Q_D(QtListWidgetNG);
    d->_q_updateScrollbars();
    QGraphicsView::resizeEvent(event);
}

QT_END_NAMESPACE

#include "moc_qlistwidgetng.cpp"