File: main.cpp

package info (click to toggle)
qcoro 0.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,700 kB
  • sloc: cpp: 8,573; python: 32; xml: 26; makefile: 23; sh: 15
file content (92 lines) | stat: -rw-r--r-- 2,432 bytes parent folder | download | duplicates (3)
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
// SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
//
// SPDX-License-Identifier: MIT

#include "qcoro/network/qcoronetworkreply.h"
#include "qcoro/qcorotask.h"

#include <QApplication>
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-enum-enum-conversion" // QSizePolicy seems problematic...
#endif // __clang__
#include <QBoxLayout>
#ifdef __clang__
#pragma clang diagnostic pop
#endif // __clang__
#include <QMainWindow>
#include <QMessageBox>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QProgressBar>
#include <QPushButton>

#include <memory>

// Programming langauges
static const QUrl wikiUrl =
    QUrl{QStringLiteral("https://www.wikidata.org/wiki/Special:EntityData/Q9143.json")};

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow() {
        mPb = new QProgressBar();
        mPb->setVisible(false);
        mPb->setMinimumWidth(200);
        mPb->setMinimum(0);
        mPb->setMaximum(0);

        mBtn = new QPushButton(tr("Start Download"));
        connect(mBtn, &QPushButton::clicked, this, &MainWindow::start);

        auto *vbox = new QVBoxLayout();
        vbox->addStretch(1);
        vbox->addWidget(mPb);
        vbox->addWidget(mBtn);
        vbox->addStretch(1);

        auto *hbox = new QHBoxLayout();
        hbox->addStretch(1);
        hbox->addLayout(vbox);
        hbox->addStretch(1);

        QWidget *w = new QWidget;
        w->setLayout(hbox);
        setCentralWidget(w);
    }

private Q_SLOTS:
    QCoro::Task<> start() {
        mPb->setVisible(true);
        mBtn->setEnabled(false);
        mBtn->setText(tr("Downloading ..."));

        std::unique_ptr<QNetworkReply> reply(co_await mNam.get(QNetworkRequest{wikiUrl}));
        if (reply->error()) {
            QMessageBox::warning(
                this, tr("Network request error"),
                tr("Error occured during network request. Error code: %1").arg(reply->error()));
            co_return;
        }

        mPb->setVisible(false);
        mBtn->setEnabled(true);
        mBtn->setText(tr("Done, download again"));
    }

private:
    QNetworkAccessManager mNam;
    QPushButton *mBtn = {};
    QProgressBar *mPb = {};
};

int main(int argc, char **argv) {
    QApplication app(argc, argv);
    MainWindow window;
    window.showNormal();
    return app.exec();
}

#include "main.moc"