3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "iconpalette.h"
17 #include "qt_helpers.h"
19 #include <QVBoxLayout>
21 #include <QHBoxLayout>
22 #include <QGridLayout>
23 #include <QResizeEvent>
25 #include <QPushButton>
37 int const button_size = 32;
40 IconPalette::IconPalette(QWidget * parent)
41 : QWidget(parent), maxcol_(-1)
43 QVBoxLayout * top = new QVBoxLayout(this);
44 QHBoxLayout * row = new QHBoxLayout(this);
45 layout_ = new QGridLayout(this);
46 top->insertLayout(-1, row);
47 row->insertLayout(-1, layout_);
53 void IconPalette::add(QPixmap const & pixmap, string name, string tooltip)
55 QPushButton * p = new QPushButton(this);
56 p->setFixedSize(button_size, button_size);
57 p->setIcon(QIcon(pixmap));
58 p->setToolTip(toqstr(tooltip));
59 connect(p, SIGNAL(clicked()), this, SLOT(clicked()));
60 buttons_.push_back(make_pair(p, name));
64 void IconPalette::clicked()
66 vector<Button>::const_iterator it = buttons_.begin();
67 vector<Button>::const_iterator const end = buttons_.end();
68 for (; it != end; ++it) {
69 if (sender() == it->first) {
71 button_clicked(it->second);
78 void IconPalette::resizeEvent(QResizeEvent * e)
80 QWidget::resizeEvent(e);
82 lyxerr[Debug::GUI] << "resize panel to "
83 << e->size().width() << ',' << e->size().height() << endl;
85 int maxcol = e->size().width() / button_size;
87 if (!layout_->isEmpty() && maxcol == maxcol_)
90 int cols = max(width() / button_size, 1);
91 int rows = max(int(buttons_.size() / cols), 1);
92 if (buttons_.size() % cols)
95 lyxerr[Debug::GUI] << "Laying out " << buttons_.size() << " widgets in a "
96 << cols << 'x' << rows << " grid." << endl;
98 setUpdatesEnabled(false);
103 while ((child = layout_->itemAt(i)) != 0) {
108 layout_->invalidate();
110 vector<Button>::const_iterator it(buttons_.begin());
111 vector<Button>::const_iterator const end(buttons_.end());
113 for (int i = 0; i < rows; ++i) {
114 for (int j = 0; j < cols; ++j) {
115 layout_->addWidget(it->first, i, j);
124 resize(cols * button_size, rows * button_size);
128 setUpdatesEnabled(true);
135 #include "iconpalette_moc.cpp"