1 | /****************************************************************************
|
---|
2 | ** $Id: qdialogbuttons.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Implementation of QDialogButtons class
|
---|
5 | **
|
---|
6 | ** Copyright (C) 2003 Trolltech AS. All rights reserved.
|
---|
7 | **
|
---|
8 | ** This file is part of the widgets module of the Qt GUI Toolkit.
|
---|
9 | **
|
---|
10 | ** This file may be distributed under the terms of the Q Public License
|
---|
11 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
12 | ** LICENSE.QPL included in the packaging of this file.
|
---|
13 | **
|
---|
14 | ** This file may be distributed and/or modified under the terms of the
|
---|
15 | ** GNU General Public License version 2 as published by the Free Software
|
---|
16 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
17 | ** packaging of this file.
|
---|
18 | **
|
---|
19 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
20 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
21 | ** Agreement provided with the Software.
|
---|
22 | **
|
---|
23 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
24 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
25 | **
|
---|
26 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
27 | ** information about Qt Commercial License Agreements.
|
---|
28 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
29 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
30 | **
|
---|
31 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
32 | ** not clear to you.
|
---|
33 | **
|
---|
34 | **********************************************************************/
|
---|
35 |
|
---|
36 | #include "qdialogbuttons_p.h"
|
---|
37 | #ifndef QT_NO_DIALOGBUTTONS
|
---|
38 |
|
---|
39 | #include <qapplication.h>
|
---|
40 | #include <qpushbutton.h>
|
---|
41 | #include <qguardedptr.h>
|
---|
42 | #include <qmap.h>
|
---|
43 | #include <qvariant.h>
|
---|
44 | #ifndef QT_NO_DIALOG
|
---|
45 | #include <qdialog.h>
|
---|
46 | #endif // QT_NO_DIALOG
|
---|
47 | #include <qlayout.h>
|
---|
48 | #include <qstyle.h>
|
---|
49 | #include <qmap.h>
|
---|
50 |
|
---|
51 | struct QDialogButtonsPrivate
|
---|
52 | {
|
---|
53 | QMap<int, QString> text;
|
---|
54 | QMap<QDialogButtons::Button, QWidget *> buttons;
|
---|
55 | QGuardedPtr<QWidget> custom;
|
---|
56 | Q_UINT32 enabled, visible;
|
---|
57 | QDialogButtons::Button def;
|
---|
58 | Qt::Orientation orient;
|
---|
59 | bool questionMode;
|
---|
60 | };
|
---|
61 |
|
---|
62 | #ifndef QT_NO_DIALOG
|
---|
63 | QDialogButtons::QDialogButtons(QDialog *parent, bool autoConnect, Q_UINT32 buttons,
|
---|
64 | Orientation orient, const char *name ) : QWidget(parent, name)
|
---|
65 | {
|
---|
66 | init(buttons, orient);
|
---|
67 | if(parent && autoConnect) {
|
---|
68 | QObject::connect(this, SIGNAL(acceptClicked()), parent, SLOT(accept()));
|
---|
69 | QObject::connect(this, SIGNAL(rejectClicked()), parent, SLOT(reject()));
|
---|
70 | }
|
---|
71 | }
|
---|
72 | #endif // QT_NO_DIALOG
|
---|
73 |
|
---|
74 | QDialogButtons::QDialogButtons(QWidget *parent, Q_UINT32 buttons,
|
---|
75 | Orientation orient, const char *name ) : QWidget(parent, name)
|
---|
76 | {
|
---|
77 | init(buttons, orient);
|
---|
78 | }
|
---|
79 |
|
---|
80 | void
|
---|
81 | QDialogButtons::init(Q_UINT32 buttons, Orientation orient)
|
---|
82 | {
|
---|
83 | if(buttons == All) {
|
---|
84 | qWarning("QDialogButtons: cannot specify All by itself!");
|
---|
85 | buttons = None;
|
---|
86 | }
|
---|
87 | d = new QDialogButtonsPrivate;
|
---|
88 | d->questionMode = FALSE;
|
---|
89 | d->orient = orient;
|
---|
90 | d->def = (Button)style().styleHint(QStyle::SH_DialogButtons_DefaultButton, this);
|
---|
91 | d->enabled = d->visible = buttons;
|
---|
92 | }
|
---|
93 |
|
---|
94 | QDialogButtons::~QDialogButtons()
|
---|
95 | {
|
---|
96 | delete (QWidget *)d->custom;
|
---|
97 | delete d;
|
---|
98 | }
|
---|
99 |
|
---|
100 | void
|
---|
101 | QDialogButtons::setQuestionMode(bool b)
|
---|
102 | {
|
---|
103 | d->questionMode = b;
|
---|
104 | }
|
---|
105 |
|
---|
106 | bool
|
---|
107 | QDialogButtons::questionMode() const
|
---|
108 | {
|
---|
109 | return d->questionMode;
|
---|
110 | }
|
---|
111 |
|
---|
112 | void
|
---|
113 | QDialogButtons::setButtonEnabled(Button button, bool enabled)
|
---|
114 | {
|
---|
115 | if(enabled)
|
---|
116 | d->enabled |= button;
|
---|
117 | else
|
---|
118 | d->enabled ^= button;
|
---|
119 | if(d->buttons.contains(button))
|
---|
120 | d->buttons[button]->setEnabled(enabled);
|
---|
121 | }
|
---|
122 |
|
---|
123 | bool
|
---|
124 | QDialogButtons::isButtonEnabled(Button button) const
|
---|
125 | {
|
---|
126 | return ((int)(d->enabled & button)) == button;
|
---|
127 | }
|
---|
128 |
|
---|
129 | void
|
---|
130 | QDialogButtons::setButtonVisible(Button button, bool visible)
|
---|
131 | {
|
---|
132 | if(visible) {
|
---|
133 | if(d->buttons.contains(button))
|
---|
134 | d->buttons[button]->show();
|
---|
135 | d->visible |= button;
|
---|
136 | } else {
|
---|
137 | if(d->buttons.contains(button))
|
---|
138 | d->buttons[button]->hide();
|
---|
139 | d->visible ^= button;
|
---|
140 | }
|
---|
141 | layoutButtons();
|
---|
142 | }
|
---|
143 |
|
---|
144 | bool
|
---|
145 | QDialogButtons::isButtonVisible(Button button) const
|
---|
146 | {
|
---|
147 | return ((int)(d->visible & button)) == button;
|
---|
148 | }
|
---|
149 |
|
---|
150 | void
|
---|
151 | QDialogButtons::addWidget(QWidget *w)
|
---|
152 | {
|
---|
153 | QBoxLayout *lay = NULL;
|
---|
154 | if(!d->custom) {
|
---|
155 | d->custom = new QWidget(this, "dialog_custom_area");
|
---|
156 | if(orientation() == Horizontal)
|
---|
157 | lay = new QHBoxLayout(d->custom);
|
---|
158 | else
|
---|
159 | lay = new QVBoxLayout(d->custom);
|
---|
160 | layoutButtons();
|
---|
161 | } else {
|
---|
162 | lay = (QBoxLayout*)d->custom->layout();
|
---|
163 | }
|
---|
164 | if(w->parent() != d->custom)
|
---|
165 | w->reparent(d->custom, 0, QPoint(0, 0), TRUE);
|
---|
166 | lay->addWidget(w);
|
---|
167 | }
|
---|
168 |
|
---|
169 | void
|
---|
170 | QDialogButtons::setDefaultButton(Button button)
|
---|
171 | {
|
---|
172 | if(!((int)(d->visible & button) == button)) {
|
---|
173 | qWarning("QDialogButtons: Button '%d' is not visible (so cannot be default)", button);
|
---|
174 | return;
|
---|
175 | }
|
---|
176 | if(d->def != button) {
|
---|
177 | #ifndef QT_NO_PROPERTIES
|
---|
178 | if(d->buttons.contains(d->def))
|
---|
179 | d->buttons[d->def]->setProperty("default", QVariant(FALSE,0));
|
---|
180 | #endif
|
---|
181 | d->def = button;
|
---|
182 | #ifndef QT_NO_PROPERTIES
|
---|
183 | if(d->buttons.contains(d->def))
|
---|
184 | d->buttons[d->def]->setProperty("default", QVariant(FALSE,0));
|
---|
185 | #endif
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | QDialogButtons::Button
|
---|
190 | QDialogButtons::defaultButton() const
|
---|
191 | {
|
---|
192 | return d->def;
|
---|
193 | }
|
---|
194 |
|
---|
195 | void
|
---|
196 | QDialogButtons::setButtonText(Button button, const QString &str)
|
---|
197 | {
|
---|
198 | d->text[button] = str;
|
---|
199 | #ifndef QT_NO_PROPERTIES
|
---|
200 | if(d->buttons.contains(button))
|
---|
201 | d->buttons[button]->setProperty("text", QVariant(str));
|
---|
202 | #endif
|
---|
203 | layoutButtons();
|
---|
204 | }
|
---|
205 |
|
---|
206 | QString
|
---|
207 | QDialogButtons::buttonText(Button b) const
|
---|
208 | {
|
---|
209 | if(d->text.contains(b))
|
---|
210 | return d->text[b];
|
---|
211 | return QString(); //null if it is default..
|
---|
212 | }
|
---|
213 |
|
---|
214 | void
|
---|
215 | QDialogButtons::setOrientation(Orientation orient)
|
---|
216 | {
|
---|
217 | if(d->orient != orient) {
|
---|
218 | d->orient = orient;
|
---|
219 | if(d->custom && d->custom->layout())
|
---|
220 | ((QBoxLayout*)d->custom->layout())->setDirection(orient == Horizontal ? QBoxLayout::LeftToRight :
|
---|
221 | QBoxLayout::TopToBottom);
|
---|
222 | layoutButtons();
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | Qt::Orientation
|
---|
227 | QDialogButtons::orientation() const
|
---|
228 | {
|
---|
229 | return d->orient;
|
---|
230 | }
|
---|
231 |
|
---|
232 | QWidget *
|
---|
233 | QDialogButtons::createButton(Button b)
|
---|
234 | {
|
---|
235 | QPushButton *ret = new QPushButton(this, "qdialog_button");
|
---|
236 | QObject::connect(ret, SIGNAL(clicked()), this, SLOT(handleClicked()));
|
---|
237 | if(d->text.contains(b)) {
|
---|
238 | ret->setText(d->text[b]);
|
---|
239 | } else {
|
---|
240 | switch(b) {
|
---|
241 | case All: {
|
---|
242 | QString txt = buttonText(defaultButton());
|
---|
243 | if(txt.isNull()) {
|
---|
244 | if(defaultButton() == Accept) {
|
---|
245 | if(questionMode())
|
---|
246 | txt = tr("Yes to All");
|
---|
247 | else
|
---|
248 | txt = tr("OK to All");
|
---|
249 | } else {
|
---|
250 | if(questionMode())
|
---|
251 | txt = tr("No to All");
|
---|
252 | else
|
---|
253 | txt = tr("Cancel All");
|
---|
254 | }
|
---|
255 | } else {
|
---|
256 | txt += tr(" to All"); //ick, I can't really do this!!
|
---|
257 | }
|
---|
258 | ret->setText(txt);
|
---|
259 | break; }
|
---|
260 | case Accept:
|
---|
261 | if(questionMode())
|
---|
262 | ret->setText(tr("Yes"));
|
---|
263 | else
|
---|
264 | ret->setText(tr("OK"));
|
---|
265 | break;
|
---|
266 | case Reject:
|
---|
267 | if(questionMode())
|
---|
268 | ret->setText(tr("No"));
|
---|
269 | else
|
---|
270 | ret->setText(tr("Cancel"));
|
---|
271 | break;
|
---|
272 | case Apply:
|
---|
273 | ret->setText(tr("Apply"));
|
---|
274 | break;
|
---|
275 | case Ignore:
|
---|
276 | ret->setText(tr("Ignore"));
|
---|
277 | break;
|
---|
278 | case Retry:
|
---|
279 | ret->setText(tr("Retry"));
|
---|
280 | break;
|
---|
281 | case Abort:
|
---|
282 | ret->setText(tr("Abort"));
|
---|
283 | break;
|
---|
284 | case Help:
|
---|
285 | ret->setText(tr("Help"));
|
---|
286 | break;
|
---|
287 | default:
|
---|
288 | break;
|
---|
289 | }
|
---|
290 | }
|
---|
291 | return ret;
|
---|
292 | }
|
---|
293 |
|
---|
294 | void
|
---|
295 | QDialogButtons::handleClicked()
|
---|
296 | {
|
---|
297 | const QObject *s = sender();
|
---|
298 | if(!s)
|
---|
299 | return;
|
---|
300 |
|
---|
301 | for(QMapIterator<QDialogButtons::Button, QWidget *> it = d->buttons.begin(); it != d->buttons.end(); ++it) {
|
---|
302 | if(it.data() == s) {
|
---|
303 | emit clicked((QDialogButtons::Button)it.key());
|
---|
304 | switch(it.key()) {
|
---|
305 | case Retry:
|
---|
306 | emit retryClicked();
|
---|
307 | break;
|
---|
308 | case Ignore:
|
---|
309 | emit ignoreClicked();
|
---|
310 | break;
|
---|
311 | case Abort:
|
---|
312 | emit abortClicked();
|
---|
313 | break;
|
---|
314 | case All:
|
---|
315 | emit allClicked();
|
---|
316 | break;
|
---|
317 | case Accept:
|
---|
318 | emit acceptClicked();
|
---|
319 | break;
|
---|
320 | case Reject:
|
---|
321 | emit rejectClicked();
|
---|
322 | break;
|
---|
323 | case Apply:
|
---|
324 | emit applyClicked();
|
---|
325 | break;
|
---|
326 | case Help:
|
---|
327 | emit helpClicked();
|
---|
328 | break;
|
---|
329 | default:
|
---|
330 | break;
|
---|
331 | }
|
---|
332 | return;
|
---|
333 | }
|
---|
334 | }
|
---|
335 | }
|
---|
336 |
|
---|
337 | void
|
---|
338 | QDialogButtons::resizeEvent(QResizeEvent *)
|
---|
339 | {
|
---|
340 | layoutButtons();
|
---|
341 | }
|
---|
342 |
|
---|
343 | void
|
---|
344 | QDialogButtons::showEvent(QShowEvent *)
|
---|
345 | {
|
---|
346 | layoutButtons();
|
---|
347 | }
|
---|
348 |
|
---|
349 | void
|
---|
350 | QDialogButtons::styleChanged(QStyle &old)
|
---|
351 | {
|
---|
352 | layoutButtons();
|
---|
353 | QWidget::styleChange(old);
|
---|
354 | }
|
---|
355 |
|
---|
356 | void
|
---|
357 | QDialogButtons::layoutButtons()
|
---|
358 | {
|
---|
359 | if(!isVisible()) //nah..
|
---|
360 | return;
|
---|
361 |
|
---|
362 | QStyle::SubRect rects[] = {
|
---|
363 | QStyle::SR_DialogButtonAccept, QStyle::SR_DialogButtonReject,
|
---|
364 | QStyle::SR_DialogButtonApply, QStyle::SR_DialogButtonHelp,
|
---|
365 | QStyle::SR_DialogButtonCustom, QStyle::SR_DialogButtonAll,
|
---|
366 | QStyle::SR_DialogButtonRetry, QStyle::SR_DialogButtonIgnore,
|
---|
367 | QStyle::SR_DialogButtonAbort };
|
---|
368 | for(unsigned int i = 0; i < (sizeof(rects) / sizeof(rects[0])); i++) {
|
---|
369 | QWidget *w = NULL;
|
---|
370 | if(rects[i] == QStyle::SR_DialogButtonCustom) {
|
---|
371 | w = d->custom;
|
---|
372 | } else {
|
---|
373 | Button b = None;
|
---|
374 | if(rects[i] == QStyle::SR_DialogButtonApply)
|
---|
375 | b = Apply;
|
---|
376 | else if(rects[i] == QStyle::SR_DialogButtonAll)
|
---|
377 | b = All;
|
---|
378 | else if(rects[i] == QStyle::SR_DialogButtonAccept)
|
---|
379 | b = Accept;
|
---|
380 | else if(rects[i] == QStyle::SR_DialogButtonReject)
|
---|
381 | b = Reject;
|
---|
382 | else if(rects[i] == QStyle::SR_DialogButtonHelp)
|
---|
383 | b = Help;
|
---|
384 | else if(rects[i] == QStyle::SR_DialogButtonRetry)
|
---|
385 | b = Retry;
|
---|
386 | else if(rects[i] == QStyle::SR_DialogButtonAbort)
|
---|
387 | b = Abort;
|
---|
388 | else if(rects[i] == QStyle::SR_DialogButtonIgnore)
|
---|
389 | b = Ignore;
|
---|
390 | if(b != None) {
|
---|
391 | if(d->buttons.contains(b)) {
|
---|
392 | w = d->buttons[b];
|
---|
393 | if(!(d->visible & b)) {
|
---|
394 | w->hide();
|
---|
395 | continue;
|
---|
396 | }
|
---|
397 | } else if(d->visible & b) {
|
---|
398 | w = createButton(b);
|
---|
399 | d->buttons.insert(b, w);
|
---|
400 | } else {
|
---|
401 | continue;
|
---|
402 | }
|
---|
403 | if(w) {
|
---|
404 | if(b == d->def) {
|
---|
405 | w->setFocus();
|
---|
406 | #ifndef QT_NO_PROPERTIES
|
---|
407 | w->setProperty("default", QVariant(TRUE,0));
|
---|
408 | #endif
|
---|
409 | }
|
---|
410 | w->setEnabled(d->enabled & b);
|
---|
411 | }
|
---|
412 | }
|
---|
413 | }
|
---|
414 | if(w) {
|
---|
415 | w->show();
|
---|
416 | w->setGeometry(style().subRect(rects[i], this));
|
---|
417 | }
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | QSize
|
---|
422 | QDialogButtons::sizeHint() const
|
---|
423 | {
|
---|
424 | constPolish();
|
---|
425 | QSize s;
|
---|
426 | if(d->custom)
|
---|
427 | s = d->custom->sizeHint();
|
---|
428 | return (style().sizeFromContents(QStyle::CT_DialogButtons, this, s.
|
---|
429 | expandedTo(QApplication::globalStrut())));
|
---|
430 | }
|
---|
431 |
|
---|
432 | QSize
|
---|
433 | QDialogButtons::sizeHint(QDialogButtons::Button button) const
|
---|
434 | {
|
---|
435 | QWidget *w = NULL;
|
---|
436 | if(d->visible & button) {
|
---|
437 | if(!d->buttons.contains(button)) {
|
---|
438 | QDialogButtons *that = (QDialogButtons*)this; //ick, constness..
|
---|
439 | w = that->createButton(button);
|
---|
440 | that->d->buttons.insert(button, w);
|
---|
441 | } else {
|
---|
442 | w = d->buttons[button];
|
---|
443 | }
|
---|
444 | }
|
---|
445 | return w->sizeHint();
|
---|
446 | }
|
---|
447 |
|
---|
448 | QSize
|
---|
449 | QDialogButtons::minimumSizeHint() const
|
---|
450 | {
|
---|
451 | return sizeHint();
|
---|
452 | }
|
---|
453 | #endif
|
---|