1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-11-09 12:32:40 +01:00

Move everything out of the main class for better unit tests

This commit is contained in:
Elias 2019-09-18 12:55:32 +02:00
parent cbc4243008
commit 1ad4366b02
4 changed files with 140 additions and 93 deletions

View File

@ -19,6 +19,7 @@ DISTFILES += \
RESOURCES += Resources.qrc RESOURCES += Resources.qrc
SOURCES += main.cpp \ SOURCES += main.cpp \
app.cpp \
src/createimportvideo.cpp \ src/createimportvideo.cpp \
src/installedlistmodel.cpp \ src/installedlistmodel.cpp \
src/monitorlistmodel.cpp \ src/monitorlistmodel.cpp \
@ -39,6 +40,7 @@ TRANSLATIONS = translations/ScreenPlay_en.ts \
translations/ScreenPlay_de.ts translations/ScreenPlay_de.ts
HEADERS += \ HEADERS += \
app.h \
src/globalvariables.h \ src/globalvariables.h \
src/createimportvideo.h \ src/createimportvideo.h \
src/installedlistmodel.h \ src/installedlistmodel.h \

65
ScreenPlay/app.cpp Normal file
View File

@ -0,0 +1,65 @@
#include "app.h"
App::App(int& argc, char** argv)
: QObject(nullptr)
, app { std::make_unique<QGuiApplication>(argc, argv) }
, mainWindowEngine{std::make_unique<QQmlApplicationEngine>()}
{
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication::setOrganizationName("ScreenPlay");
QGuiApplication::setOrganizationDomain("screen-play.app");
QGuiApplication::setApplicationName("ScreenPlay");
QGuiApplication::setApplicationVersion("0.3.0");
QGuiApplication::setQuitOnLastWindowClosed(false);
QGuiApplication::setWindowIcon(QIcon(":/assets/icons/favicon.ico"));
// Qt < 6.0 needs this init QtWebEngine
QtWebEngine::initialize();
auto globalVariables = make_shared<GlobalVariables>();
auto installedListModel = make_shared<InstalledListModel>(globalVariables);
auto installedListFilter = make_shared<InstalledListFilter>(installedListModel);
auto monitorListModel = make_shared<MonitorListModel>();
auto profileListModel = make_shared<ProfileListModel>(globalVariables);
auto sdkConnector = make_shared<SDKConnector>();
auto settings = make_shared<Settings>(globalVariables);
QObject::connect(settings.get(), &Settings::resetInstalledListmodel, installedListModel.get(), &InstalledListModel::reset);
Create create(globalVariables);
Util util {
mainWindowEngine->networkAccessManager()
};
ScreenPlayManager screenPlay {
globalVariables,
monitorListModel,
sdkConnector
};
// This needs to change in the future because setContextProperty gets depricated in Qt 6
mainWindowEngine->rootContext()->setContextProperty(QStringLiteral("globalVariables"), globalVariables.get());
mainWindowEngine->rootContext()->setContextProperty(QStringLiteral("screenPlay"), &screenPlay);
mainWindowEngine->rootContext()->setContextProperty(QStringLiteral("screenPlayCreate"), &create);
mainWindowEngine->rootContext()->setContextProperty(QStringLiteral("utility"), &util);
mainWindowEngine->rootContext()->setContextProperty(QStringLiteral("installedListFilter"), installedListFilter.get());
mainWindowEngine->rootContext()->setContextProperty(QStringLiteral("monitorListModel"), monitorListModel.get());
mainWindowEngine->rootContext()->setContextProperty(QStringLiteral("installedListModel"), installedListModel.get());
mainWindowEngine->rootContext()->setContextProperty(QStringLiteral("profileListModel"), profileListModel.get());
mainWindowEngine->rootContext()->setContextProperty(QStringLiteral("screenPlaySettings"), settings.get());
mainWindowEngine->load(QUrl(QStringLiteral("qrc:/main.qml")));
// Instead of setting "renderType: Text.NativeRendering" every time
// we can set it here once :)
QQuickWindow::setTextRenderType(QQuickWindow::TextRenderType::NativeTextRendering);
// Set visible if the -silent parameter was not set
QStringList argumentList = app->arguments();
if (!argumentList.contains("-silent")) {
settings->setMainWindowVisible(true);
}
installedListModel->init();
}
int App::run()
{
return app->exec();
}

70
ScreenPlay/app.h Normal file
View File

