source: trunk/doc/src/snippets/modelview-subclasses/view.cpp@ 244

Last change on this file since 244 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 8.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 view.cpp
44
45 Provides a view to represent a one-dimensional sequence of integers
46 obtained from a list model as a series of rows.
47*/
48
49#include <QAbstractItemModel>
50#include <QBrush>
51#include <QItemSelection>
52#include <QPainter>
53#include <QPaintEvent>
54#include <QPen>
55#include <QPoint>
56#include <QResizeEvent>
57#include <QScrollBar>
58#include <QSizePolicy>
59
60#include "view.h"
61
62LinearView::LinearView(QWidget *parent)
63 : QAbstractItemView(parent)
64{
65 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
66}
67
68/*!
69 Returns the position of the item in viewport coordinates.
70*/
71
72QRect LinearView::itemViewportRect(const QModelIndex &index) const
73{
74 QRect rect = itemRect(index);
75 QRect result(rect.left() - horizontalScrollBar()->value(),
76 rect.top() - verticalScrollBar()->value(),
77 rect.width(), viewport()->height());
78
79 return result;
80}
81
82/*!
83 Returns the rectangle of the item at position \a index in the
84 model. The rectangle is in contents coordinates.
85*/
86
87QRect LinearView::itemRect(const QModelIndex &index) const
88{
89 if (!index.isValid())
90 return QRect();
91 else
92 return QRect(index.row(), 0, 1, 1);
93}
94
95
96void LinearView::ensureVisible(const QModelIndex &index)
97{
98 QRect area = viewport()->rect();
99 QRect rect = itemViewportRect(index);
100
101 if (rect.left() < area.left())
102 horizontalScrollBar()->setValue(
103 horizontalScrollBar()->value() - rect.left());
104 else if (rect.right() > area.right())
105 horizontalScrollBar()->setValue(
106 horizontalScrollBar()->value() + rect.left() - area.width());
107}
108
109/*!
110 Returns the item that covers the coordinate given in the view.
111*/
112
113QModelIndex LinearView::itemAt(int x, int /* y */) const
114{
115 int row = x + horizontalScrollBar()->value();
116
117 return model()->index(row, 0, QModelIndex());
118}
119
120//void LinearView::dataChanged(const QModelIndex &/* topLeft */,
121// const QModelIndex &/* bottomRight */)
122//{
123// updateGeometries();
124// if (isVisible())
125// repaint();
126//}
127
128void LinearView::rowsInserted(const QModelIndex &/* parent */, int /* start */,
129 int /* end */)
130{
131 updateGeometries();
132 if (isVisible())
133 repaint();
134}
135
136void LinearView::rowsRemoved(const QModelIndex &/* parent */, int /* start */,
137 int /* end */)
138{
139 updateGeometries();
140 if (isVisible())
141 repaint();
142}
143/*
144void LinearView::verticalScrollbarAction(int action)
145{
146}
147
148void LinearView::horizontalScrollbarAction(int action)
149{
150}
151*/
152
153/*!
154 Select the items in the model that lie within the rectangle specified by
155 \a rect, using the selection \a command.
156*/
157
158void LinearView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command)
159{
160 QModelIndex leftIndex = itemAt(rect.left(), 0);
161 QModelIndex rightIndex = itemAt(rect.right(), 0);
162
163 QItemSelection selection(leftIndex, rightIndex);
164
165 selectionModel()->select(selection, command);
166}
167
168QModelIndex LinearView::moveCursor(QAbstractItemView::CursorAction cursorAction,
169 Qt::KeyboardModifiers)
170{
171 QModelIndex current = currentIndex();
172
173 switch (cursorAction) {
174 case MoveLeft:{
175 if (current.row() > 0)
176 return model()->index(current.row() - 1, 0, QModelIndex());
177 else
178 return model()->index(0, 0, QModelIndex());
179 break;}
180 case MoveRight:{
181 if (current.row() < rows(current) - 1)
182 return model()->index(current.row() + 1, 0, QModelIndex());
183 else
184 return model()->index(rows(current) - 1, 0,QModelIndex());
185 break;}
186 case MoveUp:
187 return current;
188 case MoveDown:
189 return current;
190 case MovePageUp:
191 return current;
192 case MovePageDown:
193 return current;
194 case MoveHome:
195 return model()->index(0, 0, QModelIndex());
196 case MoveEnd:
197 return model()->index(rows(current) - 1, 0, QModelIndex());
198 default:
199 return current;
200 }
201}
202
203int LinearView::horizontalOffset() const
204{
205 return horizontalScrollBar()->value();
206}
207
208int LinearView::verticalOffset() const
209{
210 return verticalScrollBar()->value();
211}
212
213/*!
214 Returns a rectangle corresponding to the selection in viewport cooridinates.
215*/
216
217QRect LinearView::selectionViewportRect(const QItemSelection &selection) const
218{
219 int ranges = selection.count();
220
221 if (ranges == 0)
222 return QRect();
223
224 // Note that we use the top and bottom functions of the selection range
225 // since the data is stored in rows.
226
227 int firstRow = selection.at(0).top();
228 int lastRow = selection.at(0).top();
229
230 for (int i = 0; i < ranges; ++i) {
231 firstRow = qMin(firstRow, selection.at(i).top());
232 lastRow = qMax(lastRow, selection.at(i).bottom());
233 }
234
235 QModelIndex firstItem = model()->index(qMin(firstRow, lastRow), 0,
236 QModelIndex());
237 QModelIndex lastItem = model()->index(qMax(firstRow, lastRow), 0,
238 QModelIndex());
239
240 QRect firstRect = itemViewportRect(firstItem);
241 QRect lastRect = itemViewportRect(lastItem);
242
243 return QRect(firstRect.left(), firstRect.top(),
244 lastRect.right() - firstRect.left(), firstRect.height());
245}
246
247void LinearView::paintEvent(QPaintEvent *event)
248{
249 QPainter painter(viewport());
250
251 QRect updateRect = event->rect();
252 QBrush background(Qt::black);
253 QPen foreground(Qt::white);
254
255 painter.fillRect(updateRect, background);
256 painter.setPen(foreground);
257
258 QModelIndex firstItem = itemAt(updateRect.left(), updateRect.top());
259 if (!firstItem.isValid())
260 firstItem = model()->index(0, 0, QModelIndex());
261
262 QModelIndex lastItem = itemAt(updateRect.right(), updateRect.bottom());
263 if (!lastItem.isValid())
264 lastItem = model()->index(rows() - 1, 0, QModelIndex());
265
266 int x = updateRect.left();
267 //int top = updateRect.top();
268 //int bottom = updateRect.bottom();
269
270 int row = firstItem.row();
271 QModelIndex index = model()->index(row, 0, QModelIndex());
272 int value = model()->data(index, Qt::DisplayRole).toInt();
273 int midPoint = viewport()->height()/2;
274 int y2 = midPoint - int(value * midPoint/255.0);
275
276 while (row <= lastItem.row()) {
277
278 QModelIndex index = model()->index(row, 0, QModelIndex());
279 int value = model()->data(index, Qt::DisplayRole).toInt();
280
281 int y1 = y2;
282 y2 = midPoint - int(value * midPoint/255.0);
283
284 painter.drawLine(x-1, y1, x, y2);
285 ++row; ++x;
286 }
287}
288
289void LinearView::resizeEvent(QResizeEvent * /* event */)
290{
291 updateGeometries();
292}
293
294void LinearView::updateGeometries()
295{
296 if (viewport()->width() < rows()) {
297 horizontalScrollBar()->setPageStep(viewport()->width());
298 horizontalScrollBar()->setRange(0, rows() - viewport()->width() - 1);
299 }
300}
301
302QSize LinearView::sizeHint() const
303{
304 return QSize(rows(), 200);
305}
306
307int LinearView::rows(const QModelIndex &index) const
308{
309 return model()->rowCount(model()->parent(index));
310}
311
312bool LinearView::isIndexHidden(const QModelIndex &index) const
313{
314 return false;
315}
Note: See TracBrowser for help on using the repository browser.