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