File: lxqtcustomcommandconfiguration.cpp

package info (click to toggle)
lxqt-panel 2.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 16,680 kB
  • sloc: cpp: 30,052; xml: 1,152; makefile: 19
file content (306 lines) | stat: -rw-r--r-- 14,774 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* BEGIN_COMMON_COPYRIGHT_HEADER
 * (c)LGPL2+
 *
 * LXQt - a lightweight, Qt based, desktop toolset
 * https://lxqt.org
 *
 * Copyright: 2021 LXQt team
 *
 * This program or library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * END_COMMON_COPYRIGHT_HEADER */

#include "lxqtcustomcommandconfiguration.h"
#include "lxqtcustomcommand.h"
#include "ui_lxqtcustomcommandconfiguration.h"

#include <QDialogButtonBox>
#include <QFileDialog>
#include <QFontDialog>
#include <QFontDatabase>
#include <QWhatsThis>
#include <qnamespace.h>

#define HELP_TEXT_DESC LXQtCustomCommandConfiguration::tr("command outputs plain text to be used as text of the button")
#define HELP_ICON_DESC1 LXQtCustomCommandConfiguration::tr("command outputs icon in form of:")
#define HELP_ICON_DESC2 LXQtCustomCommandConfiguration::tr("theme name")
#define HELP_ICON_DESC3 LXQtCustomCommandConfiguration::tr("name of icon resolved to image based on XDG spec")
#define HELP_ICON_DESC4 LXQtCustomCommandConfiguration::tr("image file path")
#define HELP_ICON_DESC5 LXQtCustomCommandConfiguration::tr("image data stream")
#define HELP_ICON_DESC6 LXQtCustomCommandConfiguration::tr("plain image stream")
#define HELP_ICON_DESC7 LXQtCustomCommandConfiguration::tr("base64 encoded image data stream")
#define HELP_ICON_DESC8 LXQtCustomCommandConfiguration::tr("as above but base64 encoded")
#define HELP_STRUC_DESC1 LXQtCustomCommandConfiguration::tr("command outputs structured variables to be used for button visualization in form of:")
#define HELP_STRUC_DESC2 LXQtCustomCommandConfiguration::tr("name1:base64value1 name2:base64value2  ...")
#define HELP_STRUC_DESC3 LXQtCustomCommandConfiguration::tr("Handled names are:")
#define HELP_STRUC_DESC4 LXQtCustomCommandConfiguration::tr("string to be used as text of the button")
#define HELP_STRUC_DESC5 LXQtCustomCommandConfiguration::tr("icon to be shown in the button, in the same form as explained before")
#define HELP_STRUC_DESC6 LXQtCustomCommandConfiguration::tr("string to be used as tooltip of the button")
#define HELP_STRUC_DESC7 LXQtCustomCommandConfiguration::tr("All values should be encoded in base64")
#define HELP_STRUC_DESC8 LXQtCustomCommandConfiguration::tr("Example of script generating structured output:")
#define HELP_STRUC_EXAMPLE1 LXQtCustomCommandConfiguration::tr("My Text")
#define HELP_STRUC_EXAMPLE2 LXQtCustomCommandConfiguration::tr("my_image")
#define HELP_STRUC_EXAMPLE3 LXQtCustomCommandConfiguration::tr("My Tooltip")



//Note: strings can't actually be translated here (in static initialization time)
//      the QT_TR_NOOP here is just for qt translate tools to get the strings for translation
const QStringList LXQtCustomCommandConfiguration::msOutputFormatStrings = {
    QStringLiteral(QT_TR_NOOP("Text only"))
    , QStringLiteral(QT_TR_NOOP("Icon only"))
    , QStringLiteral(QT_TR_NOOP("Structured"))
};

