Skip to content

Commit

Permalink
Customcommand image (#1935)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
niknah authored Sep 29, 2023
1 parent 49fd09b commit 13e8273
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 142 deletions.
54 changes: 37 additions & 17 deletions plugin-customcommand/lxqtcustomcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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");
Expand All @@ -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();
}

Expand Down
2 changes: 2 additions & 0 deletions plugin-customcommand/lxqtcustomcommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions plugin-customcommand/lxqtcustomcommandconfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>::of(&QSpinBox::valueChanged), this, &LXQtCustomCommandConfiguration::repeatTimerSpinBoxChanged);
connect(ui->iconLineEdit, &QLineEdit::textChanged, this, &LXQtCustomCommandConfiguration::iconLineEditChanged);
Expand All @@ -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());
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions plugin-customcommand/lxqtcustomcommandconfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 13e8273

Please sign in to comment.