Commit e4a8bd3f authored by Leander Schulten's avatar Leander Schulten

UX: Settings: You can now 'savs as' or 'load from' a setting file. If you load...

UX: Settings: You can now 'savs as' or 'load from' a setting file. If you load a settings file, the Lichtsteuerung can automatically restart to load the new settings. Close #53
parent e70e0211
Pipeline #192345 passed with stage
in 5 minutes and 1 second
......@@ -63,11 +63,36 @@
int main(int argc, char *argv[]) {
QSharedMemory mem(QStringLiteral("Lichteuerung Leander Schulten"));
if (!mem.create(1)) {
{ // check if the app is alreandy running or should be restarted
using namespace std::string_view_literals;
qDebug() << argv[0];
qDebug() << (argv[1] == "restart"sv);
const bool restart = argc > 1 && "restart"sv == argv[1];
qDebug() << restart;
if (restart) {
if (mem.attach()) {
// the app is still running, wait for end of app
auto running = static_cast<bool *>(mem.data());
while (*running) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
} else {
// no other app is running
if (!mem.create(1)) {
// something was not successfull
return 1;
}
}
} else {
if (!mem.create(1)) {
#ifdef Q_OS_WIN
MessageBoxA(nullptr, "The Lichtsteuerung is already running on this computer.", nullptr, MB_OK);
MessageBoxA(nullptr, "The Lichtsteuerung is already running on this computer.", nullptr, MB_OK);
#endif
return 0;
return 0;
}
}
// signal that we are running
*static_cast<bool *>(mem.data()) = true;
}
error::initErrorHandler();
#ifdef DrMinGW
......@@ -262,26 +287,31 @@ int main(int argc, char *argv[]) {
modolePropertyTypeList.append(_metaEnumP.key(i));
}*/
CatchingErrorApplication::connect(&app, &QGuiApplication::lastWindowClosed, [&]() {
CatchingErrorApplication::connect(&app, &QGuiApplication::aboutToQuit, [&]() {
Modules::ModuleManager::singletone()->controller().stop();
Audio::AudioCaptureManager::get().stopCapturingAndWait();
QFile savePath(settings.getJsonSettingsFilePath());
QFile savePath(settings.getJsonSettingsFileSavePath());
ApplicationData::saveData(savePath);
Driver::stopAndUnloadDriver();
updater.runUpdateInstaller();
});
Settings::connect(&settings,&Settings::driverFilePathChanged,[&](){
Driver::loadAndStartDriver(settings.getDriverFilePath());
if (updater.getState() == Updater::ReadyToInstall) {
updater.runUpdateInstaller();
} else if (settings.shouldLoadFromSettingsPath()) {
QProcess::startDetached(QCoreApplication::applicationFilePath(), QStringList() << QStringLiteral("restart"));
}
*static_cast<bool *>(mem.data()) = false;
});
Settings::connect(&settings,&Settings::updatePauseInMsChanged,[&](){
if(Driver::getCurrentDriver()){
Settings::connect(&settings, &Settings::driverFilePathChanged, [&]() { Driver::loadAndStartDriver(settings.getDriverFilePath()); });
Settings::connect(&settings, &Settings::updatePauseInMsChanged, [&]() {
if (Driver::getCurrentDriver()) {
Driver::getCurrentDriver()->setWaitTime(std::chrono::milliseconds(settings.getUpdatePauseInMs()));
}
});
Modules::ModuleManager::singletone()->loadAllModulesInDir(settings.getModuleDirPath());
Settings::connect(&settings,&Settings::moduleDirPathChanged,[&](){
Modules::ModuleManager::singletone()->loadAllModulesInDir(settings.getModuleDirPath());
Settings::connect(&settings, &Settings::saveAs, [&](const auto &path) {
QFile saveFile(settings.getJsonSettingsFileSavePath());
ApplicationData::saveData(saveFile);
});
Modules::ModuleManager::singletone()->loadAllModulesInDir(settings.getModuleDirPath());
Settings::connect(&settings, &Settings::moduleDirPathChanged, [&]() { Modules::ModuleManager::singletone()->loadAllModulesInDir(settings.getModuleDirPath()); });
ModelManager::get().setSettings(&settings);
engine.rootContext()->setContextProperty(QStringLiteral("ModelManager"),&ModelManager::get());
......
......@@ -2,7 +2,7 @@ import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Material 2.12
import QtQuick.Layouts 1.12
import QtQuick.Dialogs 1.3
import QtQuick.Dialogs 1.3 as SystemDialog
import QtQuick.Window 2.12
import "components"
......@@ -15,12 +15,51 @@ Pane{
Label{
text: "Settings file path:"
}
TextFieldFileChooser{
Layout.fillWidth: true
folder: false
path: Settings.jsonSettingsFilePath
onPathChanged: {Settings.jsonSettingsFilePath = path;path = Settings.jsonSettingsFilePath;}
fileChooser: fileDialog
RowLayout{
id: root
Item{
Layout.fillWidth: true
Layout.preferredWidth: inputSettingsPath.implicitWidth
Layout.preferredHeight: inputSettingsPath.implicitHeight
Layout.topMargin: 4
clip: true
id: wrapper
TextInputField{
id: inputSettingsPath
readOnly: true
anchors.fill: parent
anchors.bottomMargin: 2
text: Settings.jsonSettingsFilePath
}
}
Button{
Layout.minimumWidth: implicitWidth
Layout.leftMargin: 5
Layout.preferredHeight: implicitHeight - 15
text: "Save as"
onClicked: {
fileDialog.openAt(Settings.jsonSettingsFilePath, false, false);
fileDialog.callback = function(file){
Settings.setJsonSettingsFilePath(file, false);
};
}
}
Button{
Layout.minimumWidth: implicitWidth
Layout.leftMargin: 5
Layout.preferredHeight: implicitHeight - 15
text: "Load from"
onClicked: {
fileDialog.openAt(Settings.jsonSettingsFilePath, false);
fileDialog.callback = function(file){
if(Settings.setJsonSettingsFilePath(file, true)){
popupChangedSettingsFile.visible = true;
}else{
ErrorNotifier.errorMessage += "You can not load the current opened settings file.";
}
};
}
}
}
Label{
......@@ -123,11 +162,14 @@ Pane{
onClicked: modifyThemeWindow.show()
}
}
FileDialog{
SystemDialog.FileDialog{
property var callback;
function openAt(path, isFolder){
selectExisting: false
defaultSuffix: ".json"
function openAt(path, isFolder, selectExisting_ = true){
selectFolder = isFolder;
folder = pathToUrl(path);
selectExisting = selectExisting_;
open();
}
id: fileDialog
......@@ -153,4 +195,16 @@ Pane{
anchors.fill: parent
}
}
Dialog{
id: popupChangedSettingsFile
modal: true
margins: 50
x: (parent.width - width) / 2
y: 0
closePolicy: Popup.NoAutoClose
title: "Do you want to restart the light control now to load the settings file?"
standardButtons: Dialog.No | Dialog.Yes
onAccepted: Qt.quit();
}
}
......@@ -10,6 +10,7 @@ Settings::Settings(QObject *parent) : QObject(parent), settings(QStringLiteral("
Modules::Compiler::compilerFlags = value(QStringLiteral("compilerFlags"),Modules::Compiler::compilerFlags).toString();
Modules::Compiler::compilerLibraryFlags = value(QStringLiteral("compilerLibraryFlags"),Modules::Compiler::compilerLibraryFlags).toString();
Modules::Compiler::includePath = value(QStringLiteral("includePath"),Modules::Compiler::includePath).toString();
jsonSettingsFileSavePath = getJsonSettingsFilePath();
}
void Settings::setValue(const QString &key, const QVariant &value){
......
......@@ -47,7 +47,9 @@ class Settings : public QObject {
Q_OBJECT
QSettings settings;
std::optional<QSettings> localSettings;
Q_PROPERTY(QString jsonSettingsFilePath READ getJsonSettingsFilePath WRITE setJsonSettingsFilePath NOTIFY jsonSettingsFilePathChanged)
QString jsonSettingsFileSavePath;
bool loadFromJsonSettingsFilePath = false;
Q_PROPERTY(QString jsonSettingsFilePath READ getJsonSettingsFilePath NOTIFY jsonSettingsFilePathChanged)
Q_PROPERTY(QString driverFilePath READ getDriverFilePath WRITE setDriverFilePath NOTIFY driverFilePathChanged)
Q_PROPERTY(QString moduleDirPath READ getModuleDirPath WRITE setModuleDirPath NOTIFY moduleDirPathChanged)
Q_PROPERTY(QString compilerPath READ getCompilerPath WRITE setCompilerPath NOTIFY compilerPathChanged)
......@@ -70,16 +72,43 @@ protected:
QVariant value(const QString &key, const QVariant &defaultValue = QVariant())const;
public:
explicit Settings(QObject *parent = nullptr);
void setJsonSettingsFilePath(const QString& file){
Q_INVOKABLE bool setJsonSettingsFilePath(const QString &file, bool loadFromPath) {
if(file == getJsonSettingsFilePath()) {
return;
return false;
}
if(QFile::exists(file)){
setValue(QStringLiteral("jsonSettingsFilePath"),file);
if (loadFromPath) {
if (!QFile::exists(file)) {
return false;
}
// it was load/save path, but we dident loaded and now have a new load path
this->loadFromJsonSettingsFilePath = true;
setValue(QStringLiteral("jsonSettingsFilePath"), file);
emit jsonSettingsFilePathChanged();
} else {
// it was load or save path, but we dident loaded and now want to save
this->loadFromJsonSettingsFilePath = false;
this->jsonSettingsFileSavePath = file;
setValue(QStringLiteral("jsonSettingsFilePath"), file);
emit jsonSettingsFilePathChanged();
emit saveAs(file);
}
return true;
}
[[nodiscard]] QString getJsonSettingsFilePath() const { return value(QStringLiteral("jsonSettingsFilePath")).toString(); }
/**
* @brief getJsonSettingsFileSavePath returns the path, to which the settings file should be written
* @return the path where to save the settings file
*/
[[nodiscard]] QString getJsonSettingsFileSavePath() const { return jsonSettingsFileSavePath; }
/**
* @brief shouldLoadFromSettingsPath indicates, if the settings should be loaded and not saved to the json settings path
* @return true if the settings should be loaded from the path, false if the settings should be saved to the path
*/
[[nodiscard]] bool shouldLoadFromSettingsPath() const {
return loadFromJsonSettingsFilePath;
}
QString getJsonSettingsFilePath()const{return value(QStringLiteral("jsonSettingsFilePath")).toString();}
void setDriverFilePath(const QString& file){
if(file == getDriverFilePath()) {
......@@ -185,6 +214,7 @@ signals:
void compilerLibraryFlagsChanged();
void includePathChanged();
void themeChanged();
void saveAs(QString path);
public slots:
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment