KWidgetsAddons

kassistantdialog.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2006 Olivier Goffart <ogoffart at kde.org>
4 
5  SPDX-License-Identifier: LGPL-2.0-only
6 */
7 
8 #include "kassistantdialog.h"
9 
10 #include "kpagedialog_p.h"
11 #include <QApplication>
12 #include <QDialogButtonBox>
13 #include <QIcon>
14 #include <QPushButton>
15 
16 #include <QHash>
17 
18 class KAssistantDialogPrivate : public KPageDialogPrivate
19 {
20  Q_DECLARE_PUBLIC(KAssistantDialog)
21  Q_DECLARE_TR_FUNCTIONS(KAssistantDialog)
22 
23 public:
24  KAssistantDialogPrivate(KAssistantDialog *qq)
25  : KPageDialogPrivate(qq)
26  {
27  }
28 
31  KPageWidgetModel *pageModel = nullptr;
32  QPushButton *backButton = nullptr;
33  QPushButton *nextButton = nullptr;
34  QPushButton *finishButton = nullptr;
35 
36  void init();
37  void slotUpdateButtons();
38 
39  QModelIndex getNext(QModelIndex nextIndex)
40  {
41  QModelIndex currentIndex;
42  do {
43  currentIndex = nextIndex;
44  nextIndex = pageModel->index(0, 0, currentIndex);
45  if (!nextIndex.isValid()) {
46  nextIndex = currentIndex.sibling(currentIndex.row() + 1, 0);
47  }
48  } while (nextIndex.isValid() && !appropriate.value(pageModel->item(nextIndex), true));
49  return nextIndex;
50  }
51 
52  QModelIndex getPrevious(QModelIndex nextIndex)
53  {
54  QModelIndex currentIndex;
55  do {
56  currentIndex = nextIndex;
57  nextIndex = currentIndex.sibling(currentIndex.row() - 1, 0);
58  if (!nextIndex.isValid()) {
59  nextIndex = currentIndex.parent();
60  }
61  } while (nextIndex.isValid() && !appropriate.value(pageModel->item(nextIndex), true));
62  return nextIndex;
63  }
64 };
65 
67  : KPageDialog(*new KAssistantDialogPrivate(this), nullptr, parent, flags)
68 {
69  Q_D(KAssistantDialog);
70 
71  d->init();
72  // workaround to get the page model
73  KPageWidget *pagewidget = findChild<KPageWidget *>();
74  Q_ASSERT(pagewidget);
75  d->pageModel = static_cast<KPageWidgetModel *>(pagewidget->model());
76 }
77 
79  : KPageDialog(*new KAssistantDialogPrivate(this), widget, parent, flags)
80 {
81  Q_D(KAssistantDialog);
82 
83  d->init();
84  d->pageModel = static_cast<KPageWidgetModel *>(widget->model());
85 }
86 
87 KAssistantDialog::~KAssistantDialog() = default;
88 
89 void KAssistantDialogPrivate::init()
90 {
91  Q_Q(KAssistantDialog);
92 
93  QDialogButtonBox *buttonBox = q->buttonBox();
94 
96  backButton = new QPushButton;
97 
98  const QString iconBack = QApplication::isRightToLeft() ? QStringLiteral("go-next") : QStringLiteral("go-previous");
99  const QString iconNext = QApplication::isRightToLeft() ? QStringLiteral("go-previous") : QStringLiteral("go-next");
100  backButton->setText(tr("&Back", "@action:button go back"));
101  backButton->setIcon(QIcon::fromTheme(iconBack));
102  backButton->setToolTip(tr("Go back one step", "@info:tooltip"));
103  q->connect(backButton, &QAbstractButton::clicked, q, &KAssistantDialog::back);
104  buttonBox->addButton(backButton, QDialogButtonBox::ActionRole);
105 
106  nextButton = new QPushButton;
107  nextButton->setText(tr("Next", "@action:button Opposite to Back"));
108  nextButton->setIcon(QIcon::fromTheme(iconNext));
109  nextButton->setDefault(true);
110  q->connect(nextButton, &QAbstractButton::clicked, q, &KAssistantDialog::next);
111  buttonBox->addButton(nextButton, QDialogButtonBox::ActionRole);
112 
113  finishButton = new QPushButton;
114  finishButton->setText(tr("Finish", "@action:button"));
115  finishButton->setIcon(QIcon::fromTheme(QStringLiteral("dialog-ok-apply")));
116  buttonBox->addButton(finishButton, QDialogButtonBox::AcceptRole);
117 
118  q->setFaceType(KPageDialog::Plain);
119 
120  q->connect(q, &KAssistantDialog::currentPageChanged, q, [this]() {
121  slotUpdateButtons();
122  });
123 }
124 
126 {
127  Q_D(KAssistantDialog);
128 
129  QModelIndex nextIndex = d->getPrevious(d->pageModel->index(currentPage()));
130  if (nextIndex.isValid()) {
131  setCurrentPage(d->pageModel->item(nextIndex));
132  }
133 }
134 
136 {
137  Q_D(KAssistantDialog);
138 
139  QModelIndex nextIndex = d->getNext(d->pageModel->index(currentPage()));
140  if (nextIndex.isValid()) {
141  setCurrentPage(d->pageModel->item(nextIndex));
142  } else if (isValid(currentPage())) {
143  accept();
144  }
145 }
146 
148 {
149  Q_D(KAssistantDialog);
150 
151  d->valid[page] = enable;
152  if (page == currentPage()) {
153  d->slotUpdateButtons();
154  }
155 }
156 
158 {
159  Q_D(const KAssistantDialog);
160 
161  return d->valid.value(page, true);
162 }
163 
164 void KAssistantDialogPrivate::slotUpdateButtons()
165 {
166  Q_Q(KAssistantDialog);
167 
168  QModelIndex currentIndex = pageModel->index(q->currentPage());
169  // change the caption of the next/finish button
170  QModelIndex nextIndex = getNext(currentIndex);
171  finishButton->setEnabled(!nextIndex.isValid() && q->isValid(q->currentPage()));
172  nextButton->setEnabled(nextIndex.isValid() && q->isValid(q->currentPage()));
173  finishButton->setDefault(!nextIndex.isValid());
174  nextButton->setDefault(nextIndex.isValid());
175  // enable or disable the back button;
176  nextIndex = getPrevious(currentIndex);
177  backButton->setEnabled(nextIndex.isValid());
178 }
179 
180 void KAssistantDialog::showEvent(QShowEvent *event)
181 {
182  Q_D(KAssistantDialog);
183 
184  d->slotUpdateButtons(); // called because last time that function was called is when the first page was added, so the next button show "finish"
185  KPageDialog::showEvent(event);
186 }
187 
189 {
190  Q_D(KAssistantDialog);
191 
192  d->appropriate[page] = appropriate;
193  d->slotUpdateButtons();
194 }
195 
197 {
198  Q_D(const KAssistantDialog);
199 
200  return d->appropriate.value(page, true);
201 }
202 
204 {
205  Q_D(const KAssistantDialog);
206 
207  return d->backButton;
208 }
209 
211 {
212  Q_D(const KAssistantDialog);
213 
214  return d->nextButton;
215 }
216 
218 {
219  Q_D(const KAssistantDialog);
220 
221  return d->finishButton;
222 }
223 
224 #include "moc_kassistantdialog.cpp"
bool isRightToLeft()
A dialog base class which can handle multiple pages.
Definition: kpagedialog.h:77
void setCurrentPage(KPageWidgetItem *item)
Sets the page which is associated with the given KPageWidgetItem to be the current page and emits the...
void addButton(QAbstractButton *button, QDialogButtonBox::ButtonRole role)
bool isAppropriate(KPageWidgetItem *page) const
Check if a page is appropriate for use in the assistant dialog.
KAssistantDialog(QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::WindowFlags())
Construct a new assistant dialog with parent as parent.
virtual void back()
Called when the user clicks the Back button.
virtual void next()
Called when the user clicks the Next/Finish button.
void setIcon(const QIcon &icon)
QString tr(const char *sourceText, const char *disambiguation, int n)
A normal dialog.
Definition: kpagedialog.h:97
This page model is used by KPageWidget to provide a hierarchical layout of pages. ...
QDialogButtonBox * buttonBox()
Returns the button box of the dialog or a null pointer if no button box is set.
This class provides a framework for assistant dialogs.
bool isValid() const const
QPushButton * finishButton() const
void setEnabled(bool)
KPageWidgetItem * currentPage() const
Returns the KPageWidgetItem for the current page or a null pointer if there is no current page...
void setAppropriate(KPageWidgetItem *page, bool appropriate)
Specify whether a page is appropriate.
bool isValid(KPageWidgetItem *page) const
return if a page is valid
QPushButton * nextButton() const
void clicked(bool checked)
int row() const const
QPushButton * backButton() const
QModelIndex parent() const const
QAbstractItemModel * model() const
Returns the model of the page view.
Definition: kpageview.cpp:369
virtual void accept()
Page widget with many layouts (faces).
Definition: kpagewidget.h:24
const T value(const Key &key) const const
void currentPageChanged(KPageWidgetItem *current, KPageWidgetItem *before)
This signal is emitted whenever the current page has changed.
KPageWidgetItem * item(const QModelIndex &index) const
Returns the KPageWidgetItem for a given index or a null pointer if the index is invalid.
QCA_EXPORT void init()
void setStandardButtons(QDialogButtonBox::StandardButtons buttons)
QModelIndex sibling(int row, int column) const const
KPageWidgetItem is used by KPageWidget and represents a page.
void setValid(KPageWidgetItem *page, bool enable)
Specify if the content of the page is valid, and if the next button may be enabled on this page...
virtual void showEvent(QShowEvent *event) override
void setText(const QString &text)
QIcon fromTheme(const QString &name)
void setToolTip(const QString &)
QObject * parent() const const
void setDefault(bool)
virtual bool event(QEvent *event) override
typedef WindowFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Nov 26 2021 22:47:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.