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
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS
|
|
|
|
*/
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
#include <config.h>
|
2002-10-09 08:59:02 +00:00
|
|
|
|
|
|
|
#include <qstring.h>
|
|
|
|
#include <qpixmap.h>
|
|
|
|
#include <qkeycode.h>
|
|
|
|
#include <qpainter.h>
|
|
|
|
#include <qapplication.h>
|
|
|
|
#include <qdrawutil.h>
|
|
|
|
#include <qstyle.h>
|
|
|
|
#include <qimage.h>
|
|
|
|
|
|
|
|
#include "QBrowseBox.h"
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cmath>
|
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
|
2002-11-08 00:09:00 +00:00
|
|
|
QBrowseBox::QBrowseBox(int rows, int cols, QWidget* parent, const char* name, WFlags f)
|
|
|
|
: QGridView(parent,name,f)
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
setNumRows(rows);
|
|
|
|
setNumCols(cols);
|
|
|
|
setCellWidth(width()/cols);
|
|
|
|
setCellHeight(height()/rows);
|
2002-10-09 08:59:02 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
texts_ = new QString[rows * cols];
|
|
|
|
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
|
|
|
|
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-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QBrowseBox::~QBrowseBox()
|
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
delete [] texts_;
|
|
|
|
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(QString const & text, int row, int col)
|
|
|
|
{
|
|
|
|
texts_[coordsToIndex(row, col)] = text;
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-15 18:29:01 +00:00
|
|
|
|
2002-11-08 00:09:00 +00:00
|
|
|
void QBrowseBox::insertItem(char const * text, int row, int col)
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-11-08 00:09:00 +00:00
|
|
|
insertItem(QString(text), row, col);
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-15 18:29:01 +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
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
void QBrowseBox::insertItem( QPixmap pixmap)
|
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
int w = pixmap.width() / numCols();
|
|
|
|
int h = pixmap.height() / numRows();
|
|
|
|
|
|
|
|
for (int row = 0; row < numRows(); ++row)
|
|
|
|
for (int col = 0; col < numCols(); ++col) {
|
|
|
|
QPixmap small(w,h);
|
|
|
|
bitBlt(&small,0,0,&pixmap,col*w,row*h,w,h,Qt::CopyROP,false);
|
|
|
|
insertItem(small, row, col);
|
|
|
|
}
|
|
|
|
|
|
|
|
resize(pixmap.width() + (numCols() + 1) * frameWidth(),
|
|
|
|
pixmap.height() + (numRows() + 1) * frameWidth());
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
2002-10-15 18:29:01 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
|
|
|
|
void QBrowseBox::removeItem(int row, int col)
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
texts_[coordsToIndex(row, col)] = "";
|
|
|
|
pixmaps_[coordsToIndex(row, col)].resize(0, 0);
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-15 18:29:01 +00:00
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
void QBrowseBox::clear()
|
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
for (int row = 0; row < numRows(); ++row)
|
|
|
|
for (int col = 0; col < numCols(); ++col)
|
|
|
|
removeItem(row, col);
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-15 18:29:01 +00:00
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
QString QBrowseBox::text(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 "";
|
|
|
|
return texts_[coordsToIndex(row, col)];
|
2002-10-09 08:59:02 +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
|
|
|
}
|
|
|
|
|
2002-11-08 00:09:00 +00:00
|
|
|
int QBrowseBox::exec(const QPoint& pos)
|
|
|
|
{
|
|
|
|
return exec(pos.x(),pos.y());
|
|
|
|
}
|
|
|
|
|
|
|
|
int QBrowseBox::exec(const QWidget* trigger)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (activecell_.x()!=-1 && activecell_.y()!=-1 )
|
|
|
|
return coordsToIndex( activecell_.x(),activecell_.y());
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
painter->setClipRect(cellGeometry(row,col));//, QPainter::CoordPainter);
|
|
|
|
bool ispixmap = false;
|
|
|
|
|
|
|
|
if (!pixmaps_[coordsToIndex(row,col)].isNull()) {
|
|
|
|
painter->drawPixmap(0,0,pixmaps_[coordsToIndex(row, col)]);
|
|
|
|
ispixmap = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (activecell_.x() == row && activecell_.y() == col) {
|
|
|
|
if (ispixmap)
|
|
|
|
qDrawShadeRect(painter, 0, 0, cellWidth(),
|
2002-10-20 01:48:28 +00:00
|
|
|
cellHeight(), colorGroup(), false, 1);
|
2002-10-17 09:51:18 +00:00
|
|
|
else
|
|
|
|
qDrawShadePanel(painter, 0, 0, cellWidth(),
|
2002-10-20 01:48:28 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (!texts_[coordsToIndex(row, col)].isEmpty()) {
|
|
|
|
painter->drawText(0, 0, cellWidth(),
|
2002-11-08 00:09:00 +00:00
|
|
|
cellHeight(), AlignCenter,
|
2002-10-20 01:48:28 +00:00
|
|
|
texts_[coordsToIndex(row, col)]);
|
2002-10-17 09:51:18 +00:00
|
|
|
}
|
|
|
|
painter->setClipping(false);
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
void QBrowseBox::resizeEvent(QResizeEvent * e)
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
QGridView::resizeEvent(e);
|
|
|
|
setCellWidth(contentsRect().width() / numCols());
|
|
|
|
setCellHeight(contentsRect().height() / numRows());
|
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-11-12 09:21:14 +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
|
|
|
}
|