4 * Original file taken from klyx 0.10 sources:
6 * \author Kalle Dalheimer
8 * Full author contact details are available in file CREDITS
17 #include <qapplication.h>
18 #include <qdrawutil.h>
22 #include "QBrowseBox.h"
28 QBrowseBox::QBrowseBox(int rows, int cols, QWidget* parent, const char* name, WFlags f)
29 : QGridView(parent,name,f)
33 setCellWidth(width()/cols);
34 setCellHeight(height()/rows);
36 texts_ = new QString[rows * cols];
37 pixmaps_ = new QPixmap[rows * cols];
42 if (style().inherits("QWindowsStyle"))
43 setFrameStyle(QFrame::WinPanel | QFrame::Raised);
45 setFrameStyle(QFrame::Panel | QFrame::Raised);
47 viewport()->setFocusPolicy(QWidget::StrongFocus);
48 // setMouseTracking must be called after setFocusPolicy
49 viewport()->setMouseTracking(true);
56 QBrowseBox::~QBrowseBox()
63 int QBrowseBox::coordsToIndex(int row, int col)
65 if (col < 0 || col > numCols() || row < 0 || row > numRows())
66 qDebug("coordsToIndex: invalid coords (%d, %d)\n", row, col);
67 return row + col * numCols();
71 void QBrowseBox::insertItem(QString const & text, int row, int col)
73 texts_[coordsToIndex(row, col)] = text;
77 void QBrowseBox::insertItem(char const * text, int row, int col)
79 insertItem(QString(text), row, col);
83 void QBrowseBox::insertItem(QPixmap pixmap, int row, int col)
85 pixmaps_[coordsToIndex(row, col)] = pixmap;
89 void QBrowseBox::insertItem( QPixmap pixmap)
91 int w = pixmap.width() / numCols();
92 int h = pixmap.height() / numRows();
94 for (int row = 0; row < numRows(); ++row)
95 for (int col = 0; col < numCols(); ++col) {
97 bitBlt(&small,0,0,&pixmap,col*w,row*h,w,h,Qt::CopyROP,false);
98 insertItem(small, row, col);
101 resize(pixmap.width() + (numCols() + 1) * frameWidth(),
102 pixmap.height() + (numRows() + 1) * frameWidth());
106 void QBrowseBox::removeItem(int row, int col)
108 texts_[coordsToIndex(row, col)] = "";
109 pixmaps_[coordsToIndex(row, col)].resize(0, 0);
113 void QBrowseBox::clear()
115 for (int row = 0; row < numRows(); ++row)
116 for (int col = 0; col < numCols(); ++col)
117 removeItem(row, col);
121 QString QBrowseBox::text(int row, int col)
123 if (col < 0 || col >= numCols() || row < 0 || row >= numRows())
125 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)];
135 int QBrowseBox::exec(const QPoint& pos)
137 return exec(pos.x(),pos.y());
140 int QBrowseBox::exec(const QWidget* trigger)
142 QPoint globalpos = trigger->parentWidget()->mapToGlobal( trigger->pos());
143 // is there enough space to put the box below the trigger?
144 if ( globalpos.y() + trigger->height()+height()+1<
145 QApplication::desktop()->height()) {
146 // is there enough space to set the box left-justified with the trigger
147 if (globalpos.x()+width()<QApplication::desktop()->width())
148 return exec(globalpos.x(),
149 globalpos.y()+trigger->height()+1);
151 return exec(globalpos.x()-width()-1,
152 globalpos.y()+trigger->height()+1);
154 if (globalpos.x()+width()<QApplication::desktop()->width())
155 return exec(globalpos.x(),
156 globalpos.y()-height()-1);
158 return exec(globalpos.x()-width()-1,
159 globalpos.y()-height()-1);
163 int QBrowseBox::exec(int x,int y)
165 firstrelease_ = true;
172 if (activecell_.x()!=-1 && activecell_.y()!=-1 )
173 return coordsToIndex( activecell_.x(),activecell_.y());
178 void QBrowseBox::keyPressEvent(QKeyEvent * e)
194 emit selected(activecell_.x(), activecell_.y());
195 if ( isVisible() && parentWidget() &&
196 parentWidget()->inherits("QPopupMenu") )
197 parentWidget()->close();
204 if ( isVisible() && parentWidget() &&
205 parentWidget()->inherits("QPopupMenu") )
206 parentWidget()->close();
212 void QBrowseBox::contentsMouseReleaseEvent(QMouseEvent *)
216 firstrelease_ = false;
218 emit selected( activecell_.x(), activecell_.y());
219 if ( isVisible() && parentWidget() &&
220 parentWidget()->inherits("QPopupMenu") )
221 parentWidget()->close();
225 void QBrowseBox::closeEvent(QCloseEvent * e)
231 void QBrowseBox::paintCell(QPainter * painter, int row, int col)
233 painter->setClipRect(cellGeometry(row,col));//, QPainter::CoordPainter);
234 bool ispixmap = false;
236 if (!pixmaps_[coordsToIndex(row,col)].isNull()) {
237 painter->drawPixmap(0,0,pixmaps_[coordsToIndex(row, col)]);
241 if (activecell_.x() == row && activecell_.y() == col) {
243 qDrawShadeRect(painter, 0, 0, cellWidth(),
244 cellHeight(), colorGroup(), false, 1);
246 qDrawShadePanel(painter, 0, 0, cellWidth(),
247 cellHeight(), colorGroup(), false, 1);
249 qDrawPlainRect(painter, 0, 0, cellWidth(),
250 cellHeight(), colorGroup().background(), 1);
253 if (!texts_[coordsToIndex(row, col)].isEmpty()) {
254 painter->drawText(0, 0, cellWidth(),
255 cellHeight(), AlignCenter,
256 texts_[coordsToIndex(row, col)]);
258 painter->setClipping(false);
262 void QBrowseBox::resizeEvent(QResizeEvent * e)
264 QGridView::resizeEvent(e);
265 setCellWidth(contentsRect().width() / numCols());
266 setCellHeight(contentsRect().height() / numRows());
270 void QBrowseBox::contentsMouseMoveEvent(QMouseEvent * e)
272 int x = e->pos().x();
273 int y = e->pos().y();
278 if (x < 0 || y < 0 || x > width() || y > height()) {
283 celly = (int)floor( ((double)x) / ((double)cellWidth()) );
284 cellx = (int)floor( ((double)y) / ((double)cellHeight()) );
287 if (activecell_.x() != cellx || activecell_.y() != celly) {
288 // mouse has been moved to another cell
289 int oldactivecellx = activecell_.x();
290 int oldactivecelly = activecell_.y();
291 activecell_.setX(cellx);
292 activecell_.setY(celly);
293 // remove old highlighting
294 updateCell(oldactivecellx, oldactivecelly);
295 // set new highlighting
296 updateCell(activecell_.x(), activecell_.y());
301 void QBrowseBox::moveLeft()
303 int const y = activecell_.y();
306 activecell_.setY(y - 1);
307 else if (parentWidget())
308 QWidget::focusNextPrevChild(false);
310 updateCell(activecell_.x(), y);
311 updateCell(activecell_.x(), activecell_.y());
315 void QBrowseBox::moveRight()
317 int const y = activecell_.y();
319 if (y < numCols() - 1)
320 activecell_.setY(y+1);
322 updateCell(activecell_.x(), y);
323 updateCell(activecell_.x(), activecell_.y());
327 void QBrowseBox::moveUp()
329 int const x = activecell_.x();
332 activecell_.setX(x - 1);
333 else if (parentWidget())
334 QWidget::focusNextPrevChild(false);
336 updateCell(x, activecell_.y());
337 updateCell(activecell_.x(), activecell_.y());
341 void QBrowseBox::moveDown()
343 int const x = activecell_.x();
345 if (x < numRows() - 1)
346 activecell_.setX(x + 1);
348 updateCell(x, activecell_.y());
349 updateCell(activecell_.x(), activecell_.y());