2002-08-30 16:20:27 +00:00
|
|
|
/**
|
|
|
|
* \file iconpalette.C
|
2002-09-24 13:57:09 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-08-30 16:20:27 +00:00
|
|
|
*
|
|
|
|
* \author John Levon
|
2002-09-24 13:57:09 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-08-30 16:20:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "iconpalette.h"
|
2002-12-17 20:37:13 +00:00
|
|
|
#include "qt_helpers.h"
|
2002-08-30 16:20:27 +00:00
|
|
|
|
2004-05-20 09:36:28 +00:00
|
|
|
#include "debug.h"
|
|
|
|
|
2002-08-30 16:20:27 +00:00
|
|
|
#include <qlayout.h>
|
|
|
|
#include <qpushbutton.h>
|
|
|
|
#include <qtooltip.h>
|
|
|
|
|
2002-09-07 23:28:05 +00:00
|
|
|
using std::endl;
|
2002-09-08 02:37:57 +00:00
|
|
|
using std::make_pair;
|
|
|
|
using std::max;
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
2003-09-08 00:33:41 +00:00
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
|
2002-09-08 02:37:57 +00:00
|
|
|
int const button_size = 40;
|
2002-10-20 01:48:28 +00:00
|
|
|
|
|
|
|
|
2002-08-30 16:20:27 +00:00
|
|
|
IconPalette::IconPalette(QWidget * parent, char const * name)
|
2002-09-08 02:37:57 +00:00
|
|
|
: QWidget(parent, name), maxcol_(-1)
|
2002-08-30 16:20:27 +00:00
|
|
|
{
|
|
|
|
QVBoxLayout * top = new QVBoxLayout(this);
|
|
|
|
QHBoxLayout * row = new QHBoxLayout(top);
|
|
|
|
layout_ = new QGridLayout(row);
|
|
|
|
row->addStretch(0);
|
|
|
|
top->addStretch(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IconPalette::add(QPixmap const & pixmap, string name, string tooltip)
|
|
|
|
{
|
|
|
|
QPushButton * p = new QPushButton(this);
|
2002-09-08 02:37:57 +00:00
|
|
|
p->setFixedSize(button_size, button_size);
|
2002-08-30 16:20:27 +00:00
|
|
|
p->setPixmap(pixmap);
|
2002-12-17 20:37:13 +00:00
|
|
|
QToolTip::add(p, toqstr(tooltip));
|
2002-10-20 01:48:28 +00:00
|
|
|
connect(p, SIGNAL(clicked()), this, SLOT(clicked()));
|
2002-09-08 02:37:57 +00:00
|
|
|
buttons_.push_back(make_pair(p, name));
|
2002-08-30 16:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IconPalette::clicked()
|
|
|
|
{
|
2002-09-08 02:37:57 +00:00
|
|
|
vector<Button>::const_iterator it(buttons_.begin());
|
|
|
|
vector<Button>::const_iterator const end(buttons_.end());
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
if (sender() == it->first) {
|
|
|
|
emit button_clicked(it->second);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2002-08-30 16:20:27 +00:00
|
|
|
}
|
2002-09-07 23:28:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
void IconPalette::resizeEvent(QResizeEvent * e)
|
|
|
|
{
|
2002-09-08 05:00:37 +00:00
|
|
|
QWidget::resizeEvent(e);
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-09-08 05:00:37 +00:00
|
|
|
lyxerr[Debug::GUI] << "resize panel to "
|
2002-11-27 10:30:28 +00:00
|
|
|
<< e->size().width() << ',' << e->size().height() << endl;
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-09-08 02:37:57 +00:00
|
|
|
int maxcol = e->size().width() / button_size;
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-09-08 02:37:57 +00:00
|
|
|
if (!layout_->isEmpty() && maxcol == maxcol_)
|
|
|
|
return;
|
|
|
|
|
2002-09-08 05:00:37 +00:00
|
|
|
int cols(width() / button_size);
|
2002-09-12 17:07:31 +00:00
|
|
|
int rows = max(int(buttons_.size() / cols), 1);
|
2002-09-08 05:00:37 +00:00
|
|
|
if (buttons_.size() % cols)
|
|
|
|
++rows;
|
|
|
|
|
|
|
|
lyxerr[Debug::GUI] << "Laying out " << buttons_.size() << " widgets in a "
|
2002-11-27 10:30:28 +00:00
|
|
|
<< cols << 'x' << rows << " grid." << endl;
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-09-08 02:37:57 +00:00
|
|
|
setUpdatesEnabled(false);
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-09-08 02:37:57 +00:00
|
|
|
// clear layout
|
|
|
|
QLayoutIterator lit = layout_->iterator();
|
|
|
|
while (lit.current()) {
|
|
|
|
lit.takeCurrent();
|
|
|
|
}
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-09-08 02:37:57 +00:00
|
|
|
layout_->invalidate();
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-09-08 02:37:57 +00:00
|
|
|
vector<Button>::const_iterator it(buttons_.begin());
|
|
|
|
vector<Button>::const_iterator const end(buttons_.end());
|
|
|
|
|
2002-09-08 05:00:37 +00:00
|
|
|
for (int i = 0; i < rows; ++i) {
|
|
|
|
for (int j = 0; j < cols; ++j) {
|
|
|
|
layout_->addWidget(it->first, i, j);
|
|
|
|
++it;
|
|
|
|
if (it == end)
|
|
|
|
goto out;
|
2002-09-08 02:37:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-08 05:00:37 +00:00
|
|
|
out:
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-09-08 05:00:37 +00:00
|
|
|
resize(cols * button_size, rows * button_size);
|
|
|
|
|
|
|
|
maxcol_ = cols;
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-09-08 02:37:57 +00:00
|
|
|
setUpdatesEnabled(true);
|
|
|
|
update();
|
2002-09-07 23:28:05 +00:00
|
|
|
}
|