Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CustomCommand: Allow plugin to expand to all rows in the panel + fixes #1712

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
122 changes: 57 additions & 65 deletions plugin-customcommand/custombutton.cpp
Expand Up @@ -31,45 +31,21 @@
#include <QStyleOptionToolButton>
#include <QProxyStyle>

class LeftAlignedTextStyle : public QProxyStyle
{
using QProxyStyle::QProxyStyle;
public:

virtual void drawItemText(QPainter * painter, const QRect & rect, int flags
, const QPalette & pal, bool enabled, const QString & text
, QPalette::ColorRole textRole = QPalette::NoRole) const override;
};

void LeftAlignedTextStyle::drawItemText(QPainter * painter, const QRect & rect, int flags
, const QPalette & pal, bool enabled, const QString & text
, QPalette::ColorRole textRole) const
{
QString txt = text;
// get the button text because the text that's given to this function may be middle-elided
if (const QToolButton *tb = dynamic_cast<const QToolButton*>(painter->device()))
txt = tb->text();
txt = QFontMetrics(painter->font()).elidedText(txt, Qt::ElideRight, rect.width());
QProxyStyle::drawItemText(painter, rect, (flags & ~Qt::AlignHCenter) | Qt::AlignLeft, pal, enabled, txt, textRole);
}


CustomButton::CustomButton(ILXQtPanelPlugin *plugin, QWidget* parent):
QToolButton(parent),
mPlugin(plugin),
mPanel(plugin->panel()),
mMaxWidth(200)

mAutoRotate(false),
mMaxWidth(200),
mSizeHint(QSize())
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setAutoRaise(true);
setContentsMargins(0,0,0,0);
setMinimumWidth(1);
setMinimumHeight(1);
setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
setStyle(new LeftAlignedTextStyle());
updateWidth();

updateButton();
}

CustomButton::~CustomButton() = default;
Expand All @@ -83,59 +59,75 @@ void CustomButton::wheelEvent(QWheelEvent *event)

void CustomButton::setMaxWidth(int maxWidth)
{
mMaxWidth = maxWidth;
updateWidth();
}

void CustomButton::updateWidth()
{
int newWidth = qMin(sizeHint().width(), mMaxWidth);
if (mOrigin == Qt::TopLeftCorner) {
setFixedWidth(newWidth);

setMinimumHeight(1);
setMaximumHeight(QWIDGETSIZE_MAX);
}
else {
setMinimumWidth(1);
setMaximumWidth(QWIDGETSIZE_MAX);

setFixedHeight(newWidth);
}
update();
if (mMaxWidth != maxWidth)
mMaxWidth = maxWidth;
}

void CustomButton::setOrigin(Qt::Corner newOrigin)
{
if (mOrigin != newOrigin) {
if (mOrigin != newOrigin)
mOrigin = newOrigin;
updateWidth();
}
}

