Abdelrazak Younes wrote:
(talking to myself guys, don't pay attention...)
I am listening Edwin :-)
great! (ready for abuse? ;-)
to put this in the toolbar we need to initialize the widget with the
proper button as a parent and then connect some signals (or so i think)
have you any ideas how to fit this in the current toolbar framework?
regards, edwin
ps. updated files attached
#include <QApplication>
#include <QPushButton>
#include "liconview.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPushButton * pb = new QPushButton("Push!");
LIconView * iv = new LIconView(pb);
QObject::connect(pb, SIGNAL(pressed()), iv, SLOT(show()));
QObject::connect(pb, SIGNAL(released()), iv, SLOT(hide()));
pb->show();
return app.exec();
}
#include "liconview.h"
#include <QHeaderView>
#include <QMouseEvent>
#include <QTableWidgetSelectionRange>
#include <QString>
#include <QToolTip>
#include <QSizePolicy>
LIconView::LIconView(QWidget * parent, int rows, int columns )
: QTableWidget( rows, columns, parent), colwidth_(20), rowheight_(12)
{
horizontalHeader()->hide();
verticalHeader()->hide();
setWindowFlags(Qt::Popup);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
for(int i=0; i<rowCount() ; ++i)
setRowHeight(i, rowheight_);
for(int i=0; i<columnCount() ; ++i)
setColumnWidth(i, colwidth_);
resize(sizeHint());
}
LIconView::~LIconView()
{
}
void LIconView::show()
{
move(parentWidget()->geometry().x(),
parentWidget()->geometry().y() +
parentWidget()->size().height());
setVisible(true);
setMouseTracking(true);
grabMouse();
}
QSize LIconView::sizeHint() const
{
// the window frame is around 4 pixels here
// where to get it more in general?
return QSize(4 + columnCount()*colwidth_, 4 + rowCount()*rowheight_);
}
void LIconView::addColumn()
{
int const nr = columnCount();
insertColumn(nr);
setColumnWidth(nr, colwidth_);
resize(sizeHint());
}
void LIconView::addRow()
{
int const nr = rowCount();
insertRow(nr);
setRowHeight(nr, rowheight_);
resize(sizeHint());
}
void LIconView::mouseMoveEvent(QMouseEvent * event)
{
QList<QTableWidgetSelectionRange> range = selectedRanges();
if (!range.isEmpty())
setRangeSelected(range.at(0), false);
int const bottom = event->y()/rowheight_;
int const right = event->x()/colwidth_;
if (event->x()>0 && event->y()>0) {
setRangeSelected(QTableWidgetSelectionRange(0, 0, bottom, right
), true);
}
QString status = QString("%1x%2").arg(bottom + 1).arg(right + 1);
QToolTip::showText(event->globalPos(), status , this);
if (event->x()>((columnCount()-1)*colwidth_))
addColumn();
if (event->y()>((rowCount()-1)*rowheight_))
addRow();
}
void LIconView::mousePressEvent(QMouseEvent * event )
{
int const bottom = event->y()/rowheight_;
int const right = event->x()/colwidth_;
emit size(bottom + 1, right + 1);
hide();
}
void LIconView::hide()
{
setMouseTracking(false);
releaseMouse();
setVisible(false);
}
#ifndef LICONVIEW_H
#define LICONVIEW_H
#include <QTableWidget>
class QMouseEvent;
class LIconView : public QTableWidget {
Q_OBJECT
public:
LIconView(QWidget * parent, int rows = 5, int columns = 5);
~LIconView();
QSize sizeHint() const;
signals:
void size(int , int);
public slots:
void show();
void hide();
protected slots:
void mouseMoveEvent( QMouseEvent * event );
void mousePressEvent( QMouseEvent * event );
private:
void addRow();
void addColumn();
int colwidth_;
int rowheight_;
};
#endif // LICONVIEW_H