4 * Original file taken from klyx 0.10 sources:
6 * \author Kalle Dalheimer
8 * Full author contact details are available in file CREDITS.
13 #include "QBrowseBox.h"
17 #include <QApplication>
18 #include <qdrawutil.h>
20 #include <QMouseEvent>
23 #include <QCloseEvent>
25 #include <QDesktopWidget>
30 QBrowseBox::QBrowseBox(int rows, int cols, QWidget* parent, const char * name, Qt::WFlags f)
31 : Q3GridView(parent,name,f)
36 pixmaps_ = new QPixmap[rows * cols];
42 setFrameStyle(QFrame::WinPanel | QFrame::Raised);
44 setVScrollBarMode(Q3ScrollView::AlwaysOff);
45 setHScrollBarMode(Q3ScrollView::AlwaysOff);
47 viewport()->setFocusPolicy(Qt::StrongFocus);
48 // setMouseTracking must be called after setFocusPolicy
49 viewport()->setMouseTracking(true);
55 QBrowseBox::~QBrowseBox()
61 int QBrowseBox::coordsToIndex(int row, int col)
63 if (col < 0 || col > numCols() || row < 0 || row > numRows())
64 qDebug("coordsToIndex: invalid coords (%d, %d)\n", row, col);
65 return row + col * numCols();
69 void QBrowseBox::insertItem(QPixmap pixmap, int row, int col)
71 pixmaps_[coordsToIndex(row, col)] = pixmap;
75 void QBrowseBox::insertItem(QPixmap pixmap)
77 int w = pixmap.width() / numCols();
78 int h = pixmap.height() / numRows();
80 for (int row = 0; row < numRows(); ++row) {
81 for (int col = 0; col < numCols(); ++col) {
83 small.fill(backgroundColor());
84 bitBlt(&small, 0, 0, &pixmap, col * w, row * h,
86 insertItem(small, row, col);
90 setCellWidth(pixmap.width() / numCols());
91 setCellHeight(pixmap.height() / numRows());
92 setMinimumWidth(pixmap.width() + (numCols() + 1) * 1);
93 setMinimumHeight(pixmap.height() + (numRows() + 1) * 1);
94 resize(minimumSize());
98 QPixmap QBrowseBox::pixmap(int row, int col)
100 if (col < 0 || col >= numCols() || row < 0 || row >= numRows())
102 return pixmaps_[coordsToIndex(row, col)];
106 int QBrowseBox::exec(const QPoint & pos)
108 return exec(pos.x(),pos.y());
112 int QBrowseBox::exec(const QWidget * trigger)
114 QPoint globalpos = trigger->parentWidget()->mapToGlobal( trigger->pos());
115 // is there enough space to put the box below the trigger?
116 if ( globalpos.y() + trigger->height()+height()+1<
117 QApplication::desktop()->height()) {
118 // is there enough space to set the box left-justified with the trigger
119 if (globalpos.x()+width()<QApplication::desktop()->width())
120 return exec(globalpos.x(),
121 globalpos.y()+trigger->height()+1);
123 return exec(globalpos.x()-width()-1,
124 globalpos.y()+trigger->height()+1);
126 if (globalpos.x()+width()<QApplication::desktop()->width())
127 return exec(globalpos.x(),
128 globalpos.y()-height()-1);
130 return exec(globalpos.x()-width()-1,
131 globalpos.y()-height()-1);
136 int QBrowseBox::exec(int x,int y)
138 firstrelease_ = true;
145 if (activecell_.x()!=-1 && activecell_.y()!=-1 )
146 return coordsToIndex( activecell_.x(),activecell_.y());
152 void QBrowseBox::keyPressEvent(QKeyEvent * e)
168 emit selected(activecell_.x(), activecell_.y());
169 if ( isVisible() && parentWidget() &&
170 parentWidget()->inherits("QPopupMenu") )
171 parentWidget()->close();
178 if ( isVisible() && parentWidget() &&
179 parentWidget()->inherits("QPopupMenu") )
180 parentWidget()->close();
187 void QBrowseBox::contentsMouseReleaseEvent(QMouseEvent *)
191 firstrelease_ = false;
193 emit selected( activecell_.x(), activecell_.y());
194 if ( isVisible() && parentWidget() &&
195 parentWidget()->inherits("QPopupMenu") )
196 parentWidget()->close();
201 void QBrowseBox::closeEvent(QCloseEvent * e)
208 void QBrowseBox::paintCell(QPainter * painter, int row, int col)
210 painter->setClipRect(cellGeometry(row, col));
212 int const index = coordsToIndex(row, col);
214 painter->drawPixmap(0, 0, pixmaps_[index]);
216 if (activecell_.x() == row && activecell_.y() == col) {
217 qDrawShadeRect(painter, 0, 0, cellWidth(),
218 cellHeight(), colorGroup(), false, 1);
220 qDrawPlainRect(painter, 0, 0, cellWidth(),
221 cellHeight(), colorGroup().background(), 1);
224 painter->setClipping(false);
228 void QBrowseBox::contentsMouseMoveEvent(QMouseEvent * e)
230 int x = e->pos().x();
231 int y = e->pos().y();
236 if (x < 0 || y < 0 || x > width() || y > height()) {
241 celly = (int)floor( ((double)x) / ((double)cellWidth()) );
242 cellx = (int)floor( ((double)y) / ((double)cellHeight()) );
245 if (activecell_.x() != cellx || activecell_.y() != celly) {
246 // mouse has been moved to another cell
247 int oldactivecellx = activecell_.x();
248 int oldactivecelly = activecell_.y();
249 activecell_.setX(cellx);
250 activecell_.setY(celly);
251 // remove old highlighting
252 updateCell(oldactivecellx, oldactivecelly);
253 // set new highlighting
254 updateCell(activecell_.x(), activecell_.y());
259 void QBrowseBox::moveLeft()
261 int const y = activecell_.y();
264 activecell_.setY(y - 1);
265 else if (parentWidget())
266 QWidget::focusNextPrevChild(false);
268 updateCell(activecell_.x(), y);
269 updateCell(activecell_.x(), activecell_.y());
273 void QBrowseBox::moveRight()
275 int const y = activecell_.y();
277 if (y < numCols() - 1)
278 activecell_.setY(y+1);
280 updateCell(activecell_.x(), y);
281 updateCell(activecell_.x(), activecell_.y());
285 void QBrowseBox::moveUp()
287 int const x = activecell_.x();
290 activecell_.setX(x - 1);
291 else if (parentWidget())
292 QWidget::focusNextPrevChild(false);
294 updateCell(x, activecell_.y());
295 updateCell(activecell_.x(), activecell_.y());
299 void QBrowseBox::moveDown()
301 int const x = activecell_.x();
303 if (x < numRows() - 1)
304 activecell_.setX(x + 1);
306 updateCell(x, activecell_.y());
307 updateCell(activecell_.x(), activecell_.y());