summaryrefslogtreecommitdiffstats
path: root/src/qtablewidgetng.cpp
blob: dcc8b3c2ff9c78d8f9bb67b01a39adb8fc82c387 (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
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Itemviews NG project on Trolltech Labs.
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@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 "qtablewidgetng.h"
#include "qtablewidgetng_p.h"

#include "qtablecontroller.h"
#include "qtableselectionmanager.h"
#include "qtabledefaultmodel.h"
#include "qgraphicstableview.h"
#include "qgraphicsheader.h"

#include <qdebug.h>
#include <qscrollbar.h>

QT_BEGIN_NAMESPACE

QtTableWidgetNGPrivate::QtTableWidgetNGPrivate()
    : controller(0), q_ptr(0)
{
}

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

void QtTableWidgetNGPrivate::_q_rowsInserted(int row, int count)
{
    Q_UNUSED(row);
    Q_UNUSED(count);
}

void QtTableWidgetNGPrivate::_q_rowsRemoved(int row, int count)
{
    Q_UNUSED(row);
    Q_UNUSED(count);
}

void QtTableWidgetNGPrivate::_q_rowsMoved(int from, int to, int count)
{
    Q_UNUSED(from);
    Q_UNUSED(to);
    Q_UNUSED(count);
}

void QtTableWidgetNGPrivate::_q_columnsInserted(int column, int count)
{
    Q_UNUSED(column);
    Q_UNUSED(count);
}

void QtTableWidgetNGPrivate::_q_columnsRemoved(int column, int count)
{
    Q_UNUSED(column);
    Q_UNUSED(count);
}

void QtTableWidgetNGPrivate::_q_columnsMoved(int from, int to, int count)
{
    Q_UNUSED(from);
    Q_UNUSED(to);
    Q_UNUSED(count);
}

void QtTableWidgetNGPrivate::_q_showView()
{
    controller->view()->show();
    controller->view()->grabKeyboard();
}

void QtTableWidgetNGPrivate::_q_hideView()
{
    controller->view()->ungrabKeyboard();
    controller->view()->hide();
}

void QtTableWidgetNGPrivate::_q_updateGeometries()
{
    Q_Q(QtTableWidgetNG);

    QScrollBar *vertical = q->verticalScrollBar();
    QScrollBar *horizontal = q->horizontalScrollBar();

    qreal maximum = 0;

    vertical->setPageStep(controller->verticalPageStepValue(&maximum));
    vertical->setMaximum(maximum);

    horizontal->setPageStep(controller->horizontalPageStepValue(&maximum));
    horizontal->setMaximum(maximum);

    QRect rect = q->viewport()->geometry();
    controller->view()->setGeometry(QRect(0, 0, rect.width(), rect.height()));
    controller->view()->doLayout();
}

void QtTableWidgetNGPrivate::_q_modelChanged(QtTableModelInterface *current, QtTableModelInterface *previous)
{
    Q_UNUSED(current);
    Q_UNUSED(previous);
}

void QtTableWidgetNGPrivate::_q_selectionManagerChanged(QtTableSelectionManager *current, QtTableSelectionManager *previous)
{
    Q_UNUSED(current);
    Q_UNUSED(previous);
}

void QtTableWidgetNGPrivate::_q_viewChanged(QtGraphicsTableView *current, QtGraphicsTableView *previous)
{
    Q_Q(QtTableWidgetNG);
    if (previous)
        q->scene()->removeItem(previous);
    if (current)
        q->scene()->addItem(current);
}

void QtTableWidgetNGPrivate::_q_controllerChanged(QtTableController *current, QtTableController *previous)
{
    Q_Q(QtTableWidgetNG);
    if (previous) {
        QObject::disconnect(previous, SIGNAL(modelChanged(QtTableModelInterface*,QtTableModelInterface*)),
                            q, SLOT(_q_modelChanged(QtTableModelInterface*,QtTableModelInterface*)));
        QObject::disconnect(previous, SIGNAL(selectionManagerChanged(QtTableSelectionManager*,QtTableSelectionManager*)),
                            q, SLOT(_q_selectionManagerChanged(QtTableSelectionManager*,QtTableSelectionManager*)));
        QObject::disconnect(q->verticalScrollBar(), SIGNAL(valueChanged(int)),
                            previous, SLOT(setVerticalScrollValue(int)));
        QObject::disconnect(q->horizontalScrollBar(), SIGNAL(valueChanged(int)),
                            previous, SLOT(setHorizontalScrollValue(int)));
    }
    if (current) {
        QObject::connect(current, SIGNAL(modelChanged(QtTableModelInterface*,QtTableModelInterface*)),
                         q, SLOT(_q_modelChanged(QtTableModelInterface*,QtTableModelInterface*)));
        QObject::connect(current, SIGNAL(selectionManagerChanged(QtTableSelectionManager*,QtTableSelectionManager*)),
                         q, SLOT(_q_selectionManagerChanged(QtTableSelectionManager*,QtTableSelectionManager*)));
        QObject::connect(q->verticalScrollBar(), SIGNAL(valueChanged(int)),
                         current, SLOT(setVerticalScrollValue(int)));
        QObject::connect(q->horizontalScrollBar(), SIGNAL(valueChanged(int)),
                         current, SLOT(setHorizontalScrollValue(int)));
    }
    _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 QtTableWidgetNGPrivate::initialize(int rows, int columns)
{
    Q_Q(QtTableWidgetNG);
    q->setScene(new QGraphicsScene(q));
    // other parts
    if (!controller)
        controller = new QtTableController(q);
    if (!controller->model())
        controller->setModel(new QtTableDefaultModel(rows, columns, controller));
    if (!controller->selectionManager())
        controller->setSelectionManager(new QtTableSelectionManager(controller));
    if (!controller->view()) {
        controller->setView(new QtGraphicsTableView);
        controller->view()->setParent(controller);
        controller->view()->setFlag(QGraphicsItem::ItemClipsChildrenToShape, true);
    }
    _q_controllerChanged(controller, 0);
}

// QtTableWidgetNG

/*!
 */
QtTableWidgetNG::QtTableWidgetNG(QWidget *parent, int rows, int columns)
    : QGraphicsView(parent), d_ptr(new QtTableWidgetNGPrivate)
{
    Q_D(QtTableWidgetNG);
    d->q_ptr = this;
    d->initialize(rows, columns);
}

/*!
 */
QtTableWidgetNG::QtTableWidgetNG(QtTableController *controller, QWidget *parent)
    : QGraphicsView(parent), d_ptr(new QtTableWidgetNGPrivate)
{
    Q_D(QtTableWidgetNG);
    d->q_ptr = this;
    d->controller = controller;
    d->initialize();
}

/*!
 */
QtTableWidgetNG::~QtTableWidgetNG()
{
    delete d_ptr;
}

/*!
 */
QtTableController *QtTableWidgetNG::controller() const
{
    Q_D(const QtTableWidgetNG);
    return d->controller;
}

/*!
 */
QtTableDefaultModel *QtTableWidgetNG::defaultModel() const
{
    Q_D(const QtTableWidgetNG);
    return qobject_cast<QtTableDefaultModel*>(d->controller->model());
}

/*!
 */
void QtTableWidgetNG::showEvent(QShowEvent *event)
{
    Q_D(QtTableWidgetNG);
    d->_q_updateGeometries();
    QGraphicsView::showEvent(event);
}

/*!
 */
void QtTableWidgetNG::resizeEvent(QResizeEvent *event)
{
    Q_D(QtTableWidgetNG);
    d->_q_updateGeometries();
    QAbstractScrollArea::resizeEvent(event);
}

/*!
 */
void QtTableWidgetNG::scrollContentsBy(int dx, int dy)
{
    Q_UNUSED(dx);
    Q_UNUSED(dy);
    // do nothing
}

QT_END_NAMESPACE

#include "moc_qtablewidgetng.cpp"