@ -0,0 +1,70 @@
#pragma once
#include <QDir>
#include <QGuiApplication>
#include <QIcon>
#include <QObject>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQmlEngine>
#include <QStringList>
#include <QUrl>
#include <QtWebEngine>
#include <memory>
#include "src/create.h"
#include "src/globalvariables.h"
#include "src/installedlistfilter.h"
#include "src/installedlistmodel.h"
#include "src/monitorlistmodel.h"
#include "src/profilelistmodel.h"
#include "src/screenplaymanager.h"
#include "src/sdkconnector.h"
#include "src/settings.h"
#include "src/util.h"
using std::make_shared,
std::shared_ptr,
ScreenPlay::Util,
ScreenPlay::InstalledListModel,
ScreenPlay::ScreenPlayManager,
ScreenPlay::InstalledListFilter,
ScreenPlay::MonitorListModel,
ScreenPlay::ProfileListModel,
ScreenPlay::SDKConnector,
ScreenPlay::Settings,
ScreenPlay::Create;
class App : public QObject {
Q_OBJECT
Q_PROPERTY(shared_ptr<GlobalVariables> globalVariables READ globalVariables WRITE setGlobalVariables NOTIFY globalVariablesChanged)
public:
explicit App(int &argc, char **argv);
shared_ptr<GlobalVariables> globalVariables() const
{
return m_globalVariables;
}
signals:
void globalVariablesChanged(shared_ptr<GlobalVariables> globalVariables);
public slots:
int run();
void setGlobalVariables(shared_ptr<GlobalVariables> globalVariables)
{
if (m_globalVariables == globalVariables)
return;
m_globalVariables = globalVariables;
emit globalVariablesChanged(m_globalVariables);
}
private:
std::unique_ptr<QGuiApplication> app;
std::unique_ptr<QQmlApplicationEngine> mainWindowEngine;
shared_ptr<GlobalVariables> m_globalVariables;
};

View File

@ -1,97 +1,7 @@
#include <QDir> #include "app.h"
#include <QGuiApplication>
#include <QIcon>
#include <QObject>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQmlEngine>
#include <QStringList>
#include <QUrl>
#include <QtWebEngine>
#include <memory>
#include "src/create.h"
#include "src/globalvariables.h"
#include "src/installedlistfilter.h"
#include "src/installedlistmodel.h"
#include "src/monitorlistmodel.h"
#include "src/profilelistmodel.h"
#include "src/screenplaymanager.h"
#include "src/sdkconnector.h"
#include "src/settings.h"
#include "src/util.h"
using std::make_shared,
ScreenPlay::Util,
ScreenPlay::InstalledListModel,
ScreenPlay::ScreenPlayManager,
ScreenPlay::InstalledListFilter,
ScreenPlay::MonitorListModel,
ScreenPlay::ProfileListModel,
ScreenPlay::SDKConnector,
ScreenPlay::Settings,
ScreenPlay::Create;
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
App app(argc,argv);
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); return app.run();
QGuiApplication::setOrganizationName("ScreenPlay");
QGuiApplication::setOrganizationDomain("screen-play.app");
QGuiApplication::setApplicationName("ScreenPlay");
QGuiApplication::setApplicationVersion("0.3.0");
QGuiApplication app(argc, argv);
QGuiApplication::setQuitOnLastWindowClosed(false);
QGuiApplication::setWindowIcon(QIcon(":/assets/icons/favicon.ico"));
// Qt < 6.0 needs this init QtWebEngine
QtWebEngine::initialize();
QQmlApplicationEngine mainWindowEngine;
auto globalVariables = make_shared<GlobalVariables>();
auto installedListModel = make_shared<InstalledListModel>(globalVariables);
auto installedListFilter = make_shared<InstalledListFilter>(installedListModel);
auto monitorListModel = make_shared<MonitorListModel>();
auto profileListModel = make_shared<ProfileListModel>(globalVariables);
auto sdkConnector = make_shared<SDKConnector>();
auto settings = make_shared<Settings>(globalVariables);
QObject::connect(settings.get(), &Settings::resetInstalledListmodel, installedListModel.get(), &InstalledListModel::reset);
Create create(globalVariables);
Util util {
mainWindowEngine.networkAccessManager()
};
ScreenPlayManager screenPlay {
globalVariables,
monitorListModel,
sdkConnector
};
// This needs to change in the future because setContextProperty gets depricated in Qt 6
mainWindowEngine.rootContext()->setContextProperty(QStringLiteral("globalVariables"), globalVariables.get());
mainWindowEngine.rootContext()->setContextProperty(QStringLiteral("screenPlay"), &screenPlay);
mainWindowEngine.rootContext()->setContextProperty(QStringLiteral("screenPlayCreate"), &create);
mainWindowEngine.rootContext()->setContextProperty(QStringLiteral("utility"), &util);
mainWindowEngine.rootContext()->setContextProperty(QStringLiteral("installedListFilter"), installedListFilter.get());
mainWindowEngine.rootContext()->setContextProperty(QStringLiteral("monitorListModel"), monitorListModel.get());
mainWindowEngine.rootContext()->setContextProperty(QStringLiteral("installedListModel"), installedListModel.get());
mainWindowEngine.rootContext()->setContextProperty(QStringLiteral("profileListModel"), profileListModel.get());
mainWindowEngine.rootContext()->setContextProperty(QStringLiteral("screenPlaySettings"), settings.get());
mainWindowEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
// Instead of setting "renderType: Text.NativeRendering" every time
// we can set it here once :)
QQuickWindow::setTextRenderType(QQuickWindow::TextRenderType::NativeTextRendering);
// Set visible if the -silent parameter was not set
QStringList argumentList = app.arguments();
if (!argumentList.contains("-silent")) {
settings->setMainWindowVisible(true);
}
installedListModel->init();
return app.exec();
} }