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