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