]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/iconpalette.C
delete some emits that I missed, add comments about emitting signals
[lyx.git] / src / frontends / qt4 / 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 #include <QVBoxLayout>
16 #include <QPixmap>
17 #include <QHBoxLayout>
18 #include <QGridLayout>
19 #include <QResizeEvent>
20
21 #include "debug.h"
22
23 #include <qlayout.h>
24 #include <qpushbutton.h>
25 #include <qtooltip.h>
26
27 using std::endl;
28 using std::make_pair;
29 using std::max;
30 using std::string;
31 using std::vector;
32
33
34 int const button_size = 40;
35
36
37 IconPalette::IconPalette(QWidget * parent, char const * name)
38         : QWidget(parent, name), maxcol_(-1)
39 {
40         QVBoxLayout * top = new QVBoxLayout(this);
41         QHBoxLayout * row = new QHBoxLayout(top);
42         layout_ = new QGridLayout(row);
43         row->addStretch(0);
44         top->addStretch(0);
45 }
46
47
48 void IconPalette::add(QPixmap const & pixmap, string name, string tooltip)
49 {
50         QPushButton * p = new QPushButton(this);
51         p->setFixedSize(button_size, button_size);
52         p->setPixmap(pixmap);
53         QToolTip::add(p, toqstr(tooltip));
54         connect(p, SIGNAL(clicked()), this, SLOT(clicked()));
55         buttons_.push_back(make_pair(p, name));
56 }
57
58
59 void IconPalette::clicked()
60 {
61         vector<Button>::const_iterator it(buttons_.begin());
62         vector<Button>::const_iterator const end(buttons_.end());
63         for (; it != end; ++it) {
64                 if (sender() == it->first) {
65                         // emit signal
66                         button_clicked(it->second);
67                         return;
68                 }
69         }
70 }
71
72
73 void IconPalette::resizeEvent(QResizeEvent * e)
74 {
75         QWidget::resizeEvent(e);
76
77         lyxerr[Debug::GUI] << "resize panel to "
78                 << e->size().width() << ',' << e->size().height() << endl;
79
80         int maxcol = e->size().width() / button_size;
81
82         if (!layout_->isEmpty() && maxcol == maxcol_)
83                 return;
84
85         int cols = max(width() / button_size, 1);
86         int rows = max(int(buttons_.size() / cols), 1);
87         if (buttons_.size() % cols)
88                 ++rows;
89
90         lyxerr[Debug::GUI] << "Laying out " << buttons_.size() << " widgets in a "
91                 << cols << 'x' << rows << " grid." << endl;
92
93         setUpdatesEnabled(false);
94
95         // clear layout
96         QLayoutIterator lit = layout_->iterator();
97         while (lit.current()) {
98                 lit.takeCurrent();
99         }
100
101         layout_->invalidate();
102
103         vector<Button>::const_iterator it(buttons_.begin());
104         vector<Button>::const_iterator const end(buttons_.end());
105
106         for (int i = 0; i < rows; ++i) {
107                 for (int j = 0; j < cols; ++j) {
108                         layout_->addWidget(it->first, i, j);
109                         ++it;
110                         if (it == end)
111                                 goto out;
112                 }
113         }
114
115 out:
116
117         resize(cols * button_size, rows * button_size);
118
119         maxcol_ = cols;
120
121         setUpdatesEnabled(true);
122         update();
123 }
124
125 #include "iconpalette_moc.cpp"