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
|
/* 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 "lxqtcustomcommand.h"
#include "custombutton.h"
#include "lxqtcustomcommandconfiguration.h"
#include <QProcess>
#include <QTimer>
#include <QVBoxLayout>
#include <XdgIcon>
#include <LXQt/Globals>
#include <QDebug>
#include <algorithm>
LXQtCustomCommand::LXQtCustomCommand(const ILXQtPanelPluginStartupInfo &startupInfo):
QObject(),
ILXQtPanelPlugin(startupInfo),
mProcess(new QProcess(this)),
mTimer(new QTimer(this)),
mDelayedRunTimer(new QTimer(this)),
mFirstRun(true),
mOutput(QString()),
mAutoRotate(true),
mRunWithBash(true),
mOutputImage(false),
mRepeat(true),
mRepeatTimer(5),
mMaxWidth(200)
{
mButton = new CustomButton(this);
mButton->setObjectName(QLatin1String("CustomButton"));
mFont = mButton->font().toString();
mTimer->setSingleShot(true);
mDelayedRunTimer->setSingleShot(true);
mDelayedRunTimer->setInterval(500);
connect(mButton, &CustomButton::clicked, this, &LXQtCustomCommand::handleClick);
connect(mButton, &CustomButton::wheelScrolled, this, &LXQtCustomCommand::handleWheelScrolled);
connect(mTimer, &QTimer::timeout, this, &LXQtCustomCommand::runCommand);
connect(mDelayedRunTimer, &QTimer::timeout, this, &LXQtCustomCommand::runCommand);
connect(mProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &LXQtCustomCommand::handleFinished);
settingsChanged();
}
LXQtCustomCommand::~LXQtCustomCommand()
{
// Ensure process is closed before exiting and avoids warning from QProcess.
mProcess->close();
delete mButton;
}
QWidget *LXQtCustomCommand::widget()
{
return mButton;
}
void LXQtCustomCommand::realign()
{
mButton->setAutoRotation(mAutoRotate);
}
QDialog *LXQtCustomCommand::configureDialog()
{
if (!mConfigDialog)
mConfigDialog = new LXQtCustomCommandConfiguration(settings());
return mConfigDialog;
}
void LXQtCustomCommand::settingsChanged()
{
bool shouldRun = false;
bool oldAutoRotate = mAutoRotate;
QString oldFont = mFont;
QString oldCommand = mCommand;
bool oldRunWithBash = mRunWithBash;
bool oldOutputImage = mOutputImage;
bool oldRepeat = mRepeat;
int oldRepeatTimer = mRepeatTimer;
QString oldIcon = mIcon;
QString oldText = mText;
QString oldTooltip = mTooltip;
int oldMaxWidth = mMaxWidth;
mAutoRotate = settings()->value(QStringLiteral("autoRotate"), true).toBool();
mFont = settings()->value(QStringLiteral("font"), QString()).toString(); // the default font should be empty
QColor textColor = QColor::fromString(settings()->value(QStringLiteral("textColor")).toString());
mCommand = settings()->value(QStringLiteral("command"), QStringLiteral("echo Configure...")).toString().trimmed();
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 = std::max(1, mRepeatTimer);
mIcon = settings()->value(QStringLiteral("icon"), QString()).toString();
mText = settings()->value(QStringLiteral("text"), QStringLiteral("%1")).toString();
mTooltip = settings()->value(QStringLiteral("tooltip"), QString()).toString();
mMaxWidth = settings()->value(QStringLiteral("maxWidth"), 200).toInt();
mClick = settings()->value(QStringLiteral("click"), QString()).toString().trimmed();
mWheelUp = settings()->value(QStringLiteral("wheelUp"), QString()).toString().trimmed();
mWheelDown = settings()->value(QStringLiteral("wheelDown"), QString()).toString().trimmed();
if (oldFont != mFont) {
QFont newFont;
if (!mFont.isEmpty()) // is empty when it's reset to app's font
newFont.fromString(mFont);
if (mFirstRun) {
QTimer::singleShot(0, mButton, [this, newFont] {
mButton->setFont(newFont);
updateButton();
});
}
else {
mButton->setFont(newFont);
updateButton();
}
}
if (textColor.isValid()) {
mButton->setStyleSheet(QStringLiteral("QToolButton{color: %1}").arg(textColor.name()));
}
else {
mButton->setStyleSheet(QString());
}
if (oldCommand != mCommand || oldRunWithBash != mRunWithBash || oldOutputImage != mOutputImage || oldRepeat != mRepeat)
shouldRun = true;
if (mFirstRun || oldRepeatTimer != mRepeatTimer)
mTimer->setInterval(mRepeatTimer * 1000);
if (oldIcon != mIcon) {
mButton->setIcon(XdgIcon::fromTheme(mIcon, QIcon(mIcon)));
updateButton();
}
else if (oldText != mText || oldTooltip != mTooltip)
updateButton();
if (mFirstRun || oldMaxWidth != mMaxWidth)
mButton->setMaxWidth(mMaxWidth);
if (mFirstRun || oldAutoRotate != mAutoRotate)
mButton->setAutoRotation(mAutoRotate);
if (mFirstRun) {
mFirstRun = false;
runCommand();
}
// Delay timer for running command, avoids multiple calls on settings change while typing command or clicking "Reset"
else if (shouldRun)
mDelayedRunTimer->start();
}
void LXQtCustomCommand::handleClick()
{
if (!mClick.isEmpty())
runDetached(mClick);
}
void LXQtCustomCommand::handleFinished(int exitCode, QProcess::ExitStatus /*exitStatus*/)
{
if (exitCode == 0) {
if(mOutputImage) {
mOutputByteArray = mProcess->readAllStandardOutput();
} else {
mOutput = QString::fromUtf8(mProcess->readAllStandardOutput());
if (mOutput.endsWith(QStringLiteral("\n")))
mOutput.chop(1);
}
}
else
mOutput = tr("Error");
updateButton();
if (mRepeat)
mTimer->start();
}
void LXQtCustomCommand::updateButton() {
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->setToolTip(mTooltip);
mButton->updateWidth();
}
void LXQtCustomCommand::handleWheelScrolled(int delta)
{
if (delta > 0 && !mWheelUp.isEmpty())
runDetached(mWheelUp);
else if (delta < 0 && !mWheelDown.isEmpty())
runDetached(mWheelDown);
}
void LXQtCustomCommand::runCommand()
{
if (mCommand.isEmpty())
return;
if (mProcess->state() != QProcess::NotRunning) {
mDelayedRunTimer->start();
return;
}
#if (QT_VERSION >= QT_VERSION_CHECK(5,15,0))
QStringList args;
if (mRunWithBash)
args << QStringLiteral("bash") << QStringLiteral("-c") << mCommand;
else
args = QProcess::splitCommand(mCommand);
mProcess->start(args.takeFirst(), args);
#else
QString exec;
if (mRunWithBash)
exec = QString(QStringLiteral("bash -c \"%1\"")).arg(mCommand);
else
exec = mCommand;
mProcess->start(exec);
#endif
}
void LXQtCustomCommand::runDetached(QString command)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,15,0))
QStringList args = QProcess::splitCommand(command);
QProcess::startDetached(args.takeFirst(), args);
#else
QProcess::startDetached(command);
#endif
}
|