void CustomButton::setAutoRotation(bool value)
void CustomButton::setAutoRotation(bool rotate)
{
if (value) {
switch (mPanel->position())
{
case ILXQtPanel::PositionTop:
case ILXQtPanel::PositionBottom:
setOrigin(Qt::TopLeftCorner);
break;
if (mSizeHint == sizeHint() && mAutoRotate == rotate && mPanelPosition == mPanel->position())
return;

case ILXQtPanel::PositionLeft:
mSizeHint = sizeHint();
mAutoRotate = rotate;
mPanelPosition = mPanel->position();
int length = 0;

switch (mPanel->position())
{
case ILXQtPanel::PositionTop:
case ILXQtPanel::PositionBottom:
setOrigin(Qt::TopLeftCorner);
length = qMin(mSizeHint.width(), mMaxWidth);
setFixedWidth(length);

setMinimumHeight(1);
setMaximumHeight(QWIDGETSIZE_MAX);
break;

case ILXQtPanel::PositionLeft:
if (rotate) {
setOrigin(Qt::BottomLeftCorner);
break;
length = qMin(mSizeHint.width(), mMaxWidth);
}
else {
setOrigin(Qt::TopLeftCorner);
length = qMin(mSizeHint.height(), mMaxWidth);
}
setMinimumWidth(1);
setMaximumWidth(QWIDGETSIZE_MAX);

case ILXQtPanel::PositionRight:
setFixedHeight(length);
break;

case ILXQtPanel::PositionRight:
if (rotate) {
setOrigin(Qt::TopRightCorner);
break;
length = qMin(mSizeHint.width(), mMaxWidth);
}
else {
setOrigin(Qt::TopLeftCorner);
length = qMin(mSizeHint.height(), mMaxWidth);
}
setMinimumWidth(1);
setMaximumWidth(QWIDGETSIZE_MAX);

setFixedHeight(length);
break;
}
else
setOrigin(Qt::TopLeftCorner);
update();

}

void CustomButton::updateButton()
{
setAutoRotation(mAutoRotate);
}

void CustomButton::paintEvent(QPaintEvent *event)
Expand Down
7 changes: 5 additions & 2 deletions plugin-customcommand/custombutton.h
Expand Up @@ -40,9 +40,9 @@ class CustomButton : public QToolButton
~CustomButton();

public slots:
void setAutoRotation(bool value);
void setMaxWidth(int maxWidth);
void updateWidth();
void setAutoRotation(bool rotate);
void updateButton();

protected:
void wheelEvent(QWheelEvent *event) override;
Expand All @@ -54,8 +54,11 @@ private slots:
private:
ILXQtPanelPlugin *mPlugin;
ILXQtPanel *mPanel;
ILXQtPanel::Position mPanelPosition;
bool mAutoRotate;
Qt::Corner mOrigin;
int mMaxWidth;
QSize mSizeHint;

signals:
void wheelScrolled(int);
Expand Down
24 changes: 11 additions & 13 deletions plugin-customcommand/lxqtcustomcommand.cpp
Expand Up @@ -42,7 +42,8 @@ LXQtCustomCommand::LXQtCustomCommand(const ILXQtPanelPluginStartupInfo &startupI
mDelayedRunTimer(new QTimer(this)),
mFirstRun(true),
mOutput(QString()),
mAutoRotate(true)
mAutoRotate(true),
mExpandToRows(false)
{
mButton = new CustomButton(this);
mButton->setObjectName(QLatin1String("CustomButton"));
Expand Down Expand Up @@ -76,7 +77,7 @@ QWidget *LXQtCustomCommand::widget()

void LXQtCustomCommand::realign()
{
mButton->setAutoRotation(mAutoRotate);
mButton->updateButton();
}

QDialog *LXQtCustomCommand::configureDialog()
Expand All @@ -91,17 +92,17 @@ void LXQtCustomCommand::settingsChanged()
{
bool shouldRun = false;

bool oldAutoRotate = mAutoRotate;
bool oldExpandToRows = mExpandToRows;
QString oldFont = mFont;
QString oldCommand = mCommand;
bool oldRunWithBash = mRunWithBash;
bool oldRepeat = mRepeat;
int oldRepeatTimer = mRepeatTimer;
QString oldIcon = mIcon;
QString oldText = mText;
int oldMaxWidth = mMaxWidth;

mAutoRotate = settings()->value(QStringLiteral("autoRotate"), true).toBool();
mExpandToRows = settings()->value(QStringLiteral("expandToRows")).toBool();
mFont = settings()->value(QStringLiteral("font"), mButton->font().toString()).toString();
mCommand = settings()->value(QStringLiteral("command"), QStringLiteral("echo Configure...")).toString();
mRunWithBash = settings()->value(QStringLiteral("runWithBash"), true).toBool();
Expand All @@ -126,7 +127,6 @@ void LXQtCustomCommand::settingsChanged()
}
else {
mButton->setFont(newFont);
updateButton();
}
}
if (oldCommand != mCommand || oldRunWithBash != mRunWithBash || oldRepeat != mRepeat)
Expand All @@ -135,18 +135,16 @@ void LXQtCustomCommand::settingsChanged()
if (oldRepeatTimer != mRepeatTimer)
mTimer->setInterval(mRepeatTimer * 1000);

if (oldIcon != mIcon) {
if (oldIcon != mIcon)
mButton->setIcon(XdgIcon::fromTheme(mIcon, QIcon(mIcon)));
updateButton();
}
else if (oldText != mText)
updateButton();

if (oldMaxWidth != mMaxWidth)
mButton->setMaxWidth(mMaxWidth);

if (oldAutoRotate != mAutoRotate)
mButton->setAutoRotation(mAutoRotate);
if (oldExpandToRows != mExpandToRows)
pluginFlagsChanged();

mButton->setAutoRotation(mAutoRotate);

if (mFirstRun) {
mFirstRun = false;
Expand Down Expand Up @@ -190,7 +188,7 @@ void LXQtCustomCommand::updateButton() {
mButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

mButton->setText(newText);
mButton->updateWidth();
mButton->updateButton();
}

void LXQtCustomCommand::handleWheelScrolled(int yDelta)
Expand Down
2 changes: 2 additions & 0 deletions plugin-customcommand/lxqtcustomcommand.h
Expand Up @@ -49,6 +49,7 @@ class LXQtCustomCommand : public QObject, public ILXQtPanelPlugin
virtual ILXQtPanelPlugin::Flags flags() const { return PreferRightAlignment | HaveConfigDialog ; }
void realign();
QDialog *configureDialog();
bool isSeparate() const { return mExpandToRows; }

protected slots:
virtual void settingsChanged();
Expand All @@ -74,6 +75,7 @@ private slots:
QString mOutput;

bool mAutoRotate;
bool mExpandToRows;
QString mFont;
QString mCommand;
bool mRunWithBash;
Expand Down
8 changes: 8 additions & 0 deletions plugin-customcommand/lxqtcustomcommandconfiguration.cpp
Expand Up @@ -42,6 +42,7 @@ LXQtCustomCommandConfiguration::LXQtCustomCommandConfiguration(PluginSettings *s

connect(ui->buttonBox, &QDialogButtonBox::clicked, this, &LXQtCustomCommandConfiguration::buttonBoxClicked);
connect(ui->autoRotateCheckBox, &QCheckBox::toggled, this, &LXQtCustomCommandConfiguration::autoRotateChanged);
connect(ui->expandToRowsCheckBox, &QCheckBox::toggled, this, &LXQtCustomCommandConfiguration::expandToRowsChanged);
connect(ui->fontButton, &QPushButton::clicked, this, &LXQtCustomCommandConfiguration::fontButtonClicked);
connect(ui->commandPlainTextEdit, &QPlainTextEdit::textChanged, this, &LXQtCustomCommandConfiguration::commandPlainTextEditChanged);
connect(ui->runWithBashCheckBox, &QCheckBox::toggled, this, &LXQtCustomCommandConfiguration::runWithBashCheckBoxChanged);
Expand Down Expand Up @@ -75,6 +76,7 @@ void LXQtCustomCommandConfiguration::buttonBoxClicked(QAbstractButton *btn)
void LXQtCustomCommandConfiguration::loadSettings()
{
mAutoRotate = settings().value(QStringLiteral("autoRotate"), true).toBool();
mExpandToRows = settings().value(QStringLiteral("expandToRows"), false).toBool();
mFont = settings().value(QStringLiteral("font"), font().toString()).toString();
mCommand = settings().value(QStringLiteral("command"), QStringLiteral("echo Configure...")).toString();
mRunWithBash = settings().value(QStringLiteral("runWithBash"), true).toBool();
Expand All @@ -91,6 +93,7 @@ void LXQtCustomCommandConfiguration::loadSettings()
void LXQtCustomCommandConfiguration::setUiValues()
{
ui->autoRotateCheckBox->setChecked(mAutoRotate);
ui->expandToRowsCheckBox->setChecked(mExpandToRows);
ui->fontButton->setText(mFont);
ui->commandPlainTextEdit->setPlainText(mCommand);
ui->runWithBashCheckBox->setChecked(mRunWithBash);
Expand All @@ -110,6 +113,11 @@ void LXQtCustomCommandConfiguration::autoRotateChanged(bool autoRotate)
settings().setValue(QStringLiteral("autoRotate"), autoRotate);
}

void LXQtCustomCommandConfiguration::expandToRowsChanged(bool expand)
{
settings().setValue(QStringLiteral("expandToRows"), expand);
}

void LXQtCustomCommandConfiguration::fontButtonClicked()
{
bool ok;
Expand Down
2 changes: 2 additions & 0 deletions plugin-customcommand/lxqtcustomcommandconfiguration.h
Expand Up @@ -43,6 +43,7 @@ class LXQtCustomCommandConfiguration : public LXQtPanelPluginConfigDialog

private:
bool mAutoRotate;
bool mExpandToRows;
QString mFont;
QString mCommand;
bool mRunWithBash;
Expand All @@ -59,6 +60,7 @@ private slots:
void buttonBoxClicked(QAbstractButton *btn);
void setUiValues();
void autoRotateChanged(bool autoRotate);
void expandToRowsChanged(bool expand);
void fontButtonClicked();
void fontChanged(QString fontString);
void commandPlainTextEditChanged();
Expand Down