File indexing completed on 2024-04-28 07:31:17
0001 /* 0002 SPDX-FileCopyrightText: 2022 Jasem Mutlaq <mutlaqja@ikarustech.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "profilescriptdialog.h" 0008 #include "profilescript.h" 0009 0010 #include <QHBoxLayout> 0011 #include <QComboBox> 0012 #include <QPushButton> 0013 #include <QLineEdit> 0014 #include <QSpinBox> 0015 #include <QFileDialog> 0016 #include <QDialogButtonBox> 0017 #include <QJsonDocument> 0018 0019 #include <KLocalizedString> 0020 0021 ProfileScriptDialog::ProfileScriptDialog(const QStringList &drivers, const QByteArray &settings, 0022 QWidget *parent) : QDialog(parent) 0023 { 0024 setWindowTitle(i18n("Profile Scripts Editor")); 0025 0026 m_DriversList = drivers; 0027 0028 m_MainLayout = new QVBoxLayout(this); 0029 m_ButtonBox = new QDialogButtonBox(Qt::Horizontal, this); 0030 m_ButtonBox->addButton(QDialogButtonBox::Save); 0031 connect(m_ButtonBox, &QDialogButtonBox::accepted, this, &ProfileScriptDialog::generateSettings); 0032 connect(m_ButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0033 0034 m_AddRuleB = new QPushButton(i18n("Add Rule"), this); 0035 m_AddRuleB->setIcon(QIcon::fromTheme("list-add")); 0036 connect(m_AddRuleB, &QPushButton::clicked, this, &ProfileScriptDialog::addRule); 0037 0038 QHBoxLayout *bottomLayout = new QHBoxLayout; 0039 bottomLayout->addWidget(m_AddRuleB); 0040 bottomLayout->addStretch(); 0041 bottomLayout->addWidget(m_ButtonBox); 0042 0043 m_MainLayout->addItem(bottomLayout); 0044 m_MainLayout->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding)); 0045 0046 if (!settings.isEmpty()) 0047 parseSettings(settings); 0048 } 0049 0050 void ProfileScriptDialog::addRule() 0051 { 0052 if (m_ProfileScriptWidgets.count() == m_DriversList.count()) 0053 return; 0054 0055 QStringList remainingDrivers = m_DriversList; 0056 for (auto &oneRule : m_ProfileScriptWidgets) 0057 remainingDrivers.removeOne(oneRule->property("Driver").toString()); 0058 0059 ProfileScript *newItem = new ProfileScript(this); 0060 connect(newItem, &ProfileScript::removedRequested, this, &ProfileScriptDialog::removeRule); 0061 newItem->setDriverList(remainingDrivers); 0062 m_ProfileScriptWidgets.append(newItem); 0063 m_MainLayout->insertWidget(m_ProfileScriptWidgets.count() - 1, newItem); 0064 adjustSize(); 0065 } 0066 0067 void ProfileScriptDialog::addJSONRule(QJsonObject settings) 0068 { 0069 if (m_ProfileScriptWidgets.count() == m_DriversList.count()) 0070 return; 0071 0072 ProfileScript *newItem = new ProfileScript(this); 0073 connect(newItem, &ProfileScript::removedRequested, this, &ProfileScriptDialog::removeRule); 0074 newItem->setProperty("PreDelay", settings["PreDelay"].toInt()); 0075 newItem->setProperty("PreScript", settings["PreScript"].toString()); 0076 newItem->setProperty("Driver", settings["Driver"].toString()); 0077 newItem->setProperty("PostDelay", settings["PostDelay"].toInt()); 0078 newItem->setProperty("PostScript", settings["PostScript"].toString()); 0079 newItem->setDriverList(m_DriversList); 0080 0081 // Make sure GUI reflects all property changes. 0082 newItem->syncGUI(); 0083 0084 m_ProfileScriptWidgets.append(newItem); 0085 m_MainLayout->insertWidget(m_ProfileScriptWidgets.count() - 1, newItem); 0086 adjustSize(); 0087 } 0088 0089 void ProfileScriptDialog::removeRule() 0090 { 0091 auto toRemove = sender(); 0092 auto result = std::find_if(m_ProfileScriptWidgets.begin(), m_ProfileScriptWidgets.end(), [toRemove](const auto & oneRule) 0093 { 0094 return oneRule == toRemove; 0095 }); 0096 if (result != m_ProfileScriptWidgets.end()) 0097 { 0098 m_MainLayout->removeWidget(*result); 0099 (*result)->deleteLater(); 0100 m_ProfileScriptWidgets.removeOne(*result); 0101 adjustSize(); 0102 } 0103 } 0104 0105 void ProfileScriptDialog::parseSettings(const QByteArray &settings) 0106 { 0107 QJsonParseError jsonError; 0108 QJsonDocument doc = QJsonDocument::fromJson(settings, &jsonError); 0109 0110 if (jsonError.error != QJsonParseError::NoError) 0111 return; 0112 0113 m_ProfileScripts = doc.array(); 0114 0115 for (const auto &oneRule : qAsConst(m_ProfileScripts)) 0116 addJSONRule(oneRule.toObject()); 0117 } 0118 0119 void ProfileScriptDialog::generateSettings() 0120 { 0121 m_ProfileScripts = QJsonArray(); 0122 0123 for (auto &oneRule : m_ProfileScriptWidgets) 0124 { 0125 m_ProfileScripts.append(oneRule->toJSON()); 0126 } 0127 0128 close(); 0129 }