dialogs/qdialog.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/****************************************************************************-
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/****************************************************************************
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtGui module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include "qcolordialog.h" -
43#include "qfontdialog.h" -
44#include "qfiledialog.h" -
45 -
46#include "qevent.h" -
47#include "qdesktopwidget.h" -
48#include "qpushbutton.h" -
49#include "qapplication.h" -
50#include "qlayout.h" -
51#include "qsizegrip.h" -
52#include "qwhatsthis.h" -
53#include "qmenu.h" -
54#include "qcursor.h" -
55#include <qpa/qplatformtheme.h> -
56#include "private/qdialog_p.h" -
57#include "private/qguiapplication_p.h" -
58#ifndef QT_NO_ACCESSIBILITY -
59#include "qaccessible.h" -
60#endif -
61 -
62QT_BEGIN_NAMESPACE -
63 -
64static inline int themeDialogType(const QDialog *dialog) -
65{ -
66#ifndef QT_NO_FILEDIALOG -
67 if (qobject_cast<const QFileDialog *>(dialog)) -
68 return QPlatformTheme::FileDialog; -
69#endif -
70#ifndef QT_NO_COLORDIALOG -
71 if (qobject_cast<const QColorDialog *>(dialog)) -
72 return QPlatformTheme::ColorDialog; -
73#endif -
74#ifndef QT_NO_FONTDIALOG -
75 if (qobject_cast<const QFontDialog *>(dialog)) -
76 return QPlatformTheme::FontDialog; -
77#endif -
78 return -1; -
79} -
80 -
81QPlatformDialogHelper *QDialogPrivate::platformHelper() const -
82{ -
83 // Delayed creation of the platform, ensuring that -
84 // that qobject_cast<> on the dialog works in the plugin. -
85 if (!m_platformHelperCreated) { -
86 m_platformHelperCreated = true; -
87 QDialogPrivate *ncThis = const_cast<QDialogPrivate *>(this); -
88 QDialog *dialog = ncThis->q_func(); -
89 const int type = themeDialogType(dialog); -
90 if (type >= 0) { -
91 m_platformHelper = QGuiApplicationPrivate::platformTheme() -
92 ->createPlatformDialogHelper(static_cast<QPlatformTheme::DialogType>(type)); -
93 if (m_platformHelper) { -
94 QObject::connect(m_platformHelper, SIGNAL(accept()), dialog, SLOT(accept())); -
95 QObject::connect(m_platformHelper, SIGNAL(reject()), dialog, SLOT(reject())); -
96 ncThis->initHelper(m_platformHelper); -
97 } -
98 } -
99 } -
100 return m_platformHelper; -
101} -
102 -
103QWindow *QDialogPrivate::parentWindow() const -
104{ -
105 if (const QWidget *parent = q_func()->nativeParentWidget()) -
106 return parent->windowHandle(); -
107 return 0; -
108} -
109 -
110bool QDialogPrivate::setNativeDialogVisible(bool visible) -
111{ -
112 if (QPlatformDialogHelper *helper = platformHelper()) { -
113 if (visible) { -
114 Q_Q(QDialog); -
115 helperPrepareShow(helper); -
116 nativeDialogInUse = helper->show(q->windowFlags(), q->windowModality(), parentWindow()); -
117 } else { -
118 helper->hide(); -
119 } -
120 } -
121 return nativeDialogInUse; -
122} -
123 -
124QVariant QDialogPrivate::styleHint(QPlatformDialogHelper::StyleHint hint) const -
125{ -
126 if (const QPlatformDialogHelper *helper = platformHelper()) -
127 return helper->styleHint(hint); -
128 return QPlatformDialogHelper::defaultStyleHint(hint); -
129} -
130 -
131void QDialogPrivate::deletePlatformHelper() -
132{ -
133 delete m_platformHelper; -
134 m_platformHelper = 0; -
135 m_platformHelperCreated = false; -
136 nativeDialogInUse = false; -
137} -
138 -
139/*! -
140 \class QDialog -
141 \brief The QDialog class is the base class of dialog windows. -
142 -
143 \ingroup dialog-classes -
144 \ingroup abstractwidgets -
145 \inmodule QtWidgets -
146 -
147 A dialog window is a top-level window mostly used for short-term -
148 tasks and brief communications with the user. QDialogs may be -
149 modal or modeless. QDialogs can -
150 provide a \l{#return}{return value}, and they can have \l{#default}{default buttons}. QDialogs can also have a QSizeGrip in their -
151 lower-right corner, using setSizeGripEnabled(). -
152 -
153 Note that QDialog (an any other widget that has type Qt::Dialog) uses -
154 the parent widget slightly differently from other classes in Qt. A -
155 dialog is always a top-level widget, but if it has a parent, its -
156 default location is centered on top of the parent's top-level widget -
157 (if it is not top-level itself). It will also share the parent's -
158 taskbar entry. -
159 -
160 Use the overload of the QWidget::setParent() function to change -
161 the ownership of a QDialog widget. This function allows you to -
162 explicitly set the window flags of the reparented widget; using -
163 the overloaded function will clear the window flags specifying the -
164 window-system properties for the widget (in particular it will -
165 reset the Qt::Dialog flag). -
166 -
167 \section1 Modal Dialogs -
168 -
169 A \b{modal} dialog is a dialog that blocks input to other -
170 visible windows in the same application. Dialogs that are used to -
171 request a file name from the user or that are used to set -
172 application preferences are usually modal. Dialogs can be -
173 \l{Qt::ApplicationModal}{application modal} (the default) or -
174 \l{Qt::WindowModal}{window modal}. -
175 -
176 When an application modal dialog is opened, the user must finish -
177 interacting with the dialog and close it before they can access -
178 any other window in the application. Window modal dialogs only -
179 block access to the window associated with the dialog, allowing -
180 the user to continue to use other windows in an application. -
181 -
182 The most common way to display a modal dialog is to call its -
183 exec() function. When the user closes the dialog, exec() will -
184 provide a useful \l{#return}{return value}. Typically, -
185 to get the dialog to close and return the appropriate value, we -
186 connect a default button, e.g. \uicontrol OK, to the accept() slot and a -
187 \uicontrol Cancel button to the reject() slot. -
188 Alternatively you can call the done() slot with \c Accepted or -
189 \c Rejected. -
190 -
191 An alternative is to call setModal(true) or setWindowModality(), -
192 then show(). Unlike exec(), show() returns control to the caller -
193 immediately. Calling setModal(true) is especially useful for -
194 progress dialogs, where the user must have the ability to interact -
195 with the dialog, e.g. to cancel a long running operation. If you -
196 use show() and setModal(true) together to perform a long operation, -
197 you must call QApplication::processEvents() periodically during -
198 processing to enable the user to interact with the dialog. (See -
199 QProgressDialog.) -
200 -
201 \section1 Modeless Dialogs -
202 -
203 A \b{modeless} dialog is a dialog that operates -
204 independently of other windows in the same application. Find and -
205 replace dialogs in word-processors are often modeless to allow the -
206 user to interact with both the application's main window and with -
207 the dialog. -
208 -
209 Modeless dialogs are displayed using show(), which returns control -
210 to the caller immediately. -
211 -
212 If you invoke the \l{QWidget::show()}{show()} function after hiding -
213 a dialog, the dialog will be displayed in its original position. This is -
214 because the window manager decides the position for windows that -
215 have not been explicitly placed by the programmer. To preserve the -
216 position of a dialog that has been moved by the user, save its position -
217 in your \l{QWidget::closeEvent()}{closeEvent()} handler and then -
218 move the dialog to that position, before showing it again. -
219 -
220 \target default -
221 \section1 Default Button -
222 -
223 A dialog's \e default button is the button that's pressed when the -
224 user presses Enter (Return). This button is used to signify that -
225 the user accepts the dialog's settings and wants to close the -
226 dialog. Use QPushButton::setDefault(), QPushButton::isDefault() -
227 and QPushButton::autoDefault() to set and control the dialog's -
228 default button. -
229 -
230 \target escapekey -
231 \section1 Escape Key -
232 -
233 If the user presses the Esc key in a dialog, QDialog::reject() -
234 will be called. This will cause the window to close: The \l{QCloseEvent}{close event} cannot be \l{QCloseEvent::ignore()}{ignored}. -
235 -
236 \section1 Extensibility -
237 -
238 Extensibility is the ability to show the dialog in two ways: a -
239 partial dialog that shows the most commonly used options, and a -
240 full dialog that shows all the options. Typically an extensible -
241 dialog will initially appear as a partial dialog, but with a -
242 \uicontrol More toggle button. If the user presses the \uicontrol More button down, -
243 the dialog is expanded. The \l{Extension Example} shows how to achieve -
244 extensible dialogs using Qt. -
245 -
246 \target return -
247 \section1 Return Value (Modal Dialogs) -
248 -
249 Modal dialogs are often used in situations where a return value is -
250 required, e.g. to indicate whether the user pressed \uicontrol OK or -
251 \uicontrol Cancel. A dialog can be closed by calling the accept() or the -
252 reject() slots, and exec() will return \c Accepted or \c Rejected -
253 as appropriate. The exec() call returns the result of the dialog. -
254 The result is also available from result() if the dialog has not -
255 been destroyed. -
256 -
257 In order to modify your dialog's close behavior, you can reimplement -
258 the functions accept(), reject() or done(). The -
259 \l{QWidget::closeEvent()}{closeEvent()} function should only be -
260 reimplemented to preserve the dialog's position or to override the -
261 standard close or reject behavior. -
262 -
263 \target examples -
264 \section1 Code Examples -
265 -
266 A modal dialog: -
267 -
268 \snippet dialogs/dialogs.cpp 1 -
269 -
270 A modeless dialog: -
271 -
272 \snippet dialogs/dialogs.cpp 0 -
273 -
274 \sa QDialogButtonBox, QTabWidget, QWidget, QProgressDialog, -
275 {fowler}{GUI Design Handbook: Dialogs, Standard}, {Extension Example}, -
276 {Standard Dialogs Example} -
277*/ -
278 -
279/*! \enum QDialog::DialogCode -
280 -
281 The value returned by a modal dialog. -
282 -
283 \value Accepted -
284 \value Rejected -
285*/ -
286 -
287/*! -
288 \property QDialog::sizeGripEnabled -
289 \brief whether the size grip is enabled -
290 -
291 A QSizeGrip is placed in the bottom-right corner of the dialog when this -
292 property is enabled. By default, the size grip is disabled. -
293*/ -
294 -
295 -
296/*! -
297 Constructs a dialog with parent \a parent. -
298 -
299 A dialog is always a top-level widget, but if it has a parent, its -
300 default location is centered on top of the parent. It will also -
301 share the parent's taskbar entry. -
302 -
303 The widget flags \a f are passed on to the QWidget constructor. -
304 If, for example, you don't want a What's This button in the title bar -
305 of the dialog, pass Qt::WindowTitleHint | Qt::WindowSystemMenuHint in \a f. -
306 -
307 \sa QWidget::setWindowFlags() -
308*/ -
309 -
310QDialog::QDialog(QWidget *parent, Qt::WindowFlags f) -
311 : QWidget(*new QDialogPrivate, parent, -
312 f | ((f & Qt::WindowType_Mask) == 0 ? Qt::Dialog : Qt::WindowType(0))) -
313{ -
314} -
315 -
316/*! -
317 \overload -
318 \internal -
319*/ -
320QDialog::QDialog(QDialogPrivate &dd, QWidget *parent, Qt::WindowFlags f) -
321 : QWidget(dd, parent, f | ((f & Qt::WindowType_Mask) == 0 ? Qt::Dialog : Qt::WindowType(0))) -
322{ -
323} -
324 -
325/*! -
326 Destroys the QDialog, deleting all its children. -
327*/ -
328 -
329QDialog::~QDialog() -
330{ -
331 QT_TRY { -
332 // Need to hide() here, as our (to-be) overridden hide() -
333 // will not be called in ~QWidget. -
334 hide(); -
335 } QT_CATCH(...) { -
336 // we're in the destructor - just swallow the exception -
337 } -
338} -
339 -
340/*! -
341 \internal -
342 This function is called by the push button \a pushButton when it -
343 becomes the default button. If \a pushButton is 0, the dialogs -
344 default default button becomes the default button. This is what a -
345 push button calls when it loses focus. -
346*/ -
347void QDialogPrivate::setDefault(QPushButton *pushButton) -
348{ -
349 Q_Q(QDialog); -
350 bool hasMain = false; -
351 QList<QPushButton*> list = q->findChildren<QPushButton*>(); -
352 for (int i=0; i<list.size(); ++i) { -
353 QPushButton *pb = list.at(i); -
354 if (pb->window() == q) { -
355 if (pb == mainDef) -
356 hasMain = true; -
357 if (pb != pushButton) -
358 pb->setDefault(false); -
359 } -
360 } -
361 if (!pushButton && hasMain) -
362 mainDef->setDefault(true); -
363 if (!hasMain) -
364 mainDef = pushButton; -
365} -
366 -
367/*! -
368 \internal -
369 This function sets the default default push button to \a pushButton. -
370 This function is called by QPushButton::setDefault(). -
371*/ -
372void QDialogPrivate::setMainDefault(QPushButton *pushButton) -
373{ -
374 mainDef = 0; -
375 setDefault(pushButton); -
376} -
377 -
378/*! -
379 \internal -
380 Hides the default button indicator. Called when non auto-default -
381 push button get focus. -
382 */ -
383void QDialogPrivate::hideDefault() -
384{ -
385 Q_Q(QDialog); -
386 QList<QPushButton*> list = q->findChildren<QPushButton*>(); -
387 for (int i=0; i<list.size(); ++i) { -
388 list.at(i)->setDefault(false); -
389 } -
390} -
391 -
392void QDialogPrivate::resetModalitySetByOpen() -
393{ -
394 Q_Q(QDialog); -
395 if (resetModalityTo != -1 && !q->testAttribute(Qt::WA_SetWindowModality)) { -
396 // open() changed the window modality and the user didn't touch it afterwards; restore it -
397 q->setWindowModality(Qt::WindowModality(resetModalityTo)); -
398 q->setAttribute(Qt::WA_SetWindowModality, wasModalitySet); -
399#ifdef Q_OS_MAC -
400 Q_ASSERT(resetModalityTo != Qt::WindowModal); -
401 q->setParent(q->parentWidget(), Qt::Dialog); -
402#endif -
403 } -
404 resetModalityTo = -1; -
405} -
406 -
407#if defined(Q_OS_WINCE) -
408#ifdef Q_OS_WINCE_WM -
409void QDialogPrivate::_q_doneAction() -
410{ -
411 //Done... -
412 QApplication::postEvent(q_func(), new QEvent(QEvent::OkRequest)); -
413} -
414#endif -
415 -
416/*! -
417 \reimp -
418*/ -
419bool QDialog::event(QEvent *e) -
420{ -
421 bool result = QWidget::event(e); -
422#ifdef Q_OS_WINCE -
423 if (e->type() == QEvent::OkRequest) { -
424 accept(); -
425 result = true; -
426 } -
427#endif -
428 return result; -
429} -
430#endif -
431 -
432/*! -
433 In general returns the modal dialog's result code, \c Accepted or -
434 \c Rejected. -
435 -
436 \note When called on a QMessageBox instance, the returned value is a -
437 value of the \l QMessageBox::StandardButton enum. -
438 -
439 Do not call this function if the dialog was constructed with the -
440 Qt::WA_DeleteOnClose attribute. -
441*/ -
442int QDialog::result() const -
443{ -
444 Q_D(const QDialog); -
445 return d->rescode; -
446} -
447 -
448/*! -
449 \fn void QDialog::setResult(int i) -
450 -
451 Sets the modal dialog's result code to \a i. -
452 -
453 \note We recommend that you use one of the values defined by -
454 QDialog::DialogCode. -
455*/ -
456void QDialog::setResult(int r) -
457{ -
458 Q_D(QDialog); -
459 d->rescode = r; -
460} -
461 -
462/*! -
463 \since 4.5 -
464 -
465 Shows the dialog as a \l{QDialog#Modal Dialogs}{window modal dialog}, -
466 returning immediately. -
467 -
468 \sa exec(), show(), result(), setWindowModality() -
469*/ -
470void QDialog::open() -
471{ -
472 Q_D(QDialog); -
473 -
474 Qt::WindowModality modality = windowModality(); -
475 if (modality != Qt::WindowModal) { -
476 d->resetModalityTo = modality; -
477 d->wasModalitySet = testAttribute(Qt::WA_SetWindowModality); -
478 setWindowModality(Qt::WindowModal); -
479 setAttribute(Qt::WA_SetWindowModality, false); -
480#ifdef Q_OS_MAC -
481 setParent(parentWidget(), Qt::Sheet); -
482#endif -
483 } -
484 -
485 setResult(0); -
486 show(); -
487} -
488 -
489/*! -
490 Shows the dialog as a \l{QDialog#Modal Dialogs}{modal dialog}, -
491 blocking until the user closes it. The function returns a \l -
492 DialogCode result. -
493 -
494 If the dialog is \l{Qt::ApplicationModal}{application modal}, users cannot -
495 interact with any other window in the same application until they close -
496 the dialog. If the dialog is \l{Qt::ApplicationModal}{window modal}, only -
497 interaction with the parent window is blocked while the dialog is open. -
498 By default, the dialog is application modal. -
499 -
500 \sa open(), show(), result(), setWindowModality() -
501*/ -
502 -
503int QDialog::exec() -
504{ -
505 Q_D(QDialog); -
506 -
507 if (d->eventLoop) { -
508 qWarning("QDialog::exec: Recursive call detected"); -
509 return -1; -
510 } -
511 -
512 bool deleteOnClose = testAttribute(Qt::WA_DeleteOnClose); -
513 setAttribute(Qt::WA_DeleteOnClose, false); -
514 -
515 d->resetModalitySetByOpen(); -
516 -
517 bool wasShowModal = testAttribute(Qt::WA_ShowModal); -
518 setAttribute(Qt::WA_ShowModal, true); -
519 setResult(0); -
520 -
521 show(); -
522 -
523 QPointer<QDialog> guard = this; -
524 if (d->nativeDialogInUse) { -
525 d->platformHelper()->exec(); -
526 } else { -
527 QEventLoop eventLoop; -
528 d->eventLoop = &eventLoop; -
529 (void) eventLoop.exec(QEventLoop::DialogExec); -
530 } -
531 if (guard.isNull()) -
532 return QDialog::Rejected; -
533 d->eventLoop = 0; -
534 -
535 setAttribute(Qt::WA_ShowModal, wasShowModal); -
536 -
537 int res = result(); -
538 if (d->nativeDialogInUse) -
539 d->helperDone(static_cast<QDialog::DialogCode>(res), d->platformHelper()); -
540 if (deleteOnClose) -
541 delete this; -
542 return res; -
543} -
544 -
545/*! -
546 Closes the dialog and sets its result code to \a r. If this dialog -
547 is shown with exec(), done() causes the local event loop to finish, -
548 and exec() to return \a r. -
549 -
550 As with QWidget::close(), done() deletes the dialog if the -
551 Qt::WA_DeleteOnClose flag is set. If the dialog is the application's -
552 main widget, the application terminates. If the dialog is the -
553 last window closed, the QApplication::lastWindowClosed() signal is -
554 emitted. -
555 -
556 \sa accept(), reject(), QApplication::activeWindow(), QApplication::quit() -
557*/ -
558 -
559void QDialog::done(int r) -
560{ -
561 Q_D(QDialog); -
562 hide(); -
563 setResult(r); -
564 -
565 d->close_helper(QWidgetPrivate::CloseNoEvent); -
566 d->resetModalitySetByOpen(); -
567 -
568 emit finished(r); -
569 if (r == Accepted) -
570 emit accepted(); -
571 else if (r == Rejected) -
572 emit rejected(); -
573} -
574 -
575/*! -
576 Hides the modal dialog and sets the result code to \c Accepted. -
577 -
578 \sa reject(), done() -
579*/ -
580 -
581void QDialog::accept() -
582{ -
583 done(Accepted); -
584} -
585 -
586/*! -
587 Hides the modal dialog and sets the result code to \c Rejected. -
588 -
589 \sa accept(), done() -
590*/ -
591 -
592void QDialog::reject() -
593{ -
594 done(Rejected); -
595} -
596 -
597/*! \reimp */ -
598bool QDialog::eventFilter(QObject *o, QEvent *e) -
599{ -
600 return QWidget::eventFilter(o, e); -
601} -
602 -
603/***************************************************************************** -
604 Event handlers -
605 *****************************************************************************/ -
606 -
607#ifndef QT_NO_CONTEXTMENU -
608/*! \reimp */ -
609void QDialog::contextMenuEvent(QContextMenuEvent *e) -
610{ -
611#if defined(QT_NO_WHATSTHIS) || defined(QT_NO_MENU) -
612 Q_UNUSED(e); -
613#else -
614 QWidget *w = childAt(e->pos()); -
615 if (!w) { -
616 w = rect().contains(e->pos()) ? this : 0; -
617 if (!w) -
618 return; -
619 } -
620 while (w && w->whatsThis().size() == 0 && !w->testAttribute(Qt::WA_CustomWhatsThis)) -
621 w = w->isWindow() ? 0 : w->parentWidget(); -
622 if (w) { -
623 QPointer<QMenu> p = new QMenu(this); -
624 QAction *wt = p.data()->addAction(tr("What's This?")); -
625 if (p.data()->exec(e->globalPos()) == wt) { -
626 QHelpEvent e(QEvent::WhatsThis, w->rect().center(), -
627 w->mapToGlobal(w->rect().center())); -
628 QApplication::sendEvent(w, &e); -
629 } -
630 delete p.data(); -
631 } -
632#endif -
633} -
634#endif // QT_NO_CONTEXTMENU -
635 -
636/*! \reimp */ -
637void QDialog::keyPressEvent(QKeyEvent *e) -
638{ -
639 // Calls reject() if Escape is pressed. Simulates a button -
640 // click for the default button if Enter is pressed. Move focus -
641 // for the arrow keys. Ignore the rest. -
642#ifdef Q_OS_MAC -
643 if(e->modifiers() == Qt::ControlModifier && e->key() == Qt::Key_Period) { -
644 reject(); -
645 } else -
646#endif -
647 if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) { -
648 switch (e->key()) { -
649 case Qt::Key_Enter: -
650 case Qt::Key_Return: { -
651 QList<QPushButton*> list = findChildren<QPushButton*>(); -
652 for (int i=0; i<list.size(); ++i) { -
653 QPushButton *pb = list.at(i); -
654 if (pb->isDefault() && pb->isVisible()) { -
655 if (pb->isEnabled()) -
656 pb->click(); -
657 return; -
658 } -
659 } -
660 } -
661 break; -
662 case Qt::Key_Escape: -
663 reject(); -
664 break; -
665 default: -
666 e->ignore(); -
667 return; -
668 } -
669 } else { -
670 e->ignore(); -
671 } -
672} -
673 -
674/*! \reimp */ -
675void QDialog::closeEvent(QCloseEvent *e) -
676{ -
677#ifndef QT_NO_WHATSTHIS -
678 if (isModal() && QWhatsThis::inWhatsThisMode()) -
679 QWhatsThis::leaveWhatsThisMode(); -
680#endif -
681 if (isVisible()) { -
682 QPointer<QObject> that = this; -
683 reject(); -
684 if (that && isVisible()) -
685 e->ignore(); -
686 } else { -
687 e->accept(); -
688 } -
689} -
690 -
691/***************************************************************************** -
692 Geometry management. -
693 *****************************************************************************/ -
694 -
695/*! \reimp -
696*/ -
697 -
698void QDialog::setVisible(bool visible) -
699{ -
700 Q_D(QDialog); -
701 if (visible) { -
702 if (testAttribute(Qt::WA_WState_ExplicitShowHide) && !testAttribute(Qt::WA_WState_Hidden)) -
703 return; -
704 -
705 if (!testAttribute(Qt::WA_Moved)) { -
706 Qt::WindowStates state = windowState(); -
707 adjustPosition(parentWidget()); -
708 setAttribute(Qt::WA_Moved, false); // not really an explicit position -
709 if (state != windowState()) -
710 setWindowState(state); -
711 } -
712 QWidget::setVisible(visible); -
713 showExtension(d->doShowExtension); -
714 QWidget *fw = window()->focusWidget(); -
715 if (!fw) -
716 fw = this; -
717 -
718 /* -
719 The following block is to handle a special case, and does not -
720 really follow propper logic in concern of autoDefault and TAB -
721 order. However, it's here to ease usage for the users. If a -
722 dialog has a default QPushButton, and first widget in the TAB -
723 order also is a QPushButton, then we give focus to the main -
724 default QPushButton. This simplifies code for the developers, -
725 and actually catches most cases... If not, then they simply -
726 have to use [widget*]->setFocus() themselves... -
727 */ -
728 if (d->mainDef && fw->focusPolicy() == Qt::NoFocus) { -
729 QWidget *first = fw; -
730 while ((first = first->nextInFocusChain()) != fw && first->focusPolicy() == Qt::NoFocus) -
731 ; -
732 if (first != d->mainDef && qobject_cast<QPushButton*>(first)) -
733 d->mainDef->setFocus(); -
734 } -
735 if (!d->mainDef && isWindow()) { -
736 QWidget *w = fw; -
737 while ((w = w->nextInFocusChain()) != fw) { -
738 QPushButton *pb = qobject_cast<QPushButton *>(w); -
739 if (pb && pb->autoDefault() && pb->focusPolicy() != Qt::NoFocus) { -
740 pb->setDefault(true); -
741 break; -
742 } -
743 } -
744 } -
745 if (fw && !fw->hasFocus()) { -
746 QFocusEvent e(QEvent::FocusIn, Qt::TabFocusReason); -
747 QApplication::sendEvent(fw, &e); -
748 } -
749 -
750#ifndef QT_NO_ACCESSIBILITY -
751 QAccessibleEvent event(this, QAccessible::DialogStart); -
752 QAccessible::updateAccessibility(&event); -
753#endif -
754 -
755 } else { -
756 if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden)) -
757 return; -
758 -
759#ifndef QT_NO_ACCESSIBILITY -
760 if (isVisible()) { -
761 QAccessibleEvent event(this, QAccessible::DialogEnd); -
762 QAccessible::updateAccessibility(&event); -
763 } -
764#endif -
765 -
766 // Reimplemented to exit a modal event loop when the dialog is hidden. -
767 QWidget::setVisible(visible); -
768 if (d->eventLoop) -
769 d->eventLoop->exit(); -
770 } -
771 if (d->mainDef && isActiveWindow() -
772 && d->styleHint(QPlatformDialogHelper::SnapToDefaultButton).toBool()) -
773 QCursor::setPos(d->mainDef->mapToGlobal(d->mainDef->rect().center())); -
774} -
775 -
776/*!\reimp */ -
777void QDialog::showEvent(QShowEvent *event) -
778{ -
779 if (!event->spontaneous() && !testAttribute(Qt::WA_Moved)) { -
780 Qt::WindowStates state = windowState(); -
781 adjustPosition(parentWidget()); -
782 setAttribute(Qt::WA_Moved, false); // not really an explicit position -
783 if (state != windowState()) -
784 setWindowState(state); -
785 } -
786} -
787 -
788/*! \internal */ -
789void QDialog::adjustPosition(QWidget* w) -
790{ -
791 -
792 if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme())
partially evaluated: const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()
TRUEFALSE
yes
Evaluation Count:458
no
Evaluation Count:0
0-458
793 if (theme->themeHint(QPlatformTheme::WindowAutoPlacement).toBool())
partially evaluated: theme->themeHint(QPlatformTheme::WindowAutoPlacement).toBool()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:458
0-458
794 return;
never executed: return;
0
795 QPoint p(0, 0);
executed (the execution status of this line is deduced): QPoint p(0, 0);
-
796 int extraw = 0, extrah = 0, scrn = 0;
executed (the execution status of this line is deduced): int extraw = 0, extrah = 0, scrn = 0;
-
797 if (w)
evaluated: w
TRUEFALSE
yes
Evaluation Count:78
yes
Evaluation Count:380
78-380
798 w = w->window();
executed: w = w->window();
Execution Count:78
78
799 QRect desk;
executed (the execution status of this line is deduced): QRect desk;
-
800 if (w) {
evaluated: w
TRUEFALSE
yes
Evaluation Count:78
yes
Evaluation Count:380
78-380
801 scrn = QApplication::desktop()->screenNumber(w);
executed (the execution status of this line is deduced): scrn = QApplication::desktop()->screenNumber(w);
-
802 } else if (QApplication::desktop()->isVirtualDesktop()) {
executed: }
Execution Count:78
partially evaluated: QApplication::desktop()->isVirtualDesktop()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:380
0-380
803 scrn = QApplication::desktop()->screenNumber(QCursor::pos());
never executed (the execution status of this line is deduced): scrn = QApplication::desktop()->screenNumber(QCursor::pos());
-
804 } else {
never executed: }
0
805 scrn = QApplication::desktop()->screenNumber(this);
executed (the execution status of this line is deduced): scrn = QApplication::desktop()->screenNumber(this);
-
806 }
executed: }
Execution Count:380
380
807 desk = QApplication::desktop()->availableGeometry(scrn);
executed (the execution status of this line is deduced): desk = QApplication::desktop()->availableGeometry(scrn);
-
808 -
809 QWidgetList list = QApplication::topLevelWidgets();
executed (the execution status of this line is deduced): QWidgetList list = QApplication::topLevelWidgets();
-
810 for (int i = 0; (extraw == 0 || extrah == 0) && i < list.size(); ++i) {
evaluated: extraw == 0
TRUEFALSE
yes
Evaluation Count:1133
yes
Evaluation Count:60
partially evaluated: extrah == 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:60
evaluated: i < list.size()
TRUEFALSE
yes
Evaluation Count:735
yes
Evaluation Count:398
0-1133
811 QWidget * current = list.at(i);
executed (the execution status of this line is deduced): QWidget * current = list.at(i);
-
812 if (current->isVisible()) {
evaluated: current->isVisible()
TRUEFALSE
yes
Evaluation Count:292
yes
Evaluation Count:443
292-443
813 int framew = current->geometry().x() - current->x();
executed (the execution status of this line is deduced): int framew = current->geometry().x() - current->x();
-
814 int frameh = current->geometry().y() - current->y();
executed (the execution status of this line is deduced): int frameh = current->geometry().y() - current->y();
-
815 -
816 extraw = qMax(extraw, framew);
executed (the execution status of this line is deduced): extraw = qMax(extraw, framew);
-
817 extrah = qMax(extrah, frameh);
executed (the execution status of this line is deduced): extrah = qMax(extrah, frameh);
-
818 }
executed: }
Execution Count:292
292
819 }
executed: }
Execution Count:735
735
820 -
821 // sanity check for decoration frames. With embedding, we -
822 // might get extraordinary values -
823 if (extraw == 0 || extrah == 0 || extraw >= 10 || extrah >= 40) {
evaluated: extraw == 0
TRUEFALSE
yes
Evaluation Count:398
yes
Evaluation Count:60
partially evaluated: extrah == 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:60
partially evaluated: extraw >= 10
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:60
partially evaluated: extrah >= 40
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:60
0-398
824 extrah = 40;
executed (the execution status of this line is deduced): extrah = 40;
-
825 extraw = 10;
executed (the execution status of this line is deduced): extraw = 10;
-
826 }
executed: }
Execution Count:398
398
827 -
828 -
829 if (w) {
evaluated: w
TRUEFALSE
yes
Evaluation Count:78
yes
Evaluation Count:380
78-380
830 // Use mapToGlobal rather than geometry() in case w might-
// be embedded in another application
/ Use pos() if the widget is embedded into a native window
831 QPoint pp;
executed (the execution status of this line is deduced): QPoint pp;
-
832 if (w->windowHandle() && w->windowHandle()->property("_q_embedded_native_parent_handle").value<WId>())
evaluated: w->windowHandle()
TRUEFALSE
yes
Evaluation Count:46
yes
Evaluation Count:32
partially evaluated: w->windowHandle()->property("_q_embedded_native_parent_handle").value<WId>()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:46
0-46
833 pp = w->pos();
never executed: pp = w->pos();
0
834 else -
835 pp = w->mapToGlobal(QPoint(0,0));
executed: pp = w->mapToGlobal(QPoint(0,0));
Execution Count:78
78
836 p = QPoint(pp.x() + w->width()/2,
executed (the execution status of this line is deduced): p = QPoint(pp.x() + w->width()/2,
-
837 pp.y() + w->height()/ 2);
executed (the execution status of this line is deduced): pp.y() + w->height()/ 2);
-
838 } else {
executed: }
Execution Count:78
78
839 // p = middle of the desktop -
840 p = QPoint(desk.x() + desk.width()/2, desk.y() + desk.height()/2);
executed (the execution status of this line is deduced): p = QPoint(desk.x() + desk.width()/2, desk.y() + desk.height()/2);
-
841 }
executed: }
Execution Count:380
380
842 -
843 // p = origin of this -
844 p = QPoint(p.x()-width()/2 - extraw,
executed (the execution status of this line is deduced): p = QPoint(p.x()-width()/2 - extraw,
-
845 p.y()-height()/2 - extrah);
executed (the execution status of this line is deduced): p.y()-height()/2 - extrah);
-
846 -
847 -
848 if (p.x() + extraw + width() > desk.x() + desk.width())
evaluated: p.x() + extraw + width() > desk.x() + desk.width()
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:446
12-446
849 p.setX(desk.x() + desk.width() - width() - extraw);
executed: p.setX(desk.x() + desk.width() - width() - extraw);
Execution Count:12
12
850 if (p.x() < desk.x())
evaluated: p.x() < desk.x()
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:446
12-446
851 p.setX(desk.x());
executed: p.setX(desk.x());
Execution Count:12
12
852 -
853 if (p.y() + extrah + height() > desk.y() + desk.height())
evaluated: p.y() + extrah + height() > desk.y() + desk.height()
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:446
12-446
854 p.setY(desk.y() + desk.height() - height() - extrah);
executed: p.setY(desk.y() + desk.height() - height() - extrah);
Execution Count:12
12
855 if (p.y() < desk.y())
evaluated: p.y() < desk.y()
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:446
12-446
856 p.setY(desk.y());
executed: p.setY(desk.y());
Execution Count:12
12
857 -
858 move(p);
executed (the execution status of this line is deduced): move(p);
-
859}
executed: }
Execution Count:458
458
860 -
861/*! -
862 \obsolete -
863 -
864 If \a orientation is Qt::Horizontal, the extension will be displayed -
865 to the right of the dialog's main area. If \a orientation is -
866 Qt::Vertical, the extension will be displayed below the dialog's main -
867 area. -
868 -
869 Instead of using this functionality, we recommend that you simply call -
870 show() or hide() on the part of the dialog that you want to use as an -
871 extension. See the \l{Extension Example} for details. -
872 -
873 \sa setExtension() -
874*/ -
875void QDialog::setOrientation(Qt::Orientation orientation) -
876{ -
877 Q_D(QDialog); -
878 d->orientation = orientation; -
879} -
880 -
881/*! -
882 \obsolete -
883 -
884 Returns the dialog's extension orientation. -
885 -
886 Instead of using this functionality, we recommend that you simply call -
887 show() or hide() on the part of the dialog that you want to use as an -
888 extension. See the \l{Extension Example} for details. -
889 -
890 \sa extension() -
891*/ -
892Qt::Orientation QDialog::orientation() const -
893{ -
894 Q_D(const QDialog); -
895 return d->orientation; -
896} -
897 -
898/*! -
899 \obsolete -
900 -
901 Sets the widget, \a extension, to be the dialog's extension, -
902 deleting any previous extension. The dialog takes ownership of the -
903 extension. Note that if 0 is passed any existing extension will be -
904 deleted. This function must only be called while the dialog is hidden. -
905 -
906 Instead of using this functionality, we recommend that you simply call -
907 show() or hide() on the part of the dialog that you want to use as an -
908 extension. See the \l{Extension Example} for details. -
909 -
910 \sa showExtension(), setOrientation() -
911*/ -
912void QDialog::setExtension(QWidget* extension) -
913{ -
914 Q_D(QDialog); -
915 delete d->extension; -
916 d->extension = extension; -
917 -
918 if (!extension) -
919 return; -
920 -
921 if (extension->parentWidget() != this) -
922 extension->setParent(this); -
923 extension->hide(); -
924} -
925 -
926/*! -
927 \obsolete -
928 -
929 Returns the dialog's extension or 0 if no extension has been -
930 defined. -
931 -
932 Instead of using this functionality, we recommend that you simply call -
933 show() or hide() on the part of the dialog that you want to use as an -
934 extension. See the \l{Extension Example} for details. -
935 -
936 \sa showExtension(), setOrientation() -
937*/ -
938QWidget* QDialog::extension() const -
939{ -
940 Q_D(const QDialog); -
941 return d->extension; -
942} -
943 -
944 -
945/*! -
946 \obsolete -
947 -
948 If \a showIt is true, the dialog's extension is shown; otherwise the -
949 extension is hidden. -
950 -
951 Instead of using this functionality, we recommend that you simply call -
952 show() or hide() on the part of the dialog that you want to use as an -
953 extension. See the \l{Extension Example} for details. -
954 -
955 \sa show(), setExtension(), setOrientation() -
956*/ -
957void QDialog::showExtension(bool showIt) -
958{ -
959 Q_D(QDialog); -
960 d->doShowExtension = showIt; -
961 if (!d->extension) -
962 return; -
963 if (!testAttribute(Qt::WA_WState_Visible)) -
964 return; -
965 if (d->extension->isVisible() == showIt) -
966 return; -
967 -
968 if (showIt) { -
969 d->size = size(); -
970 d->min = minimumSize(); -
971 d->max = maximumSize(); -
972 if (layout()) -
973 layout()->setEnabled(false); -
974 QSize s(d->extension->sizeHint() -
975 .expandedTo(d->extension->minimumSize()) -
976 .boundedTo(d->extension->maximumSize())); -
977 if (d->orientation == Qt::Horizontal) { -
978 int h = qMax(height(), s.height()); -
979 d->extension->setGeometry(width(), 0, s.width(), h); -
980 setFixedSize(width() + s.width(), h); -
981 } else { -
982 int w = qMax(width(), s.width()); -
983 d->extension->setGeometry(0, height(), w, s.height()); -
984 setFixedSize(w, height() + s.height()); -
985 } -
986 d->extension->show(); -
987#ifndef QT_NO_SIZEGRIP -
988 const bool sizeGripEnabled = isSizeGripEnabled(); -
989 setSizeGripEnabled(false); -
990 d->sizeGripEnabled = sizeGripEnabled; -
991#endif -
992 } else { -
993 d->extension->hide(); -
994 // workaround for CDE window manager that won't shrink with (-1,-1) -
995 setMinimumSize(d->min.expandedTo(QSize(1, 1))); -
996 setMaximumSize(d->max); -
997 resize(d->size); -
998 if (layout()) -
999 layout()->setEnabled(true); -
1000#ifndef QT_NO_SIZEGRIP -
1001 setSizeGripEnabled(d->sizeGripEnabled); -
1002#endif -
1003 } -
1004} -
1005 -
1006 -
1007/*! \reimp */ -
1008QSize QDialog::sizeHint() const -
1009{ -
1010 Q_D(const QDialog); -
1011 if (d->extension) { -
1012 if (d->orientation == Qt::Horizontal) -
1013 return QSize(QWidget::sizeHint().width(), -
1014 qMax(QWidget::sizeHint().height(),d->extension->sizeHint().height())); -
1015 else -
1016 return QSize(qMax(QWidget::sizeHint().width(), d->extension->sizeHint().width()), -
1017 QWidget::sizeHint().height()); -
1018 } -
1019 return QWidget::sizeHint(); -
1020} -
1021 -
1022 -
1023/*! \reimp */ -
1024QSize QDialog::minimumSizeHint() const -
1025{ -
1026 Q_D(const QDialog); -
1027 if (d->extension) { -
1028 if (d->orientation == Qt::Horizontal) -
1029 return QSize(QWidget::minimumSizeHint().width(), -
1030 qMax(QWidget::minimumSizeHint().height(), d->extension->minimumSizeHint().height())); -
1031 else -
1032 return QSize(qMax(QWidget::minimumSizeHint().width(), d->extension->minimumSizeHint().width()), -
1033 QWidget::minimumSizeHint().height()); -
1034 } -
1035 -
1036 return QWidget::minimumSizeHint(); -
1037} -
1038 -
1039/*! -
1040 \property QDialog::modal -
1041 \brief whether show() should pop up the dialog as modal or modeless -
1042 -
1043 By default, this property is false and show() pops up the dialog -
1044 as modeless. Setting his property to true is equivalent to setting -
1045 QWidget::windowModality to Qt::ApplicationModal. -
1046 -
1047 exec() ignores the value of this property and always pops up the -
1048 dialog as modal. -
1049 -
1050 \sa QWidget::windowModality, show(), exec() -
1051*/ -
1052 -
1053void QDialog::setModal(bool modal) -
1054{ -
1055 setAttribute(Qt::WA_ShowModal, modal); -
1056} -
1057 -
1058 -
1059bool QDialog::isSizeGripEnabled() const -
1060{ -
1061#ifndef QT_NO_SIZEGRIP -
1062 Q_D(const QDialog); -
1063 return !!d->resizer; -
1064#else -
1065 return false; -
1066#endif -
1067} -
1068 -
1069 -
1070void QDialog::setSizeGripEnabled(bool enabled) -
1071{ -
1072#ifdef QT_NO_SIZEGRIP -
1073 Q_UNUSED(enabled); -
1074#else -
1075 Q_D(QDialog); -
1076#ifndef QT_NO_SIZEGRIP -
1077 d->sizeGripEnabled = enabled; -
1078 if (enabled && d->doShowExtension) -
1079 return; -
1080#endif -
1081 if (!enabled != !d->resizer) { -
1082 if (enabled) { -
1083 d->resizer = new QSizeGrip(this); -
1084 // adjustSize() processes all events, which is suboptimal -
1085 d->resizer->resize(d->resizer->sizeHint()); -
1086 if (isRightToLeft()) -
1087 d->resizer->move(rect().bottomLeft() -d->resizer->rect().bottomLeft()); -
1088 else -
1089 d->resizer->move(rect().bottomRight() -d->resizer->rect().bottomRight()); -
1090 d->resizer->raise(); -
1091 d->resizer->show(); -
1092 } else { -
1093 delete d->resizer; -
1094 d->resizer = 0; -
1095 } -
1096 } -
1097#endif //QT_NO_SIZEGRIP -
1098} -
1099 -
1100 -
1101 -
1102/*! \reimp */ -
1103void QDialog::resizeEvent(QResizeEvent *) -
1104{ -
1105#ifndef QT_NO_SIZEGRIP -
1106 Q_D(QDialog); -
1107 if (d->resizer) { -
1108 if (isRightToLeft()) -
1109 d->resizer->move(rect().bottomLeft() -d->resizer->rect().bottomLeft()); -
1110 else -
1111 d->resizer->move(rect().bottomRight() -d->resizer->rect().bottomRight()); -
1112 d->resizer->raise(); -
1113 } -
1114#endif -
1115} -
1116 -
1117/*! \fn void QDialog::finished(int result) -
1118 \since 4.1 -
1119 -
1120 This signal is emitted when the dialog's \a result code has been -
1121 set, either by the user or by calling done(), accept(), or -
1122 reject(). -
1123 -
1124 Note that this signal is \e not emitted when hiding the dialog -
1125 with hide() or setVisible(false). This includes deleting the -
1126 dialog while it is visible. -
1127 -
1128 \sa accepted(), rejected() -
1129*/ -
1130 -
1131/*! \fn void QDialog::accepted() -
1132 \since 4.1 -
1133 -
1134 This signal is emitted when the dialog has been accepted either by -
1135 the user or by calling accept() or done() with the -
1136 QDialog::Accepted argument. -
1137 -
1138 Note that this signal is \e not emitted when hiding the dialog -
1139 with hide() or setVisible(false). This includes deleting the -
1140 dialog while it is visible. -
1141 -
1142 \sa finished(), rejected() -
1143*/ -
1144 -
1145/*! \fn void QDialog::rejected() -
1146 \since 4.1 -
1147 -
1148 This signal is emitted when the dialog has been rejected either by -
1149 the user or by calling reject() or done() with the -
1150 QDialog::Rejected argument. -
1151 -
1152 Note that this signal is \e not emitted when hiding the dialog -
1153 with hide() or setVisible(false). This includes deleting the -
1154 dialog while it is visible. -
1155 -
1156 \sa finished(), accepted() -
1157*/ -
1158 -
1159QT_END_NAMESPACE -
1160#include "moc_qdialog.cpp" -
1161 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial