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>
35 int const button_size = 40;
38 IconPalette::IconPalette(QWidget * parent)
39 : QWidget(parent), maxcol_(-1)
41 QVBoxLayout * top = new QVBoxLayout(this);
42 QHBoxLayout * row = new QHBoxLayout(this);
43 layout_ = new QGridLayout(this);
44 top->insertLayout(-1, row);
45 row->insertLayout(-1, layout_);
51 void IconPalette::add(QPixmap const & pixmap, string name, string tooltip)
53 QPushButton * p = new QPushButton(this);
54 p->setFixedSize(button_size, button_size);
55 p->setIcon(QIcon(pixmap));
56 p->setToolTip(toqstr(tooltip));
57 connect(p, SIGNAL(clicked()), this, SLOT(clicked()));
58 buttons_.push_back(make_pair(p, name));
62 void IconPalette::clicked()
64 vector<Button>::const_iterator it(buttons_.begin());
65 vector<Button>::const_iterator const end(buttons_.end());
66 for (; it != end; ++it) {
67 if (sender() == it->first) {
69 button_clicked(it->second);
76 void IconPalette::resizeEvent(QResizeEvent * e)
78 QWidget::resizeEvent(e);
80 lyxerr[Debug::GUI] << "resize panel to "
81 << e->size().width() << ',' << e->size().height() << endl;
83 int maxcol = e->size().width() / button_size;
85 if (!layout_->isEmpty() && maxcol == maxcol_)
88 int cols = max(width() / button_size, 1);
89 int rows = max(int(buttons_.size() / cols), 1);
90 if (buttons_.size() % cols)
93 lyxerr[Debug::GUI] << "Laying out " << buttons_.size() << " widgets in a "
94 << cols << 'x' << rows << " grid." << endl;
96 setUpdatesEnabled(false);
101 while ((child = layout_->itemAt(i)) != 0) {
106 layout_->invalidate();
108 vector<Button>::const_iterator it(buttons_.begin());
109 vector<Button>::const_iterator const end(buttons_.end());
111 for (int i = 0; i < rows; ++i) {
112 for (int j = 0; j < cols; ++j) {
113 layout_->addWidget(it->first, i, j);
122 resize(cols * button_size, rows * button_size);
126 setUpdatesEnabled(true);
130 #include "iconpalette_moc.cpp"