]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/iconpalette.C
* src/frontends/qt4/GuiSelection.C
[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 #include "qt_helpers.h"
15 #include "controllers/ControlMath.h" // for find_xpm
16
17 #include <QPixmap>
18 #include <QGridLayout>
19 #include <QPushButton>
20 #include <QToolTip>
21
22 using std::make_pair;
23 using std::string;
24 using std::vector;
25
26
27 namespace lyx {
28 namespace frontend {
29
30 FlowLayout::FlowLayout(QWidget *parent) : QLayout(parent)
31 {
32         setMargin(0);
33         setSpacing(-1);
34 }
35
36
37 FlowLayout::~FlowLayout()
38 {
39         QLayoutItem *item;
40         while ((item = takeAt(0)))
41                 delete item;
42 }
43
44
45 void FlowLayout::addItem(QLayoutItem *item)
46 {
47         itemList.append(item);
48 }
49
50
51 int FlowLayout::count() const
52 {
53         return itemList.size();
54 }
55
56
57 QLayoutItem *FlowLayout::itemAt(int index) const
58 {
59         return itemList.value(index);
60 }
61
62
63 QLayoutItem *FlowLayout::takeAt(int index)
64 {
65         if (index >= 0 && index < itemList.size())
66                 return itemList.takeAt(index);
67         else
68                 return 0;
69 }
70
71
72 Qt::Orientations FlowLayout::expandingDirections() const
73 {
74         return 0;
75 }
76
77
78 bool FlowLayout::hasHeightForWidth() const
79 {
80         return true;
81 }
82
83
84 int FlowLayout::heightForWidth(int width) const
85 {
86         int height = doLayout(QRect(0, 0, width, 0), true);
87         return height;
88 }
89
90
91 void FlowLayout::setGeometry(const QRect &rect)
92 {
93         QLayout::setGeometry(rect);
94         doLayout(rect, false);
95 }
96
97
98 QSize FlowLayout::sizeHint() const
99 {
100         // default to (max) 6 columns
101         int const cols = qMin(itemList.size(), 6);
102         int const rows = (itemList.size() - 1 )/6 + 1;
103         return QSize(cols * minimumSize().width() + 1,
104                 rows * minimumSize().height() + 1);
105 }
106
107
108 QSize FlowLayout::minimumSize() const
109 {
110         QSize size;
111         int const n = itemList.size();
112     for (int i = 0; i < n; ++i) {
113                 size = size.expandedTo(itemList.at(i)->minimumSize());
114         }
115         return size;
116 }
117
118
119 int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
120 {
121         int x = rect.x();
122         int y = rect.y();
123         int lineHeight = 0;
124
125     for (int i = 0; i < itemList.size(); ++i) {
126                 QLayoutItem * item = itemList.at(i);
127                 int nextX = x + item->sizeHint().width() + spacing();
128                 if (nextX - spacing() > rect.right() && lineHeight > 0) {
129                         x = rect.x();
130                         y = y + lineHeight + spacing();
131                 nextX = x + item->sizeHint().width() + spacing();
132                         lineHeight = 0;
133                 }
134
135                 if (!testOnly)
136                         item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
137
138                 x = nextX;
139                 lineHeight = qMax(lineHeight, item->sizeHint().height());
140         }
141         return y + lineHeight - rect.y();
142 }
143
144
145 IconPalette::IconPalette(QWidget * parent, char const ** entries)
146         : QWidget(parent)
147 {
148         FlowLayout * layout_ = new FlowLayout(this);
149         layout_->setSpacing(0);
150
151         int const button_size = 40;
152         for (int i = 0; *entries[i]; ++i) {
153                 QPushButton * p = new QPushButton;
154                 p->setFixedSize(button_size, button_size);
155                 p->setIcon(QPixmap(toqstr(lyx::frontend::find_xpm(entries[i]))));
156                 p->setToolTip(toqstr(string("\\") + entries[i]));
157                 connect(p, SIGNAL(clicked()), this, SLOT(clicked()));
158                 buttons_.push_back(make_pair(p, entries[i]));
159                 layout_->addWidget(p);
160         }
161
162 }
163
164
165 void IconPalette::clicked()
166 {
167         vector<Button>::const_iterator it = buttons_.begin();
168         vector<Button>::const_iterator const end = buttons_.end();
169         for (; it != end; ++it) {
170                 if (sender() == it->first) {
171                         // emit signal
172                         button_clicked(it->second);
173                         return;
174                 }
175         }
176 }
177
178
179 } // namespace frontend
180 } // namespace lyx
181
182 #include "iconpalette_moc.cpp"