Skip to content

Commit

Permalink
Added options for button label and style of Directory Menu (#1731)
Browse files Browse the repository at this point in the history
Closes #1730

Also, two bugs are found and fixed here.
  • Loading branch information
tsujan authored Mar 12, 2022
1 parent 86dd6fa commit f384ab7
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 11 deletions.
25 changes: 21 additions & 4 deletions plugin-directorymenu/directorymenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
65 changes: 60 additions & 5 deletions plugin-directorymenu/directorymenuconfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <QInputDialog>
#include <QFileDialog>
#include <QMessageBox>
#include <QStandardPaths>

#include <XdgIcon>

Expand All @@ -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<int>::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);
}

Expand All @@ -67,19 +74,29 @@ 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())
{
QIcon buttonIcon = QIcon(mIcon);
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());
}
Expand All @@ -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);
}

Expand All @@ -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();
Expand All @@ -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())
Expand All @@ -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();
}
}
1 change: 1 addition & 0 deletions plugin-directorymenu/directorymenuconfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private slots:
void saveSettings();
void showDirectoryDialog();
void showIconDialog();
void showLabelDialog();
void showTermDialog();

private:
Expand Down
34 changes: 32 additions & 2 deletions plugin-directorymenu/directorymenuconfiguration.ui
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,49 @@
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="terminalL">
<widget class="QLabel" name="label">
<property name="text">
<string>Terminal</string>
<string>Label</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="labelB">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Button style</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="buttonStyleCB"/>
</item>
<item row="4" column="1">
<widget class="QPushButton" name="terminalB">
<property name="text">
<string>Choose Default Terminal</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="terminalL">
<property name="text">
<string>Terminal</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit f384ab7

Please sign in to comment.