]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/iconpalette.C
relayout on resize for the icon palette
[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         lyxerr << "resize panel to " << e->size().width() << "," << e->size().height() << endl;
65  
66         int maxcol = e->size().width() / button_size;
67  
68         if (!layout_->isEmpty() && maxcol == maxcol_)
69                 return;
70
71         lyxerr << "doing layout !" << maxcol << " " << width() << endl;
72         lyxerr << "before is " << maxcol_ << endl; 
73  
74         setUpdatesEnabled(false);
75  
76         // clear layout
77         QLayoutIterator lit = layout_->iterator();
78         while (lit.current()) {
79                 lit.takeCurrent();
80         }
81                  
82         layout_->invalidate();
83  
84         vector<Button>::const_iterator it(buttons_.begin());
85         vector<Button>::const_iterator const end(buttons_.end());
86
87         int row = 0;
88         int col = 0;
89          
90         for (; it != end; ++it) {
91                 layout_->addWidget(it->first, row, col++);
92                 if (col >= maxcol) {
93                         col = 0;
94                         ++row;
95                 }
96         }
97
98         maxcol_ = maxcol;
99  
100         // this is OK because width won't change, and we have the check above
101         setGeometry(x(), y(), width(), (row + 1) * button_size);
102  
103         repaint();
104         lyxerr << "after is " << row << "," << maxcol << endl; 
105         setUpdatesEnabled(true);
106         update();
107 }