From f384ab7e4d4ffbf48af90ff8edd3ec5ca823afeb Mon Sep 17 00:00:00 2001 From: tsujan Date: Sat, 12 Mar 2022 21:46:10 +0330 Subject: [PATCH] Added options for button label and style of Directory Menu (#1731) Closes https://github.com/lxqt/lxqt-panel/issues/1730 Also, two bugs are found and fixed here. --- plugin-directorymenu/directorymenu.cpp | 25 +++++-- .../directorymenuconfiguration.cpp | 65 +++++++++++++++++-- .../directorymenuconfiguration.h | 1 + .../directorymenuconfiguration.ui | 34 +++++++++- 4 files changed, 114 insertions(+), 11 deletions(-) diff --git a/plugin-directorymenu/directorymenu.cpp b/plugin-directorymenu/directorymenu.cpp index 494abd4c5..b17420901 100644 --- a/plugin-directorymenu/directorymenu.cpp +++ b/plugin-directorymenu/directorymenu.cpp @@ -159,20 +159,37 @@ void DirectoryMenu::settingsChanged() { mBaseDirectory.setPath(settings()->value(QStringLiteral("baseDirectory"), QDir::homePath()).toString()); + // icon + bool customIcon = false; QString iconPath = settings()->value(QStringLiteral("icon"), QString()).toString(); QIcon icon = QIcon(iconPath); - if(!icon.isNull()) { QIcon buttonIcon = QIcon(icon); if(!buttonIcon.pixmap(QSize(24,24)).isNull()) { mButton.setIcon(buttonIcon); - return; + customIcon = true; } } - - mButton.setIcon(mDefaultIcon); + if (!customIcon) + mButton.setIcon(mDefaultIcon); + + // label + QString label = settings()->value(QStringLiteral("label"), QString()).toString(); + mButton.setText(label); + + // style + QString style = settings()->value(QStringLiteral("buttonStyle")).toString().toUpper(); + if (style == QStringLiteral("ICON")) + mButton.setToolButtonStyle(Qt::ToolButtonIconOnly); + else if (!label.isEmpty()) + { + if (style == QStringLiteral("TEXT")) + mButton.setToolButtonStyle(Qt::ToolButtonTextOnly); + else + mButton.setToolButtonStyle(Qt::ToolButtonTextBesideIcon); + } // Set default terminal mDefaultTerminal = settings()->value(QStringLiteral("defaultTerminal"), QString()).toString(); diff --git a/plugin-directorymenu/directorymenuconfiguration.cpp b/plugin-directorymenu/directorymenuconfiguration.cpp index 13b00eb73..66c2e4c20 100644 --- a/plugin-directorymenu/directorymenuconfiguration.cpp +++ b/plugin-directorymenu/directorymenuconfiguration.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include @@ -50,11 +51,17 @@ DirectoryMenuConfiguration::DirectoryMenuConfiguration(PluginSettings *settings, connect(ui->buttons, &QDialogButtonBox::clicked, this, &DirectoryMenuConfiguration::dialogButtonsAction); + ui->buttonStyleCB->addItem(tr("Only icon"), QLatin1String("Icon")); + ui->buttonStyleCB->addItem(tr("Only text"), QLatin1String("Text")); + ui->buttonStyleCB->addItem(tr("Icon and text"), QLatin1String("IconText")); + connect(ui->buttonStyleCB, QOverload::of(&QComboBox::activated), this, &DirectoryMenuConfiguration::saveSettings); + loadSettings(); ui->baseDirectoryB->setIcon(mDefaultIcon); connect(ui->baseDirectoryB, &QPushButton::clicked, this, &DirectoryMenuConfiguration::showDirectoryDialog); connect(ui->iconB, &QPushButton::clicked, this, &DirectoryMenuConfiguration::showIconDialog); + connect(ui->labelB, &QPushButton::clicked, this, &DirectoryMenuConfiguration::showLabelDialog); connect(ui->terminalB, &QPushButton::clicked, this, &DirectoryMenuConfiguration::showTermDialog); } @@ -67,7 +74,8 @@ void DirectoryMenuConfiguration::loadSettings() { mBaseDirectory.setPath(settings().value(QStringLiteral("baseDirectory"), QDir::homePath()).toString()); ui->baseDirectoryB->setText(mBaseDirectory.dirName()); - + // icon + bool iconSet = false; mIcon = settings().value(QStringLiteral("icon"), QString()).toString(); if(!mIcon.isNull()) { @@ -75,11 +83,20 @@ void DirectoryMenuConfiguration::loadSettings() if(!buttonIcon.pixmap(QSize(24,24)).isNull()) { ui->iconB->setIcon(buttonIcon); - return; + iconSet = true; } } + if (!iconSet) + ui->iconB->setIcon(mDefaultIcon); + + // label + ui->labelB->setText(settings().value(QStringLiteral("label"), QString()).toString()); - ui->iconB->setIcon(mDefaultIcon); + // style + int index = ui->buttonStyleCB->findData(settings().value(QStringLiteral("buttonStyle"), QLatin1String("Icon"))); + if (index == -1) + index = 0; + ui->buttonStyleCB->setCurrentIndex(index); ui->terminalB->setText(settings().value(QStringLiteral("defaultTerminal"), QString()).toString()); } @@ -88,6 +105,8 @@ void DirectoryMenuConfiguration::saveSettings() { settings().setValue(QStringLiteral("baseDirectory"), mBaseDirectory.absolutePath()); settings().setValue(QStringLiteral("icon"), mIcon); + settings().setValue(QStringLiteral("label"), ui->labelB->text()); + settings().setValue(QStringLiteral("buttonStyle"), ui->buttonStyleCB->itemData(ui->buttonStyleCB->currentIndex())); settings().setValue(QStringLiteral("defaultTerminal"), mDefaultTerminal); } @@ -112,7 +131,7 @@ void DirectoryMenuConfiguration::showTermDialog() QFileDialog d(this, tr("Choose Default Terminal"), QStringLiteral("/usr/bin")); d.setFileMode(QFileDialog::ExistingFile); d.setWindowModality(Qt::WindowModal); - + if (d.exec() && !d.selectedFiles().isEmpty()) { mDefaultTerminal = d.selectedFiles().constFirst(); @@ -123,7 +142,26 @@ void DirectoryMenuConfiguration::showTermDialog() void DirectoryMenuConfiguration::showIconDialog() { - QFileDialog d(this, tr("Choose Icon"), QDir::homePath(), tr("Icons (*.png *.xpm *.jpg)")); + // prefer the icon theme folder and give priority to the "places" folder + QString iconDir; + QString iconThemeName = QIcon::themeName(); + const auto icons = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, + QStringLiteral("icons"), + QStandardPaths::LocateDirectory); + for (const auto& icon : icons) + { + QString iconThemeFolder = icon + QLatin1String("/") + iconThemeName; + if (QDir(iconThemeFolder).exists() && QFileInfo(iconThemeFolder).permission(QFileDevice::ReadUser)) + { + const QString places = iconThemeFolder + QLatin1String("/places"); + if (QDir(places).exists() && QFileInfo(places).permission(QFileDevice::ReadUser)) + iconDir = places; + else + iconDir = iconThemeFolder; + break; + } + } + QFileDialog d(this, tr("Choose Icon"), iconDir, tr("Icons (*.png *.xpm *.jpg *.svg)")); d.setWindowModality(Qt::WindowModal); if(d.exec() && !d.selectedFiles().isEmpty()) @@ -141,3 +179,20 @@ void DirectoryMenuConfiguration::showIconDialog() saveSettings(); } } + +void DirectoryMenuConfiguration::showLabelDialog() +{ + QInputDialog d(this); + d.setWindowModality(Qt::WindowModal); + d.setInputMode(QInputDialog::TextInput); + d.setWindowTitle(tr("Choose Label")); + d.setLabelText(tr("Label:")); + QString label = settings().value(QStringLiteral("label"), QString()).toString(); + if (!label.isEmpty()) + d.setTextValue(label); + if (d.exec()) + { + ui->labelB->setText(d.textValue()); + saveSettings(); + } +} diff --git a/plugin-directorymenu/directorymenuconfiguration.h b/plugin-directorymenu/directorymenuconfiguration.h index 673b0cac8..188a74b15 100644 --- a/plugin-directorymenu/directorymenuconfiguration.h +++ b/plugin-directorymenu/directorymenuconfiguration.h @@ -69,6 +69,7 @@ private slots: void saveSettings(); void showDirectoryDialog(); void showIconDialog(); + void showLabelDialog(); void showTermDialog(); private: diff --git a/plugin-directorymenu/directorymenuconfiguration.ui b/plugin-directorymenu/directorymenuconfiguration.ui index 291b6028f..3613ad1bb 100644 --- a/plugin-directorymenu/directorymenuconfiguration.ui +++ b/plugin-directorymenu/directorymenuconfiguration.ui @@ -61,19 +61,49 @@ - + - Terminal + Label + + + + 0 + 0 + + + + + + + + + + + Button style + + + + + + + Choose Default Terminal + + + + Terminal + + +