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