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
18class KAssistantDialogPrivate : public KPageDialogPrivate
19{
20 Q_DECLARE_PUBLIC(KAssistantDialog)
21 Q_DECLARE_TR_FUNCTIONS(KAssistantDialog)
22
23public:
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{
70
71 d->init();
72 // workaround to get the page model
74 Q_ASSERT(pagewidget);
75 d->pageModel = static_cast<KPageWidgetModel *>(pagewidget->model());
76}
77
79 : KPageDialog(*new KAssistantDialogPrivate(this), widget, parent, flags)
80{
82
83 d->init();
84 d->pageModel = static_cast<KPageWidgetModel *>(widget->model());
85}
86
87KAssistantDialog::~KAssistantDialog() = default;
88
89void KAssistantDialogPrivate::init()
90{
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
121 slotUpdateButtons();
122 });
123}
124
126{
128
129 QModelIndex nextIndex = d->getPrevious(d->pageModel->index(currentPage()));
130 if (nextIndex.isValid()) {
131 setCurrentPage(d->pageModel->item(nextIndex));
132 }
133}
134
136{
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{
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
164void KAssistantDialogPrivate::slotUpdateButtons()
165{
166 Q_Q(KAssistantDialog);
167
168 QModelIndex currentIndex = pageModel->index(q->currentPage());
169 // change the title 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
180void KAssistantDialog::showEvent(QShowEvent *event)
181{
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"
186}
187
189{
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"
This class provides a framework for assistant dialogs.
KAssistantDialog(QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::WindowFlags())
Construct a new assistant dialog with parent as parent.
bool isValid(KPageWidgetItem *page) const
return if a page is valid
QPushButton * finishButton() const
virtual void back()
Called when the user clicks the Back button.
virtual void next()
Called when the user clicks the Next/Finish button.
bool isAppropriate(KPageWidgetItem *page) const
Check if a page is appropriate for use in the assistant dialog.
QPushButton * backButton() const
void setAppropriate(KPageWidgetItem *page, bool appropriate)
Specify whether a page is appropriate.
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.
QPushButton * nextButton() const
A dialog base class which can handle multiple pages.
Definition kpagedialog.h:78
@ Plain
A normal dialog.
Definition kpagedialog.h:97
void currentPageChanged(KPageWidgetItem *current, KPageWidgetItem *before)
This signal is emitted whenever the current page has changed.
void setCurrentPage(KPageWidgetItem *item)
Sets the page which is associated with the given KPageWidgetItem to be the current page and emits the...
KPageWidgetItem * currentPage() const
Returns the KPageWidgetItem for the current page or a null pointer if there is no current page.
QAbstractItemModel * model() const
Returns the model of the page view.
KPageWidgetItem is used by KPageWidget and represents a page.
This page model is used by KPageWidget to provide a hierarchical layout of pages.
KPageWidgetItem * item(const QModelIndex &index) const
Returns the KPageWidgetItem for a given index or a null pointer if the index is invalid.
Page widget with many layouts (faces).
Definition kpagewidget.h:25
void clicked(bool checked)
void setIcon(const QIcon &icon)
void setText(const QString &text)
virtual void accept()
virtual void showEvent(QShowEvent *event) override
QPushButton * addButton(StandardButton button)
void setStandardButtons(StandardButtons buttons)
bool isRightToLeft()
T value(const Key &key) const const
QIcon fromTheme(const QString &name)
bool isValid() const const
QModelIndex parent() const const
int row() const const
QModelIndex sibling(int row, int column) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T findChild(const QString &name, Qt::FindChildOptions options) const const
void setDefault(bool)
typedef WindowFlags
void setEnabled(bool)
virtual bool event(QEvent *event) override
void setToolTip(const QString &)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:51 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.