From 13e827320d9e0ab2b43cb14080bc4695fda832c9 Mon Sep 17 00:00:00 2001 From: niknah Date: Fri, 29 Sep 2023 21:42:00 +1000 Subject: [PATCH] Customcommand image (#1935) * Added "output as image" option to custom command widget. * Default Output Image to false * Init bool, int in contructor. * Patch to fix high CPU usage from stefonarch --- plugin-customcommand/lxqtcustomcommand.cpp | 54 ++-- plugin-customcommand/lxqtcustomcommand.h | 2 + .../lxqtcustomcommandconfiguration.cpp | 8 + .../lxqtcustomcommandconfiguration.h | 1 + .../lxqtcustomcommandconfiguration.ui | 257 +++++++++--------- 5 files changed, 180 insertions(+), 142 deletions(-) diff --git a/plugin-customcommand/lxqtcustomcommand.cpp b/plugin-customcommand/lxqtcustomcommand.cpp index 722cac878..e89e3dc75 100644 --- a/plugin-customcommand/lxqtcustomcommand.cpp +++ b/plugin-customcommand/lxqtcustomcommand.cpp @@ -42,7 +42,12 @@ LXQtCustomCommand::LXQtCustomCommand(const ILXQtPanelPluginStartupInfo &startupI mDelayedRunTimer(new QTimer(this)), mFirstRun(true), mOutput(QString()), - mAutoRotate(true) + mAutoRotate(true), + mRunWithBash(true), + mOutputImage(false), + mRepeat(true), + mRepeatTimer(5), + mMaxWidth(200) { mButton = new CustomButton(this); mButton->setObjectName(QLatin1String("CustomButton")); @@ -95,6 +100,7 @@ void LXQtCustomCommand::settingsChanged() QString oldFont = mFont; QString oldCommand = mCommand; bool oldRunWithBash = mRunWithBash; + bool oldOutputImage = mOutputImage; bool oldRepeat = mRepeat; int oldRepeatTimer = mRepeatTimer; QString oldIcon = mIcon; @@ -105,6 +111,7 @@ void LXQtCustomCommand::settingsChanged() mFont = settings()->value(QStringLiteral("font"), QString()).toString(); // the default font should be empty mCommand = settings()->value(QStringLiteral("command"), QStringLiteral("echo Configure...")).toString(); mRunWithBash = settings()->value(QStringLiteral("runWithBash"), true).toBool(); + mOutputImage = settings()->value(QStringLiteral("outputImage"), false).toBool(); mRepeat = settings()->value(QStringLiteral("repeat"), true).toBool(); mRepeatTimer = settings()->value(QStringLiteral("repeatTimer"), 5).toInt(); mRepeatTimer = qMax(1, mRepeatTimer); @@ -130,10 +137,10 @@ void LXQtCustomCommand::settingsChanged() updateButton(); } } - if (oldCommand != mCommand || oldRunWithBash != mRunWithBash || oldRepeat != mRepeat) + if (oldCommand != mCommand || oldRunWithBash != mRunWithBash || oldOutputImage != mOutputImage || oldRepeat != mRepeat) shouldRun = true; - if (oldRepeatTimer != mRepeatTimer) + if (mFirstRun || oldRepeatTimer != mRepeatTimer) mTimer->setInterval(mRepeatTimer * 1000); if (oldIcon != mIcon) { @@ -143,10 +150,10 @@ void LXQtCustomCommand::settingsChanged() else if (oldText != mText) updateButton(); - if (oldMaxWidth != mMaxWidth) + if (mFirstRun || oldMaxWidth != mMaxWidth) mButton->setMaxWidth(mMaxWidth); - if (oldAutoRotate != mAutoRotate) + if (mFirstRun || oldAutoRotate != mAutoRotate) mButton->setAutoRotation(mAutoRotate); if (mFirstRun) { @@ -166,11 +173,14 @@ void LXQtCustomCommand::handleClick() void LXQtCustomCommand::handleFinished(int exitCode, QProcess::ExitStatus /*exitStatus*/) { - if (exitCode == 0) { - mOutput = QString::fromUtf8(mProcess->readAllStandardOutput()); - if (mOutput.endsWith(QStringLiteral("\n"))) - mOutput.chop(1); + if(mOutputImage) { + mOutputByteArray = mProcess->readAllStandardOutput(); + } else { + mOutput = QString::fromUtf8(mProcess->readAllStandardOutput()); + if (mOutput.endsWith(QStringLiteral("\n"))) + mOutput.chop(1); + } } else mOutput = tr("Error"); @@ -181,16 +191,26 @@ void LXQtCustomCommand::handleFinished(int exitCode, QProcess::ExitStatus /*exit } void LXQtCustomCommand::updateButton() { - QString newText = mText; - if (newText.contains(QStringLiteral("%1"))) - newText = newText.arg(mOutput); - if (mButton->icon().isNull()) - mButton->setToolButtonStyle(Qt::ToolButtonTextOnly); - else - mButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); + if(mOutputImage) { + QPixmap pixmap; + pixmap.loadFromData(mOutputByteArray); + QIcon icon(pixmap); + mButton->setIcon(icon); + mButton->setToolButtonStyle(Qt::ToolButtonIconOnly); + } else { + QString newText = mText; + if (newText.contains(QStringLiteral("%1"))) + newText = newText.arg(mOutput); + + mButton->setText(newText); + + if (mButton->icon().isNull()) + mButton->setToolButtonStyle(Qt::ToolButtonTextOnly); + else + mButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); + } - mButton->setText(newText); mButton->updateWidth(); } diff --git a/plugin-customcommand/lxqtcustomcommand.h b/plugin-customcommand/lxqtcustomcommand.h index d37cf03ef..ffb5679d3 100644 --- a/plugin-customcommand/lxqtcustomcommand.h +++ b/plugin-customcommand/lxqtcustomcommand.h @@ -72,11 +72,13 @@ private slots: bool mFirstRun; QString mOutput; + QByteArray mOutputByteArray; bool mAutoRotate; QString mFont; QString mCommand; bool mRunWithBash; + bool mOutputImage; bool mRepeat; int mRepeatTimer; QString mIcon; diff --git a/plugin-customcommand/lxqtcustomcommandconfiguration.cpp b/plugin-customcommand/lxqtcustomcommandconfiguration.cpp index e76bfa775..95ba031e5 100644 --- a/plugin-customcommand/lxqtcustomcommandconfiguration.cpp +++ b/plugin-customcommand/lxqtcustomcommandconfiguration.cpp @@ -46,6 +46,7 @@ LXQtCustomCommandConfiguration::LXQtCustomCommandConfiguration(PluginSettings *s connect(ui->fontButton, &QPushButton::clicked, this, &LXQtCustomCommandConfiguration::fontButtonClicked); connect(ui->commandPlainTextEdit, &QPlainTextEdit::textChanged, this, &LXQtCustomCommandConfiguration::commandPlainTextEditChanged); connect(ui->runWithBashCheckBox, &QCheckBox::toggled, this, &LXQtCustomCommandConfiguration::runWithBashCheckBoxChanged); + connect(ui->outputImageCheckBox, &QCheckBox::toggled, this, &LXQtCustomCommandConfiguration::outputImageCheckBoxChanged); connect(ui->repeatCheckBox, &QCheckBox::toggled, this, &LXQtCustomCommandConfiguration::repeatCheckBoxChanged); connect(ui->repeatTimerSpinBox, QOverload::of(&QSpinBox::valueChanged), this, &LXQtCustomCommandConfiguration::repeatTimerSpinBoxChanged); connect(ui->iconLineEdit, &QLineEdit::textChanged, this, &LXQtCustomCommandConfiguration::iconLineEditChanged); @@ -70,6 +71,7 @@ void LXQtCustomCommandConfiguration::loadSettings() ui->fontButton->setText(settings().value(QStringLiteral("font"), font().toString()).toString()); ui->commandPlainTextEdit->setPlainText(settings().value(QStringLiteral("command"), QStringLiteral("echo Configure...")).toString()); ui->runWithBashCheckBox->setChecked(settings().value(QStringLiteral("runWithBash"), true).toBool()); + ui->outputImageCheckBox->setChecked(settings().value(QStringLiteral("outputImage"), 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()); @@ -115,6 +117,12 @@ void LXQtCustomCommandConfiguration::runWithBashCheckBoxChanged(bool runWithBash settings().setValue(QStringLiteral("runWithBash"), runWithBash); } +void LXQtCustomCommandConfiguration::outputImageCheckBoxChanged(bool outputImage) +{ + if (!mLockSettingChanges) + settings().setValue(QStringLiteral("outputImage"), outputImage); +} + void LXQtCustomCommandConfiguration::repeatCheckBoxChanged(bool repeat) { if (!mLockSettingChanges) diff --git a/plugin-customcommand/lxqtcustomcommandconfiguration.h b/plugin-customcommand/lxqtcustomcommandconfiguration.h index 26dc9e114..b449488fb 100644 --- a/plugin-customcommand/lxqtcustomcommandconfiguration.h +++ b/plugin-customcommand/lxqtcustomcommandconfiguration.h @@ -46,6 +46,7 @@ private slots: void fontButtonClicked(); void commandPlainTextEditChanged(); void runWithBashCheckBoxChanged(bool runWithBash); + void outputImageCheckBoxChanged(bool outputImage); void repeatCheckBoxChanged(bool repeat); void repeatTimerSpinBoxChanged(int repeatTimer); void iconLineEditChanged(QString icon); diff --git a/plugin-customcommand/lxqtcustomcommandconfiguration.ui b/plugin-customcommand/lxqtcustomcommandconfiguration.ui index afa860c56..b8287cab1 100644 --- a/plugin-customcommand/lxqtcustomcommandconfiguration.ui +++ b/plugin-customcommand/lxqtcustomcommandconfiguration.ui @@ -7,7 +7,7 @@ 0 0 500 - 500 + 535 @@ -22,43 +22,113 @@ - - - - Use icon from theme or browse file + + + + + 0 + 0 + - - - - - - Run with "bash -c" + + + 0 + 0 + - - true + + QFrame::NoFrame + + + QFrame::Raised + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + 0 + + + + Repeat command after: + + + + + + + false + + + + 0 + 0 + + + + second(s) + + + 1 + + + 86400 + + + 1 + + + 5 + + + + - - + + - Select Font + Browse false - - + + - Text + Max Width Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + + Font + + + @@ -69,10 +139,20 @@ - - + + - Font + Icon + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + + + + + Autorotate when the panel is vertical @@ -95,40 +175,50 @@ - - - - %1 - + + - Use %1 to display command output + Use icon from theme or browse file - - + + - Icon + Run with "bash -c" - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + true - - + + - Browse + Select Font false - - + + - Autorotate when the panel is vertical + %1 + + + Use %1 to display command output + + + + + + + Text + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter @@ -154,94 +244,11 @@ - - + + - Max Width - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter - - - - - - - - 0 - 0 - - - - - 0 - 0 - - - - QFrame::NoFrame - - - QFrame::Raised - - - 0 + Command outputs an image - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - 0 - 0 - - - - Repeat command after: - - - - - - - false - - - - 0 - 0 - - - - second(s) - - - 1 - - - 86400 - - - 1 - - - 5 - - - -