2002-10-09 08:59:02 +00:00
|
|
|
/**
|
|
|
|
* \file QBrowseBox.C
|
|
|
|
*
|
|
|
|
* Original file taken from klyx 0.10 sources:
|
2002-10-17 09:51:18 +00:00
|
|
|
* $Id: QBrowseBox.C,v 1.4 2002/10/17 09:51:18 poenitz Exp $
|
2002-10-09 08:59:02 +00:00
|
|
|
*
|
|
|
|
* \author Kalle Dalheimer ?
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#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 <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "QBrowseBox.h"
|
|
|
|
|
|
|
|
|
2002-10-15 17:45:12 +00:00
|
|
|
QBrowseBox::QBrowseBox(int rows, int cols, QWidget * parent,
|
2002-10-17 09:51:18 +00:00
|
|
|
char const * name, WFlags fl)
|
|
|
|
: QGridView()
|
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-10-17 09:51:18 +00:00
|
|
|
activecell_.setX(0);
|
|
|
|
activecell_.setY(0);
|
|
|
|
updateCell(0, 0);
|
|
|
|
setMouseTracking(true);
|
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-10-17 09:51:18 +00:00
|
|
|
setFocusPolicy(QWidget::StrongFocus);
|
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-10-17 09:51:18 +00:00
|
|
|
void QBrowseBox::insertItem(char const * text, int x, int y)
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
insertItem(QString(text), x, y);
|
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-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
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
break;
|
2002-10-09 08:59:02 +00:00
|
|
|
default:
|
|
|
|
e->ignore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
|
|
|
|
void QBrowseBox::mouseReleaseEvent(QMouseEvent * e)
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
qWarning("mouse release");
|
|
|
|
emit selected( activecell_.x(), activecell_.y());
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-17 09:51:18 +00:00
|
|
|
|
|
|
|
//void QBrowseBox::closeEvent(QCloseEvent * e)
|
2002-10-09 08:59:02 +00:00
|
|
|
//{
|
2002-10-17 09:51:18 +00:00
|
|
|
// 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(),
|
|
|
|
cellHeight(), colorGroup(), false, 1);
|
|
|
|
else
|
|
|
|
qDrawShadePanel(painter, 0, 0, cellWidth(),
|
|
|
|
cellHeight(), colorGroup(), false, 1);
|
|
|
|
} else {
|
|
|
|
qDrawPlainRect(painter, 0, 0, cellWidth(),
|
|
|
|
cellHeight(), colorGroup().background(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!texts_[coordsToIndex(row, col)].isEmpty()) {
|
|
|
|
painter->drawText(0, 0, cellWidth(),
|
|
|
|
cellHeight(), AlignLeft,
|
|
|
|
texts_[coordsToIndex(row, col)]);
|
|
|
|
}
|
|
|
|
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-10-17 09:51:18 +00:00
|
|
|
void QBrowseBox::mouseMoveEvent(QMouseEvent * e)
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-10-17 09:51:18 +00:00
|
|
|
qWarning("mouseMoveEvent");
|
|
|
|
int x = e->pos().x();
|
|
|
|
int y = e->pos().y();
|
|
|
|
|
|
|
|
int cellx;
|
|
|
|
int celly;
|
|
|
|
|
|
|
|
if (x < 0 || y < 0 || x > width() || y > height()) {
|
|
|
|
// outside the box
|
|
|
|
cellx = -1;
|
|
|
|
celly = -1;
|
|
|
|
} else {
|
|
|
|
#ifdef WITH_WARNINGS
|
|
|
|
#warning Is the switch of x and y intended here? (Andre)
|
|
|
|
#endif
|
|
|
|
celly = (int)floor( ((double)x) / ((double)cellWidth()) );
|
|
|
|
cellx = (int)floor( ((double)y) / ((double)cellHeight()) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (activecell_.x() != cellx || activecell_.y() != celly) {
|
|
|
|
qWarning("update");
|
|
|
|
// 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-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-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
|
|
|
}
|
2002-10-17 09:51:18 +00:00
|
|
|
|