Kstars

profilescript.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 "profilescript.h"
8
9#include <QHBoxLayout>
10#include <QComboBox>
11#include <QPushButton>
12#include <QLineEdit>
13#include <QSpinBox>
14#include <QFileDialog>
15
16#include <KLocalizedString>
17
18ProfileScript::ProfileScript(QWidget *parent) : QWidget(parent)
19{
20 QHBoxLayout *mainLayout = new QHBoxLayout(this);
21
22 m_DriverCombo = new QComboBox(this);
23 m_DriverCombo->setEditable(true);
24#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
25 connect(m_DriverCombo, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated),
26 this, [this](const QString & value)
27 {
28 m_Driver = value;
29 });
30#else
31 connect(m_DriverCombo, &QComboBox::textActivated, this, [this](const QString & value)
32 {
33 m_Driver = value;
34 });
35#endif
36
37 m_PreDelaySpin = new QSpinBox(this);
38 m_PreDelaySpin->setToolTip(i18n("Delay this many seconds before executing pre driver script."));
39 m_PreDelaySpin->setRange(0, 300);
40 m_PreDelaySpin->setSingleStep(5);
41 connect(m_PreDelaySpin, &QSpinBox::editingFinished, this, [this]()
42 {
43 m_PreDelay = m_PreDelaySpin->value();
44 });
45
46 m_PostDelaySpin = new QSpinBox(this);
47 m_PostDelaySpin->setToolTip(i18n("Delay this many seconds after driver startup before executing post driver script."));
48 m_PostDelaySpin->setRange(0, 300);
49 m_PostDelaySpin->setSingleStep(5);
50 connect(m_PostDelaySpin, &QSpinBox::editingFinished, this, [this]()
51 {
52 m_PostDelay = m_PostDelaySpin->value();
53 });
54
55 m_PreScriptEdit = new QLineEdit(this);
56 m_PreScriptEdit->setMinimumWidth(100);
57 m_PreScriptEdit->setToolTip(i18n("Select script to execute before starting the driver"));
58 m_PreScriptEdit->setClearButtonEnabled(true);
59 connect(m_PreScriptEdit, &QLineEdit::editingFinished, this, [this]()
60 {
61 m_PreScript = m_PreScriptEdit->text();
62 });
63
64 m_PostScriptEdit = new QLineEdit(this);
65 m_PostScriptEdit->setMinimumWidth(100);
66 m_PostScriptEdit->setToolTip(i18n("Select script to execute after starting the driver"));
67 m_PostScriptEdit->setClearButtonEnabled(true);
68 connect(m_PostScriptEdit, &QLineEdit::editingFinished, this, [this]()
69 {
70 m_PostScript = m_PostScriptEdit->text();
71 });
72
73 m_PreScriptB = new QPushButton(this);
74 m_PreScriptB->setIcon(QIcon::fromTheme("document-open"));
75 connect(m_PreScriptB, &QPushButton::clicked, this, &ProfileScript::selectPreScript);
76
77 m_PostScriptB = new QPushButton(this);
78 m_PostScriptB->setIcon(QIcon::fromTheme("document-open"));
79 connect(m_PostScriptB, &QPushButton::clicked, this, &ProfileScript::selectPostScript);
80
81 m_RemoveB = new QPushButton(this);
82 m_RemoveB->setIcon(QIcon::fromTheme("list-remove"));
83 connect(m_RemoveB, &QPushButton::clicked, this, &ProfileScript::removedRequested);
84
85 // Add widgets to layout
86 mainLayout->addWidget(m_PreDelaySpin);
87 mainLayout->addWidget(m_PreScriptEdit);
88 mainLayout->addWidget(m_PreScriptB);
89 mainLayout->addWidget(m_DriverCombo);
90 mainLayout->addWidget(m_PostDelaySpin);
91 mainLayout->addWidget(m_PostScriptEdit);
92 mainLayout->addWidget(m_PostScriptB);
93 mainLayout->addWidget(m_RemoveB);
94}
95
96void ProfileScript::syncGUI()
97{
98 m_PreDelaySpin->setValue(m_PreDelay);
99 m_PreScriptEdit->setText(m_PreScript);
100 m_DriverCombo->setCurrentText(m_Driver);
101 m_PostDelaySpin->setValue(m_PostDelay);
102 m_PostScriptEdit->setText(m_PostScript);
103}
104
105void ProfileScript::setDriverList(const QStringList &value)
106{
107 m_DriverCombo->clear();
108 m_DriverCombo->addItems(value);
109 if (m_Driver.isEmpty())
110 m_Driver = value.first();
111}
112
113void ProfileScript::selectPreScript()
114{
115 m_PreScript = QFileDialog::getOpenFileName(this, i18nc("@title:window", "Select Pre Driver Startup Script"));
116 m_PreScriptEdit->setText(m_PreScript);
117}
118
119void ProfileScript::selectPostScript()
120{
121 m_PostScript = QFileDialog::getOpenFileName(this, i18nc("@title:window", "Select Post Driver Startup Script"));
122 m_PostScriptEdit->setText(m_PostScript);
123}
124
125QJsonObject ProfileScript::toJSON() const
126{
127 QJsonObject settings =
128 {
129 {"Driver", m_Driver},
130 {"PreDelay", static_cast<int>(m_PreDelay)},
131 {"PreScript", m_PreScript},
132 {"PostDelay", static_cast<int>(m_PostDelay)},
133 {"PostScript", m_PostScript},
134 };
135
136 return settings;
137}
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void clicked(bool checked)
void editingFinished()
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void activated(int index)
void addItems(const QStringList &texts)
void clear()
void setCurrentText(const QString &text)
void textActivated(const QString &text)
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
QIcon fromTheme(const QString &name)
void editingFinished()
T & first()
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:57:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.