4 * Original file taken from klyx 0.10 sources:
5 * $Id: QBrowseBox.C,v 1.6 2002/10/20 01:48:27 larsbj Exp $
7 * \author Kalle Dalheimer ?
9 * Full author contact details are available in file CREDITS
18 #include <qapplication.h>
19 #include <qdrawutil.h>
23 #include "QBrowseBox.h"
29 QBrowseBox::QBrowseBox(int rows, int cols)
34 setCellWidth(width()/cols);
35 setCellHeight(height()/rows);
37 texts_ = new QString[rows * cols];
38 pixmaps_ = new QPixmap[rows * cols];
43 setMouseTracking(true);
45 if (style().inherits("QWindowsStyle"))
46 setFrameStyle(QFrame::WinPanel | QFrame::Raised);
48 setFrameStyle(QFrame::Panel | QFrame::Raised);
50 setFocusPolicy(QWidget::StrongFocus);
55 QBrowseBox::~QBrowseBox()
62 int QBrowseBox::coordsToIndex(int row, int col)
64 if (col < 0 || col > numCols() || row < 0 || row > numRows())
65 qDebug("coordsToIndex: invalid coords (%d, %d)\n", row, col);
66 return row + col * numCols();
70 void QBrowseBox::insertItem(QString const & text, int row, int col)
72 texts_[coordsToIndex(row, col)] = text;
76 void QBrowseBox::insertItem(char const * text, int x, int y)
78 insertItem(QString(text), x, y);
82 void QBrowseBox::insertItem(QPixmap pixmap, int row, int col)
84 pixmaps_[coordsToIndex(row, col)] = pixmap;
88 void QBrowseBox::insertItem( QPixmap pixmap)
90 int w = pixmap.width() / numCols();
91 int h = pixmap.height() / numRows();
93 for (int row = 0; row < numRows(); ++row)
94 for (int col = 0; col < numCols(); ++col) {
96 bitBlt(&small,0,0,&pixmap,col*w,row*h,w,h,Qt::CopyROP,false);
97 insertItem(small, row, col);
100 resize(pixmap.width() + (numCols() + 1) * frameWidth(),
101 pixmap.height() + (numRows() + 1) * frameWidth());
105 void QBrowseBox::removeItem(int row, int col)
107 texts_[coordsToIndex(row, col)] = "";
108 pixmaps_[coordsToIndex(row, col)].resize(0, 0);
112 void QBrowseBox::clear()
114 for (int row = 0; row < numRows(); ++row)
115 for (int col = 0; col < numCols(); ++col)
116 removeItem(row, col);
120 QString QBrowseBox::text(int row, int col)
122 if (col < 0 || col >= numCols() || row < 0 || row >= numRows())
124 return texts_[coordsToIndex(row, col)];
128 QPixmap QBrowseBox::pixmap(int row, int col)
130 if (col < 0 || col >= numCols() || row < 0 || row >= numRows())
132 return pixmaps_[coordsToIndex(row, col)];
136 void QBrowseBox::keyPressEvent(QKeyEvent * e)
152 emit selected(activecell_.x(), activecell_.y());
160 void QBrowseBox::mouseReleaseEvent(QMouseEvent *)
162 qWarning("mouse release");
163 emit selected( activecell_.x(), activecell_.y());
167 //void QBrowseBox::closeEvent(QCloseEvent * e)
170 // qApp->exit_loop();
173 void QBrowseBox::paintCell(QPainter * painter, int row, int col)
175 painter->setClipRect(cellGeometry(row,col));//, QPainter::CoordPainter);
176 bool ispixmap = false;
178 if (!pixmaps_[coordsToIndex(row,col)].isNull()) {
179 painter->drawPixmap(0,0,pixmaps_[coordsToIndex(row, col)]);
183 if (activecell_.x() == row && activecell_.y() == col) {
185 qDrawShadeRect(painter, 0, 0, cellWidth(),
186 cellHeight(), colorGroup(), false, 1);
188 qDrawShadePanel(painter, 0, 0, cellWidth(),
189 cellHeight(), colorGroup(), false, 1);
191 qDrawPlainRect(painter, 0, 0, cellWidth(),
192 cellHeight(), colorGroup().background(), 1);
195 if (!texts_[coordsToIndex(row, col)].isEmpty()) {
196 painter->drawText(0, 0, cellWidth(),
197 cellHeight(), AlignLeft,
198 texts_[coordsToIndex(row, col)]);
200 painter->setClipping(false);
204 void QBrowseBox::resizeEvent(QResizeEvent * e)
206 QGridView::resizeEvent(e);
207 setCellWidth(contentsRect().width() / numCols());
208 setCellHeight(contentsRect().height() / numRows());
212 void QBrowseBox::mouseMoveEvent(QMouseEvent * e)
214 qWarning("mouseMoveEvent");
215 int x = e->pos().x();
216 int y = e->pos().y();
221 if (x < 0 || y < 0 || x > width() || y > height()) {
227 #warning Is the switch of x and y intended here? (Andre)
229 celly = (int)floor( ((double)x) / ((double)cellWidth()) );
230 cellx = (int)floor( ((double)y) / ((double)cellHeight()) );
233 if (activecell_.x() != cellx || activecell_.y() != celly) {
235 // mouse has been moved to another cell
236 int oldactivecellx = activecell_.x();
237 int oldactivecelly = activecell_.y();
238 activecell_.setX(cellx);
239 activecell_.setY(celly);
240 // remove old highlighting
241 updateCell(oldactivecellx, oldactivecelly);
242 // set new highlighting
243 updateCell(activecell_.x(), activecell_.y());
248 void QBrowseBox::moveLeft()
250 int const y = activecell_.y();
253 activecell_.setY(y - 1);
255 updateCell(activecell_.x(), y);
256 updateCell(activecell_.x(), activecell_.y());
260 void QBrowseBox::moveRight()
262 int const y = activecell_.y();
264 if (y < numCols() - 1)
265 activecell_.setY(y+1);
267 updateCell(activecell_.x(), y);
268 updateCell(activecell_.x(), activecell_.y());
272 void QBrowseBox::moveUp()
274 int const x = activecell_.x();
277 activecell_.setX(x - 1);
279 updateCell(x, activecell_.y());
280 updateCell(activecell_.x(), activecell_.y());
284 void QBrowseBox::moveDown()
286 int const x = activecell_.x();
288 if (x < numRows() - 1)
289 activecell_.setX(x + 1);
291 updateCell(x, activecell_.y());
292 updateCell(activecell_.x(), activecell_.y());