]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/iconpalette.C
Removed all redundant using directives from the source.
[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 #include "debug.h"
14
15 #include "iconpalette.h"
16 #include "qt_helpers.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
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 button_clicked(it->second);
61                         return;
62                 }
63         }
64 }
65
66
67 void IconPalette::resizeEvent(QResizeEvent * e)
68 {
69         QWidget::resizeEvent(e);
70
71         lyxerr[Debug::GUI] << "resize panel to "
72                 << e->size().width() << ',' << e->size().height() << endl;
73
74         int maxcol = e->size().width() / button_size;
75
76         if (!layout_->isEmpty() && maxcol == maxcol_)
77                 return;
78
79         int cols(width() / button_size);
80         int rows = max(int(buttons_.size() / cols), 1);
81         if (buttons_.size() % cols)
82                 ++rows;
83
84         lyxerr[Debug::GUI] << "Laying out " << buttons_.size() << " widgets in a "
85                 << cols << 'x' << rows << " grid." << endl;
86
87         setUpdatesEnabled(false);
88
89         // clear layout
90         QLayoutIterator lit = layout_->iterator();
91         while (lit.current()) {
92                 lit.takeCurrent();
93         }
94
95         layout_->invalidate();
96
97         vector<Button>::const_iterator it(buttons_.begin());
98         vector<Button>::const_iterator const end(buttons_.end());
99
100         for (int i = 0; i < rows; ++i) {
101                 for (int j = 0; j < cols; ++j) {
102                         layout_->addWidget(it->first, i, j);
103                         ++it;
104                         if (it == end)
105                                 goto out;
106                 }
107         }
108
109 out:
110
111         resize(cols * button_size, rows * button_size);
112
113         maxcol_ = cols;
114
115         setUpdatesEnabled(true);
116         update();
117 }