Kstars

profilescriptdialog.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Jasem Mutlaq <mutlaqja@ikarustech.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "profilescriptdialog.h"
8#include "profilescript.h"
9
10#include <QHBoxLayout>
11#include <QComboBox>
12#include <QPushButton>
13#include <QLineEdit>
14#include <QSpinBox>
15#include <QFileDialog>
16#include <QDialogButtonBox>
17#include <QJsonDocument>
18
19#include <KLocalizedString>
20
21ProfileScriptDialog::ProfileScriptDialog(const QStringList &drivers, const QByteArray &settings,
22 QWidget *parent) : QDialog(parent)
23{
24 setWindowTitle(i18n("Profile Scripts Editor"));
25
26 m_DriversList = drivers;
27
28 m_MainLayout = new QVBoxLayout(this);
29 m_ButtonBox = new QDialogButtonBox(Qt::Horizontal, this);
30 m_ButtonBox->addButton(QDialogButtonBox::Save);
31 connect(m_ButtonBox, &QDialogButtonBox::accepted, this, &ProfileScriptDialog::generateSettings);
33
34 m_AddRuleB = new QPushButton(i18n("Add Rule"), this);
35 m_AddRuleB->setIcon(QIcon::fromTheme("list-add"));
36 connect(m_AddRuleB, &QPushButton::clicked, this, &ProfileScriptDialog::addRule);
37
38 QHBoxLayout *bottomLayout = new QHBoxLayout;
39 bottomLayout->addWidget(m_AddRuleB);
40 bottomLayout->addStretch();
41 bottomLayout->addWidget(m_ButtonBox);
42
43 m_MainLayout->addItem(bottomLayout);
44 m_MainLayout->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding));
45
46 if (!settings.isEmpty())
47 parseSettings(settings);
48}
49
50void ProfileScriptDialog::addRule()
51{
52 if (m_ProfileScriptWidgets.count() == m_DriversList.count())
53 return;
54
55 QStringList remainingDrivers = m_DriversList;
56 for (auto &oneRule : m_ProfileScriptWidgets)
57 remainingDrivers.removeOne(oneRule->property("Driver").toString());
58
59 ProfileScript *newItem = new ProfileScript(this);
60 connect(newItem, &ProfileScript::removedRequested, this, &ProfileScriptDialog::removeRule);
61 newItem->setDriverList(remainingDrivers);
62 m_ProfileScriptWidgets.append(newItem);
63 m_MainLayout->insertWidget(m_ProfileScriptWidgets.count() - 1, newItem);
64 adjustSize();
65}
66
67void ProfileScriptDialog::addJSONRule(QJsonObject settings)
68{
69 if (m_ProfileScriptWidgets.count() == m_DriversList.count())
70 return;
71
72 ProfileScript *newItem = new ProfileScript(this);
73 connect(newItem, &ProfileScript::removedRequested, this, &ProfileScriptDialog::removeRule);
74 newItem->setProperty("PreDelay", settings["PreDelay"].toInt());
75 newItem->setProperty("PreScript", settings["PreScript"].toString());
76 newItem->setProperty("Driver", settings["Driver"].toString());
77 newItem->setProperty("PostDelay", settings["PostDelay"].toInt());
78 newItem->setProperty("PostScript", settings["PostScript"].toString());
79 newItem->setDriverList(m_DriversList);
80
81 // Make sure GUI reflects all property changes.
82 newItem->syncGUI();
83
84 m_ProfileScriptWidgets.append(newItem);
85 m_MainLayout->insertWidget(m_ProfileScriptWidgets.count() - 1, newItem);
86 adjustSize();
87}
88
89void ProfileScriptDialog::removeRule()
90{
91 auto toRemove = sender();
92 auto result = std::find_if(m_ProfileScriptWidgets.begin(), m_ProfileScriptWidgets.end(), [toRemove](const auto & oneRule)
93 {
94 return oneRule == toRemove;
95 });
96 if (result != m_ProfileScriptWidgets.end())
97 {
98 m_MainLayout->removeWidget(*result);
99 (*result)->deleteLater();
100 m_ProfileScriptWidgets.removeOne(*result);
101 adjustSize();
102 }
103}
104
105void ProfileScriptDialog::parseSettings(const QByteArray &settings)
106{
107 QJsonParseError jsonError;
108 QJsonDocument doc = QJsonDocument::fromJson(settings, &jsonError);
109
110 if (jsonError.error != QJsonParseError::NoError)
111 return;
112
113 m_ProfileScripts = doc.array();
114
115 for (const auto &oneRule : qAsConst(m_ProfileScripts))
116 addJSONRule(oneRule.toObject());
117}
118
119void ProfileScriptDialog::generateSettings()
120{
121 m_ProfileScripts = QJsonArray();
122
123 for (auto &oneRule : m_ProfileScriptWidgets)
124 {
125 m_ProfileScripts.append(oneRule->toJSON());
126 }
127
128 close();
129}
QString i18n(const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
void clicked(bool checked)
void addStretch(int stretch)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void insertWidget(int index, QWidget *widget, int stretch, Qt::Alignment alignment)
bool isEmpty() const const
virtual void reject()
int result() const const
QIcon fromTheme(const QString &name)
void append(const QJsonValue &value)
QJsonArray array() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
void removeWidget(QWidget *widget)
qsizetype count() const const
bool removeOne(const AT &t)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * sender() const const
bool setProperty(const char *name, QVariant &&value)
Horizontal
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void adjustSize()
bool close()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 4 2024 12:01:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.