1 /****************************************************************************
2 ** $Id: qgridview.cpp,v 1.1 2002/10/09 08:59:02 leuven 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 **********************************************************************/
39 #include "qgridview.h"
41 #ifndef QT_NO_GRIDVIEW
46 \class QGridView qgridview.h
47 \brief The QGridView class provides an abstract base for fixed-size
50 \ingroup abstractwidgets
52 A grid view consists of a number of abstract cells organized in rows
53 and columns. The cells have a fixed size and are identified with a
54 row index and a column index. The top-left cell is in row 0, column
55 0. The bottom-right cell is in row numRows()-1, column numCols()-1.
57 You can define \l numRows, \l numCols, \l cellWidth and
58 \l cellHeight. Reimplement the pure virtual function paintCell() to
59 draw the content of a cell.
61 With ensureCellVisible(), you can ensure a certain cell is
62 visible. With rowAt() and columnAt() you can find a cell based on
63 the given x- and y-coordinates.
65 If you need to monitor changes to the grid's dimensions (i.e. when
66 numRows or numCols is changed), reimplement the dimensionChange()
69 Note: the row, column indices are always given in the order, row
70 (vertical offset) then column (horizontal offset). This order is the
71 opposite of all pixel operations, which are given in the order x
72 (horizontal offset), y (vertical offset).
74 QGridView is a very simple abstract class based on QScrollView. It
75 is designed to simplify the task of drawing many cells of the same
76 size in a potentially scrollable canvas. If you need rows and
77 columns in different sizes, use a QTable instead. If you need a
78 simple list of items, use a QListBox. If you need to present
79 hierachical data use a QListView, and if you need random objects at
80 random positions, consider using either a QIconView or a QCanvas.
86 Constructs a grid view.
88 The \a parent, \a name and widget flag, \a f, arguments are passed to the
89 QScrollView constructor.
91 QGridView::QGridView( QWidget *parent, const char *name, WFlags f )
92 :QScrollView( parent, name ,f ),nrows(5),ncols(5),cellw(12),cellh(12)
94 viewport()->setBackgroundMode( PaletteBase );
95 setBackgroundMode( PaletteBackground );
96 viewport()->setFocusProxy( this );
100 Destroys the grid view.
102 QGridView::~QGridView()
106 void QGridView::updateGrid()
108 resizeContents( ncols * cellw, nrows * cellh );
111 /*! \property QGridView::numRows
112 \brief The number of rows in the grid
116 void QGridView::setNumRows( int numRows )
118 int oldnrows = nrows;
120 dimensionChange( oldnrows, ncols );
124 /*! \property QGridView::numCols
125 \brief The number of columns in the grid
129 void QGridView::setNumCols( int numCols )
131 int oldncols = ncols;
133 dimensionChange( nrows, oldncols );
137 /*! \property QGridView::cellWidth
138 \brief The width of a grid column
140 All columns in a grid view have the same width.
144 void QGridView::setCellWidth( int cellWidth )
148 updateContents( contentsX(), contentsY(), visibleWidth(), visibleHeight() );
152 /*! \property QGridView::cellHeight
153 \brief The height of a grid row
155 All rows in a grid view have the same height.
159 void QGridView::setCellHeight( int cellHeight )
163 updateContents( contentsX(), contentsY(), visibleWidth(), visibleHeight() );
168 Returns the geometry of cell (\a row, \a column) in the content
173 QRect QGridView::cellGeometry( int row, int column )
176 if ( row >= 0 && row < nrows && column >= 0 && column < ncols )
177 r.setRect( cellw * column, cellh * row, cellw, cellh );
181 /*! Repaints cell (\a row, \a column).
183 If \a erase is TRUE, Qt erases the area of the cell before the
184 paintCell() call; otherwise no erasing takes place.
186 \sa QWidget::repaint()
188 void QGridView::repaintCell( int row, int column, bool erase )
190 repaintContents( cellGeometry( row, column ), erase );
193 /*! Updates cell (\a row, \a column).
195 \sa QWidget::update()
197 void QGridView::updateCell( int row, int column )
199 updateContents( cellGeometry( row, column ) );
203 Ensure cell (\a row, \a column) is visible, scrolling the grid view
206 void QGridView::ensureCellVisible( int row, int column )
208 QRect r = cellGeometry( row, column );
209 ensureVisible( r.x(), r.y(), r.width(), r.height() );
212 /*! This function fills the \a cw pixels wide and \a ch pixels high
213 rectangle starting at position (\a cx, \a cy) with the
214 background color using the painter \a p.
216 paintEmptyArea() is invoked by drawContents() to erase
217 or fill unused areas.
220 void QGridView::paintEmptyArea( QPainter *p, int cx ,int cy, int cw, int ch)
222 if ( gridSize().width() >= contentsWidth() && gridSize().height() >= contentsHeight() )
224 // Region of the rect we should draw
225 contentsToViewport( cx, cy, cx, cy );
226 QRegion reg( QRect( cx, cy, cw, ch ) );
227 // Subtract the table from it
228 reg = reg.subtract( QRect( contentsToViewport( QPoint( 0, 0 ) ), gridSize() ) );
230 // And draw the rectangles (transformed as needed)
231 QArray<QRect> r = reg.rects();
232 const QBrush &brush = colorGroup().brush(QColorGroup::Background);
233 for ( int i = 0; i < (int)r.count(); ++i)
234 p->fillRect( r[ i ], brush );
239 void QGridView::drawContents( QPainter *p, int cx, int cy, int cw, int ch )
241 int colfirst = columnAt( cx );
242 int collast = columnAt( cx + cw );
243 int rowfirst = rowAt( cy );
244 int rowlast = rowAt( cy + ch );
246 if ( rowfirst == -1 || colfirst == -1 ) {
247 paintEmptyArea( p, cx, cy, cw, ch );
251 if ( collast < 0 || collast >= ncols )
253 if ( rowlast < 0 || rowlast >= nrows )
256 // Go through the rows
257 for ( int r = rowfirst; r <= rowlast; ++r ) {
258 // get row position and height
259 int rowp = r * cellh;
261 // Go through the columns in the row r
262 // if we know from where to where, go through [colfirst, collast],
263 // else go through all of them
264 for ( int c = colfirst; c <= collast; ++c ) {
265 // get position and width of column c
266 int colp = c * cellw;
267 // Translate painter and draw the cell
268 p->translate( colp, rowp );
269 paintCell( p, r, c );
270 p->translate( -colp, -rowp );
275 paintEmptyArea( p, cx, cy, cw, ch );
281 (Implemented to get rid of a compiler warning.)
283 void QGridView::drawContents( QPainter * )
287 /*! \fn void QGridView::dimensionChange( int oldNumRows, int oldNumCols )
289 This change handler is called whenever any of the grid's dimensions
290 changes. \a oldNumRows and \a oldNumCols contain the old dimensions,
291 numRows() and numCols() contain the new dimensions.
293 void QGridView::dimensionChange( int, int ) {}
297 /*! \fn int QGridView::rowAt( int y ) const
299 Returns the number of the row at position \a y. \a y must be given in
305 /*! \fn int QGridView::columnAt( int x ) const
307 Returns the number of the column at position \a x. \a x must be
308 given in content coordinates.
314 \fn void QGridView::paintCell( QPainter *p, int row, int col )
316 This pure virtual function is called to paint the single cell at
317 (\a row, \a col) using painter \a p. The painter must be open when
318 paintCell() is called and must remain open.
320 The coordinate system is \link QPainter::translate() translated \endlink
321 so that the origin is at the top-left corner of the cell to be
322 painted, i.e. \e cell coordinates. Do not scale or shear the coordinate
323 system (or if you do, restore the transformation matrix before you
326 The painter is not clipped by default in order to get maximum
327 efficiency. If you want clipping, use
330 p->setClipRect( cellRect(), QPainter::CoordPainter );
331 //... your drawing code
332 p->setClipping( FALSE );
338 /*! \fn QRect QGridView::cellRect() const
340 Returns the geometry of a cell in a cell's coordinate system. This
341 is a convenience function useful in paintCell(). It is equivalent to
342 QRect( 0, 0, cellWidth(), cellHeight() ).
348 /*!\fn QSize QGridView::gridSize() const
350 Returns the size of the grid in pixels.
354 #endif // QT_NO_GRIDVIEW