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
|
/****************************************************************************
**
** 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 documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * 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.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) 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
** OWNER 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."
** $QT_END_LICENSE$
**
****************************************************************************/
/*!
view.cpp
Provides a view to represent a one-dimensional sequence of integers
obtained from a list model as a series of rows.
*/
#include <QAbstractItemModel>
#include <QBrush>
#include <QItemSelection>
#include <QPainter>
#include <QPaintEvent>
#include <QPen>
#include <QPoint>
#include <QResizeEvent>
#include <QScrollBar>
#include <QSizePolicy>
#include "view.h"
LinearView::LinearView(QWidget *parent)
: QAbstractItemView(parent)
{
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
}
/*!
Returns the position of the item in viewport coordinates.
*/
QRect LinearView::itemViewportRect(const QModelIndex &index) const
{
QRect rect = itemRect(index);
QRect result(rect.left() - horizontalScrollBar()->value(),
rect.top() - verticalScrollBar()->value(),
rect.width(), viewport()->height());
return result;
}
/*!
Returns the rectangle of the item at position \a index in the
model. The rectangle is in contents coordinates.
*/
QRect LinearView::itemRect(const QModelIndex &index) const
{
if (!index.isValid())
return QRect();
else
return QRect(index.row(), 0, 1, 1);
}
void LinearView::ensureVisible(const QModelIndex &index)
{
QRect area = viewport()->rect();
QRect rect = itemViewportRect(index);
if (rect.left() < area.left())
horizontalScrollBar()->setValue(
horizontalScrollBar()->value() - rect.left());
else if (rect.right() > area.right())
horizontalScrollBar()->setValue(
horizontalScrollBar()->value() + rect.left() - area.width());
}
/*!
Returns the item that covers the coordinate given in the view.
*/
QModelIndex LinearView::itemAt(int x, int /* y */) const
{
int row = x + horizontalScrollBar()->value();
return model()->index(row, 0, QModelIndex());
}
//void LinearView::dataChanged(const QModelIndex &/* topLeft */,
// const QModelIndex &/* bottomRight */)
//{
// updateGeometries();
// if (isVisible())
// repaint();
//}
void LinearView::rowsInserted(const QModelIndex &/* parent */, int /* start */,
int /* end */)
{
updateGeometries();
if (isVisible())
repaint();
}
void LinearView::rowsRemoved(const QModelIndex &/* parent */, int /* start */,
int /* end */)
{
updateGeometries();
if (isVisible())
repaint();
}
/*
void LinearView::verticalScrollbarAction(int action)
{
}
void LinearView::horizontalScrollbarAction(int action)
{
}
*/
/*!
Select the items in the model that lie within the rectangle specified by
\a rect, using the selection \a command.
*/
void LinearView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command)
{
QModelIndex leftIndex = itemAt(rect.left(), 0);
QModelIndex rightIndex = itemAt(rect.right(), 0);
QItemSelection selection(leftIndex, rightIndex);
selectionModel()->select(selection, command);
}
QModelIndex LinearView::moveCursor(QAbstractItemView::CursorAction cursorAction,
Qt::KeyboardModifiers)
{
QModelIndex current = currentIndex();
switch (cursorAction) {
case MoveLeft:{
if (current.row() > 0)
return model()->index(current.row() - 1, 0, QModelIndex());
else
return model()->index(0, 0, QModelIndex());
break;}
case MoveRight:{
if (current.row() < rows(current) - 1)
return model()->index(current.row() + 1, 0, QModelIndex());
else
return model()->index(rows(current) - 1, 0,QModelIndex());
break;}
case MoveUp:
return current;
case MoveDown:
return current;
case MovePageUp:
return current;
case MovePageDown:
return current;
case MoveHome:
return model()->index(0, 0, QModelIndex());
case MoveEnd:
return model()->index(rows(current) - 1, 0, QModelIndex());
default:
return current;
}
}
int LinearView::horizontalOffset() const
{
return horizontalScrollBar()->value();
}
int LinearView::verticalOffset() const
{
return verticalScrollBar()->value();
}
/*!
Returns a rectangle corresponding to the selection in viewport cooridinates.
*/
QRect LinearView::selectionViewportRect(const QItemSelection &selection) const
{
int ranges = selection.count();
if (ranges == 0)
return QRect();
// Note that we use the top and bottom functions of the selection range
// since the data is stored in rows.
int firstRow = selection.at(0).top();
int lastRow = selection.at(0).top();
for (int i = 0; i < ranges; ++i) {
firstRow = qMin(firstRow, selection.at(i).top());
lastRow = qMax(lastRow, selection.at(i).bottom());
}
QModelIndex firstItem = model()->index(qMin(firstRow, lastRow), 0,
QModelIndex());
QModelIndex lastItem = model()->index(qMax(firstRow, lastRow), 0,
QModelIndex());
QRect firstRect = itemViewportRect(firstItem);
QRect lastRect = itemViewportRect(lastItem);
return QRect(firstRect.left(), firstRect.top(),
lastRect.right() - firstRect.left(), firstRect.height());
}
void LinearView::paintEvent(QPaintEvent *event)
{
QPainter painter(viewport());
QRect updateRect = event->rect();
QBrush background(Qt::black);
QPen foreground(Qt::white);
painter.fillRect(updateRect, background);
painter.setPen(foreground);
QModelIndex firstItem = itemAt(updateRect.left(), updateRect.top());
if (!firstItem.isValid())
firstItem = model()->index(0, 0, QModelIndex());
QModelIndex lastItem = itemAt(updateRect.right(), updateRect.bottom());
if (!lastItem.isValid())
lastItem = model()->index(rows() - 1, 0, QModelIndex());
int x = updateRect.left();
//int top = updateRect.top();
//int bottom = updateRect.bottom();
int row = firstItem.row();
QModelIndex index = model()->index(row, 0, QModelIndex());
int value = model()->data(index, Qt::DisplayRole).toInt();
int midPoint = viewport()->height()/2;
int y2 = midPoint - int(value * midPoint/255.0);
while (row <= lastItem.row()) {
QModelIndex index = model()->index(row, 0, QModelIndex());
int value = model()->data(index, Qt::DisplayRole).toInt();
int y1 = y2;
y2 = midPoint - int(value * midPoint/255.0);
painter.drawLine(x-1, y1, x, y2);
++row; ++x;
}
}
void LinearView::resizeEvent(QResizeEvent * /* event */)
{
updateGeometries();
}
void LinearView::updateGeometries()
{
if (viewport()->width() < rows()) {
horizontalScrollBar()->setPageStep(viewport()->width());
horizontalScrollBar()->setRange(0, rows() - viewport()->width() - 1);
}
}
QSize LinearView::sizeHint() const
{
return QSize(rows(), 200);
}
int LinearView::rows(const QModelIndex &index) const
{
return model()->rowCount(model()->parent(index));
}
bool LinearView::isIndexHidden(const QModelIndex &index) const
{
return false;
}
|