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

kleopatra

  • sources
  • kde-4.14
  • 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 <KLocalizedString>
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 
53 #include <map>
54 #include <set>
55 
56 #include <cassert>
57 
58 using namespace Kleo::Crypto::Gui;
59 
60 class Wizard::Private {
61  friend class ::Wizard;
62  Wizard * const q;
63 public:
64  explicit Private( Wizard * qq );
65  ~Private();
66 
67  void updateButtonStates();
68  bool isLastPage( int id ) const;
69  int previousPage() const;
70  void updateHeader();
71 
72 private:
73  std::vector<int> pageOrder;
74  std::set<int> hiddenPages;
75  std::map<int, WizardPage*> idToPage;
76  int currentId;
77  QStackedWidget* stack;
78  KPushButton* nextButton;
79  QPushButton* backButton;
80  QPushButton* cancelButton;
81  KGuiItem finishItem;
82  KGuiItem nextItem;
83  QFrame* titleFrame;
84  QLabel* titleLabel;
85  QLabel* subTitleLabel;
86  QFrame* explanationFrame;
87  QLabel* explanationLabel;
88  QTimer* nextPageTimer;
89 };
90 
91 
92 Wizard::Private::Private( Wizard * qq )
93  : q( qq ), currentId( -1 ), stack( new QStackedWidget )
94 {
95  nextPageTimer = new QTimer( q );
96  nextPageTimer->setInterval( 0 );
97  connect( nextPageTimer, SIGNAL(timeout()), q, SLOT(next()) );
98  nextItem = KGuiItem( i18n( "&Next" ) );
99  finishItem = KStandardGuiItem::ok();
100  QVBoxLayout * const top = new QVBoxLayout( q );
101  top->setMargin( 0 );
102  titleFrame = new QFrame;
103  titleFrame->setFrameShape( QFrame::StyledPanel );
104  titleFrame->setAutoFillBackground( true );
105  titleFrame->setBackgroundRole( QPalette::Base );
106  QVBoxLayout* const titleLayout = new QVBoxLayout( titleFrame );
107  titleLabel = new QLabel;
108  titleLayout->addWidget( titleLabel );
109  subTitleLabel = new QLabel;
110  subTitleLabel->setWordWrap( true );
111  titleLayout->addWidget( subTitleLabel );
112  top->addWidget( titleFrame );
113  titleFrame->setVisible( false );
114 
115  top->addWidget( stack );
116 
117  explanationFrame = new QFrame;
118  explanationFrame->setFrameShape( QFrame::StyledPanel );
119  explanationFrame->setAutoFillBackground( true );
120  explanationFrame->setBackgroundRole( QPalette::Base );
121  QVBoxLayout* const explanationLayout = new QVBoxLayout( explanationFrame );
122  explanationLabel = new QLabel;
123  explanationLabel->setWordWrap( true );
124  explanationLayout->addWidget( explanationLabel );
125  top->addWidget( explanationFrame );
126  explanationFrame->setVisible( false );
127 
128  QWidget* buttonWidget = new QWidget;
129  QHBoxLayout* buttonLayout = new QHBoxLayout( buttonWidget );
130  QDialogButtonBox * const box = new QDialogButtonBox;
131 
132  cancelButton = box->addButton( QDialogButtonBox::Cancel );
133  q->connect( cancelButton, SIGNAL(clicked()), q, SLOT(reject()) );
134 
135  backButton = new QPushButton;
136  backButton->setText( i18n( "Back" ) );
137  q->connect( backButton, SIGNAL(clicked()), q, SLOT(back()) );
138  box->addButton( backButton, QDialogButtonBox::ActionRole );
139 
140  nextButton = new KPushButton;
141  nextButton->setGuiItem( nextItem );
142  q->connect( nextButton, SIGNAL(clicked()), q, SLOT(next()) );
143  box->addButton( nextButton, QDialogButtonBox::ActionRole );
144  buttonLayout->addWidget( box );
145 
146  top->addWidget( buttonWidget );
147 
148  q->connect( q, SIGNAL(rejected()), q, SIGNAL(canceled()) );
149 }
150 
151 Wizard::Private::~Private() { kDebug(); }
152 
153 
154 
155 bool Wizard::Private::isLastPage( int id ) const
156 {
157  return !pageOrder.empty() ? pageOrder.back() == id : false;
158 }
159 
160 void Wizard::Private::updateButtonStates()
161 {
162  const bool isLast = isLastPage( currentId );
163  const bool canGoToNext = q->canGoToNextPage();
164  WizardPage* const page = q->page( currentId );
165  const KGuiItem customNext = page ? page->customNextButton() : KGuiItem();
166  if ( customNext.text().isEmpty() && customNext.icon().isNull() )
167  nextButton->setGuiItem( isLast ? finishItem : nextItem );
168  else
169  nextButton->setGuiItem( customNext );
170  nextButton->setEnabled( canGoToNext );
171  cancelButton->setEnabled( !isLast || !canGoToNext );
172  backButton->setEnabled( q->canGoToPreviousPage() );
173  if ( page && page->autoAdvance() && page->isComplete() )
174  nextPageTimer->start();
175 }
176 
177 void Wizard::Private::updateHeader()
178 {
179  WizardPage * const widget = q->page( currentId );
180  assert( !widget || stack->indexOf( widget ) != -1 );
181  if ( widget )
182  stack->setCurrentWidget( widget );
183  const QString title = widget ? widget->title() : QString();
184  const QString subTitle = widget ? widget->subTitle() : QString();
185  const QString explanation = widget ? widget->explanation() : QString();
186  titleFrame->setVisible( !title.isEmpty() || !subTitle.isEmpty() || !explanation.isEmpty() );
187  titleLabel->setVisible( !title.isEmpty() );
188  titleLabel->setText( title );
189  subTitleLabel->setText( subTitle );
190  subTitleLabel->setVisible( !subTitle.isEmpty() );
191  explanationFrame->setVisible( !explanation.isEmpty() );
192  explanationLabel->setVisible( !explanation.isEmpty() );
193  explanationLabel->setText( explanation );
194  q->resize( q->sizeHint().expandedTo( q->size() ) );
195 }
196 
197 Wizard::Wizard( QWidget * parent, Qt::WindowFlags f )
198  : QDialog( parent, f ), d( new Private( this ) )
199 {
200 
201 }
202 
203 Wizard::~Wizard() { kDebug(); }
204 
205 void Wizard::setPage( int id, WizardPage* widget )
206 {
207  kleo_assert( id != InvalidPage );
208  kleo_assert( d->idToPage.find( id ) == d->idToPage.end() );
209  d->idToPage[id] = widget;
210  d->stack->addWidget( widget );
211  connect( widget, SIGNAL(completeChanged()), this, SLOT(updateButtonStates()) );
212  connect( widget, SIGNAL(titleChanged()), this, SLOT(updateHeader()) );
213  connect( widget, SIGNAL(subTitleChanged()), this, SLOT(updateHeader()) );
214  connect( widget, SIGNAL(explanationChanged()), this, SLOT(updateHeader()) );
215  connect( widget, SIGNAL(autoAdvanceChanged()), this, SLOT(updateButtonStates()) );
216  connect( widget, SIGNAL(windowTitleChanged(QString)), this, SLOT(setWindowTitle(QString)) );
217 }
218 
219 void Wizard::setPageOrder( const std::vector<int>& pageOrder )
220 {
221  d->pageOrder = pageOrder;
222  d->hiddenPages.clear();
223  if ( pageOrder.empty() )
224  return;
225  setCurrentPage( pageOrder.front() );
226 }
227 
228 void Wizard::setCurrentPage( int id )
229 {
230  d->currentId = id;
231  if ( id == InvalidPage )
232  return;
233  d->updateHeader();
234  d->updateButtonStates();
235 }
236 
237 void Wizard::setPageVisible( int id, bool visible )
238 {
239  if ( visible )
240  d->hiddenPages.erase( id );
241  else
242  d->hiddenPages.insert( id );
243  if ( currentPage() == id && !visible )
244  next();
245 }
246 
247 int Wizard::currentPage() const
248 {
249  return d->currentId;
250 }
251 
252 bool Wizard::canGoToNextPage() const
253 {
254  const WizardPage * const current = currentPageWidget();
255  return current ? current->isComplete() : false;
256 }
257 
258 bool Wizard::canGoToPreviousPage() const
259 {
260  const int prev = d->previousPage();
261  if ( prev == InvalidPage )
262  return false;
263  const WizardPage * const prevPage = page( prev );
264  assert( prevPage );
265  return !prevPage->isCommitPage();
266 }
267 
268 
269 void Wizard::next()
270 {
271  WizardPage* const current = currentPageWidget();
272  if ( current )
273  current->onNext();
274  onNext( d->currentId );
275  std::vector<int>::const_iterator it = qBinaryFind( d->pageOrder.begin(), d->pageOrder.end(), d->currentId );
276  assert( it != d->pageOrder.end() );
277 
278  do {
279  ++it;
280  }
281  while ( d->hiddenPages.find( *it ) != d->hiddenPages.end() );
282 
283  if ( it == d->pageOrder.end() ) // "Finish"
284  {
285  d->currentId = InvalidPage;
286  close();
287  }
288  else // "next"
289  {
290  setCurrentPage( *it );
291  }
292 }
293 
294 int Wizard::Private::previousPage() const
295 {
296  if ( pageOrder.empty() )
297  return InvalidPage;
298 
299  std::vector<int>::const_iterator it = qBinaryFind( pageOrder.begin(), pageOrder.end(), currentId );
300  if ( it == pageOrder.begin() || it == pageOrder.end() )
301  return InvalidPage;
302 
303  do {
304  --it;
305  } while ( it != pageOrder.begin() && hiddenPages.find( *it ) != hiddenPages.end() );
306  return *it;
307 }
308 
309 void Wizard::back()
310 {
311  onBack( d->currentId );
312  const int prev = d->previousPage();
313  if ( prev == InvalidPage )
314  return;
315  setCurrentPage( prev );
316 }
317 
318 const WizardPage* Wizard::page( int id ) const
319 {
320  if ( id == InvalidPage )
321  return 0;
322 
323  const std::map<int, WizardPage*>::const_iterator it = d->idToPage.find( id );
324  kleo_assert( it != d->idToPage.end() );
325  return (*it).second;
326 }
327 
328 const WizardPage* Wizard::currentPageWidget() const
329 {
330  return page( d->currentId );
331 }
332 
333 
334 WizardPage* Wizard::currentPageWidget()
335 {
336  return page( d->currentId );
337 }
338 
339 void Wizard::onNext( int currentId )
340 {
341  Q_UNUSED( currentId )
342 }
343 
344 
345 void Wizard::onBack( int currentId )
346 {
347  Q_UNUSED( currentId )
348 }
349 
350 WizardPage* Wizard::page( int id )
351 {
352  if ( id == InvalidPage )
353  return 0;
354 
355  const std::map<int, WizardPage*>::const_iterator it = d->idToPage.find( id );
356  kleo_assert( it != d->idToPage.end() );
357  return (*it).second;
358 }
359 
360 #include "moc_wizard.cpp"
QWidget::close
bool close()
QWidget
Kleo::Crypto::Gui::Wizard::~Wizard
~Wizard()
Definition: wizard.cpp:203
Kleo::Crypto::Gui::Wizard::page
const WizardPage * page(int id) const
Definition: wizard.cpp:318
Kleo::Crypto::Gui::Wizard::setPageOrder
void setPageOrder(const std::vector< int > &pages)
Definition: wizard.cpp:219
QFrame::setFrameShape
void setFrameShape(Shape)
QDialogButtonBox::addButton
void addButton(QAbstractButton *button, ButtonRole role)
Kleo::Crypto::Gui::WizardPage::title
QString title() const
Definition: wizardpage.cpp:97
QHBoxLayout
Kleo::Crypto::Gui::Wizard::back
void back()
Definition: wizard.cpp:309
Kleo::Crypto::Gui::WizardPage::subTitle
QString subTitle() const
Definition: wizardpage.cpp:110
kleo_assert.h
wizard.h
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
d
#define d
Definition: adduseridcommand.cpp:89
Kleo::Crypto::Gui::Wizard::onNext
virtual void onNext(int currentId)
Definition: wizard.cpp:339
QTimer
Kleo::Crypto::Gui::Wizard::canGoToNextPage
bool canGoToNextPage() const
Definition: wizard.cpp:252
Kleo::Crypto::Gui::Wizard::next
void next()
Definition: wizard.cpp:269
Kleo::Crypto::Gui::Wizard::setCurrentPage
void setCurrentPage(int id)
Definition: wizard.cpp:228
QString::isEmpty
bool isEmpty() const
QVBoxLayout
Kleo::Crypto::Gui::Wizard::canGoToPreviousPage
bool canGoToPreviousPage() const
Definition: wizard.cpp:258
QStackedWidget
wizardpage.h
Kleo::Crypto::Gui::Wizard::currentPage
int currentPage() const
Definition: wizard.cpp:247
QString
QLayout::setMargin
void setMargin(int margin)
Kleo::Crypto::Gui::WizardPage::isCommitPage
bool isCommitPage() const
Definition: wizardpage.cpp:74
QFrame
Kleo::Crypto::Gui::WizardPage::explanation
QString explanation() const
Definition: wizardpage.cpp:123
kleo_assert
#define kleo_assert(cond)
Definition: kleo_assert.h:86
Kleo::Crypto::Gui::Wizard::onBack
virtual void onBack(int currentId)
Definition: wizard.cpp:345
Kleo::Crypto::Gui::Wizard::currentPageWidget
const WizardPage * currentPageWidget() const
Definition: wizard.cpp:328
QDialogButtonBox
Kleo::Crypto::Gui::Wizard
Definition: wizard.h:49
QWidget::setWindowTitle
void setWindowTitle(const QString &)
q
#define q
Definition: adduseridcommand.cpp:90
Kleo::Crypto::Gui::WizardPage::autoAdvance
bool autoAdvance() const
Definition: wizardpage.cpp:84
Kleo::Crypto::Gui::WizardPage::customNextButton
KGuiItem customNextButton() const
Definition: wizardpage.cpp:136
QAbstractButton::setText
void setText(const QString &text)
QDialog
QPushButton
Qt::WindowFlags
typedef WindowFlags
Kleo::Crypto::Gui::WizardPage::isComplete
virtual bool isComplete() const =0
Kleo::Crypto::Gui::Wizard::InvalidPage
Definition: wizard.h:56
Kleo::Crypto::Gui::Wizard::setPage
void setPage(int id, WizardPage *page)
Definition: wizard.cpp:205
Kleo::Crypto::Gui::WizardPage
Definition: wizardpage.h:48
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QLabel
QLabel::setWordWrap
void setWordWrap(bool on)
Kleo::Crypto::Gui::Wizard::setPageVisible
void setPageVisible(int id, bool visible)
Definition: wizard.cpp:237
Kleo::Crypto::Gui::WizardPage::onNext
virtual void onNext()
Definition: wizardpage.cpp:147
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:11 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
  • pimprint

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