7#include "profilescriptdialog.h"
8#include "profilescript.h"
16#include <QDialogButtonBox>
17#include <QJsonDocument>
20#include <KLocalizedString>
25 setWindowTitle(
i18n(
"Profile Scripts Editor"));
27 m_DriversList = drivers;
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);
42 m_MainLayout->insertLayout(m_MainLayout->count() - 1, labelLayout);
58 m_MainLayout->addItem(bottomLayout);
61 if (!settings.isEmpty())
62 parseSettings(settings);
65void ProfileScriptDialog::addRule()
67 if (m_ProfileScriptWidgets.count() == m_DriversList.count())
70 QStringList remainingDrivers = m_DriversList;
71 for (
auto &oneRule : m_ProfileScriptWidgets)
72 remainingDrivers.
removeOne(oneRule->property(
"Driver").toString());
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);
82void ProfileScriptDialog::addJSONRule(
QJsonObject settings)
84 if (m_ProfileScriptWidgets.count() == m_DriversList.count())
87 ProfileScript *newItem =
new ProfileScript(
this);
88 connect(newItem, &ProfileScript::removedRequested,
this, &ProfileScriptDialog::removeRule);
89 newItem->
setProperty(
"PreDelay", settings[
"PreDelay"].toInt());
92 newItem->
setProperty(
"PostDelay", settings[
"PostDelay"].toInt());
94 newItem->
setProperty(
"StoppingDelay", settings[
"StoppingDelay"].toInt());
96 newItem->
setProperty(
"StoppedDelay", settings[
"StoppedDelay"].toInt());
98 newItem->setDriverList(m_DriversList);
102 m_ProfileScriptWidgets.append(newItem);
103 m_MainLayout->insertWidget(m_MainLayout->count() - 2, newItem);
108void ProfileScriptDialog::removeRule()
111 auto result = std::find_if(m_ProfileScriptWidgets.begin(), m_ProfileScriptWidgets.end(), [toRemove](
const auto & oneRule)
113 return oneRule == toRemove;
115 if (
result != m_ProfileScriptWidgets.end())
117 m_MainLayout->removeWidget(*
result);
118 (*result)->deleteLater();
119 m_ProfileScriptWidgets.removeOne(*
result);
124void ProfileScriptDialog::parseSettings(
const QByteArray &settings)
126 QJsonParseError jsonError;
132 m_ProfileScripts = doc.
array();
134 for (
const auto &oneRule : qAsConst(m_ProfileScripts))
135 addJSONRule(oneRule.toObject());
138void ProfileScriptDialog::generateSettings()
140 m_ProfileScripts = QJsonArray();
142 for (
auto &oneRule : m_ProfileScriptWidgets)
144 m_ProfileScripts.append(oneRule->toJSON());
QString i18n(const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
void addStretch(int stretch)
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)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)