LXQtCustomCommandConfiguration::LXQtCustomCommandConfiguration(PluginSettings *settings, QWidget *parent) :
    LXQtPanelPluginConfigDialog(settings, parent),
    ui(new Ui::LXQtCustomCommandConfiguration),
    mLockSettingChanges(false)
{
    assert(msOutputFormatStrings.size() == OUTPUT_END);

    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose, true);
    
    const QFont monoFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
    ui->commandPlainTextEdit->setFont(monoFont);

    mHelpText = QStringLiteral("<html><head/><body><p><span style=\" font-weight:700;\">%1</span> - %2</p>"
    "<p><span style=\" font-weight:700;\">%3</span> - %4<br/>"
    "- <span style=\" font-style:italic;\">%5</span> - %6<br/>"
    "- <span style=\" font-style:italic;\">%7</span> - %7<br/>"
    "- <span style=\" font-style:italic;\">%8</span> - %9<br/>"
    "- <span style=\" font-style:italic;\">%10</span> - %11</p>"
    "<p><span style=\" font-weight:700;\">%12</span> - %13 \"<span style=\" text-decoration: underline;\">%14</span>\".<br/>"
    "%15<br/>"
    "- <span style=\" font-style:italic;\">text</span> - %16<br/>"
    "- <span style=\" font-style:italic;\">icon</span> - %17<br/>"
    "- <span style=\" font-style:italic;\">tooltip</span> - %18<br/>"
    "<span style=\" text-decoration: underline;\">%19</span><br/><br/>"
    "%20<br/>"
    "<span style=\" font-style:italic;\">echo \"text:$(echo -n \"%21\" | base64 --wrap=0) icon:$(base64 --wrap=0 %22.svg) tooltip:$(echo -n \"%23\" | base64 --wrap=0)\"</span></p></body></html>")
    .arg(msOutputFormatStrings[OUTPUT_TEXT],
        HELP_TEXT_DESC,
        msOutputFormatStrings[OUTPUT_ICON],
        HELP_ICON_DESC1,
        HELP_ICON_DESC2,
        HELP_ICON_DESC3,
        HELP_ICON_DESC4,
        HELP_ICON_DESC5,
        HELP_ICON_DESC6,
        HELP_ICON_DESC7,
        HELP_ICON_DESC8,
        msOutputFormatStrings[OUTPUT_STRUCTURED],
        HELP_STRUC_DESC1,
        HELP_STRUC_DESC2,
        HELP_STRUC_DESC3,
        HELP_STRUC_DESC4,
        HELP_STRUC_DESC5,
        HELP_STRUC_DESC6,
        HELP_STRUC_DESC7,
        HELP_STRUC_DESC8,
        HELP_STRUC_EXAMPLE1,
        HELP_STRUC_EXAMPLE2,
        HELP_STRUC_EXAMPLE3
    );

    //Note: translation is needed here in runtime (translator is attached already)
    for (int format = OUTPUT_BEGIN; format < OUTPUT_END; ++format)
        ui->outputFormatComboBox->addItem(tr(msOutputFormatStrings[format].toStdString().c_str()), format);

    loadSettings();

    connect(ui->buttonBox, &QDialogButtonBox::clicked, this, &LXQtCustomCommandConfiguration::dialogButtonsAction);

    connect(ui->autoRotateCheckBox, &QCheckBox::toggled, this, &LXQtCustomCommandConfiguration::autoRotateChanged);
    connect(ui->fontButton, &QPushButton::clicked, this, &LXQtCustomCommandConfiguration::fontButtonClicked);
    connect(ui->textColorLabel, &ColorLabel::colorChanged, this, &LXQtCustomCommandConfiguration::textColorChanged);
    connect(ui->textColorResetButton, &QPushButton::clicked, this, &LXQtCustomCommandConfiguration::textColorResetButtonClicked);
    connect(ui->commandPlainTextEdit, &QPlainTextEdit::textChanged, this, &LXQtCustomCommandConfiguration::commandPlainTextEditChanged);
    connect(ui->runWithBashCheckBox, &QCheckBox::toggled, this, &LXQtCustomCommandConfiguration::runWithBashCheckBoxChanged);
    connect(ui->outputFormatComboBox, &QComboBox::currentIndexChanged, this, &LXQtCustomCommandConfiguration::outputFormatComboBoxChanged);
    connect(ui->continuousOutputCheckBox, &QCheckBox::toggled, this, &LXQtCustomCommandConfiguration::continuousOutputCheckBoxChanged);
    connect(ui->repeatCheckBox, &QCheckBox::toggled, this, &LXQtCustomCommandConfiguration::repeatCheckBoxChanged);
    connect(ui->repeatTimerSpinBox, &QSpinBox::editingFinished, this, &LXQtCustomCommandConfiguration::repeatTimerSpinBoxChanged);
    connect(ui->iconLineEdit, &QLineEdit::editingFinished, this, &LXQtCustomCommandConfiguration::iconLineEditChanged);
    connect(ui->iconBrowseButton, &QPushButton::clicked, this, &LXQtCustomCommandConfiguration::iconBrowseButtonClicked);
    connect(ui->textLineEdit, &QLineEdit::editingFinished, this, &LXQtCustomCommandConfiguration::textLineEditChanged);
    connect(ui->tooltipLineEdit, &QLineEdit::editingFinished, this, &LXQtCustomCommandConfiguration::tooltipLineEditChanged);
    connect(ui->maxWidthSpinBox, &QSpinBox::editingFinished, this, &LXQtCustomCommandConfiguration::maxWidthSpinBoxChanged);
    connect(ui->clickLineEdit, &QLineEdit::editingFinished, this, &LXQtCustomCommandConfiguration::clickLineEditChanged);
    connect(ui->wheelUpLineEdit, &QLineEdit::editingFinished, this, &LXQtCustomCommandConfiguration::wheelUpLineEditChanged);
    connect(ui->wheelDownLineEdit, &QLineEdit::editingFinished, this, &LXQtCustomCommandConfiguration::wheelDownLineEditChanged);
    connect(ui->helpToolButton, &QToolButton::clicked, this, [this] () {
        QWhatsThis::showText(QCursor::pos(), mHelpText, this);
    });
}

