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