[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the Qt3Support module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
[561] | 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
[2] | 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 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
[2] | 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 | #include "q3gridview.h"
|
---|
| 44 | #include "qpainter.h"
|
---|
| 45 |
|
---|
| 46 | QT_BEGIN_NAMESPACE
|
---|
| 47 |
|
---|
| 48 | using namespace Qt;
|
---|
| 49 |
|
---|
| 50 | /*!
|
---|
| 51 | \class Q3GridView
|
---|
| 52 | \brief The Q3GridView class provides an abstract base for
|
---|
| 53 | fixed-size grids.
|
---|
| 54 |
|
---|
| 55 | \compat
|
---|
| 56 |
|
---|
| 57 | A grid view consists of a number of abstract cells organized in
|
---|
| 58 | rows and columns. The cells have a fixed size and are identified
|
---|
| 59 | with a row index and a column index. The top-left cell is in row
|
---|
| 60 | 0, column 0. The bottom-right cell is in row numRows()-1, column
|
---|
| 61 | numCols()-1.
|
---|
| 62 |
|
---|
| 63 | You can define \l{Q3GridView::numRows} {numRows},
|
---|
| 64 | \l{Q3GridView::numCols} {numCols}, \l{Q3GridView::cellWidth}
|
---|
| 65 | {cellWidth} and \l{Q3GridView::cellHeight} {cellHeight}. Reimplement
|
---|
| 66 | the pure virtual function paintCell() to draw the contents of a
|
---|
| 67 | cell.
|
---|
| 68 |
|
---|
| 69 | With ensureCellVisible(), you can ensure a certain cell is
|
---|
| 70 | visible. With rowAt() and columnAt() you can find a cell based on
|
---|
| 71 | the given x- and y-coordinates.
|
---|
| 72 |
|
---|
| 73 | If you need to monitor changes to the grid's dimensions (i.e. when
|
---|
| 74 | numRows or numCols is changed), reimplement the dimensionChange()
|
---|
| 75 | change handler.
|
---|
| 76 |
|
---|
| 77 | Note: the row and column indices are always given in the order,
|
---|
| 78 | row (vertical offset) then column (horizontal offset). This order
|
---|
| 79 | is the opposite of all pixel operations, which are given in the
|
---|
| 80 | order x (horizontal offset), y (vertical offset).
|
---|
| 81 |
|
---|
| 82 | Q3GridView is a very simple abstract class based on Q3ScrollView. It
|
---|
| 83 | is designed to simplify the task of drawing many cells of the same
|
---|
| 84 | size in a potentially scrollable canvas. If you need rows and
|
---|
| 85 | columns with different sizes, use a Q3Table instead. If you need a
|
---|
| 86 | simple list of items, use a Q3ListBox. If you need to present
|
---|
[846] | 87 | hierarichal data use a Q3ListView, and if you need random objects
|
---|
[2] | 88 | at random positions, consider using either a Q3IconView or a
|
---|
| 89 | Q3Canvas.
|
---|
| 90 | */
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 | /*!
|
---|
| 94 | Constructs a grid view.
|
---|
| 95 |
|
---|
| 96 | The \a parent, \a name and widget flag, \a f, arguments are passed
|
---|
| 97 | to the Q3ScrollView constructor.
|
---|
| 98 | */
|
---|
| 99 | Q3GridView::Q3GridView(QWidget *parent, const char *name, Qt::WindowFlags f)
|
---|
| 100 | : Q3ScrollView(parent, name, f | WStaticContents),
|
---|
| 101 | nrows(5), ncols(5), cellw(12), cellh(12)
|
---|
| 102 | {
|
---|
| 103 | viewport()->setBackgroundMode(PaletteBase);
|
---|
| 104 | setBackgroundMode(PaletteBackground, PaletteBase);
|
---|
| 105 | viewport()->setFocusProxy(this);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /*!
|
---|
| 109 | Destroys the grid view.
|
---|
| 110 | */
|
---|
| 111 | Q3GridView::~Q3GridView()
|
---|
| 112 | {
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | void Q3GridView::updateGrid()
|
---|
| 116 | {
|
---|
| 117 | resizeContents(ncols * cellw, nrows * cellh);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /*!
|
---|
| 121 | \property Q3GridView::numRows
|
---|
| 122 | \brief The number of rows in the grid
|
---|
| 123 |
|
---|
| 124 | \sa numCols
|
---|
| 125 | */
|
---|
| 126 | void Q3GridView::setNumRows(int numRows)
|
---|
| 127 | {
|
---|
| 128 | int oldnrows = nrows;
|
---|
| 129 | nrows = numRows;
|
---|
| 130 | dimensionChange(oldnrows, ncols);
|
---|
| 131 | updateGrid();
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /*!
|
---|
| 135 | \property Q3GridView::numCols
|
---|
| 136 | \brief The number of columns in the grid
|
---|
| 137 |
|
---|
| 138 | \sa numRows
|
---|
| 139 | */
|
---|
| 140 | void Q3GridView::setNumCols(int numCols)
|
---|
| 141 | {
|
---|
| 142 | int oldncols = ncols;
|
---|
| 143 | ncols = numCols;
|
---|
| 144 | dimensionChange(nrows, oldncols);
|
---|
| 145 | updateGrid();
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /*!
|
---|
| 149 | \property Q3GridView::cellWidth
|
---|
| 150 | \brief The width of a grid column
|
---|
| 151 |
|
---|
| 152 | All columns in a grid view have the same width.
|
---|
| 153 |
|
---|
| 154 | \sa cellHeight
|
---|
| 155 | */
|
---|
| 156 | void Q3GridView::setCellWidth(int cellWidth)
|
---|
| 157 | {
|
---|
| 158 | cellw = cellWidth;
|
---|
| 159 | updateGrid();
|
---|
| 160 | updateContents();
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /*!
|
---|
| 164 | \property Q3GridView::cellHeight
|
---|
| 165 | \brief The height of a grid row
|
---|
| 166 |
|
---|
| 167 | All rows in a grid view have the same height.
|
---|
| 168 |
|
---|
| 169 | \sa cellWidth
|
---|
| 170 | */
|
---|
| 171 | void Q3GridView::setCellHeight(int cellHeight)
|
---|
| 172 | {
|
---|
| 173 | cellh = cellHeight;
|
---|
| 174 | updateGrid();
|
---|
| 175 | updateContents();
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /*!
|
---|
| 179 | Returns the geometry of cell (\a row, \a column) in the content
|
---|
| 180 | coordinate system.
|
---|
| 181 |
|
---|
| 182 | \sa cellRect()
|
---|
| 183 | */
|
---|
| 184 | QRect Q3GridView::cellGeometry(int row, int column)
|
---|
| 185 | {
|
---|
| 186 | QRect r;
|
---|
| 187 | if (row >= 0 && row < nrows && column >= 0 && column < ncols)
|
---|
| 188 | r.setRect(cellw * column, cellh * row, cellw, cellh);
|
---|
| 189 | return r;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | /*!
|
---|
| 193 | Repaints cell (\a row, \a column).
|
---|
| 194 |
|
---|
| 195 | If \a erase is true, Qt erases the area of the cell before the
|
---|
| 196 | paintCell() call; otherwise no erasing takes place.
|
---|
| 197 |
|
---|
| 198 | \sa QWidget::repaint()
|
---|
| 199 | */
|
---|
| 200 | void Q3GridView::repaintCell(int row, int column, bool erase)
|
---|
| 201 | {
|
---|
| 202 | repaintContents(cellGeometry(row, column), erase);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /*!
|
---|
| 206 | Updates cell (\a row, \a column).
|
---|
| 207 |
|
---|
| 208 | \sa QWidget::update()
|
---|
| 209 | */
|
---|
| 210 | void Q3GridView::updateCell(int row, int column)
|
---|
| 211 | {
|
---|
| 212 | updateContents(cellGeometry(row, column));
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | /*!
|
---|
| 216 | Ensures cell (\a row, \a column) is visible, scrolling the grid
|
---|
| 217 | view if necessary.
|
---|
| 218 | */
|
---|
| 219 | void Q3GridView::ensureCellVisible(int row, int column)
|
---|
| 220 | {
|
---|
| 221 | QRect r = cellGeometry(row, column);
|
---|
| 222 | ensureVisible(r.x(), r.y(), r.width(), r.height());
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | /*!
|
---|
| 226 | This function fills the \a cw pixels wide and \a ch pixels high
|
---|
| 227 | rectangle starting at position (\a cx, \a cy) with the background
|
---|
| 228 | color using the painter \a p.
|
---|
| 229 |
|
---|
| 230 | paintEmptyArea() is invoked by drawContents() to erase or fill
|
---|
| 231 | unused areas.
|
---|
| 232 | */
|
---|
| 233 |
|
---|
| 234 | void Q3GridView::paintEmptyArea(QPainter *p, int cx ,int cy, int cw, int ch)
|
---|
| 235 | {
|
---|
| 236 | if (gridSize().width() >= contentsWidth() && gridSize().height() >= contentsHeight())
|
---|
| 237 | return;
|
---|
| 238 | // Region of the rect we should draw
|
---|
| 239 | contentsToViewport(cx, cy, cx, cy);
|
---|
| 240 | QRegion reg(QRect(cx, cy, cw, ch));
|
---|
| 241 | // Subtract the table from it
|
---|
| 242 | reg = reg.subtracted(QRect(contentsToViewport(QPoint(0, 0)), gridSize()));
|
---|
| 243 |
|
---|
| 244 | // And draw the rectangles (transformed as needed)
|
---|
| 245 | QVector<QRect> r = reg.rects();
|
---|
| 246 | const QBrush &brush = backgroundBrush();
|
---|
| 247 | for (int i = 0; i < (int)r.count(); ++i)
|
---|
| 248 | p->fillRect(r[ i ], brush);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | /*!\reimp
|
---|
| 252 | */
|
---|
| 253 | void Q3GridView::drawContents(QPainter *p, int cx, int cy, int cw, int ch)
|
---|
| 254 | {
|
---|
| 255 | int colfirst = columnAt(cx);
|
---|
| 256 | int collast = columnAt(cx + cw);
|
---|
| 257 | int rowfirst = rowAt(cy);
|
---|
| 258 | int rowlast = rowAt(cy + ch);
|
---|
| 259 |
|
---|
| 260 | if (rowfirst == -1 || colfirst == -1) {
|
---|
| 261 | paintEmptyArea(p, cx, cy, cw, ch);
|
---|
| 262 | return;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | if (collast < 0 || collast >= ncols)
|
---|
| 266 | collast = ncols-1;
|
---|
| 267 | if (rowlast < 0 || rowlast >= nrows)
|
---|
| 268 | rowlast = nrows-1;
|
---|
| 269 |
|
---|
| 270 | // Go through the rows
|
---|
| 271 | for (int r = rowfirst; r <= rowlast; ++r) {
|
---|
| 272 | // get row position and height
|
---|
| 273 | int rowp = r * cellh;
|
---|
| 274 |
|
---|
| 275 | // Go through the columns in the row r
|
---|
| 276 | // if we know from where to where, go through [colfirst, collast],
|
---|
| 277 | // else go through all of them
|
---|
| 278 | for (int c = colfirst; c <= collast; ++c) {
|
---|
| 279 | // get position and width of column c
|
---|
| 280 | int colp = c * cellw;
|
---|
| 281 | // Translate painter and draw the cell
|
---|
| 282 | p->translate(colp, rowp);
|
---|
| 283 | paintCell(p, r, c);
|
---|
| 284 | p->translate(-colp, -rowp);
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | // Paint empty rects
|
---|
| 289 | paintEmptyArea(p, cx, cy, cw, ch);
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | /*!
|
---|
| 293 | \reimp
|
---|
| 294 |
|
---|
| 295 | (Implemented to get rid of a compiler warning.)
|
---|
| 296 | */
|
---|
| 297 | void Q3GridView::drawContents(QPainter *)
|
---|
| 298 | {
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | /*!
|
---|
| 302 | \fn void Q3GridView::dimensionChange(int oldNumRows, int oldNumCols)
|
---|
| 303 |
|
---|
| 304 | This change handler is called whenever any of the grid's
|
---|
| 305 | dimensions change. \a oldNumRows and \a oldNumCols contain the
|
---|
| 306 | old dimensions, numRows() and numCols() contain the new
|
---|
| 307 | dimensions.
|
---|
| 308 | */
|
---|
| 309 | void Q3GridView::dimensionChange(int, int) {}
|
---|
| 310 |
|
---|
| 311 |
|
---|
| 312 |
|
---|
| 313 | /*!
|
---|
| 314 | \fn int Q3GridView::rowAt(int y) const
|
---|
| 315 |
|
---|
| 316 | Returns the number of the row at position \a y. \a y must be given
|
---|
| 317 | in content coordinates.
|
---|
| 318 |
|
---|
| 319 | \sa columnAt()
|
---|
| 320 | */
|
---|
| 321 |
|
---|
| 322 | /*!
|
---|
| 323 | \fn int Q3GridView::columnAt(int x) const
|
---|
| 324 |
|
---|
| 325 | Returns the number of the column at position \a x. \a x must be
|
---|
| 326 | given in content coordinates.
|
---|
| 327 |
|
---|
| 328 | \sa rowAt()
|
---|
| 329 | */
|
---|
| 330 |
|
---|
| 331 | /*!
|
---|
| 332 | \fn void Q3GridView::paintCell(QPainter *p, int row, int col)
|
---|
| 333 |
|
---|
| 334 | This pure virtual function is called to paint the single cell at
|
---|
| 335 | (\a row, \a col) using painter \a p. The painter must be open when
|
---|
| 336 | paintCell() is called and must remain open.
|
---|
| 337 |
|
---|
| 338 | The coordinate system is \link QPainter::translate() translated
|
---|
| 339 | \endlink so that the origin is at the top-left corner of the cell
|
---|
| 340 | to be painted, i.e. \e cell coordinates. Do not scale or shear
|
---|
| 341 | the coordinate system (or if you do, restore the transformation
|
---|
| 342 | matrix before you return).
|
---|
| 343 |
|
---|
| 344 | The painter is not clipped by default in order to get maximum
|
---|
| 345 | efficiency. If you want clipping, use
|
---|
| 346 |
|
---|
| 347 | \snippet doc/src/snippets/code/src_qt3support_widgets_q3gridview.cpp 0
|
---|
| 348 | */
|
---|
| 349 |
|
---|
| 350 | /*!
|
---|
| 351 | \fn QRect Q3GridView::cellRect() const
|
---|
| |
---|