2002-10-09 08:59:02 +00:00
|
|
|
/**
|
|
|
|
* \file QBrowseBox.C
|
|
|
|
*
|
|
|
|
* Original file taken from klyx 0.10 sources:
|
|
|
|
*
|
2002-11-08 00:09:00 +00:00
|
|
|
* \author Kalle Dalheimer
|
2002-10-09 08:59:02 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-10-09 08:59:02 +00:00
|
|
|
*/
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
#include <config.h>
|
2002-10-09 08:59:02 +00:00
|
|
|
|
|
|
|
#include <qpixmap.h>
|
|
|
|
#include <qpainter.h>
|
|
|
|
#include <qapplication.h>
|
|
|
|
#include <qdrawutil.h>
|
|
|
|
#include <qstyle.h>
|
|
|
|
|
|
|
|
#include "QBrowseBox.h"
|
|
|
|
|
2003-09-06 10:16:15 +00:00
|
|
|
#include <cmath>
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
|
2003-06-28 01:23:11 +00:00
|
|
|
QBrowseBox::QBrowseBox(int rows, int cols, QWidget* parent, const char * name, WFlags f)
|
2002-11-08 00:09:00 +00:00
|
|
|
: QGridView(parent,name,f)
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
setNumRows(rows);
|
|
|
|
setNumCols(cols);
|
2002-10-09 08:59:02 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
pixmaps_ = new QPixmap[rows * cols];
|
2002-10-09 08:59:02 +00:00
|
|
|
|
2002-11-08 00:09:00 +00:00
|
|
|
activecell_.setX(-1);
|
|
|
|
activecell_.setY(-1);
|
2002-10-09 08:59:02 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
if (style().inherits("QWindowsStyle"))
|
|
|
|
setFrameStyle(QFrame::WinPanel | QFrame::Raised);
|
|
|
|
else
|
|
|
|
setFrameStyle(QFrame::Panel | QFrame::Raised);
|
2002-10-09 08:59:02 +00:00
|
|
|
|
2003-03-29 15:28:58 +00:00
|
|
|
setVScrollBarMode(QScrollView::AlwaysOff);
|
|
|
|
setHScrollBarMode(QScrollView::AlwaysOff);
|
|
|
|
|
2002-11-12 09:21:14 +00:00
|
|
|
viewport()->setFocusPolicy(QWidget::StrongFocus);
|
2002-11-08 00:09:00 +00:00
|
|
|
// setMouseTracking must be called after setFocusPolicy
|
2002-11-12 09:21:14 +00:00
|
|
|
viewport()->setMouseTracking(true);
|
2002-11-08 00:09:00 +00:00
|
|
|
inloop=false;
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QBrowseBox::~QBrowseBox()
|
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
delete [] pixmaps_;
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
int QBrowseBox::coordsToIndex(int row, int col)
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
if (col < 0 || col > numCols() || row < 0 || row > numRows())
|
|
|
|
qDebug("coordsToIndex: invalid coords (%d, %d)\n", row, col);
|
|
|
|
return row + col * numCols();
|
|
|
|
}
|
2002-10-09 08:59:02 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
|
|
|
|
void QBrowseBox::insertItem(QPixmap pixmap, int row, int col)
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
pixmaps_[coordsToIndex(row, col)] = pixmap;
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
|
2003-03-29 15:28:58 +00:00
|
|
|
void QBrowseBox::insertItem(QPixmap pixmap)
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
int w = pixmap.width() / numCols();
|
|
|
|
int h = pixmap.height() / numRows();
|
|
|
|
|
2003-03-29 15:28:58 +00:00
|
|
|
for (int row = 0; row < numRows(); ++row) {
|
2002-10-17 09:51:18 +00:00
|
|
|
for (int col = 0; col < numCols(); ++col) {
|
|
|
|
QPixmap small(w,h);
|
2003-03-29 16:43:40 +00:00
|
|
|
small.fill(backgroundColor());
|
|
|
|
bitBlt(&small, 0, 0, &pixmap, col * w, row * h,
|
|
|
|
w, h, Qt::CopyROP, false);
|
2002-10-17 09:51:18 +00:00
|
|
|
insertItem(small, row, col);
|
|
|
|
}
|
2003-03-29 15:28:58 +00:00
|
|
|
}
|
2002-10-17 09:51:18 +00:00
|
|
|
|
2003-03-29 15:28:58 +00:00
|
|
|
setCellWidth(pixmap.width() / numCols());
|
|
|
|
setCellHeight(pixmap.height() / numRows());
|
|
|
|
setMinimumWidth(pixmap.width() + (numCols() + 1) * 1);
|
|
|
|
setMinimumHeight(pixmap.height() + (numRows() + 1) * 1);
|
|
|
|
resize(minimumSize());
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-15 18:29:01 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
QPixmap QBrowseBox::pixmap(int row, int col)
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
if (col < 0 || col >= numCols() || row < 0 || row >= numRows())
|
|
|
|
return QPixmap();
|
|
|
|
return pixmaps_[coordsToIndex(row, col)];
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
2003-03-29 15:28:58 +00:00
|
|
|
|
2003-06-28 01:23:11 +00:00
|
|
|
int QBrowseBox::exec(const QPoint & pos)
|
2002-11-08 00:09:00 +00:00
|
|
|
{
|
|
|
|
return exec(pos.x(),pos.y());
|
|
|
|
}
|
|
|
|
|
2003-03-29 15:28:58 +00:00
|
|
|
|
2003-06-28 01:23:11 +00:00
|
|
|
int QBrowseBox::exec(const QWidget * trigger)
|
2002-11-08 00:09:00 +00:00
|
|
|
{
|
|
|
|
QPoint globalpos = trigger->parentWidget()->mapToGlobal( trigger->pos());
|
|
|
|
// is there enough space to put the box below the trigger?
|
|
|
|
if ( globalpos.y() + trigger->height()+height()+1<
|
|
|
|
QApplication::desktop()->height()) {
|
|
|
|
// is there enough space to set the box left-justified with the trigger
|
|
|
|
if (globalpos.x()+width()<QApplication::desktop()->width())
|
|
|
|
return exec(globalpos.x(),
|
|
|
|
globalpos.y()+trigger->height()+1);
|
|
|
|
else
|
|
|
|
return exec(globalpos.x()-width()-1,
|
|
|
|
globalpos.y()+trigger->height()+1);
|
|
|
|
} else {
|
|
|
|
if (globalpos.x()+width()<QApplication::desktop()->width())
|
|
|
|
return exec(globalpos.x(),
|
|
|
|
globalpos.y()-height()-1);
|
|
|
|
else
|
|
|
|
return exec(globalpos.x()-width()-1,
|
|
|
|
globalpos.y()-height()-1);
|
|
|
|
}
|
|
|
|
}
|
2002-10-17 09:51:18 +00:00
|
|
|
|
2003-03-29 15:28:58 +00:00
|
|
|
|
2002-11-08 00:09:00 +00:00
|
|
|
int QBrowseBox::exec(int x,int y)
|
|
|
|
{
|
|
|
|
firstrelease_ = true;
|
|
|
|
move(x,y);
|
|
|
|
show();
|
|
|
|
repaint();
|
|
|
|
qApp->enter_loop();
|
|
|
|
inloop = true;
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2002-11-08 00:09:00 +00:00
|
|
|
if (activecell_.x()!=-1 && activecell_.y()!=-1 )
|
|
|
|
return coordsToIndex( activecell_.x(),activecell_.y());
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2003-03-29 15:28:58 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
void QBrowseBox::keyPressEvent(QKeyEvent * e)
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
switch(e->key()) {
|
2002-10-09 08:59:02 +00:00
|
|
|
case Key_Up:
|
|
|
|
moveUp();
|
|
|
|
break;
|
|
|
|
case Key_Down:
|
|
|
|
moveDown();
|
|
|
|
break;
|
|
|
|
case Key_Left:
|
|
|
|
moveLeft();
|
|
|
|
break;
|
|
|
|
case Key_Right:
|
|
|
|
moveRight();
|
|
|
|
break;
|
|
|
|
case Key_Return:
|
2002-10-17 09:51:18 +00:00
|
|
|
emit selected(activecell_.x(), activecell_.y());
|
2002-11-08 00:09:00 +00:00
|
|
|
if ( isVisible() && parentWidget() &&
|
|
|
|
parentWidget()->inherits("QPopupMenu") )
|
|
|
|
parentWidget()->close();
|
|
|
|
else
|
|
|
|
close();
|
2002-10-17 09:51:18 +00:00
|
|
|
break;
|
2002-11-08 00:09:00 +00:00
|
|
|
case Key_Escape:
|
|
|
|
if (inloop)
|
|
|
|
qApp->exit_loop();
|
|
|
|
if ( isVisible() && parentWidget() &&
|
|
|
|
parentWidget()->inherits("QPopupMenu") )
|
|
|
|
parentWidget()->close();
|
2002-10-09 08:59:02 +00:00
|
|
|
default:
|
|
|
|
e->ignore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-29 15:28:58 +00:00
|
|
|
|
2002-11-12 09:21:14 +00:00
|
|
|
void QBrowseBox::contentsMouseReleaseEvent(QMouseEvent *)
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
|
|
|
|
2002-11-08 00:09:00 +00:00
|
|
|
if (firstrelease_)
|
|
|
|
firstrelease_ = false;
|
|
|
|
else {
|
|
|
|
emit selected( activecell_.x(), activecell_.y());
|
|
|
|
if ( isVisible() && parentWidget() &&
|
|
|
|
parentWidget()->inherits("QPopupMenu") )
|
|
|
|
parentWidget()->close();
|
|
|
|
}
|
|
|
|
}
|
2002-10-17 09:51:18 +00:00
|
|
|
|
2003-03-29 15:28:58 +00:00
|
|
|
|
2002-11-08 00:09:00 +00:00
|
|
|
void QBrowseBox::closeEvent(QCloseEvent * e)
|
|
|
|
{
|
|
|
|
e->accept();
|
|
|
|
qApp->exit_loop();
|
|
|
|
}
|
2002-10-09 08:59:02 +00:00
|
|
|
|
2003-03-29 15:28:58 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
void QBrowseBox::paintCell(QPainter * painter, int row, int col)
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2003-03-29 15:28:58 +00:00
|
|
|
painter->setClipRect(cellGeometry(row, col));
|
2002-10-17 09:51:18 +00:00
|
|
|
|
2003-03-29 15:28:58 +00:00
|
|
|
int const index = coordsToIndex(row, col);
|
|
|
|
|
|
|
|
painter->drawPixmap(0, 0, pixmaps_[index]);
|
2002-10-17 09:51:18 +00:00
|
|
|
|
|
|
|
if (activecell_.x() == row && activecell_.y() == col) {
|
2003-03-29 15:28:58 +00:00
|
|
|
qDrawShadeRect(painter, 0, 0, cellWidth(),
|
|
|
|
cellHeight(), colorGroup(), false, 1);
|
2002-10-17 09:51:18 +00:00
|
|
|
} else {
|
|
|
|
qDrawPlainRect(painter, 0, 0, cellWidth(),
|
2002-10-20 01:48:28 +00:00
|
|
|
cellHeight(), colorGroup().background(), 1);
|
2002-10-17 09:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
painter->setClipping(false);
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-12 09:21:14 +00:00
|
|
|
void QBrowseBox::contentsMouseMoveEvent(QMouseEvent * e)
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
int x = e->pos().x();
|
|
|
|
int y = e->pos().y();
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
int cellx;
|
|
|
|
int celly;
|
|
|
|
|
|
|
|
if (x < 0 || y < 0 || x > width() || y > height()) {
|
|
|
|
// outside the box
|
|
|
|
cellx = -1;
|
|
|
|
celly = -1;
|
|
|
|
} else {
|
|
|
|
celly = (int)floor( ((double)x) / ((double)cellWidth()) );
|
|
|
|
cellx = (int)floor( ((double)y) / ((double)cellHeight()) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (activecell_.x() != cellx || activecell_.y() != celly) {
|
|
|
|
// mouse has been moved to another cell
|
|
|
|
int oldactivecellx = activecell_.x();
|
|
|
|
int oldactivecelly = activecell_.y();
|
|
|
|
activecell_.setX(cellx);
|
|
|
|
activecell_.setY(celly);
|
|
|
|
// remove old highlighting
|
|
|
|
updateCell(oldactivecellx, oldactivecelly);
|
|
|
|
// set new highlighting
|
|
|
|
updateCell(activecell_.x(), activecell_.y());
|
|
|
|
}
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
|
|
|
|
void QBrowseBox::moveLeft()
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
int const y = activecell_.y();
|
2002-10-15 18:29:01 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
if (y>0)
|
|
|
|
activecell_.setY(y - 1);
|
2002-11-08 00:09:00 +00:00
|
|
|
else if (parentWidget())
|
|
|
|
QWidget::focusNextPrevChild(false);
|
2002-10-15 18:29:01 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
updateCell(activecell_.x(), y);
|
|
|
|
updateCell(activecell_.x(), activecell_.y());
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
|
|
|
|
void QBrowseBox::moveRight()
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
int const y = activecell_.y();
|
2002-10-15 18:29:01 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
if (y < numCols() - 1)
|
|
|
|
activecell_.setY(y+1);
|
2002-10-15 18:29:01 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
updateCell(activecell_.x(), y);
|
|
|
|
updateCell(activecell_.x(), activecell_.y());
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
|
|
|
|
void QBrowseBox::moveUp()
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
int const x = activecell_.x();
|
2002-10-15 18:29:01 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
if (x > 0)
|
|
|
|
activecell_.setX(x - 1);
|
2002-11-08 00:09:00 +00:00
|
|
|
else if (parentWidget())
|
|
|
|
QWidget::focusNextPrevChild(false);
|
2002-10-15 18:29:01 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
updateCell(x, activecell_.y());
|
|
|
|
updateCell(activecell_.x(), activecell_.y());
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
|
|
|
|
void QBrowseBox::moveDown()
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
int const x = activecell_.x();
|
2002-10-15 18:29:01 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
if (x < numRows() - 1)
|
|
|
|
activecell_.setX(x + 1);
|
2002-10-15 18:29:01 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
updateCell(x, activecell_.y());
|
|
|
|
updateCell(activecell_.x(), activecell_.y());
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|