]> git.lyx.org Git - features.git/blob - src/frontends/qt3/iconpalette.C
82d56690154d7b94fa84138da7fcaaa1434ad3e2
[features.git] / src / frontends / qt3 / iconpalette.C
1 /**
2  * \file iconpalette.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "iconpalette.h"
14 #include "qt_helpers.h"
15
16 #include "debug.h"
17
18 #include <qlayout.h>
19 #include <qpushbutton.h>
20 #include <qtooltip.h>
21
22 using std::endl;
23 using std::make_pair;
24 using std::max;
25 using std::string;
26 using std::vector;
27
28
29 int const button_size = 40;
30
31
32 IconPalette::IconPalette(QWidget * parent, char const * name)
33         : QWidget(parent, name), maxcol_(-1)
34 {
35         QVBoxLayout * top = new QVBoxLayout(this);
36         QHBoxLayout * row = new QHBoxLayout(top);
37         layout_ = new QGridLayout(row);
38         row->addStretch(0);
39         top->addStretch(0);
40 }
41
42
43 void IconPalette::add(QPixmap const & pixmap, string name, string tooltip)
44 {
45         QPushButton * p = new QPushButton(this);
46         p->setFixedSize(button_size, button_size);
47         p->setPixmap(pixmap);
48         QToolTip::add(p, toqstr(tooltip));
49         connect(p, SIGNAL(clicked()), this, SLOT(clicked()));
50         buttons_.push_back(make_pair(p, name));
51 }
52
53
54 void IconPalette::clicked()
55 {
56         vector<Button>::const_iterator it(buttons_.begin());
57         vector<Button>::const_iterator const end(buttons_.end());
58         for (; it != end; ++it) {
59                 if (sender() == it->first) {
60                         // emit signal
61                         button_clicked(it->second);
62                         return;
63                 }
64         }
65 }
66
67
68 void IconPalette::resizeEvent(QResizeEvent * e)
69 {
70         QWidget::resizeEvent(e);
71
72         lyxerr[Debug::GUI] << "resize panel to "
73                 << e->size().width() << ',' << e->size().height() << endl;
74
75         int maxcol = e->size().width() / button_size;
76
77         if (!layout_->isEmpty() && maxcol == maxcol_)
78                 return;
79
80         int cols(width() / button_size);
81         int rows = max(int(buttons_.size() / cols), 1);
82         if (buttons_.size() % cols)
83                 ++rows;
84
85         lyxerr[Debug::GUI] << "Laying out " << buttons_.size() << " widgets in a "
86                 << cols << 'x' << rows << " grid." << endl;
87
88         setUpdatesEnabled(false);
89
90         // clear layout
91         QLayoutIterator lit = layout_->iterator();
92         while (lit.current()) {
93                 lit.takeCurrent();
94         }
95
96         layout_->invalidate();
97
98         vector<Button>::const_iterator it(buttons_.begin());
99         vector<Button>::const_iterator const end(buttons_.end());
100
101         for (int i = 0; i < rows; ++i) {
102                 for (int j = 0; j < cols; ++j) {
103                         layout_->addWidget(it->first, i, j);
104                         ++it;
105                         if (it == end)
106                                 goto out;
107                 }
108         }
109
110 out:
111
112         resize(cols * button_size, rows * button_size);
113
114         maxcol_ = cols;
115
116         setUpdatesEnabled(true);
117         update();
118 }