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