LXQtCustomCommandConfiguration::~LXQtCustomCommandConfiguration()
{
    delete ui;
}

void LXQtCustomCommandConfiguration::loadSettings()
{
    mLockSettingChanges = true;

    ui->autoRotateCheckBox->setChecked(settings().value(QStringLiteral("autoRotate"), true).toBool());
    ui->fontButton->setText(settings().value(QStringLiteral("font"), font().toString()).toString());
    ui->textColorLabel->setColor(QColor::fromString(settings().value(QStringLiteral("textColor")).toString()));
    ui->commandPlainTextEdit->setPlainText(settings().value(QStringLiteral("command"), QStringLiteral("echo Configure...")).toString());
    ui->runWithBashCheckBox->setChecked(settings().value(QStringLiteral("runWithBash"), true).toBool());
    // backward compatibility check
    if (settings().contains(QStringLiteral("outputFormat")))
        ui->outputFormatComboBox->setCurrentIndex(ui->outputFormatComboBox->findData(settings().value(QStringLiteral("outputFormat")).toInt()));
    else {
        const bool image = settings().value(QStringLiteral("outputImage"), false).toBool();
        ui->outputFormatComboBox->setCurrentIndex(ui->outputFormatComboBox->findData(image ? OUTPUT_ICON : OUTPUT_TEXT));
    }
    ui->continuousOutputCheckBox->setChecked(settings().value(QStringLiteral("continuousOutput"), false).toBool());
    ui->repeatCheckBox->setChecked(settings().value(QStringLiteral("repeat"), true).toBool());
    ui->repeatTimerSpinBox->setEnabled(ui->repeatCheckBox->isChecked());
    ui->repeatTimerSpinBox->setValue(settings().value(QStringLiteral("repeatTimer"), 5).toInt());
    ui->iconLineEdit->setText(settings().value(QStringLiteral("icon"), QString()).toString());
    ui->textLineEdit->setText(settings().value(QStringLiteral("text"), QStringLiteral("%1")).toString());
    ui->tooltipLineEdit->setText(settings().value(QStringLiteral("tooltip"), QString()).toString());
    ui->maxWidthSpinBox->setValue(settings().value(QStringLiteral("maxWidth"), 200).toInt());
    ui->clickLineEdit->setText(settings().value(QStringLiteral("click"), QString()).toString());
    ui->wheelUpLineEdit->setText(settings().value(QStringLiteral("wheelUp"), QString()).toString());
    ui->wheelDownLineEdit->setText(settings().value(QStringLiteral("wheelDown"), QString()).toString());

    mLockSettingChanges = false;
}

void LXQtCustomCommandConfiguration::autoRotateChanged(bool autoRotate)
{
    if (!mLockSettingChanges)
        settings().setValue(QStringLiteral("autoRotate"), autoRotate);
}

