1 | /****************************************************************************
|
---|
2 | ** $Id: qwizard.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Implementation of QWizard class.
|
---|
5 | **
|
---|
6 | ** Created : 990124
|
---|
7 | **
|
---|
8 | ** Copyright (C) 1999-2000 Trolltech AS. All rights reserved.
|
---|
9 | **
|
---|
10 | ** This file is part of the dialogs module of the Qt GUI Toolkit.
|
---|
11 | **
|
---|
12 | ** This file may be distributed under the terms of the Q Public License
|
---|
13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
14 | ** LICENSE.QPL included in the packaging of this file.
|
---|
15 | **
|
---|
16 | ** This file may be distributed and/or modified under the terms of the
|
---|
17 | ** GNU General Public License version 2 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
19 | ** packaging of this file.
|
---|
20 | **
|
---|
21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
22 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
23 | ** Agreement provided with the Software.
|
---|
24 | **
|
---|
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
27 | **
|
---|
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
29 | ** information about Qt Commercial License Agreements.
|
---|
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
32 | **
|
---|
33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
34 | ** not clear to you.
|
---|
35 | **
|
---|
36 | **********************************************************************/
|
---|
37 |
|
---|
38 | #include "qwizard.h"
|
---|
39 |
|
---|
40 | #ifndef QT_NO_WIZARD
|
---|
41 |
|
---|
42 | #include "qlayout.h"
|
---|
43 | #include "qpushbutton.h"
|
---|
44 | #include "qcursor.h"
|
---|
45 | #include "qlabel.h"
|
---|
46 | #include "qwidgetstack.h"
|
---|
47 | #include "qapplication.h"
|
---|
48 | #include "qptrlist.h"
|
---|
49 | #include "qpainter.h"
|
---|
50 | #include "qaccel.h"
|
---|
51 |
|
---|
52 | /*! \file wizard/wizard.cpp */
|
---|
53 | /*! \file wizard/wizard.h */
|
---|
54 |
|
---|
55 | /*!
|
---|
56 | \class QWizard qwizard.h
|
---|
57 | \brief The QWizard class provides a framework for wizard dialogs.
|
---|
58 |
|
---|
59 | \ingroup abstractwidgets
|
---|
60 | \ingroup organizers
|
---|
61 | \ingroup dialogs
|
---|
62 | \mainclass
|
---|
63 |
|
---|
64 | A wizard is a special type of input dialog that consists of a
|
---|
65 | sequence of dialog pages. A wizard's purpose is to walk the user
|
---|
66 | through a process step by step. Wizards are useful for complex or
|
---|
67 | infrequently occurring tasks that people may find difficult to
|
---|
68 | learn or do.
|
---|
69 |
|
---|
70 | QWizard provides page titles and displays Next, Back, Finish,
|
---|
71 | Cancel, and Help push buttons, as appropriate to the current
|
---|
72 | position in the page sequence. These buttons can be
|
---|
73 | enabled/disabled using setBackEnabled(), setNextEnabled(),
|
---|
74 | setFinishEnabled() and setHelpEnabled().
|
---|
75 |
|
---|
76 | Create and populate dialog pages that inherit from QWidget and add
|
---|
77 | them to the wizard using addPage(). Use insertPage() to add a
|
---|
78 | dialog page at a certain position in the page sequence. Use
|
---|
79 | removePage() to remove a page from the page sequence.
|
---|
80 |
|
---|
81 | Use currentPage() to retrieve a pointer to the currently displayed
|
---|
82 | page. page() returns a pointer to the page at a certain position
|
---|
83 | in the page sequence.
|
---|
84 |
|
---|
85 | Use pageCount() to retrieve the total number of pages in the page
|
---|
86 | sequence. indexOf() will return the index of a page in the page
|
---|
87 | sequence.
|
---|
88 |
|
---|
89 | QWizard provides functionality to mark pages as appropriate (or
|
---|
90 | not) in the current context with setAppropriate(). The idea is
|
---|
91 | that a page may be irrelevant and should be skipped depending on
|
---|
92 | the data entered by the user on a preceding page.
|
---|
93 |
|
---|
94 | It is generally considered good design to provide a greater number
|
---|
95 | of simple pages with fewer choices rather than a smaller number of
|
---|
96 | complex pages.
|
---|
97 |
|
---|
98 | Example code is available here: \l wizard/wizard.cpp \l wizard/wizard.h
|
---|
99 |
|
---|
100 | \img qwizard.png A QWizard page
|
---|
101 | \caption A QWizard page
|
---|
102 |
|
---|
103 | */
|
---|
104 |
|
---|
105 |
|
---|
106 | class QWizardPrivate
|
---|
107 | {
|
---|
108 | public:
|
---|
109 | struct Page {
|
---|
110 | Page( QWidget * widget, const QString & title ):
|
---|
111 | w( widget ), t( title ),
|
---|
112 | backEnabled( TRUE ), nextEnabled( TRUE ), finishEnabled( FALSE ),
|
---|
113 | helpEnabled( TRUE ),
|
---|
114 | appropriate( TRUE )
|
---|
115 | {}
|
---|
116 | QWidget * w;
|
---|
117 | QString t;
|
---|
118 | bool backEnabled;
|
---|
119 | bool nextEnabled;
|
---|
120 | bool finishEnabled;
|
---|
121 | bool helpEnabled;
|
---|
122 | bool appropriate;
|
---|
123 | };
|
---|
124 |
|
---|
125 | QVBoxLayout * v;
|
---|
126 | Page * current;
|
---|
127 | QWidgetStack * ws;
|
---|
128 | QPtrList<Page> pages;
|
---|
129 | QLabel * title;
|
---|
130 | QPushButton * backButton;
|
---|
131 | QPushButton * nextButton;
|
---|
132 | QPushButton * finishButton;
|
---|
133 | QPushButton * cancelButton;
|
---|
134 | QPushButton * helpButton;
|
---|
135 |
|
---|
136 | QFrame * hbar1, * hbar2;
|
---|
137 |
|
---|
138 | #ifndef QT_NO_ACCEL
|
---|
139 | QAccel * accel;
|
---|
140 | int backAccel;
|
---|
141 | int nextAccel;
|
---|
142 | #endif
|
---|
143 |
|
---|
144 | Page * page( const QWidget * w )
|
---|
145 | {
|
---|
146 | if ( !w )
|
---|
147 | return 0;
|
---|
148 | int i = pages.count();
|
---|
149 | while( --i >= 0 && pages.at( i ) && pages.at( i )->w != w ) { }
|
---|
150 | return i >= 0 ? pages.at( i ) : 0;
|
---|
151 | }
|
---|
152 |
|
---|
153 | };
|
---|
154 |
|
---|
155 |
|
---|
156 | /*!
|
---|
157 | Constructs an empty wizard dialog. The \a parent, \a name, \a
|
---|
158 | modal and \a f arguments are passed to the QDialog constructor.
|
---|
159 | */
|
---|
160 |
|
---|
161 | QWizard::QWizard( QWidget *parent, const char *name, bool modal,
|
---|
162 | WFlags f )
|
---|
163 | : QDialog( parent, name, modal, f )
|
---|
164 | {
|
---|
165 | d = new QWizardPrivate();
|
---|
166 | d->current = 0; // not quite true, but...
|
---|
167 | d->ws = new QWidgetStack( this, "qt_widgetstack" );
|
---|
168 | d->pages.setAutoDelete( TRUE );
|
---|
169 | d->title = new QLabel( this, "title label" );
|
---|
170 |
|
---|
171 | // create in nice tab order
|
---|
172 | d->nextButton = new QPushButton( this, "next" );
|
---|
173 | d->finishButton = new QPushButton( this, "finish" );
|
---|
174 | d->helpButton = new QPushButton( this, "help" );
|
---|
175 | d->backButton = new QPushButton( this, "back" );
|
---|
176 | d->cancelButton = new QPushButton( this, "cancel" );
|
---|
177 |
|
---|
178 | d->ws->installEventFilter( this );
|
---|
179 |
|
---|
180 | d->v = 0;
|
---|
181 | d->hbar1 = 0;
|
---|
182 | d->hbar2 = 0;
|
---|
183 |
|
---|
184 | d->cancelButton->setText( tr( "&Cancel" ) );
|
---|
185 | d->backButton->setText( tr( "< &Back" ) );
|
---|
186 | d->nextButton->setText( tr( "&Next >" ) );
|
---|
187 | d->finishButton->setText( tr( "&Finish" ) );
|
---|
188 | d->helpButton->setText( tr( "&Help" ) );
|
---|
189 |
|
---|
190 | d->nextButton->setDefault( TRUE );
|
---|
191 |
|
---|
192 | connect( d->backButton, SIGNAL(clicked()),
|
---|
193 | this, SLOT(back()) );
|
---|
194 | connect( d->nextButton, SIGNAL(clicked()),
|
---|
195 | this, SLOT(next()) );
|
---|
196 | connect( d->finishButton, SIGNAL(clicked()),
|
---|
197 | this, SLOT(accept()) );
|
---|
198 | connect( d->cancelButton, SIGNAL(clicked()),
|
---|
199 | this, SLOT(reject()) );
|
---|
200 | connect( d->helpButton, SIGNAL(clicked()),
|
---|
201 | this, SLOT(help()) );
|
---|
202 |
|
---|
203 | #ifndef QT_NO_ACCEL
|
---|
204 | d->accel = new QAccel( this, "arrow-key accel" );
|
---|
205 | d->backAccel = d->accel->insertItem( Qt::ALT + Qt::Key_Left );
|
---|
206 | d->accel->connectItem( d->backAccel, this, SLOT(back()) );
|
---|
207 | d->nextAccel = d->accel->insertItem( Qt::ALT + Qt::Key_Right );
|
---|
208 | d->accel->connectItem( d->nextAccel, this, SLOT(next()) );
|
---|
209 | #endif
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | /*!
|
---|
214 | Destroys the object and frees any allocated resources, including
|
---|
215 | all pages and controllers.
|
---|
216 | */
|
---|
217 |
|
---|
218 | QWizard::~QWizard()
|
---|
219 | {
|
---|
220 | delete d;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | /*!
|
---|
225 | \reimp
|
---|
226 | */
|
---|
227 |
|
---|
228 | void QWizard::show()
|
---|
229 | {
|
---|
230 | if ( !d->current ) {
|
---|
231 | // No page yet
|
---|
232 | if ( pageCount() > 0 )
|
---|
233 | showPage( d->pages.at( 0 )->w );
|
---|
234 | else
|
---|
235 | showPage( 0 );
|
---|
236 | }
|
---|
237 |
|
---|
238 | QDialog::show();
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | /*!
|
---|
243 | \reimp
|
---|
244 | */
|
---|
245 |
|
---|
246 | void QWizard::setFont( const QFont & font )
|
---|
247 | {
|
---|
248 | QApplication::postEvent( this, new QEvent( QEvent::LayoutHint ) );
|
---|
249 | QDialog::setFont( font );
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | /*!
|
---|
254 | Adds \a page to the end of the page sequence, with the title, \a
|
---|
255 | title.
|
---|
256 | */
|
---|
257 |
|
---|
258 | void QWizard::addPage( QWidget * page, const QString & title )
|
---|
259 | {
|
---|
260 | if ( !page )
|
---|
261 | return;
|
---|
262 | if ( d->page( page ) ) {
|
---|
263 | #if defined(QT_CHECK_STATE)
|
---|
264 | qWarning( "QWizard::addPage(): already added %s/%s to %s/%s",
|
---|
265 | page->className(), page->name(),
|
---|
266 | className(), name() );
|
---|
267 | #endif
|
---|
268 | return;
|
---|
269 | }
|
---|
270 | int i = d->pages.count();
|
---|
271 |
|
---|
272 | if( i > 0 )
|
---|
273 | d->pages.at( i - 1 )->nextEnabled = TRUE;
|
---|
274 |
|
---|
275 | QWizardPrivate::Page * p = new QWizardPrivate::Page( page, title );
|
---|
276 | p->backEnabled = ( i > 0 );
|
---|
277 | d->ws->addWidget( page, i );
|
---|
278 | d->pages.append( p );
|
---|
279 | }
|
---|
280 |
|
---|
281 | /*!
|
---|
282 | Inserts \a page at position \a index into the page sequence, with
|
---|
283 | title \a title. If \a index is -1, the page will be appended to
|
---|
284 | the end of the wizard's page sequence.
|
---|
285 | */
|
---|
286 |
|
---|
287 | void QWizard::insertPage( QWidget * page, const QString & title, int index )
|
---|
288 | {
|
---|
289 | if ( !page )
|
---|
290 | return;
|
---|
291 | if ( d->page( page ) ) {
|
---|
292 | #if defined(QT_CHECK_STATE)
|
---|
293 | qWarning( "QWizard::insertPage(): already added %s/%s to %s/%s",
|
---|
294 | page->className(), page->name(),
|
---|
295 | className(), name() );
|
---|
296 | #endif
|
---|
297 | return;
|
---|
298 | }
|
---|
299 |
|
---|
300 | if ( index < 0 || index > (int)d->pages.count() )
|
---|
301 | index = d->pages.count();
|
---|
302 |
|
---|
303 | if( index > 0 && ( index == (int)d->pages.count() ) )
|
---|
304 | d->pages.at( index - 1 )->nextEnabled = TRUE;
|
---|
305 |
|
---|
306 | QWizardPrivate::Page * p = new QWizardPrivate::Page( page, title );
|
---|
307 | p->backEnabled = ( index > 0 );
|
---|
308 | p->nextEnabled = ( index < (int)d->pages.count() );
|
---|
309 |
|
---|
310 | d->ws->addWidget( page, index );
|
---|
311 | d->pages.insert( index, p );
|
---|
312 | }
|
---|
313 |
|
---|
314 | /*!
|
---|
315 | \fn void QWizard::selected(const QString&)
|
---|
316 |
|
---|
317 | This signal is emitted when the current page changes. The
|
---|
318 | parameter contains the title of the selected page.
|
---|
319 | */
|
---|
320 |
|
---|
321 |
|
---|
322 | /*!
|
---|
323 | Makes \a page the current page and emits the selected() signal.
|
---|
324 |
|
---|
325 | This virtual function is called whenever a different page is to
|
---|
326 | be shown, including the first time the QWizard is shown.
|
---|
327 | By reimplementing it (and calling QWizard::showPage()),
|
---|
328 | you can prepare each page prior to it being shown.
|
---|
329 | */
|
---|
330 |
|
---|
331 | void QWizard::showPage( QWidget * page )
|
---|
332 | {
|
---|
333 | QWizardPrivate::Page * p = d->page( page );
|
---|
334 | if ( p ) {
|
---|
335 | int i;
|
---|
336 | for( i = 0; i < (int)d->pages.count() && d->pages.at( i ) != p; i++ );
|
---|
337 | bool notFirst( FALSE );
|
---|
338 |
|
---|
339 | if( i ) {
|
---|
340 | i--;
|
---|
341 | while( ( i >= 0 ) && !notFirst ) {
|
---|
342 | notFirst |= appropriate( d->pages.at( i )->w );
|
---|
343 | i--;
|
---|
344 | }
|
---|
345 | }
|
---|
346 | setBackEnabled( notFirst );
|
---|
347 | setNextEnabled( TRUE );
|
---|
348 | d->ws->raiseWidget( page );
|
---|
349 | d->current = p;
|
---|
350 | }
|
---|
351 |
|
---|
352 | layOut();
|
---|
353 | updateButtons();
|
---|
354 | emit selected( p ? p->t : QString::null );
|
---|
355 | }
|
---|
356 |
|
---|
357 |
|
---|
358 | /*!
|
---|
359 | Returns the number of pages in the wizard.
|
---|
360 | */
|
---|
361 |
|
---|
362 | int QWizard::pageCount() const
|
---|
363 | {
|
---|
364 | return d->pages.count();
|
---|
365 | }
|
---|
366 |
|
---|
367 | /*!
|
---|
368 | Returns the position of page \a page. If the page is not part of
|
---|
369 | the wizard -1 is returned.
|
---|
370 | */
|
---|
371 |
|
---|
372 | int QWizard::indexOf( QWidget* page ) const
|
---|
373 | {
|
---|
374 | QWizardPrivate::Page * p = d->page( page );
|
---|
375 | if ( !p ) return -1;
|
---|
376 |
|
---|
377 | return d->pages.find( p );
|
---|
378 | }
|
---|
379 |
|
---|
380 | /*!
|
---|
381 | Called when the user clicks the Back button; this function shows
|
---|
382 | the preceding relevant page in the sequence.
|
---|
383 |
|
---|
384 | \sa appropriate()
|
---|
385 | */
|
---|
386 | void QWizard::back()
|
---|
387 | {
|
---|
388 | int i = 0;
|
---|
389 |
|
---|
390 | while( i < (int)d->pages.count() && d->pages.at( i ) &&
|
---|
391 | d->current && d->pages.at( i )->w != d->current->w )
|
---|
392 | i++;
|
---|
393 |
|
---|
394 | i--;
|
---|
395 | while( i >= 0 && ( !d->pages.at( i ) || !appropriate( d->pages.at( i )->w ) ) )
|
---|
396 | i--;
|
---|
397 |
|
---|
398 | if( i >= 0 )
|
---|
399 | if( d->pages.at( i ) )
|
---|
400 | showPage( d->pages.at( i )->w );
|
---|
401 | }
|
---|
402 |
|
---|
403 |
|
---|
404 | /*!
|
---|
405 | Called when the user clicks the Next button, this function shows
|
---|
406 | the next relevant page in the sequence.
|
---|
407 |
|
---|
408 | \sa appropriate()
|
---|
409 | */
|
---|
410 | void QWizard::next()
|
---|
411 | {
|
---|
412 | int i = 0;
|
---|
413 | while( i < (int)d->pages.count() && d->pages.at( i ) &&
|
---|
414 | d->current && d->pages.at( i )->w != d->current->w )
|
---|
415 | i++;
|
---|
416 | i++;
|
---|
417 | while( i <= (int)d->pages.count()-1 &&
|
---|
418 | ( !d->pages.at( i ) || !appropriate( d->pages.at( i )->w ) ) )
|
---|
419 | i++;
|
---|
420 | // if we fell of the end of the world, step back
|
---|
421 | while ( i > 0 && (i >= (int)d->pages.count() || !d->pages.at( i ) ) )
|
---|
422 | i--;
|
---|
423 | if ( d->pages.at( i ) )
|
---|
424 | showPage( d->pages.at( i )->w );
|
---|
425 | }
|
---|
426 |
|
---|
427 |
|
---|
428 | /*!
|
---|
429 | \fn void QWizard::helpClicked()
|
---|
430 |
|
---|
431 | This signal is emitted when the user clicks on the Help button.
|
---|
432 | */
|
---|
433 |
|
---|
434 | /*!
|
---|
435 | Called when the user clicks the Help button, this function emits
|
---|
436 | the helpClicked() signal.
|
---|
437 | */
|
---|
438 |
|
---|
439 | void QWizard::help()
|
---|
440 | {
|
---|
441 | QWidget * page = d->ws->visibleWidget();
|
---|
442 | if ( !page )
|
---|
443 | return;
|
---|
444 |
|
---|
445 | #if 0
|
---|
446 | QWizardPage *wpage = ::qt_cast<QWizardPage*>(page);
|
---|
447 | if ( wpage )
|
---|
448 | emit wpage->helpClicked();
|
---|
449 | #endif
|
---|
450 | emit helpClicked();
|
---|
451 | }
|
---|
452 |
|
---|
453 |
|
---|
454 | void QWizard::setBackEnabled( bool enable )
|
---|
455 | {
|
---|
456 | d->backButton->setEnabled( enable );
|
---|
457 | #ifndef QT_NO_ACCEL
|
---|
458 | d->accel->setItemEnabled( d->backAccel, enable );
|
---|
459 | #endif
|
---|
460 | }
|
---|
461 |
|
---|
462 |
|
---|
463 | void QWizard::setNextEnabled( bool enable )
|
---|
464 | {
|
---|
465 | d->nextButton->setEnabled( enable );
|
---|
466 | #ifndef QT_NO_ACCEL
|
---|
467 | d->accel->setItemEnabled( d->nextAccel, enable );
|
---|
468 | #endif
|
---|
469 | }
|
---|
470 |
|
---|
471 |
|
---|
472 | void QWizard::setHelpEnabled( bool enable )
|
---|
473 | {
|
---|
474 | d->helpButton->setEnabled( enable );
|
---|
475 | }
|
---|
476 |
|
---|
477 |
|
---|
478 | /*!
|
---|
479 | \fn void QWizard::setFinish( QWidget *, bool )
|
---|
480 | \obsolete
|
---|
481 |
|
---|
482 | Use setFinishEnabled instead
|
---|
483 | */
|
---|
484 |
|
---|
485 | /*!
|
---|
486 | If \a enable is TRUE, page \a page has a Back button; otherwise \a
|
---|
487 | page has no Back button. By default all pages have this button.
|
---|
488 | */
|
---|
489 | void QWizard::setBackEnabled( QWidget * page, bool enable )
|
---|
490 | {
|
---|
491 | QWizardPrivate::Page * p = d->page( page );
|
---|
492 | if ( !p )
|
---|
493 | return;
|
---|
494 |
|
---|
495 | p->backEnabled = enable;
|
---|
496 | updateButtons();
|
---|
497 | }
|
---|
498 |
|
---|
499 |
|
---|
500 | /*!
|
---|
501 | If \a enable is TRUE, page \a page has a Next button; otherwise
|
---|
502 | the Next button on \a page is disabled. By default all pages have
|
---|
503 | this button.
|
---|
504 | */
|
---|
505 |
|
---|
506 | void QWizard::setNextEnabled( QWidget * page, bool enable )
|
---|
507 | {
|
---|
508 | QWizardPrivate::Page * p = d->page( page );
|
---|
509 | if ( !p )
|
---|
510 | return;
|
---|
511 |
|
---|
512 | p->nextEnabled = enable;
|
---|
513 | updateButtons();
|
---|
514 | }
|
---|
515 |
|
---|
516 |
|
---|
517 | /*!
|
---|
518 | If \a enable is TRUE, page \a page has a Finish button; otherwise
|
---|
519 | \a page has no Finish button. By default \e no page has this
|
---|
520 | button.
|
---|
521 | */
|
---|
522 | void QWizard::setFinishEnabled( QWidget * page, bool enable )
|
---|
523 | {
|
---|
524 | QWizardPrivate::Page * p = d->page( page );
|
---|
525 | if ( !p )
|
---|
526 | return;
|
---|
527 |
|
---|
528 | p->finishEnabled = enable;
|
---|
529 | updateButtons();
|
---|
530 | }
|
---|
531 |
|
---|
532 |
|
---|
533 | /*!
|
---|
534 | If \a enable is TRUE, page \a page has a Help button; otherwise \a
|
---|
535 | page has no Help button. By default all pages have this button.
|
---|
536 | */
|
---|
537 | void QWizard::setHelpEnabled( QWidget * page, bool enable )
|
---|
538 | {
|
---|
539 | QWizardPrivate::Page * p = d->page( page );
|
---|
540 | if ( !p )
|
---|
541 | return;
|
---|
542 |
|
---|
543 | p->helpEnabled = enable;
|
---|
544 | updateButtons();
|
---|
545 | }
|
---|
546 |
|
---|
547 |
|
---|
548 | /*!
|
---|
549 | Called when the Next button is clicked; this virtual function
|
---|
550 | returns TRUE if \a page is relevant for display in the current
|
---|
551 | context; otherwise it is ignored by QWizard and returns FALSE. The
|
---|
552 | default implementation returns the value set using
|
---|
553 | setAppropriate(). The ultimate default is TRUE.
|
---|
554 |
|
---|
555 | \warning The last page of the wizard will be displayed if no page
|
---|
556 | is relevant in the current context.
|
---|
557 | */
|
---|
558 |
|
---|
559 | bool QWizard::appropriate( QWidget * page ) const
|
---|
560 | {
|
---|
561 | QWizardPrivate::Page * p = d->page( page );
|
---|
562 | return p ? p->appropriate : TRUE;
|
---|
563 | }
|
---|
564 |
|
---|
565 |
|
---|
566 | /*!
|
---|
567 | If \a appropriate is TRUE then page \a page is considered relevant
|
---|
568 | in the current context and should be displayed in the page
|
---|
569 | sequence; otherwise \a page should not be displayed in the page
|
---|
570 | sequence.
|
---|
571 |
|
---|
572 | \sa appropriate()
|
---|
573 | */
|
---|
574 | void QWizard::setAppropriate( QWidget * page, bool appropriate )
|
---|
575 | {
|
---|
576 | QWizardPrivate::Page * p = d->page( page );
|
---|
577 | if ( p )
|
---|
578 | p->appropriate = appropriate;
|
---|
579 | }
|
---|
580 |
|
---|
581 |
|
---|
582 | void QWizard::updateButtons()
|
---|
583 | {
|
---|
584 | if ( !d->current )
|
---|
585 | return;
|
---|
586 |
|
---|
587 | int i;
|
---|
588 | for( i = 0; i < (int)d->pages.count() && d->pages.at( i ) != d->current; i++ );
|
---|
589 | bool notFirst( FALSE );
|
---|
590 | if( i ) {
|
---|
591 | i--;
|
---|
592 | while( ( i >= 0 ) && !notFirst ) {
|
---|
593 | notFirst |= appropriate( d->pages.at( i )->w );
|
---|
594 | i--;
|
---|
595 | }
|
---|
596 | }
|
---|
597 | setBackEnabled( d->current->backEnabled && notFirst );
|
---|
598 | setNextEnabled( d->current->nextEnabled );
|
---|
599 | d->finishButton->setEnabled( d->current->finishEnabled );
|
---|
600 | d->helpButton->setEnabled( d->current->helpEnabled );
|
---|
601 |
|
---|
602 | if ( ( d->current->finishEnabled && !d->finishButton->isVisible() ) ||
|
---|
603 | ( d->current->backEnabled && !d->backButton->isVisible() ) ||
|
---|
604 | ( d->current->nextEnabled && !d->nextButton->isVisible() ) ||
|
---|
605 | ( d->current->helpEnabled && !d->helpButton->isVisible() ) )
|
---|
606 | layOut();
|
---|
607 | }
|
---|
608 |
|
---|
609 |
|
---|
610 | /*!
|
---|
611 | Returns a pointer to the current page in the sequence. Although
|
---|
612 | the wizard does its best to make sure that this value is never 0,
|
---|
613 | it can be if you try hard enough.
|
---|
614 | */
|
---|
615 |
|
---|
616 | QWidget * QWizard::currentPage() const
|
---|
617 | {
|
---|
618 | return d->ws->visibleWidget();
|
---|
619 | }
|
---|
620 |
|
---|
621 |
|
---|
622 | /*!
|
---|
623 | Returns the title of page \a page.
|
---|
624 | */
|
---|
625 |
|
---|
626 | QString QWizard::title( QWidget * page ) const
|
---|
627 | {
|
---|
628 | QWizardPrivate::Page * p = d->page( page );
|
---|
629 | return p ? p->t : QString::null;
|
---|
630 | }
|
---|
631 |
|
---|
632 | /*!
|
---|
633 | Sets the title for page \a page to \a title.
|
---|
634 | */
|
---|
635 |
|
---|
636 | void QWizard::setTitle( QWidget *page, const QString &title )
|
---|
637 | {
|
---|
638 | QWizardPrivate::Page * p = d->page( page );
|
---|
639 | if ( p )
|
---|
640 | p->t = title;
|
---|
641 | if ( page == currentPage() )
|
---|
642 | d->title->setText( title );
|
---|
643 | }
|
---|
644 |
|
---|
645 | /*!
|
---|
646 | \property QWizard::titleFont
|
---|
647 | \brief the font used for page titles
|
---|
648 |
|
---|
649 | The default is QApplication::font().
|
---|
650 | */
|
---|
651 | QFont QWizard::titleFont() const
|
---|
652 | {
|
---|
653 | return d->title->font();
|
---|
654 | }
|
---|
655 |
|
---|
656 | void QWizard::setTitleFont( const QFont & font )
|
---|
657 | {
|
---|
658 | d->title->setFont( font );
|
---|
659 | }
|
---|
660 |
|
---|
661 |
|
---|
662 | /*!
|
---|
663 | Returns a pointer to the dialog's Back button
|
---|
664 |
|
---|
665 | By default, this button is connected to the back() slot, which is
|
---|
666 | virtual so you can reimplement it in a QWizard subclass. Use
|
---|
667 | setBackEnabled() to enable/disable this button.
|
---|
668 | */
|
---|
669 | QPushButton * QWizard::backButton() const
|
---|
670 | {
|
---|
671 | return d->backButton;
|
---|
672 | }
|
---|
673 |
|
---|
674 |
|
---|
675 | /*!
|
---|
676 | Returns a pointer to the dialog's Next button
|
---|
677 |
|
---|
678 | By default, this button is connected to the next() slot, which is
|
---|
679 | virtual so you can reimplement it in a QWizard subclass. Use
|
---|
680 | setNextEnabled() to enable/disable this button.
|
---|
681 | */
|
---|
682 | QPushButton * QWizard::nextButton() const
|
---|
683 | {
|
---|
684 | return d->nextButton;
|
---|
685 | }
|
---|
686 |
|
---|
687 |
|
---|
688 | /*!
|
---|
689 | Returns a pointer to the dialog's Finish button
|
---|
690 |
|
---|
691 | By default, this button is connected to the QDialog::accept()
|
---|
692 | slot, which is virtual so you can reimplement it in a QWizard
|
---|
693 | subclass. Use setFinishEnabled() to enable/disable this button.
|
---|
694 | */
|
---|
695 | QPushButton * QWizard::finishButton() const
|
---|
696 | {
|
---|
697 | return d->finishButton;
|
---|
698 | }
|
---|
699 |
|
---|
700 |
|
---|
701 | /*!
|
---|
702 | Returns a pointer to the dialog's Cancel button
|
---|
703 |
|
---|
704 | By default, this button is connected to the QDialog::reject()
|
---|
705 | slot, which is virtual so you can reimplement it in a QWizard
|
---|
706 | subclass.
|
---|
707 | */
|
---|
708 | QPushButton * QWizard::cancelButton() const
|
---|
709 | {
|
---|
710 | return d->cancelButton;
|
---|
711 | }
|
---|
712 |
|
---|
713 |
|
---|
714 | /*!
|
---|
715 | Returns a pointer to the dialog's Help button
|
---|
716 |
|
---|
717 | By default, this button is connected to the help() slot, which is
|
---|
718 | virtual so you can reimplement it in a QWizard subclass. Use
|
---|
719 | setHelpEnabled() to enable/disable this button.
|
---|
720 | */
|
---|
721 | QPushButton * QWizard::helpButton() const
|
---|
722 | {
|
---|
723 | return d->helpButton;
|
---|
724 | }
|
---|
725 |
|
---|
726 |
|
---|
727 | /*!
|
---|
728 | This virtual function is responsible for adding the buttons below
|
---|
729 | the bottom divider.
|
---|
730 |
|
---|
731 | \a layout is the horizontal layout of the entire wizard.
|
---|
732 | */
|
---|
733 |
|
---|
734 | void QWizard::layOutButtonRow( QHBoxLayout * layout )
|
---|
735 | {
|
---|
736 | bool hasHelp = FALSE;
|
---|
737 | bool hasEarlyFinish = FALSE;
|
---|
738 |
|
---|
739 | int i = d->pages.count() - 2;
|
---|
740 | while ( !hasEarlyFinish && i >= 0 ) {
|
---|
741 | if ( d->pages.at( i ) && d->pages.at( i )->finishEnabled )
|
---|
742 | hasEarlyFinish = TRUE;
|
---|
743 | i--;
|
---|
744 | }
|
---|
745 | i = 0;
|
---|
746 | while ( !hasHelp && i < (int)d->pages.count() ) {
|
---|
747 | if ( d->pages.at( i ) && d->pages.at( i )->helpEnabled )
|
---|
748 | hasHelp = TRUE;
|
---|
749 | i++;
|
---|
750 | }
|
---|
751 |
|
---|
752 | QBoxLayout * h = new QBoxLayout( QBoxLayout::LeftToRight );
|
---|
753 | layout->addLayout( h );
|
---|
754 |
|
---|
755 | if ( hasHelp )
|
---|
756 | h->addWidget( d->helpButton );
|
---|
757 | else
|
---|
758 | d->helpButton->hide();
|
---|
759 |
|
---|
760 | h->addStretch( 42 );
|
---|
761 |
|
---|
762 | h->addWidget( d->backButton );
|
---|
763 |
|
---|
764 | h->addSpacing( 6 );
|
---|
765 |
|
---|
766 | if ( hasEarlyFinish ) {
|
---|
767 | d->nextButton->show();
|
---|
768 | d->finishButton->show();
|
---|
769 | h->addWidget( d->nextButton );
|
---|
770 | h->addSpacing( 12 );
|
---|
771 | h->addWidget( d->finishButton );
|
---|
772 | } else if ( d->pages.count() == 0 ||
|
---|
773 | d->current->finishEnabled ||
|
---|
774 | d->current == d->pages.at( d->pages.count()-1 ) ) {
|
---|
775 | d->nextButton->hide();
|
---|
776 | d->finishButton->show();
|
---|
777 | h->addWidget( d->finishButton );
|
---|
778 | } else {
|
---|
779 | d->nextButton->show();
|
---|
780 | d->finishButton->hide();
|
---|
781 | h->addWidget( d->nextButton );
|
---|
782 | }
|
---|
783 |
|
---|
784 | // if last page is disabled - show finished btn. at lastpage-1
|
---|
785 | i = d->pages.count()-1;
|
---|
786 | if ( i >= 0 && !appropriate( d->pages.at( i )->w ) &&
|
---|
787 | d->current == d->pages.at( d->pages.count()-2 ) ) {
|
---|
788 | d->nextButton->hide();
|
---|
789 | d->finishButton->show();
|
---|
790 | h->addWidget( d->finishButton );
|
---|
791 | }
|
---|
792 |
|
---|
793 | h->addSpacing( 12 );
|
---|
794 | h->addWidget( d->cancelButton );
|
---|
795 | }
|
---|
796 |
|
---|
797 |
|
---|
798 | /*!
|
---|
799 | This virtual function is responsible for laying out the title row.
|
---|
800 |
|
---|
801 | \a layout is the horizontal layout for the wizard, and \a
|
---|
802 | title is the title for this page. This function is called every
|
---|
803 | time \a title changes.
|
---|
804 | */
|
---|
805 |
|
---|
806 | void QWizard::layOutTitleRow( QHBoxLayout * layout, const QString & title )
|
---|
807 | {
|
---|
808 | d->title->setText( title );
|
---|
809 | layout->addWidget( d->title, 10 );
|
---|
810 | }
|
---|
811 |
|
---|
812 |
|
---|
813 | /*
|
---|
814 |
|
---|
815 | */
|
---|
816 |
|
---|
817 | void QWizard::layOut()
|
---|
818 | {
|
---|
819 | delete d->v;
|
---|
820 | d->v = new QVBoxLayout( this, 6, 0, "top-level layout" );
|
---|
821 |
|
---|
822 | QHBoxLayout * l;
|
---|
823 | l = new QHBoxLayout( 6 );
|
---|
824 | d->v->addLayout( l, 0 );
|
---|
825 | layOutTitleRow( l, d->current ? d->current->t : QString::null );
|
---|
826 |
|
---|
827 | if ( ! d->hbar1 ) {
|
---|
828 | d->hbar1 = new QFrame( this, "<hr>", 0 );
|
---|
829 | d->hbar1->setFrameStyle( QFrame::Sunken + QFrame::HLine );
|
---|
830 | d->hbar1->setFixedHeight( 12 );
|
---|
831 | }
|
---|
832 |
|
---|
833 | d->v->addWidget( d->hbar1 );
|
---|
834 |
|
---|
835 | d->v->addWidget( d->ws, 10 );
|
---|
836 |
|
---|
837 | if ( ! d->hbar2 ) {
|
---|
838 | d->hbar2 = new QFrame( this, "<hr>", 0 );
|
---|
839 | d->hbar2->setFrameStyle( QFrame::Sunken + QFrame::HLine );
|
---|
840 | d->hbar2->setFixedHeight( 12 );
|
---|
841 | }
|
---|
842 | d->v->addWidget( d->hbar2 );
|
---|
843 |
|
---|
844 | l = new QHBoxLayout( 6 );
|
---|
845 | d->v->addLayout( l );
|
---|
846 | layOutButtonRow( l );
|
---|
847 | d->v->activate();
|
---|
848 | }
|
---|
849 |
|
---|
850 |
|
---|
851 | /*!
|
---|
852 | \reimp
|
---|
853 | */
|
---|
854 |
|
---|
855 | bool QWizard::eventFilter( QObject * o, QEvent * e )
|
---|
856 | {
|
---|
857 | if ( o == d->ws && e && e->type() == QEvent::ChildRemoved ) {
|
---|
858 | QChildEvent * c = (QChildEvent*)e;
|
---|
859 | if ( c->child() && c->child()->isWidgetType() )
|
---|
860 | removePage( (QWidget *)c->child() );
|
---|
861 | }
|
---|
862 | return QDialog::eventFilter( o, e );
|
---|
863 | }
|
---|
864 |
|
---|
865 |
|
---|
866 | /*!
|
---|
867 | Removes \a page from the page sequence but does not delete the
|
---|
868 | page. If \a page is currently being displayed, QWizard will
|
---|
869 | display the page that precedes it, or the first page if this was
|
---|
870 | the first page.
|
---|
871 | */
|
---|
872 |
|
---|
873 | void QWizard::removePage( QWidget * page )
|
---|
874 | {
|
---|
875 | if ( !page )
|
---|
876 | return;
|
---|
877 |
|
---|
878 | int i = d->pages.count();
|
---|
879 | QWidget* cp = currentPage();
|
---|
880 | while( --i >= 0 && d->pages.at( i ) && d->pages.at( i )->w != page ) { }
|
---|
881 | if ( i < 0 )
|
---|
882 | return;
|
---|
883 | QWizardPrivate::Page * p = d->pages.at( i );
|
---|
884 | d->pages.removeRef( p );
|
---|
885 | d->ws->removeWidget( page );
|
---|
886 |
|
---|
887 | if( cp == page ) {
|
---|
888 | i--;
|
---|
889 | if( i < 0 )
|
---|
890 | i = 0;
|
---|
891 | if ( pageCount() > 0 )
|
---|
892 | showPage( QWizard::page( i ) );
|
---|
893 | }
|
---|
894 | }
|
---|
895 |
|
---|
896 |
|
---|
897 | /*!
|
---|
898 | Returns a pointer to the page at position \a index in the
|
---|
899 | sequence, or 0 if \a index is out of range. The first page has
|
---|
900 | index 0.
|
---|
901 | */
|
---|
902 |
|
---|
903 | QWidget* QWizard::page( int index ) const
|
---|
904 | {
|
---|
905 | if ( index >= pageCount() || index < 0 )
|
---|
906 | return 0;
|
---|
907 |
|
---|
908 | return d->pages.at( index )->w;
|
---|
909 | }
|
---|
910 |
|
---|
911 | #endif // QT_NO_WIZARD
|
---|