]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/iconpalette.C
rename LFUN enum values according to their command (as used in th minibuffer/bind...
[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 //Added by qt3to4:
16 #include <QVBoxLayout>
17 #include <QPixmap>
18 #include <QHBoxLayout>
19 #include <Q3GridLayout>
20 #include <QResizeEvent>
21
22 #include "debug.h"
23
24 #include <qlayout.h>
25 #include <qpushbutton.h>
26 #include <qtooltip.h>
27
28 using std::endl;
29 using std::make_pair;
30 using std::max;
31 using std::string;
32 using std::vector;
33
34
35 int const button_size = 40;
36
37
38 IconPalette::IconPalette(QWidget * parent, char const * name)
39         : QWidget(parent, name), maxcol_(-1)
40 {
41         QVBoxLayout * top = new QVBoxLayout(this);
42         QHBoxLayout * row = new QHBoxLayout(top);
43         layout_ = new Q3GridLayout(row);
44         row->addStretch(0);
45         top->addStretch(0);
46 }
47
48
49 void IconPalette::add(QPixmap const & pixmap, string name, string tooltip)
50 {
51         QPushButton * p = new QPushButton(this);
52         p->setFixedSize(button_size, button_size);
53         p->setPixmap(pixmap);
54         QToolTip::add(p, toqstr(tooltip));
55         connect(p, SIGNAL(clicked()), this, SLOT(clicked()));
56         buttons_.push_back(make_pair(p, name));
57 }
58
59
60 void IconPalette::clicked()
61 {
62         vector<Button>::const_iterator it(buttons_.begin());
63         vector<Button>::const_iterator const end(buttons_.end());
64         for (; it != end; ++it) {
65                 if (sender() == it->first) {
66                         emit 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 }