1 /****************************************************************************
2 ** $Id: qgridview.C,v 1.3 2003/08/23 00:16:39 leeming Exp $
4 ** Implementation of QGridView class
8 ** Copyright (C) 1992-2001 Trolltech AS. All rights reserved.
10 ** This file is part of the widgets module of the Qt GUI Toolkit.
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.
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.
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.
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.
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.
33 ** Contact info@trolltech.com if any conditions of this licensing are
36 **********************************************************************/
41 #include "qgridview.h"
43 #ifndef QT_NO_GRIDVIEW
48 \class QGridView qgridview.h
49 \brief The QGridView class provides an abstract base for fixed-size
52 \ingroup abstractwidgets
54 A grid view consists of a number of abstract cells organized in rows
55 and columns. The cells have a fixed size and are identified with a
56 row index and a column index. The top-left cell is in row 0, column
57 0. The bottom-right cell is in row numRows()-1, column numCols()-1.
59 You can define \l numRows, \l numCols, \l cellWidth and
60 \l cellHeight. Reimplement the pure virtual function paintCell() to
61 draw the content of a cell.
63 With ensureCellVisible(), you can ensure a certain cell is
64 visible. With rowAt() and columnAt() you can find a cell based on
65 the given x- and y-coordinates.
67 If you need to monitor changes to the grid's dimensions (i.e. when
68 numRows or numCols is changed), reimplement the dimensionChange()
71 Note: the row, column indices are always given in the order, row
72 (vertical offset) then column (horizontal offset). This order is the
73 opposite of all pixel operations, which are given in the order x
74 (horizontal offset), y (vertical offset).
76 QGridView is a very simple abstract class based on QScrollView. It
77 is designed to simplify the task of drawing many cells of the same
78 size in a potentially scrollable canvas. If you need rows and
79 columns in different sizes, use a QTable instead. If you need a
80 simple list of items, use a QListBox. If you need to present
81 hierachical data use a QListView, and if you need random objects at
82 random positions, consider using either a QIconView or a QCanvas.
88 Constructs a grid view.
90 The \a parent, \a name and widget flag, \a f, arguments are passed to the
91 QScrollView constructor.
93 QGridView::QGridView( QWidget *parent, const char *name, WFlags f )
94 :QScrollView( parent, name ,f ),nrows(5),ncols(5),cellw(12),cellh(12)
96 viewport()->setBackgroundMode( PaletteBase );
97 setBackgroundMode( PaletteBackground );
98 viewport()->setFocusProxy( this );
102 Destroys the grid view.
104 QGridView::~QGridView()
108 void QGridView::updateGrid()
110 resizeContents( ncols * cellw, nrows * cellh );
113 /*! \property QGridView::numRows
114 \brief The number of rows in the grid
118 void QGridView::setNumRows( int numRows )
120 int oldnrows = nrows;
122 dimensionChange( oldnrows, ncols );
126 /*! \property QGridView::numCols
127 \brief The number of columns in the grid
131 void QGridView::setNumCols( int numCols )
133 int oldncols = ncols;
135 dimensionChange( nrows, oldncols );
139 /*! \property QGridView::cellWidth
140 \brief The width of a grid column
142 All columns in a grid view have the same width.
146 void QGridView::setCellWidth( int cellWidth )
150 updateContents( contentsX(), contentsY(), visibleWidth(), visibleHeight() );
154 /*! \property QGridView::cellHeight
155 \brief The height of a grid row
157 All rows in a grid view have the same height.
161 void QGridView::setCellHeight( int cellHeight )
165 updateContents( contentsX(), contentsY(), visibleWidth(), visibleHeight() );
170 Returns the geometry of cell (\a row, \a column) in the content
175 QRect QGridView::cellGeometry( int row, int column )
178 if ( row >= 0 && row < nrows && column >= 0 && column < ncols )
179 r.setRect( cellw * column, cellh * row, cellw, cellh );
183 /*! Repaints cell (\a row, \a column).
185 If \a erase is TRUE, Qt erases the area of the cell before the
186 paintCell() call; otherwise no erasing takes place.
188 \sa QWidget::repaint()
190 void QGridView::repaintCell( int row, int column, bool erase )
192 repaintContents( cellGeometry( row, column ), erase );
195 /*! Updates cell (\a row, \a column).
197 \sa QWidget::update()
199 void QGridView::updateCell( int row, int column )
201 updateContents( cellGeometry( row, column ) );
205 Ensure cell (\a row, \a column) is visible, scrolling the grid view
208 void QGridView::ensureCellVisible( int row, int column )
210 QRect r = cellGeometry( row, column );
211 ensureVisible( r.x(), r.y(), r.width(), r.height() );
214 /*! This function fills the \a cw pixels wide and \a ch pixels high
215 rectangle starting at position (\a cx, \a cy) with the
216 background color using the painter \a p.
218 paintEmptyArea() is invoked by drawContents() to erase
219 or fill unused areas.
222 void QGridView::paintEmptyArea( QPainter *p, int cx ,int cy, int cw, int ch)
224 if ( gridSize().width() >= contentsWidth() && gridSize().height() >= contentsHeight() )
226 // Region of the rect we should draw
227 contentsToViewport( cx, cy, cx, cy );
228 QRegion reg( QRect( cx, cy, cw, ch ) );
229 // Subtract the table from it
230 reg = reg.subtract( QRect( contentsToViewport( QPoint( 0, 0 ) ), gridSize() ) );
232 // And draw the rectangles (transformed as needed)
233 QArray<QRect> r = reg.rects();
234 const QBrush &brush = colorGroup().brush(QColorGroup::Background);
235 for ( int i = 0; i < (int)r.count(); ++i)
236 p->fillRect( r[ i ], brush );
241 void QGridView::drawContents( QPainter *p, int cx, int cy, int cw, int ch )
243 int colfirst = columnAt( cx );
244 int collast = columnAt( cx + cw );
245 int rowfirst = rowAt( cy );
246 int rowlast = rowAt( cy + ch );
248 if ( rowfirst == -1 || colfirst == -1 ) {
249 paintEmptyArea( p, cx, cy, cw, ch );
253 if ( collast < 0 || collast >= ncols )
255 if ( rowlast < 0 || rowlast >= nrows )
258 // Go through the rows
259 for ( int r = rowfirst; r <= rowlast; ++r ) {
260 // get row position and height
261 int rowp = r * cellh;
263 // Go through the columns in the row r
264 // if we know from where to where, go through [colfirst, collast],
265 // else go through all of them
266 for ( int c = colfirst; c <= collast; ++c ) {
267 // get position and width of column c
268 int colp = c * cellw;
269 // Translate painter and draw the cell
270 p->translate( colp, rowp );
271 paintCell( p, r, c );
272 p->translate( -colp, -rowp );
277 paintEmptyArea( p, cx, cy, cw, ch );
283 (Implemented to get rid of a compiler warning.)
285 void QGridView::drawContents( QPainter * )
289 /*! \fn void QGridView::dimensionChange( int oldNumRows, int oldNumCols )
291 This change handler is called whenever any of the grid's dimensions
292 changes. \a oldNumRows and \a oldNumCols contain the old dimensions,
293 numRows() and numCols() contain the new dimensions.
295 void QGridView::dimensionChange( int, int ) {}
299 /*! \fn int QGridView::rowAt( int y ) const
301 Returns the number of the row at position \a y. \a y must be given in
307 /*! \fn int QGridView::columnAt( int x ) const
309 Returns the number of the column at position \a x. \a x must be
310 given in content coordinates.
316 \fn void QGridView::paintCell( QPainter *p, int row, int col )
318 This pure virtual function is called to paint the single cell at
319 (\a row, \a col) using painter \a p. The painter must be open when
320 paintCell() is called and must remain open.
322 The coordinate system is \link QPainter::translate() translated \endlink
323 so that the origin is at the top-left corner of the cell to be
324 painted, i.e. \e cell coordinates. Do not scale or shear the coordinate
325 system (or if you do, restore the transformation matrix before you
328 The painter is not clipped by default in order to get maximum
329 efficiency. If you want clipping, use
332 p->setClipRect( cellRect(), QPainter::CoordPainter );
333 //... your drawing code
334 p->setClipping( FALSE );
340 /*! \fn QRect QGridView::cellRect() const
342 Returns the geometry of a cell in a cell's coordinate system. This
343 is a convenience function useful in paintCell(). It is equivalent to
344 QRect( 0, 0, cellWidth(), cellHeight() ).
350 /*!\fn QSize QGridView::gridSize() const
352 Returns the size of the grid in pixels.
356 #endif // QT_NO_GRIDVIEW