Welcome to mirror list, hosted at ThFree Co, Russian Federation.

setupwizardwindow.cpp « newwizard « gui « src - github.com/owncloud/client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 179665c268010a7da400caf07eda8c0c3f443c0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "setupwizardwindow.h"
#include "ui_setupwizardwindow.h"

#include "gui/application.h"
#include "gui/owncloudgui.h"
#include "gui/settingsdialog.h"
#include "theme.h"

#include <QLabel>
#include <QStyleFactory>

using namespace std::chrono_literals;

namespace {

using namespace OCC;

QString replaceCssColors(QString stylesheet)
{
    return Utility::renderTemplate(stylesheet, {
                                                   { QStringLiteral("WIZARD_BACKGROUND_COLOR"), Theme::instance()->wizardHeaderBackgroundColor().name() }, //
                                                   { QStringLiteral("WIZARD_FONT_COLOR"), Theme::instance()->wizardHeaderTitleColor().name() } //
                                               });
}

}

namespace OCC::Wizard {

Q_LOGGING_CATEGORY(lcSetupWizardWindow, "setupwizard.window")

SetupWizardWindow::SetupWizardWindow(QWidget *parent)
    : QDialog(parent)
    , _ui(new ::Ui::SetupWizardWindow)
{
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

    _ui->setupUi(this);

    slotHideErrorMessageWidget();

    // cannot do this in Qt Designer
    _ui->contentWidget->layout()->setAlignment(Qt::AlignCenter);

    connect(_ui->cancelButton, &QPushButton::clicked, this, &SetupWizardWindow::reject);

    connect(_ui->nextButton, &QPushButton::clicked, this, &SetupWizardWindow::slotMoveToNextPage);

    connect(_ui->backButton, &QPushButton::clicked, this, [this]() {
        slotStartTransition();
        emit backButtonClicked(_ui->pagination->activePageIndex());
    });

    connect(_ui->pagination, &Pagination::paginationEntryClicked, this, [this](PageIndex clickedPageIndex) {
        slotStartTransition();
        emit paginationEntryClicked(_ui->pagination->activePageIndex(), clickedPageIndex);
    });

    resize(ocApp()->gui()->settingsDialog()->sizeHintForChild());

    // different styles (e.g., 'Windows', 'Fusion') may require different approaches in the stylesheet
    // therefore we want to force a standard style on all platforms
    // this further makes sure the wizard (well, its contents) looks exactly the same on all platforms
    // Fusion should be available everywhere
    auto fusionStyle = QStyleFactory::create(QStringLiteral("Fusion"));
    if (OC_ENSURE(fusionStyle != nullptr)) {
        _ui->contentWidget->setStyle(fusionStyle);
    } else {
        qCDebug(lcSetupWizardWindow) << "Could not set up default style, wizard contents will be shown using default style";
    }

    loadStylesheet();

    _ui->transitionProgressIndicator->setFixedSize(32, 32);
    _ui->transitionProgressIndicator->setColor(Theme::instance()->wizardHeaderTitleColor());

    // handle user pressing enter/return key
    installEventFilter(this);
}

void SetupWizardWindow::loadStylesheet()
{
    QString path = QStringLiteral(":/client/resources/wizard/style.qss");

    QFile file(path);
    Q_ASSERT(file.exists());
    if (!OC_ENSURE(file.open(QIODevice::ReadOnly))) {
        qCCritical(lcSetupWizardWindow) << "failed to load stylesheet";
    }

    QString stylesheet = replaceCssColors(QString::fromUtf8(file.readAll()));
    _ui->contentWidget->setStyleSheet(stylesheet);
}

void SetupWizardWindow::displayPage(AbstractSetupWizardPage *page, PageIndex index)
{
    _transitioning = false;
    _ui->backButton->setEnabled(true);
    _ui->nextButton->setEnabled(true);

    if (index == 0) {
        _ui->backButton->setEnabled(false);
    } else if (index == _ui->pagination->entriesCount()) {
        _ui->nextButton->setEnabled(false);
    }

    if (index >= (_ui->pagination->entriesCount() - 1)) {
        _ui->nextButton->setText(tr("Finish"));
    } else {
        _ui->nextButton->setText(tr("Next >"));
    }

    _currentPage = page;
    slotReplaceContent(_currentPage);

    // initial check whether to enable the next button right away
    slotUpdateNextButton();

    _ui->pagination->setActivePageIndex(index);
    _ui->pagination->setEnabled(true);

    connect(_ui->errorMessageDismissButton, &QPushButton::clicked, this, &SetupWizardWindow::slotHideErrorMessageWidget);

    // by default, set focus on the next button
    _ui->nextButton->setFocus();

    // bring to front if necessary
    ownCloudGui::raiseDialog(this);

    // this can optionally be overwritten by the page
    Q_EMIT _currentPage->pageDisplayed();
}

void SetupWizardWindow::slotStartTransition()
{
    _transitioning = true;

    _ui->transitionProgressIndicator->startAnimation();
    _ui->contentWidget->setCurrentWidget(_ui->transitionPage);

    // until a new page is displayed by the controller, we want to prevent the user from initiating another page change
    _ui->backButton->setEnabled(false);
    _ui->nextButton->setEnabled(false);
    _ui->pagination->setEnabled(false);
    // also, we should assume the user has seen the error message in case one is shown
    slotHideErrorMessageWidget();
}

void SetupWizardWindow::slotReplaceContent(QWidget *newWidget)
{
    _ui->contentWidget->removeWidget(_currentContentWidget);

    _ui->contentWidget->addWidget(newWidget);
    _ui->contentWidget->setCurrentWidget(newWidget);

    _currentContentWidget = newWidget;

    // inheriting the style sheet from content widget doesn't work in all cases
    _currentContentWidget->setStyleSheet(_ui->contentWidget->styleSheet());
}

void SetupWizardWindow::slotHideErrorMessageWidget()
{
    _ui->errorMessageWidget->hide();
}

void SetupWizardWindow::showErrorMessage(const QString &errorMessage)
{
    _ui->errorMessageLabel->setText(errorMessage);
    _ui->errorMessageLabel->setWordWrap(true);
    _ui->errorMessageWidget->show();
}

void SetupWizardWindow::setPaginationEntries(const QStringList &paginationEntries)
{
    _ui->pagination->setEntries(paginationEntries);
}

void SetupWizardWindow::slotUpdateNextButton()
{
    _ui->nextButton->setEnabled(_currentPage->validateInput());
}

bool SetupWizardWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (!_transitioning) {
        // whenever the user types another character somewhere inside the page, we can re-evaluate whether to enable the next button
        switch (event->type()) {
        case QEvent::KeyPress:
        case QEvent::KeyRelease:
            slotUpdateNextButton();
            break;
        default:
            break;
        }

        if (obj == _currentPage.data() || obj == this) {
            if (event->type() == QEvent::KeyPress) {
                auto keyEvent = dynamic_cast<QKeyEvent *>(event);

                switch (keyEvent->key()) {
                case Qt::Key_Enter:
                    Q_FALLTHROUGH();
                case Qt::Key_Return:
                    slotMoveToNextPage();
                    return true;
                default:
                    // no action required, give other handlers a chance
                    break;
                }
            }
        }
    }

    return QDialog::eventFilter(obj, event);
}

void SetupWizardWindow::disableNextButton()
{
    _ui->nextButton->setEnabled(false);
}

SetupWizardWindow::~SetupWizardWindow() noexcept
{
    delete _ui;
}

void SetupWizardWindow::slotMoveToNextPage()
{
    slotStartTransition();
    emit nextButtonClicked(_ui->pagination->activePageIndex());
}
}