• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kleopatra

  • sources
  • kde-4.12
  • kdepim
  • kleopatra
  • crypto
  • gui
wizard.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  crypto/gui/wizard.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2007 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "wizard.h"
36 #include "wizardpage.h"
37 
38 #include <utils/kleo_assert.h>
39 
40 #include <KDebug>
41 #include <KGuiItem>
42 #include <KLocale>
43 #include <KPushButton>
44 #include <KStandardGuiItem>
45 
46 #include <QDialogButtonBox>
47 #include <QFrame>
48 #include <QLabel>
49 #include <QStackedWidget>
50 #include <QTimer>
51 #include <QVBoxLayout>
52 #ifndef QT_NO_WIZARD
53 #include <QWizard>
54 #else
55 #include <utils/qwizard.h>
56 #endif
57 
58 #include <map>
59 #include <set>
60 
61 #include <cassert>
62 
63 using namespace Kleo::Crypto::Gui;
64 
65 class Wizard::Private {
66  friend class ::Wizard;
67  Wizard * const q;
68 public:
69  explicit Private( Wizard * qq );
70  ~Private();
71 
72  void updateButtonStates();
73  bool isLastPage( int id ) const;
74  int previousPage() const;
75  void updateHeader();
76 
77 private:
78  std::vector<int> pageOrder;
79  std::set<int> hiddenPages;
80  std::map<int, WizardPage*> idToPage;
81  int currentId;
82  QStackedWidget* stack;
83  KPushButton* nextButton;
84  QPushButton* backButton;
85  QPushButton* cancelButton;
86  KGuiItem finishItem;
87  KGuiItem nextItem;
88  QFrame* titleFrame;
89  QLabel* titleLabel;
90  QLabel* subTitleLabel;
91  QFrame* explanationFrame;
92  QLabel* explanationLabel;
93  QTimer* nextPageTimer;
94 };
95 
96 
97 Wizard::Private::Private( Wizard * qq )
98  : q( qq ), currentId( -1 ), stack( new QStackedWidget )
99 {
100  nextPageTimer = new QTimer( q );
101  nextPageTimer->setInterval( 0 );
102  connect( nextPageTimer, SIGNAL(timeout()), q, SLOT(next()) );
103  nextItem = KGuiItem( i18n( "&Next" ) );
104  finishItem = KStandardGuiItem::ok();
105  QVBoxLayout * const top = new QVBoxLayout( q );
106  top->setMargin( 0 );
107  titleFrame = new QFrame;
108  titleFrame->setFrameShape( QFrame::StyledPanel );
109  titleFrame->setAutoFillBackground( true );
110  titleFrame->setBackgroundRole( QPalette::Base );
111  QVBoxLayout* const titleLayout = new QVBoxLayout( titleFrame );
112  titleLabel = new QLabel;
113  titleLayout->addWidget( titleLabel );
114  subTitleLabel = new QLabel;
115  subTitleLabel->setWordWrap( true );
116  titleLayout->addWidget( subTitleLabel );
117  top->addWidget( titleFrame );
118  titleFrame->setVisible( false );
119 
120  top->addWidget( stack );
121 
122  explanationFrame = new QFrame;
123  explanationFrame->setFrameShape( QFrame::StyledPanel );
124  explanationFrame->setAutoFillBackground( true );
125  explanationFrame->setBackgroundRole( QPalette::Base );
126  QVBoxLayout* const explanationLayout = new QVBoxLayout( explanationFrame );
127  explanationLabel = new QLabel;
128  explanationLabel->setWordWrap( true );
129  explanationLayout->addWidget( explanationLabel );
130  top->addWidget( explanationFrame );
131  explanationFrame->setVisible( false );
132 
133  QWidget* buttonWidget = new QWidget;
134  QHBoxLayout* buttonLayout = new QHBoxLayout( buttonWidget );
135  QDialogButtonBox * const box = new QDialogButtonBox;
136 
137  cancelButton = box->addButton( QDialogButtonBox::Cancel );
138  q->connect( cancelButton, SIGNAL(clicked()), q, SLOT(reject()) );
139 
140  backButton = new QPushButton;
141  backButton->setText( i18n( "Back" ) );
142  q->connect( backButton, SIGNAL(clicked()), q, SLOT(back()) );
143  box->addButton( backButton, QDialogButtonBox::ActionRole );
144 
145  nextButton = new KPushButton;
146  nextButton->setGuiItem( nextItem );
147  q->connect( nextButton, SIGNAL(clicked()), q, SLOT(next()) );
148  box->addButton( nextButton, QDialogButtonBox::ActionRole );
149  buttonLayout->addWidget( box );
150 
151  top->addWidget( buttonWidget );
152 
153  q->connect( q, SIGNAL(rejected()), q, SIGNAL(canceled()) );
154 }
155 
156 Wizard::Private::~Private() { kDebug(); }
157 
158 
159 
160 bool Wizard::Private::isLastPage( int id ) const
161 {
162  return !pageOrder.empty() ? pageOrder.back() == id : false;
163 }
164 
165 void Wizard::Private::updateButtonStates()
166 {
167  const bool isLast = isLastPage( currentId );
168  const bool canGoToNext = q->canGoToNextPage();
169  WizardPage* const page = q->page( currentId );
170  const KGuiItem customNext = page ? page->customNextButton() : KGuiItem();
171  if ( customNext.text().isEmpty() && customNext.icon().isNull() )
172  nextButton->setGuiItem( isLast ? finishItem : nextItem );
173  else
174  nextButton->setGuiItem( customNext );
175  nextButton->setEnabled( canGoToNext );
176  cancelButton->setEnabled( !isLast || !canGoToNext );
177  backButton->setEnabled( q->canGoToPreviousPage() );
178  if ( page && page->autoAdvance() && page->isComplete() )
179  nextPageTimer->start();
180 }
181 
182 void Wizard::Private::updateHeader()
183 {
184  WizardPage * const widget = q->page( currentId );
185  assert( !widget || stack->indexOf( widget ) != -1 );
186  if ( widget )
187  stack->setCurrentWidget( widget );
188  const QString title = widget ? widget->title() : QString();
189  const QString subTitle = widget ? widget->subTitle() : QString();
190  const QString explanation = widget ? widget->explanation() : QString();
191  titleFrame->setVisible( !title.isEmpty() || !subTitle.isEmpty() || !explanation.isEmpty() );
192  titleLabel->setVisible( !title.isEmpty() );
193  titleLabel->setText( title );
194  subTitleLabel->setText( subTitle );
195  subTitleLabel->setVisible( !subTitle.isEmpty() );
196  explanationFrame->setVisible( !explanation.isEmpty() );
197  explanationLabel->setVisible( !explanation.isEmpty() );
198  explanationLabel->setText( explanation );
199  q->resize( q->sizeHint().expandedTo( q->size() ) );
200 }
201 
202 Wizard::Wizard( QWidget * parent, Qt::WindowFlags f )
203  : QDialog( parent, f ), d( new Private( this ) )
204 {
205 
206 }
207 
208 Wizard::~Wizard() { kDebug(); }
209 
210 void Wizard::setPage( int id, WizardPage* widget )
211 {
212  kleo_assert( id != InvalidPage );
213  kleo_assert( d->idToPage.find( id ) == d->idToPage.end() );
214  d->idToPage[id] = widget;
215  d->stack->addWidget( widget );
216  connect( widget, SIGNAL(completeChanged()), this, SLOT(updateButtonStates()) );
217  connect( widget, SIGNAL(titleChanged()), this, SLOT(updateHeader()) );
218  connect( widget, SIGNAL(subTitleChanged()), this, SLOT(updateHeader()) );
219  connect( widget, SIGNAL(explanationChanged()), this, SLOT(updateHeader()) );
220  connect( widget, SIGNAL(autoAdvanceChanged()), this, SLOT(updateButtonStates()) );
221  connect( widget, SIGNAL(windowTitleChanged(QString)), this, SLOT(setWindowTitle(QString)) );
222 }
223 
224 void Wizard::setPageOrder( const std::vector<int>& pageOrder )
225 {
226  d->pageOrder = pageOrder;
227  d->hiddenPages.clear();
228  if ( pageOrder.empty() )
229  return;
230  setCurrentPage( pageOrder.front() );
231 }
232 
233 void Wizard::setCurrentPage( int id )
234 {
235  d->currentId = id;
236  if ( id == InvalidPage )
237  return;
238  d->updateHeader();
239  d->updateButtonStates();
240 }
241 
242 void Wizard::setPageVisible( int id, bool visible )
243 {
244  if ( visible )
245  d->hiddenPages.erase( id );
246  else
247  d->hiddenPages.insert( id );
248  if ( currentPage() == id && !visible )
249  next();
250 }
251 
252 int Wizard::currentPage() const
253 {
254  return d->currentId;
255 }
256 
257 bool Wizard::canGoToNextPage() const
258 {
259  const WizardPage * const current = currentPageWidget();
260  return current ? current->isComplete() : false;
261 }
262 
263 bool Wizard::canGoToPreviousPage() const
264 {
265  const int prev = d->previousPage();
266  if ( prev == InvalidPage )
267  return false;
268  const WizardPage * const prevPage = page( prev );
269  assert( prevPage );
270  return !prevPage->isCommitPage();
271 }
272 
273 
274 void Wizard::next()
275 {
276  WizardPage* const current = currentPageWidget();
277  if ( current )
278  current->onNext();
279  onNext( d->currentId );
280  std::vector<int>::const_iterator it = qBinaryFind( d->pageOrder.begin(), d->pageOrder.end(), d->currentId );
281  assert( it != d->pageOrder.end() );
282 
283  do {
284  ++it;
285  }
286  while ( d->hiddenPages.find( *it ) != d->hiddenPages.end() );
287 
288  if ( it == d->pageOrder.end() ) // "Finish"
289  {
290  d->currentId = InvalidPage;
291  close();
292  }
293  else // "next"
294  {
295  setCurrentPage( *it );
296  }
297 }
298 
299 int Wizard::Private::previousPage() const
300 {
301  if ( pageOrder.empty() )
302  return InvalidPage;
303 
304  std::vector<int>::const_iterator it = qBinaryFind( pageOrder.begin(), pageOrder.end(), currentId );
305  if ( it == pageOrder.begin() || it == pageOrder.end() )
306  return InvalidPage;
307 
308  do {
309  --it;
310  } while ( it != pageOrder.begin() && hiddenPages.find( *it ) != hiddenPages.end() );
311  return *it;
312 }
313 
314 void Wizard::back()
315 {
316  onBack( d->currentId );
317  const int prev = d->previousPage();
318  if ( prev == InvalidPage )
319  return;
320  setCurrentPage( prev );
321 }
322 
323 const WizardPage* Wizard::page( int id ) const
324 {
325  if ( id == InvalidPage )
326  return 0;
327 
328  const std::map<int, WizardPage*>::const_iterator it = d->idToPage.find( id );
329  kleo_assert( it != d->idToPage.end() );
330  return (*it).second;
331 }
332 
333 const WizardPage* Wizard::currentPageWidget() const
334 {
335  return page( d->currentId );
336 }
337 
338 
339 WizardPage* Wizard::currentPageWidget()
340 {
341  return page( d->currentId );
342 }
343 
344 void Wizard::onNext( int currentId )
345 {
346  Q_UNUSED( currentId )
347 }
348 
349 
350 void Wizard::onBack( int currentId )
351 {
352  Q_UNUSED( currentId )
353 }
354 
355 WizardPage* Wizard::page( int id )
356 {
357  if ( id == InvalidPage )
358  return 0;
359 
360  const std::map<int, WizardPage*>::const_iterator it = d->idToPage.find( id );
361  kleo_assert( it != d->idToPage.end() );
362  return (*it).second;
363 }
364 
365 #include "moc_wizard.cpp"
Kleo::Crypto::Gui::Wizard::~Wizard
~Wizard()
Definition: wizard.cpp:208
Kleo::Crypto::Gui::Wizard::page
const WizardPage * page(int id) const
Definition: wizard.cpp:323
Kleo::Crypto::Gui::Wizard::setPageOrder
void setPageOrder(const std::vector< int > &pages)
Definition: wizard.cpp:224
QDialog
QWidget
Kleo::Crypto::Gui::WizardPage::title
QString title() const
Definition: wizardpage.cpp:97
Kleo::Crypto::Gui::Wizard::back
void back()
Definition: wizard.cpp:314
Kleo::Crypto::Gui::WizardPage::subTitle
QString subTitle() const
Definition: wizardpage.cpp:110
kleo_assert.h
wizard.h
d
#define d
Definition: adduseridcommand.cpp:90
Kleo::Crypto::Gui::Wizard::onNext
virtual void onNext(int currentId)
Definition: wizard.cpp:344
Kleo::Crypto::Gui::Wizard::canGoToNextPage
bool canGoToNextPage() const
Definition: wizard.cpp:257
Kleo::Crypto::Gui::Wizard::next
void next()
Definition: wizard.cpp:274
Kleo::Crypto::Gui::Wizard::setCurrentPage
void setCurrentPage(int id)
Definition: wizard.cpp:233
Kleo::Crypto::Gui::Wizard::canGoToPreviousPage
bool canGoToPreviousPage() const
Definition: wizard.cpp:263
wizardpage.h
Kleo::Crypto::Gui::Wizard::currentPage
int currentPage() const
Definition: wizard.cpp:252
Kleo::Crypto::Gui::WizardPage::isCommitPage
bool isCommitPage() const
Definition: wizardpage.cpp:74
qwizard.h
Kleo::Crypto::Gui::WizardPage::explanation
QString explanation() const
Definition: wizardpage.cpp:123
kleo_assert
#define kleo_assert(cond)
Definition: kleo_assert.h:84
Kleo::Crypto::Gui::Wizard::onBack
virtual void onBack(int currentId)
Definition: wizard.cpp:350
Kleo::Crypto::Gui::Wizard::currentPageWidget
const WizardPage * currentPageWidget() const
Definition: wizard.cpp:333
Kleo::Crypto::Gui::Wizard
Definition: wizard.h:48
q
#define q
Definition: adduseridcommand.cpp:91
Kleo::Crypto::Gui::WizardPage::autoAdvance
bool autoAdvance() const
Definition: wizardpage.cpp:84
Kleo::Crypto::Gui::WizardPage::customNextButton
KGuiItem customNextButton() const
Definition: wizardpage.cpp:136
Kleo::Crypto::Gui::WizardPage::isComplete
virtual bool isComplete() const =0
Kleo::Crypto::Gui::Wizard::InvalidPage
Definition: wizard.h:55
Kleo::Crypto::Gui::Wizard::setPage
void setPage(int id, WizardPage *page)
Definition: wizard.cpp:210
Kleo::Crypto::Gui::WizardPage
Definition: wizardpage.h:48
Kleo::Crypto::Gui::Wizard::setPageVisible
void setPageVisible(int id, bool visible)
Definition: wizard.cpp:242
Kleo::Crypto::Gui::WizardPage::onNext
virtual void onNext()
Definition: wizardpage.cpp:147
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:42 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal