1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
/***************************************************************************
* Copyright (C) 2004 by Alexander Dymo *
* adymo@kdevelop.org *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "buttonbar.h"
#include <qlayout.h>
#include <kdebug.h>
#include <kconfig.h>
#include <kstringhandler.h>
#include <klocale.h>
#include "button.h"
namespace Ideal {
//ButtonLayout class
ButtonLayout::ButtonLayout(ButtonBar *parent, Direction d, int margin, int spacing, const char *name)
:QBoxLayout(parent, d, margin, spacing, name), m_buttonBar(parent)
{
}
QSize ButtonLayout::minimumSize() const
{
QSize size = QBoxLayout::minimumSize();
if (!m_buttonBar->autoResize())
return size;
switch (m_buttonBar->place())
{
case Ideal::Left:
case Ideal::Right:
return QSize(size.width(),0);
break;
case Ideal::Top:
case Ideal::Bottom:
return QSize(0,size.height());
}
return QBoxLayout::minimumSize();
}
//ButtonBar class
ButtonBar::ButtonBar(Place place, ButtonMode mode, QWidget *parent, const char *name)
:QWidget(parent, name), m_place(place), l(0), m_shrinked(false), m_autoResize(true)
{
switch (m_place)
{
case Ideal::Left:
case Ideal::Right:
l = new ButtonLayout(this, QBoxLayout::TopToBottom, 0, 0);
break;
case Ideal::Top:
case Ideal::Bottom:
l = new ButtonLayout(this, QBoxLayout::LeftToRight, 0, 0);
break;
}
l->setResizeMode(QLayout::Minimum);
setMode(mode);
l->insertStretch(-1);
}
ButtonBar::~ButtonBar()
{
}
void ButtonBar::addButton(Button *button)
{
int buttonCount = m_buttons.count();
button->setMode(m_mode);
m_buttons.append(button);
l->insertWidget(buttonCount, button);
button->show();
fixDimensions();
}
void ButtonBar::removeButton(Button *button)
{
m_buttons.remove(button);
l->remove(button);
delete button;
}
void ButtonBar::setMode(ButtonMode mode)
{
m_mode = mode;
for (ButtonList::iterator it = m_buttons.begin(); it != m_buttons.end(); ++it)
(*it)->setMode(mode);
}
ButtonMode ButtonBar::mode() const
{
return m_mode;
}
Place ButtonBar::place() const
{
return m_place;
}
void ButtonBar::fixDimensions()
{
switch (m_place)
{
case Ideal::Left:
case Ideal::Right:
setFixedWidth(sizeHint().width());
setMinimumHeight(sizeHint().height());
setMaximumHeight(32767);
break;
case Ideal::Top:
case Ideal::Bottom:
setFixedHeight(sizeHint().height());
setMinimumWidth(sizeHint().width());
setMaximumWidth(32767);
break;
}
}
void ButtonBar::setButtonsPlace(Ideal::Place place)
{
for (ButtonList::iterator it = m_buttons.begin(); it != m_buttons.end(); ++it)
(*it)->setPlace(place);
}
void ButtonBar::resizeEvent(QResizeEvent *ev)
{
int preferredDimension = 0;
int actualDimension = 0;
int oldDimension = 0;
switch (m_place)
{
case Ideal::Left:
case Ideal::Right:
preferredDimension = l->QBoxLayout::minimumSize().height();
actualDimension = size().height();
oldDimension = ev->oldSize().height();
break;
case Ideal::Top:
case Ideal::Bottom:
preferredDimension = l->QBoxLayout::minimumSize().width();
actualDimension = size().width();
oldDimension = ev->oldSize().width();
break;
}
if (preferredDimension > actualDimension)
shrink(preferredDimension, actualDimension);
else if (m_shrinked && (originalDimension() < actualDimension))
unshrink();
else if (m_shrinked && actualDimension > oldDimension)
deshrink(preferredDimension, actualDimension);
QWidget::resizeEvent(ev);
}
void ButtonBar::shrink(int preferredDimension, int actualDimension)
{
if (!preferredDimension)
return;
m_shrinked = true;
uint textLength = 0;
QValueList<uint> texts;
uint maxLength = 0;
for (ButtonList::const_iterator it = m_buttons.constBegin(); it != m_buttons.constEnd(); ++it)
{
uint length = (*it)->text().length();
maxLength = length > maxLength ? length : maxLength ;
texts.append(length);
textLength += length;
}
uint newPreferredLength = actualDimension * textLength / preferredDimension;
uint newMaxLength = maxLength;
uint newTextLength;
do {
newMaxLength -= 1;
newTextLength = 0;
for (QValueList<uint>::iterator it = texts.begin(); it != texts.end(); ++it)
{
if (*it > newMaxLength)
*it = newMaxLength;
newTextLength += *it;
}
} while (newTextLength > newPreferredLength);
int i = 0;
for (ButtonList::iterator it = m_buttons.begin(); it != m_buttons.end(); ++it)
{
(*it)->setText(KStringHandler::rsqueeze((*it)->realText(), texts[i++]));
(*it)->updateSize();
}
}
void ButtonBar::deshrink(int preferredDimension, int actualDimension)
{
if (!preferredDimension)
return;
m_shrinked = true;
uint textLength = 0;
QValueList<uint> texts;
uint maxLength = 0;
for (ButtonList::const_iterator it = m_buttons.constBegin(); it != m_buttons.constEnd(); ++it)
{
uint length = (*it)->text().length();
maxLength = length > maxLength ? length : maxLength ;
texts.append(length);
textLength += length;
}
uint newPreferredLength = actualDimension * textLength / preferredDimension;
if (newPreferredLength <= textLength)
return;
uint newTextLength;
uint prevTextLength = 0;
do {
newTextLength = 0;
int i = 0;
for (QValueList<uint>::iterator it = texts.begin(); it != texts.end(); ++it, i++)
{
if (m_buttons[i]->text().contains("..."))
(*it)++;
newTextLength += *it;
}
if (newTextLength == prevTextLength)
break;
prevTextLength = newTextLength;
} while (newTextLength < newPreferredLength);
int i = 0;
for (ButtonList::iterator it = m_buttons.begin(); it != m_buttons.end(); ++it)
{
if (texts[i] >= (*it)->realText().length())
(*it)->setText((*it)->realText());
else
(*it)->setText(KStringHandler::rsqueeze((*it)->realText(), texts[i]));
(*it)->updateSize();
++i;
}
}
void ButtonBar::unshrink()
{
for (ButtonList::iterator it = m_buttons.begin(); it != m_buttons.end(); ++it)
{
(*it)->setText((*it)->realText());
(*it)->updateSize();
}
m_shrinked = false;
}
int ButtonBar::originalDimension()
{
int size = 0;
for (ButtonList::const_iterator it = m_buttons.constBegin(); it != m_buttons.constEnd(); ++it)
{
size += (*it)->sizeHint((*it)->realText()).width();
}
return size;
}
bool ButtonBar::autoResize() const
{
return m_autoResize;
}
void ButtonBar::setAutoResize(bool b)
{
m_autoResize = b;
}
Button *ButtonBar::firstButton()
{
if (!m_buttons.isEmpty())
return m_buttons.first();
return 0;
}
Button *ButtonBar::nextTo(Button *button)
{
ButtonList::iterator it = m_buttons.find(button);
Button *next = 0;
if ((*it) == m_buttons.last())
next = m_buttons.first();
else
{
it++;
next = *it;
}
if (next->isVisible())
return next;
else
return nextTo(next);
}
Button *ButtonBar::prevTo(Button *button)
{
ButtonList::iterator it = m_buttons.find(button);
Button *prev = 0;
if (it == m_buttons.begin())
prev = m_buttons.last();
else
{
it--;
prev = *it;
}
if (prev->isVisible())
return prev;
else
return prevTo(prev);
}
}
#include "buttonbar.moc"
|