File indexing completed on 2024-04-28 16:55:46

0001 /*  This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "quickdialog.h"
0008 #include <KLocalizedString>
0009 #include <QQmlApplicationEngine>
0010 #include <QQmlContext>
0011 #include <QQuickWindow>
0012 #include <QTimer>
0013 
0014 QuickDialog::QuickDialog(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 QuickDialog::~QuickDialog() noexcept
0020 {
0021     delete m_theDialog;
0022 }
0023 
0024 void QuickDialog::create(const QString &file, const QVariantMap &props)
0025 {
0026     auto engine = new QQmlApplicationEngine(this);
0027     auto context = new KLocalizedContext(engine);
0028     context->setTranslationDomain(TRANSLATION_DOMAIN);
0029     engine->rootContext()->setContextObject(context);
0030 
0031     engine->setInitialProperties(props);
0032     engine->load(file);
0033 
0034     connect(engine, &QQmlEngine::warnings, this, [](const QList<QQmlError> &warnings) {
0035         for (const QQmlError &warning : warnings) {
0036             qWarning() << warning;
0037         }
0038     });
0039 
0040     m_theDialog = qobject_cast<QQuickWindow *>(engine->rootObjects().constFirst());
0041     connect(m_theDialog, SIGNAL(accept()), this, SLOT(accept()));
0042     connect(m_theDialog, SIGNAL(reject()), this, SLOT(reject()));
0043 
0044     QTimer::singleShot(0, m_theDialog, SLOT(present()));
0045 }
0046 
0047 bool QuickDialog::exec()
0048 {
0049     if (!m_theDialog) {
0050         qWarning() << "Failed to load dialog, cannot exec";
0051         return false;
0052     }
0053     return m_execLoop.exec() == 0;
0054 }
0055 
0056 void QuickDialog::reject()
0057 {
0058     m_execLoop.exit(1);
0059 }
0060 
0061 void QuickDialog::accept()
0062 {
0063     m_execLoop.quit();
0064 }