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#include <QLabel>
19
20#include <KLocalizedString>
21
22ProfileScriptDialog::ProfileScriptDialog(const QStringList &drivers, const QByteArray &settings,
23 QWidget *parent) : QDialog(parent)
24{
25 setWindowTitle(i18n("Profile Scripts Editor"));
26
27 m_DriversList = drivers;
28 m_MainLayout = new QVBoxLayout(this);
29
30 QHBoxLayout *labelLayout = new QHBoxLayout;
31 m_DriverLabel = new QLabel(i18n("Driver"), this);
32 m_PreLabel = new QLabel(i18n("Pre start"), this);
33 m_PostLabel = new QLabel(i18n("Post start"), this);
34 m_PreStopLabel = new QLabel(i18n("Pre stop"), this);
35 m_PostStopLabel = new QLabel(i18n("Post stop"), this);
36
37 labelLayout->addWidget(m_DriverLabel);
38 labelLayout->addWidget(m_PreLabel);
39 labelLayout->addWidget(m_PostLabel);
40 labelLayout->addWidget(m_PreStopLabel);
41 labelLayout->addWidget(m_PostStopLabel);
42 m_MainLayout->insertLayout(m_MainLayout->count() - 1, labelLayout);
43
44 m_ButtonBox = new QDialogButtonBox(Qt::Horizontal, this);
45 m_ButtonBox->addButton(QDialogButtonBox::Save);
46 connect(m_ButtonBox, &QDialogButtonBox::accepted, this, &ProfileScriptDialog::generateSettings);
48
49 m_AddRuleB = new QPushButton(i18n("Add Rule"), this);
50 m_AddRuleB->setIcon(QIcon::fromTheme("list-add"));
51 connect(m_AddRuleB, &QPushButton::clicked, this, &ProfileScriptDialog::addRule);
52
53 QHBoxLayout *bottomLayout = new QHBoxLayout;
54 bottomLayout->addWidget(m_AddRuleB);
55 bottomLayout->addStretch();
56 bottomLayout->addWidget(m_ButtonBox);
57
58 m_MainLayout->addItem(bottomLayout);
59 m_MainLayout->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding));
60
61 if (!settings.isEmpty())
62 parseSettings(settings);
63}
64
65void ProfileScriptDialog::addRule()
66{
67 if (m_ProfileScriptWidgets.count() == m_DriversList.count())
68 return;
69
70 QStringList remainingDrivers = m_DriversList;
71 for (auto &oneRule : m_ProfileScriptWidgets)
72 remainingDrivers.removeOne(oneRule->property("Driver").toString());
73
74 ProfileScript *newItem = new ProfileScript(this);
75 connect(newItem, &ProfileScript::removedRequested, this, &ProfileScriptDialog::removeRule);
76 newItem->setDriverList(remainingDrivers);
77 m_ProfileScriptWidgets.append(newItem);
78 m_MainLayout->insertWidget(m_MainLayout->count() - 2, newItem);
79 adjustSize();
80}
81
82void ProfileScriptDialog::addJSONRule(QJsonObject settings)
83{
84 if (m_ProfileScriptWidgets.count() == m_DriversList.count())
85 return;
86
87 ProfileScript *newItem = new ProfileScript(this);
88 connect(newItem, &ProfileScript::removedRequested, this, &ProfileScriptDialog::removeRule);
89 newItem->setProperty("PreDelay", settings["PreDelay"].toInt());
90 newItem->setProperty("PreScript", settings["PreScript"].toString());
91 newItem->setProperty("Driver", settings["Driver"].toString());
92 newItem->setProperty("PostDelay", settings["PostDelay"].toInt());
93 newItem->setProperty("PostScript", settings["PostScript"].toString());
94 newItem->setProperty("StoppingDelay", settings["StoppingDelay"].toInt());
95 newItem->setProperty("StoppingScript", settings["StoppingScript"].toString());
96 newItem->setProperty("StoppedDelay", settings["StoppedDelay"].toInt());
97 newItem->setProperty("StoppedScript", settings["StoppedScript"].toString());
98 newItem->setDriverList(m_DriversList);
99
100 // Make sure GUI reflects all property changes.
101 newItem->syncGUI();
102 m_ProfileScriptWidgets.append(newItem);
103 m_MainLayout->insertWidget(m_MainLayout->count() - 2, newItem);
104
105 adjustSize();
106}
107
108void ProfileScriptDialog::removeRule()
109{
110 auto toRemove = sender();
111 auto result = std::find_if(m_ProfileScriptWidgets.begin(), m_ProfileScriptWidgets.end(), [toRemove](const auto & oneRule)
112 {
113 return oneRule == toRemove;
114 });
115 if (result != m_ProfileScriptWidgets.end())
116 {
117 m_MainLayout->removeWidget(*result);
118 (*result)->deleteLater();
119 m_ProfileScriptWidgets.removeOne(*result);
120 adjustSize();
121 }
122}
123
124void ProfileScriptDialog::parseSettings(const QByteArray &settings)
125{
126 QJsonParseError jsonError;
127 QJsonDocument doc = QJsonDocument::fromJson(settings, &jsonError);
128
129 if (jsonError.error != QJsonParseError::NoError)
130 return;
131
132 m_ProfileScripts = doc.array();
133
134 for (const auto &oneRule : qAsConst(m_ProfileScripts))
135 addJSONRule(oneRule.toObject());
136}
137
138void ProfileScriptDialog::generateSettings()
139{
140 m_ProfileScripts = QJsonArray();
141
142 for (auto &oneRule : m_ProfileScriptWidgets)
143 {
144 m_ProfileScripts.append(oneRule->toJSON());
145 }
146
147 close();
148}
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)
virtual void reject()
int result() const const
QIcon fromTheme(const QString &name)
QJsonArray array() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
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-2025 The KDE developers.
Generated on Fri Apr 25 2025 11:58:36 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.