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)
164 emit selected(activecell_.x(), activecell_.y());
165 if ( isVisible() && parentWidget() &&
166 parentWidget()->inherits("QPopupMenu") )
167 parentWidget()->close();
174 if ( isVisible() && parentWidget() &&
175 parentWidget()->inherits("QPopupMenu") )
176 parentWidget()->close();
183 void QBrowseBox::contentsMouseReleaseEvent(QMouseEvent *)
187 firstrelease_ = false;
189 emit selected( activecell_.x(), activecell_.y());
190 if ( isVisible() && parentWidget() &&
191 parentWidget()->inherits("QPopupMenu") )
192 parentWidget()->close();
197 void QBrowseBox::closeEvent(QCloseEvent * e)
204 void QBrowseBox::paintCell(QPainter * painter, int row, int col)
206 painter->setClipRect(cellGeometry(row, col));
208 int const index = coordsToIndex(row, col);
210 painter->drawPixmap(0, 0, pixmaps_[index]);
212 if (activecell_.x() == row && activecell_.y() == col) {
213 qDrawShadeRect(painter, 0, 0, cellWidth(),
214 cellHeight(), colorGroup(), false, 1);
216 qDrawPlainRect(painter, 0, 0, cellWidth(),
217 cellHeight(), colorGroup().background(), 1);
220 painter->setClipping(false);
224 void QBrowseBox::contentsMouseMoveEvent(QMouseEvent * e)
226 int x = e->pos().x();
227 int y = e->pos().y();
232 if (x < 0 || y < 0 || x > width() || y > height()) {
237 celly = (int)floor( ((double)x) / ((double)cellWidth()) );
238 cellx = (int)floor( ((double)y) / ((double)cellHeight()) );
241 if (activecell_.x() != cellx || activecell_.y() != celly) {
242 // mouse has been moved to another cell
243 int oldactivecellx = activecell_.x();
244 int oldactivecelly = activecell_.y();
245 activecell_.setX(cellx);
246 activecell_.setY(celly);
247 // remove old highlighting
248 updateCell(oldactivecellx, oldactivecelly);
249 // set new highlighting
250 updateCell(activecell_.x(), activecell_.y());
255 void QBrowseBox::moveLeft()
257 int const y = activecell_.y();
260 activecell_.setY(y - 1);
261 else if (parentWidget())
262 QWidget::focusNextPrevChild(false);
264 updateCell(activecell_.x(), y);
265 updateCell(activecell_.x(), activecell_.y());
269 void QBrowseBox::moveRight()
271 int const y = activecell_.y();
273 if (y < numCols() - 1)
274 activecell_.setY(y+1);
276 updateCell(activecell_.x(), y);
277 updateCell(activecell_.x(), activecell_.y());
281 void QBrowseBox::moveUp()
283 int const x = activecell_.x();
286 activecell_.setX(x - 1);
287 else if (parentWidget())
288 QWidget::focusNextPrevChild(false);
290 updateCell(x, activecell_.y());
291 updateCell(activecell_.x(), activecell_.y());
295 void QBrowseBox::moveDown()
297 int const x = activecell_.x();
299 if (x < numRows() - 1)
300 activecell_.setX(x + 1);
302 updateCell(x, activecell_.y());
303 updateCell(activecell_.x(), activecell_.y());