void LXQtCustomCommandConfiguration::fontButtonClicked()
{
    bool ok;
    QFont currentFont;
    currentFont.fromString(ui->fontButton->text());
    QFont getFont = QFontDialog::getFont(&ok, currentFont, this);
    if (ok)
    {
        auto fontString = getFont.toString();
        ui->fontButton->setText(fontString);
        settings().setValue(QStringLiteral("font"), fontString);
    }
}

void LXQtCustomCommandConfiguration::textColorChanged()
{
    QColor color = ui->textColorLabel->getColor();
    QColor oldColor = QColor::fromString(settings().value(QStringLiteral("textColor")).toString());
    if (color != oldColor)
        settings().setValue(QStringLiteral("textColor"), color.name());
}

void LXQtCustomCommandConfiguration::textColorResetButtonClicked()
{
    ui->textColorLabel->reset();
    settings().remove(QStringLiteral("textColor"));
}

void LXQtCustomCommandConfiguration::commandPlainTextEditChanged()
{
    if (!mLockSettingChanges)
        settings().setValue(QStringLiteral("command"), ui->commandPlainTextEdit->toPlainText().trimmed());
}

void LXQtCustomCommandConfiguration::runWithBashCheckBoxChanged(bool runWithBash)
{
    if (!mLockSettingChanges)
        settings().setValue(QStringLiteral("runWithBash"), runWithBash);
}

void LXQtCustomCommandConfiguration::outputFormatComboBoxChanged(int index)
{
    if (!mLockSettingChanges) {
        settings().setValue(QStringLiteral("outputFormat"), ui->outputFormatComboBox->itemData(index, Qt::UserRole));
        settings().remove(QStringLiteral("outputImage"));
    }
}

void LXQtCustomCommandConfiguration::continuousOutputCheckBoxChanged(bool continuousOutput)
{
    if (!mLockSettingChanges)
        settings().setValue(QStringLiteral("continuousOutput"), continuousOutput);
}

void LXQtCustomCommandConfiguration::repeatCheckBoxChanged(bool repeat)
{
    if (!mLockSettingChanges)
        settings().setValue(QStringLiteral("repeat"), repeat);
    ui->repeatTimerSpinBox->setEnabled(repeat);
}

void LXQtCustomCommandConfiguration::repeatTimerSpinBoxChanged()
{
    if (!mLockSettingChanges)
        settings().setValue(QStringLiteral("repeatTimer"), ui->repeatTimerSpinBox->value());
}

void LXQtCustomCommandConfiguration::iconLineEditChanged()
{
    if (!mLockSettingChanges)
        settings().setValue(QStringLiteral("icon"), ui->iconLineEdit->text());
}

void LXQtCustomCommandConfiguration::iconBrowseButtonClicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Select Icon File"), QString(), tr("Images (*.png *.svg *.xpm *.jpg)"));
    ui->iconLineEdit->setText(fileName);
}

void LXQtCustomCommandConfiguration::textLineEditChanged()
{
    if (!mLockSettingChanges)
        settings().setValue(QStringLiteral("text"), ui->textLineEdit->text());
}

void LXQtCustomCommandConfiguration::tooltipLineEditChanged()
{
    if (!mLockSettingChanges)
        settings().setValue(QStringLiteral("tooltip"), ui->tooltipLineEdit->text());
}

void LXQtCustomCommandConfiguration::maxWidthSpinBoxChanged()
{
    if (!mLockSettingChanges)
        settings().setValue(QStringLiteral("maxWidth"), ui->maxWidthSpinBox->value());
}

void LXQtCustomCommandConfiguration::clickLineEditChanged()
{
    if (!mLockSettingChanges)
        settings().setValue(QStringLiteral("click"), ui->clickLineEdit->text().trimmed());
}

void LXQtCustomCommandConfiguration::wheelUpLineEditChanged()
{
    if (!mLockSettingChanges)
        settings().setValue(QStringLiteral("wheelUp"), ui->wheelUpLineEdit->text().trimmed());
}

void LXQtCustomCommandConfiguration::wheelDownLineEditChanged()
{
    if (!mLockSettingChanges)
        settings().setValue(QStringLiteral("wheelDown"), ui->wheelDownLineEdit->text().trimmed());
}