KNewStuff

dialog.cpp
1/*
2 SPDX-FileCopyrightText: 2020-2023 Alexander Lohnau <alexander.lohnau@gmx.de>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "dialog.h"
8
9#include <QQmlContext>
10#include <QQmlEngine>
11#include <QQmlIncubationController>
12#include <QQuickItem>
13#include <QQuickWidget>
14#include <QVBoxLayout>
15
16#include <KLocalizedContext>
17
18#include "core/enginebase.h"
19#include "knewstuffwidgets_debug.h"
20
21using namespace KNSWidgets;
22
23class KNSWidgets::DialogPrivate
24{
25public:
26 KNSCore::EngineBase *engine = nullptr;
27 QQuickItem *item = nullptr;
28 QList<KNSCore::Entry> changedEntries;
29 void onEntryEvent(const KNSCore::Entry &entry, KNSCore::Entry::EntryEvent event)
30 {
32 if (entry.status() == KNSCore::Entry::Installing || entry.status() == KNSCore::Entry::Updating) {
33 return; // We do not care about intermediate states
34 }
35 // To make sure we have no duplicates and always the latest entry
36 changedEntries.removeOne(entry);
37 changedEntries.append(entry);
38 }
39 }
40};
41
42class PeriodicIncubationController : public QObject, public QQmlIncubationController
43{
44public:
45 explicit PeriodicIncubationController()
46 : QObject()
47 {
48 startTimer(16);
49 }
50
51protected:
52 void timerEvent(QTimerEvent *) override
53 {
54 incubateFor(5);
55 }
56};
57
58Dialog::Dialog(const QString &configFile, QWidget *parent)
59 : QDialog(parent)
60 , d(new DialogPrivate())
61{
62 auto engine = new QQmlEngine(this);
63 auto context = new KLocalizedContext(engine);
64 engine->setIncubationController(new PeriodicIncubationController());
65
66 setMinimumSize(600, 400);
67 // Keep in sync with the sizes in Dialog.qml and DialogContent.qml
68 // (reminder that a default gridUnit is 18px).
69 // TODO: It would be best to use a Kirigami.ApplicationWindow and use
70 // a multiple of gridUnit directly!
71 resize(792, 540);
72
73 context->setTranslationDomain(QStringLiteral("knewstuff6"));
74 engine->rootContext()->setContextObject(context);
75 engine->rootContext()->setContextProperty(QStringLiteral("knsrcfile"), configFile);
76
77 auto page = new QQuickWidget(engine, this);
78 page->setSource(QUrl(QStringLiteral("qrc:/knswidgets/page.qml")));
79 page->setResizeMode(QQuickWidget::SizeRootObjectToView);
80
81 auto layout = new QVBoxLayout(this);
82 layout->addWidget(page);
83 layout->setContentsMargins(0, 0, 0, 0);
84
85 if (QQuickItem *root = page->rootObject()) {
86 d->item = root;
87 d->engine = qvariant_cast<KNSCore::EngineBase *>(root->property("engine"));
88 Q_ASSERT(d->engine);
89
90 // clang-format off
91 // Old-style connect, because we don't want the QML engine to be exported
92 connect(d->engine,
94 this,
96 // clang-format on
97 } else {
98 qWarning(KNEWSTUFFWIDGETS) << "Error creating QtQuickDialogWrapper component:" << page->errors();
99 }
100}
101
102Dialog::~Dialog()
103{
104 delete d->item;
105}
106
108{
109 return d->engine;
110}
111
113{
114 return d->changedEntries;
115}
116
117void Dialog::open()
118{
120 d->changedEntries.clear();
121}
122
123#include "moc_dialog.cpp"
KNewStuff engine.
Definition enginebase.h:52
KNewStuff data entry container.
Definition entry.h:48
@ StatusChangedEvent
Used when an event's status is set (use Entry::status() to get the new status)
Definition entry.h:122
QList< KNSCore::Entry > changedEntries() const
Entries that were changed while the user interacted with the dialog.
Definition dialog.cpp:112
KNSCore::EngineBase * engine()
Engine that is used by the dialog, might be null if the engine failed to initialize.
virtual void open()
void addWidget(QWidget *w)
void setContentsMargins(const QMargins &margins)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
int startTimer(int interval, Qt::TimerType timerType)
void incubateFor(int msecs)
QLayout * layout() const const
void setMinimumSize(const QSize &)
void resize(const QSize &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:54:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.