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.h>
18 #include <qdrawutil.h>
24 QBrowseBox::QBrowseBox(int rows, int cols, QWidget* parent, const char * name, WFlags f)
25 : QGridView(parent,name,f)
30 pixmaps_ = new QPixmap[rows * cols];
35 if (style().inherits("QWindowsStyle"))
36 setFrameStyle(QFrame::WinPanel | QFrame::Raised);
38 setFrameStyle(QFrame::Panel | QFrame::Raised);
40 setVScrollBarMode(QScrollView::AlwaysOff);
41 setHScrollBarMode(QScrollView::AlwaysOff);
43 viewport()->setFocusPolicy(QWidget::StrongFocus);
44 // setMouseTracking must be called after setFocusPolicy
45 viewport()->setMouseTracking(true);
51 QBrowseBox::~QBrowseBox()
57 int QBrowseBox::coordsToIndex(int row, int col)
59 if (col < 0 || col > numCols() || row < 0 || row > numRows())
60 qDebug("coordsToIndex: invalid coords (%d, %d)\n", row, col);
61 return row + col * numCols();
65 void QBrowseBox::insertItem(QPixmap pixmap, int row, int col)
67 pixmaps_[coordsToIndex(row, col)] = pixmap;
71 void QBrowseBox::insertItem(QPixmap pixmap)
73 int w = pixmap.width() / numCols();
74 int h = pixmap.height() / numRows();
76 for (int row = 0; row < numRows(); ++row) {
77 for (int col = 0; col < numCols(); ++col) {
79 small.fill(backgroundColor());
80 bitBlt(&small, 0, 0, &pixmap, col * w, row * h,
81 w, h, Qt::CopyROP, false);
82 insertItem(small, row, col);
86 setCellWidth(pixmap.width() / numCols());
87 setCellHeight(pixmap.height() / numRows());
88 setMinimumWidth(pixmap.width() + (numCols() + 1) * 1);
89 setMinimumHeight(pixmap.height() + (numRows() + 1) * 1);
90 resize(minimumSize());
94 QPixmap QBrowseBox::pixmap(int row, int col)
96 if (col < 0 || col >= numCols() || row < 0 || row >= numRows())
98 return pixmaps_[coordsToIndex(row, col)];
102 int QBrowseBox::exec(const QPoint & pos)
104 return exec(pos.x(),pos.y());
108 int QBrowseBox::exec(const QWidget * trigger)
110 QPoint globalpos = trigger->parentWidget()->mapToGlobal( trigger->pos());
111 // is there enough space to put the box below the trigger?
112 if ( globalpos.y() + trigger->height()+height()+1<
113 QApplication::desktop()->height()) {
114 // is there enough space to set the box left-justified with the trigger
115 if (globalpos.x()+width()<QApplication::desktop()->width())
116 return exec(globalpos.x(),
117 globalpos.y()+trigger->height()+1);
119 return exec(globalpos.x()-width()-1,
120 globalpos.y()+trigger->height()+1);
122 if (globalpos.x()+width()<QApplication::desktop()->width())
123 return exec(globalpos.x(),
124 globalpos.y()-height()-1);
126 return exec(globalpos.x()-width()-1,
127 globalpos.y()-height()-1);
132 int QBrowseBox::exec(int x,int y)
134 firstrelease_ = true;
141 if (activecell_.x()!=-1 && activecell_.y()!=-1 )
142 return coordsToIndex( activecell_.x(),activecell_.y());
148 void QBrowseBox::keyPressEvent(QKeyEvent * e)
165 selected(activecell_.x(), activecell_.y());
166 if ( isVisible() && parentWidget() &&
167 parentWidget()->inherits("QPopupMenu") )
168 parentWidget()->close();
175 if ( isVisible() && parentWidget() &&
176 parentWidget()->inherits("QPopupMenu") )
177 parentWidget()->close();
184 void QBrowseBox::contentsMouseReleaseEvent(QMouseEvent *)
188 firstrelease_ = false;
190 selected( activecell_.x(), activecell_.y());
191 if ( isVisible() && parentWidget() &&
192 parentWidget()->inherits("QPopupMenu") )
193 parentWidget()->close();
198 void QBrowseBox::closeEvent(QCloseEvent * e)
205 void QBrowseBox::paintCell(QPainter * painter, int row, int col)
207 painter->setClipRect(cellGeometry(row, col));
209 int const index = coordsToIndex(row, col);
211 painter->drawPixmap(0, 0, pixmaps_[index]);
213 if (activecell_.x() == row && activecell_.y() == col) {
214 qDrawShadeRect(painter, 0, 0, cellWidth(),
215 cellHeight(), colorGroup(), false, 1);
217 qDrawPlainRect(painter, 0, 0, cellWidth(),
218 cellHeight(), colorGroup().background(), 1);
221 painter->setClipping(false);
225 void QBrowseBox::contentsMouseMoveEvent(QMouseEvent * e)
227 int x = e->pos().x();
228 int y = e->pos().y();
233 if (x < 0 || y < 0 || x > width() || y > height()) {
238 celly = (int)floor( ((double)x) / ((double)cellWidth()) );
239 cellx = (int)floor( ((double)y) / ((double)cellHeight()) );
242 if (activecell_.x() != cellx || activecell_.y() != celly) {
243 // mouse has been moved to another cell
244 int oldactivecellx = activecell_.x();
245 int oldactivecelly = activecell_.y();
246 activecell_.setX(cellx);
247 activecell_.setY(celly);
248 // remove old highlighting
249 updateCell(oldactivecellx, oldactivecelly);
250 // set new highlighting
251 updateCell(activecell_.x(), activecell_.y());
256 void QBrowseBox::moveLeft()
258 int const y = activecell_.y();
261 activecell_.setY(y - 1);
262 else if (parentWidget())
263 QWidget::focusNextPrevChild(false);
265 updateCell(activecell_.x(), y);
266 updateCell(activecell_.x(), activecell_.y());
270 void QBrowseBox::moveRight()
272 int const y = activecell_.y();
274 if (y < numCols() - 1)
275 activecell_.setY(y+1);
277 updateCell(activecell_.x(), y);
278 updateCell(activecell_.x(), activecell_.y());
282 void QBrowseBox::moveUp()
284 int const x = activecell_.x();
287 activecell_.setX(x - 1);
288 else if (parentWidget())
289 QWidget::focusNextPrevChild(false);
291 updateCell(x, activecell_.y());
292 updateCell(activecell_.x(), activecell_.y());
296 void QBrowseBox::moveDown()
298 int const x = activecell_.x();
300 if (x < numRows() - 1)
301 activecell_.setX(x + 1);
303 updateCell(x, activecell_.y());
304 updateCell(activecell_.x(), activecell_.y());