1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QPaintEvent>
#include <QPainter>
#include <QStyle>
#include <QStyleOptionFrame>
#include "wellarray.hpp"
namespace pv {
namespace widgets {
void WellArray::paintEvent(QPaintEvent *event)
{
QRect r = event->rect();
int cx = r.x();
int cy = r.y();
int ch = r.height();
int cw = r.width();
int colfirst = columnAt(cx);
int collast = columnAt(cx + cw);
int rowfirst = rowAt(cy);
int rowlast = rowAt(cy + ch);
if (isRightToLeft()) {
int t = colfirst;
colfirst = collast;
collast = t;
}
QPainter painter(this);
QPainter *p = &painter;
QRect rect(0, 0, cellWidth(), cellHeight());
if (collast < 0 || collast >= ncols)
collast = ncols-1;
if (rowlast < 0 || rowlast >= nrows)
rowlast = nrows-1;
// Go through the rows
for (int r = rowfirst; r <= rowlast; ++r) {
// get row position and height
int rowp = rowY(r);
// Go through the columns in the row r
// if we know from where to where, go through [colfirst, collast],
// else go through all of them
for (int c = colfirst; c <= collast; ++c) {
// get position and width of column c
int colp = columnX(c);
// Translate painter and draw the cell
rect.translate(colp, rowp);
paintCell(p, r, c, rect);
rect.translate(-colp, -rowp);
}
}
}
struct WellArrayData {
QBrush *brush;
};
WellArray::WellArray(int rows, int cols, QWidget *parent)
: QWidget(parent)
,nrows(rows), ncols(cols)
{
d = nullptr;
setFocusPolicy(Qt::StrongFocus);
cellw = 28;
cellh = 24;
curCol = 0;
curRow = 0;
selCol = -1;
selRow = -1;
}
QSize WellArray::sizeHint() const
{
ensurePolished();
return gridSize().boundedTo(QSize(640, 480));
}
void WellArray::paintCell(QPainter* p, int row, int col, const QRect &rect)
{
int b = 3; //margin
const QPalette& g = palette();
QStyleOptionFrame opt;
int dfw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
opt.lineWidth = dfw;
opt.midLineWidth = 1;
opt.rect = rect.adjusted(b, b, -b, -b);
opt.palette = g;
opt.state = QStyle::State_Enabled | QStyle::State_Sunken;
style()->drawPrimitive(QStyle::PE_Frame, &opt, p, this);
b += dfw;
if ((row == curRow) && (col == curCol)) {
if (hasFocus()) {
QStyleOptionFocusRect opt;
opt.palette = g;
opt.rect = rect;
opt.state = QStyle::State_None | QStyle::State_KeyboardFocusChange;
style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, p, this);
}
}
paintCellContents(p, row, col, opt.rect.adjusted(dfw, dfw, -dfw, -dfw));
}
/*!
Reimplement this function to change the contents of the well array.
*/
void WellArray::paintCellContents(QPainter *p, int row, int col, const QRect &r)
{
if (d) {
p->fillRect(r, d->brush[row*numCols()+col]);
} else {
p->fillRect(r, Qt::white);
p->setPen(Qt::black);
p->drawLine(r.topLeft(), r.bottomRight());
p->drawLine(r.topRight(), r.bottomLeft());
}
}
void WellArray::mousePressEvent(QMouseEvent *event)
{
// The current cell marker is set to the cell the mouse is pressed in
QPoint pos = event->pos();
setCurrent(rowAt(pos.y()), columnAt(pos.x()));
}
void WellArray::mouseReleaseEvent(QMouseEvent * /* event */)
{
// The current cell marker is set to the cell the mouse is clicked in
setSelected(curRow, curCol);
}
/*
Sets the cell currently having the focus. This is not necessarily
the same as the currently selected cell.
*/
void WellArray::setCurrent(int row, int col)
{
if ((curRow == row) && (curCol == col))
return;
if (row < 0 || col < 0)
row = col = -1;
int oldRow = curRow;
int oldCol = curCol;
curRow = row;
curCol = col;
updateCell(oldRow, oldCol);
updateCell(curRow, curCol);
}
/*
Sets the currently selected cell to \a row, \a column. If \a row or
\a column are less than zero, the current cell is unselected.
Does not set the position of the focus indicator.
*/
void WellArray::setSelected(int row, int col)
{
int oldRow = selRow;
int oldCol = selCol;
if (row < 0 || col < 0)
row = col = -1;
selCol = col;
selRow = row;
updateCell(oldRow, oldCol);
updateCell(selRow, selCol);
if (row >= 0)
selected(row, col);
}
void WellArray::focusInEvent(QFocusEvent*)
{
updateCell(curRow, curCol);
}
void WellArray::setCellBrush(int row, int col, const QBrush &b)
{
if (!d) {
d = new WellArrayData;
int i = numRows()*numCols();
d->brush = new QBrush[i];
}
if (row >= 0 && row < numRows() && col >= 0 && col < numCols())
d->brush[row*numCols()+col] = b;
}
/*
Returns the brush set for the cell at \a row, \a column. If no brush is
set, Qt::NoBrush is returned.
*/
QBrush WellArray::cellBrush(int row, int col)
{
if (d && row >= 0 && row < numRows() && col >= 0 && col < numCols())
return d->brush[row*numCols()+col];
return Qt::NoBrush;
}
/*!\reimp
*/
void WellArray::focusOutEvent(QFocusEvent*)
{
updateCell(curRow, curCol);
}
/*\reimp
*/
void WellArray::keyPressEvent(QKeyEvent* event)
{
switch (event->key()) { // Look at the key code
case Qt::Key_Left: // If 'left arrow'-key,
if (curCol > 0) // and cr't not in leftmost col
setCurrent(curRow, curCol - 1); // set cr't to next left column
break;
case Qt::Key_Right: // Correspondingly...
if (curCol < numCols()-1)
setCurrent(curRow, curCol + 1);
break;
case Qt::Key_Up:
if (curRow > 0)
setCurrent(curRow - 1, curCol);
break;
case Qt::Key_Down:
if (curRow < numRows()-1)
setCurrent(curRow + 1, curCol);
break;
case Qt::Key_Space:
setSelected(curRow, curCol);
break;
default: // If not an interesting key,
event->ignore(); // we don't accept the event
return;
}
}
} // namespace widgets
} // namespace pv
|