source: vendor/trolltech/current/src/widgets/qgridview.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 10.2 KB
Line 
1/****************************************************************************
2** $Id: qgridview.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Implementation of QGridView class
5**
6** Created : 010523
7**
8** Copyright (C) 1992-2001 Trolltech AS. All rights reserved.
9**
10** This file is part of the widgets module of the Qt GUI Toolkit.
11**
12** This file may be distributed under the terms of the Q Public License
13** as defined by Trolltech AS of Norway and appearing in the file
14** LICENSE.QPL included in the packaging of this file.
15**
16** This file may be distributed and/or modified under the terms of the
17** GNU General Public License version 2 as published by the Free Software
18** Foundation and appearing in the file LICENSE.GPL included in the
19** packaging of this file.
20**
21** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22** licenses may use this file in accordance with the Qt Commercial License
23** Agreement provided with the Software.
24**
25** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27**
28** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29** information about Qt Commercial License Agreements.
30** See http://www.trolltech.com/qpl/ for QPL licensing information.
31** See http://www.trolltech.com/gpl/ for GPL licensing information.
32**
33** Contact info@trolltech.com if any conditions of this licensing are
34** not clear to you.
35**
36**********************************************************************/
37
38
39#include "qgridview.h"
40
41#ifndef QT_NO_GRIDVIEW
42
43#include "qpainter.h"
44
45/*!
46 \class QGridView qgridview.h
47 \brief The QGridView class provides an abstract base for
48 fixed-size grids.
49
50 \ingroup abstractwidgets
51
52 A grid view consists of a number of abstract cells organized in
53 rows and columns. The cells have a fixed size and are identified
54 with a row index and a column index. The top-left cell is in row
55 0, column 0. The bottom-right cell is in row numRows()-1, column
56 numCols()-1.
57
58 You can define \l numRows, \l numCols, \l cellWidth and \l
59 cellHeight. Reimplement the pure virtual function paintCell() to
60 draw the contents of a cell.
61
62 With ensureCellVisible(), you can ensure a certain cell is
63 visible. With rowAt() and columnAt() you can find a cell based on
64 the given x- and y-coordinates.
65
66 If you need to monitor changes to the grid's dimensions (i.e. when
67 numRows or numCols is changed), reimplement the dimensionChange()
68 change handler.
69
70 Note: the row and column indices are always given in the order,
71 row (vertical offset) then column (horizontal offset). This order
72 is the opposite of all pixel operations, which are given in the
73 order x (horizontal offset), y (vertical offset).
74
75 QGridView is a very simple abstract class based on QScrollView. It
76 is designed to simplify the task of drawing many cells of the same
77 size in a potentially scrollable canvas. If you need rows and
78 columns with different sizes, use a QTable instead. If you need a
79 simple list of items, use a QListBox. If you need to present
80 hierachical data use a QListView, and if you need random objects
81 at random positions, consider using either a QIconView or a
82 QCanvas.
83*/
84
85
86/*!
87 Constructs a grid view.
88
89 The \a parent, \a name and widget flag, \a f, arguments are passed
90 to the QScrollView constructor.
91*/
92QGridView::QGridView( QWidget *parent, const char *name, WFlags f )
93 :QScrollView( parent, name, f | WStaticContents ),
94 nrows( 5 ), ncols( 5 ), cellw( 12 ), cellh( 12 )
95{
96 viewport()->setBackgroundMode( PaletteBase );
97 setBackgroundMode( PaletteBackground, PaletteBase );
98 viewport()->setFocusProxy( this );
99}
100
101/*!
102 Destroys the grid view.
103*/
104QGridView::~QGridView()
105{
106}
107
108void QGridView::updateGrid()
109{
110 resizeContents( ncols * cellw, nrows * cellh );
111}
112
113/*!
114 \property QGridView::numRows
115 \brief The number of rows in the grid
116
117 \sa numCols
118*/
119void QGridView::setNumRows( int numRows )
120{
121 int oldnrows = nrows;
122 nrows = numRows;
123 dimensionChange( oldnrows, ncols );
124 updateGrid();
125}
126
127/*!
128 \property QGridView::numCols
129 \brief The number of columns in the grid
130
131 \sa numRows
132*/
133void QGridView::setNumCols( int numCols )
134{
135 int oldncols = ncols;
136 ncols = numCols;
137 dimensionChange( nrows, oldncols );
138 updateGrid();
139}
140
141/*!
142 \property QGridView::cellWidth
143 \brief The width of a grid column
144
145 All columns in a grid view have the same width.
146
147 \sa cellHeight
148*/
149void QGridView::setCellWidth( int cellWidth )
150{
151 cellw = cellWidth;
152 updateGrid();
153 updateContents();
154}
155
156/*!
157 \property QGridView::cellHeight
158 \brief The height of a grid row
159
160 All rows in a grid view have the same height.
161
162 \sa cellWidth
163*/
164void QGridView::setCellHeight( int cellHeight )
165{
166 cellh = cellHeight;
167 updateGrid();
168 updateContents();
169}
170
171/*!
172 Returns the geometry of cell (\a row, \a column) in the content
173 coordinate system.
174
175 \sa cellRect()
176 */
177QRect QGridView::cellGeometry( int row, int column )
178{
179 QRect r;
180 if ( row >= 0 && row < nrows && column >= 0 && column < ncols )
181 r.setRect( cellw * column, cellh * row, cellw, cellh );
182 return r;
183}
184
185/*!
186 Repaints cell (\a row, \a column).
187
188 If \a erase is TRUE, Qt erases the area of the cell before the
189 paintCell() call; otherwise no erasing takes place.
190
191 \sa QWidget::repaint()
192*/
193void QGridView::repaintCell( int row, int column, bool erase )
194{
195 repaintContents( cellGeometry( row, column ), erase );
196}
197
198/*!
199 Updates cell (\a row, \a column).
200
201 \sa QWidget::update()
202*/
203void QGridView::updateCell( int row, int column )
204{
205 updateContents( cellGeometry( row, column ) );
206}
207
208/*!
209 Ensures cell (\a row, \a column) is visible, scrolling the grid
210 view if necessary.
211*/
212void QGridView::ensureCellVisible( int row, int column )
213{
214 QRect r = cellGeometry( row, column );
215 ensureVisible( r.x(), r.y(), r.width(), r.height() );
216}
217
218/*!
219 This function fills the \a cw pixels wide and \a ch pixels high
220 rectangle starting at position (\a cx, \a cy) with the background
221 color using the painter \a p.
222
223 paintEmptyArea() is invoked by drawContents() to erase or fill
224 unused areas.
225*/
226
227void QGridView::paintEmptyArea( QPainter *p, int cx ,int cy, int cw, int ch)
228{
229 if ( gridSize().width() >= contentsWidth() && gridSize().height() >= contentsHeight() )
230 return;
231 // Region of the rect we should draw
232 contentsToViewport( cx, cy, cx, cy );
233 QRegion reg( QRect( cx, cy, cw, ch ) );
234 // Subtract the table from it
235 reg = reg.subtract( QRect( contentsToViewport( QPoint( 0, 0 ) ), gridSize() ) );
236
237 // And draw the rectangles (transformed as needed)
238 QMemArray<QRect> r = reg.rects();
239 const QBrush &brush = backgroundBrush();
240 for ( int i = 0; i < (int)r.count(); ++i)
241 p->fillRect( r[ i ], brush );
242}
243
244/*!\reimp
245 */
246void QGridView::drawContents( QPainter *p, int cx, int cy, int cw, int ch )
247{
248 int colfirst = columnAt( cx );
249 int collast = columnAt( cx + cw );
250 int rowfirst = rowAt( cy );
251 int rowlast = rowAt( cy + ch );
252
253 if ( rowfirst == -1 || colfirst == -1 ) {
254 paintEmptyArea( p, cx, cy, cw, ch );
255 return;
256 }
257
258 if ( collast < 0 || collast >= ncols )
259 collast = ncols-1;
260 if ( rowlast < 0 || rowlast >= nrows )
261 rowlast = nrows-1;
262
263 // Go through the rows
264 for ( int r = rowfirst; r <= rowlast; ++r ) {
265 // get row position and height
266 int rowp = r * cellh;
267
268 // Go through the columns in the row r
269 // if we know from where to where, go through [colfirst, collast],
270 // else go through all of them
271 for ( int c = colfirst; c <= collast; ++c ) {
272 // get position and width of column c
273 int colp = c * cellw;
274 // Translate painter and draw the cell
275 p->translate( colp, rowp );
276 paintCell( p, r, c );
277 p->translate( -colp, -rowp );
278 }
279 }
280
281 // Paint empty rects
282 paintEmptyArea( p, cx, cy, cw, ch );
283}
284
285/*!
286 \reimp
287
288 (Implemented to get rid of a compiler warning.)
289*/
290void QGridView::drawContents( QPainter * )
291{
292}
293
294/*!
295 \fn void QGridView::dimensionChange( int oldNumRows, int oldNumCols )
296
297 This change handler is called whenever any of the grid's
298 dimensions change. \a oldNumRows and \a oldNumCols contain the
299 old dimensions, numRows() and numCols() contain the new
300 dimensions.
301*/
302void QGridView::dimensionChange( int, int ) {}
303
304
305
306/*!
307 \fn int QGridView::rowAt( int y ) const
308
309 Returns the number of the row at position \a y. \a y must be given
310 in content coordinates.
311
312 \sa columnAt()
313*/
314
315/*!
316 \fn int QGridView::columnAt( int x ) const
317
318 Returns the number of the column at position \a x. \a x must be
319 given in content coordinates.
320
321 \sa rowAt()
322*/
323
324/*!
325 \fn void QGridView::paintCell( QPainter *p, int row, int col )
326
327 This pure virtual function is called to paint the single cell at
328 (\a row, \a col) using painter \a p. The painter must be open when
329 paintCell() is called and must remain open.
330
331 The coordinate system is \link QPainter::translate() translated
332 \endlink so that the origin is at the top-left corner of the cell
333 to be painted, i.e. \e cell coordinates. Do not scale or shear
334 the coordinate system (or if you do, restore the transformation
335 matrix before you return).
336
337 The painter is not clipped by default in order to get maximum
338 efficiency. If you want clipping, use
339
340 \code
341 p->setClipRect( cellRect(), QPainter::CoordPainter );
342 //... your drawing code
343 p->setClipping( FALSE );
344
345 \endcode
346*/
347
348/*!
349 \fn QRect QGridView::cellRect() const
350
351 Returns the geometry of a cell in a cell's coordinate system. This
352 is a convenience function useful in paintCell(). It is equivalent
353 to QRect( 0, 0, cellWidth(), cellHeight() ).
354
355 \sa cellGeometry()
356
357*/
358
359/*!
360 \fn QSize QGridView::gridSize() const
361
362 Returns the size of the grid in pixels.
363
364*/
365
366#endif // QT_NO_GRIDVIEW
Note: See TracBrowser for help on using the repository browser.