File indexing completed on 2022-09-20 12:22:40

0001 /*
0002  *  SPDX-FileCopyrightText: 2012 Sebastian Gottfried <sebastiangottfried@web.de>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "mainwindow.h"
0008 
0009 #include <QVariant>
0010 #include <QStandardPaths>
0011 #include <QQmlContext>
0012 #include <QQmlError>
0013 #include <QMessageBox>
0014 
0015 #include <KAboutData>
0016 #include <KLocalizedString>
0017 
0018 #include "application.h"
0019 #include "ktouchcontext.h"
0020 
0021 MainWindow::MainWindow(QWidget* parent):
0022     KMainWindow(parent),
0023     m_view(new QQuickView()),
0024     m_context(new KTouchContext(this, m_view, this))
0025 {
0026     init();
0027 }
0028 
0029 MainWindow::~MainWindow()
0030 {
0031 }
0032 
0033 
0034 void MainWindow::init()
0035 {
0036     QWidget* viewWidget = QWidget::createWindowContainer(m_view, this);
0037     const int unit = fontMetrics().height();
0038     viewWidget->setMinimumSize(56 * unit, 39 * unit);
0039     viewWidget->setFocusPolicy(Qt::StrongFocus);
0040     setCentralWidget(viewWidget);
0041 
0042     Application::setupDeclarativeBindings(m_view->engine());
0043 
0044     m_view->connect(m_view, &QQuickView::statusChanged, this, &MainWindow::onViewStatusChanged);
0045     m_view->rootContext()->setContextProperty(QStringLiteral("ktouch"), m_context);
0046     m_view->setResizeMode(QQuickView::SizeRootObjectToView);
0047     m_view->setSource(QUrl(QStringLiteral("qrc:/ktouch/qml/main.qml")));
0048 }
0049 
0050 void MainWindow::onViewStatusChanged(QQuickView::Status status)
0051 {
0052     if (status == QQuickView::Error)
0053     {
0054         QStringList errorMessages;
0055         foreach (auto error, m_view->errors())
0056         {
0057             errorMessages.append(error.toString());
0058         }
0059         QMessageBox qmlErrorMsgBox;
0060         qmlErrorMsgBox.setText(i18n("%1 has encountered a runtime error and has to be closed.", KAboutData::applicationData().displayName()));
0061         qmlErrorMsgBox.setDetailedText(errorMessages.join(QLatin1Char('\n')));
0062         qmlErrorMsgBox.setStandardButtons(QMessageBox::Close);
0063         qmlErrorMsgBox.setIcon(QMessageBox::Critical);
0064         qmlErrorMsgBox.exec();
0065         exit(1);
0066     }
0067 }