summaryrefslogtreecommitdiffstats
path: root/src/designer
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2024-02-09 13:53:42 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2024-04-15 17:40:39 +0200
commitc1276e62ff75e71e87c281c9a93c8491ab306b58 (patch)
treea233209faa83a622275717bb94bdf3976ce46540 /src/designer
parent141ef147527ba46c16ba7439a5b4f78e7fc2e613 (diff)
Qt Designer: Add IconThemeEnumEditor and dialog
Task-number: QTBUG-121823 Pick-to: 6.7 Change-Id: I4c838b469f925ab924d94520cb786114c86e3182 Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Diffstat (limited to 'src/designer')
-rw-r--r--src/designer/src/components/propertyeditor/pixmapeditor.cpp43
-rw-r--r--src/designer/src/components/propertyeditor/pixmapeditor.h12
-rw-r--r--src/designer/src/lib/shared/iconselector.cpp107
-rw-r--r--src/designer/src/lib/shared/iconselector_p.h27
4 files changed, 160 insertions, 29 deletions
diff --git a/src/designer/src/components/propertyeditor/pixmapeditor.cpp b/src/designer/src/components/propertyeditor/pixmapeditor.cpp
index 19f5a428a..0a8e2d905 100644
--- a/src/designer/src/components/propertyeditor/pixmapeditor.cpp
+++ b/src/designer/src/components/propertyeditor/pixmapeditor.cpp
@@ -32,23 +32,27 @@ static constexpr QSize ICON_SIZE{16, 16};
namespace qdesigner_internal {
-IconThemeDialog::IconThemeDialog(QWidget *parent)
- : QDialog(parent)
+static void createIconThemeDialog(QDialog *topLevel, const QString &labelText,
+ QWidget *themeEditor)
{
- setWindowTitle(tr("Set Icon From Theme"));
-
- QVBoxLayout *layout = new QVBoxLayout(this);
- QLabel *label = new QLabel(tr("Select icon name from theme:"), this);
- m_editor = new IconThemeEditor(this);
- QDialogButtonBox *buttons = new QDialogButtonBox(this);
+ QVBoxLayout *layout = new QVBoxLayout(topLevel);
+ QLabel *label = new QLabel(labelText, topLevel);
+ QDialogButtonBox *buttons = new QDialogButtonBox(topLevel);
buttons->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
+ QObject::connect(buttons, &QDialogButtonBox::accepted, topLevel, &QDialog::accept);
+ QObject::connect(buttons, &QDialogButtonBox::rejected, topLevel, &QDialog::reject);
layout->addWidget(label);
- layout->addWidget(m_editor);
+ layout->addWidget(themeEditor);
layout->addWidget(buttons);
+}
- connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
- connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
+IconThemeDialog::IconThemeDialog(QWidget *parent)
+ : QDialog(parent)
+{
+ setWindowTitle(tr("Set Icon From XDG Theme"));
+ m_editor = new IconThemeEditor(this);
+ createIconThemeDialog(this, tr("Select icon name from XDG theme:"), m_editor);
}
std::optional<QString> IconThemeDialog::getTheme(QWidget *parent, const QString &theme)
@@ -60,6 +64,23 @@ std::optional<QString> IconThemeDialog::getTheme(QWidget *parent, const QString
return std::nullopt;
}
+IconThemeEnumDialog::IconThemeEnumDialog(QWidget *parent)
+ : QDialog(parent)
+{
+ setWindowTitle(tr("Set Icon From Theme"));
+ m_editor = new IconThemeEnumEditor(this);
+ createIconThemeDialog(this, tr("Select icon name from theme:"), m_editor);
+}
+
+std::optional<int> IconThemeEnumDialog::getTheme(QWidget *parent, int theme)
+{
+ IconThemeEnumDialog dlg(parent);
+ dlg.m_editor->setThemeEnum(theme);
+ if (dlg.exec() == QDialog::Accepted)
+ return dlg.m_editor->themeEnum();
+ return std::nullopt;
+}
+
PixmapEditor::PixmapEditor(QDesignerFormEditorInterface *core, QWidget *parent) :
QWidget(parent),
m_iconThemeModeEnabled(false),
diff --git a/src/designer/src/components/propertyeditor/pixmapeditor.h b/src/designer/src/components/propertyeditor/pixmapeditor.h
index ed5f918f0..8ed334830 100644
--- a/src/designer/src/components/propertyeditor/pixmapeditor.h
+++ b/src/designer/src/components/propertyeditor/pixmapeditor.h
@@ -22,6 +22,7 @@ namespace qdesigner_internal {
class DesignerPixmapCache;
class IconThemeEditor;
+class IconThemeEnumEditor;
class PropertySheetIconValue;
class IconThemeDialog : public QDialog
@@ -34,6 +35,17 @@ private:
IconThemeEditor *m_editor;
};
+class IconThemeEnumDialog : public QDialog
+{
+ Q_OBJECT
+public:
+ static std::optional<int> getTheme(QWidget *parent, int theme);
+
+private:
+ IconThemeEnumDialog(QWidget *parent);
+ IconThemeEnumEditor *m_editor;
+};
+
class PixmapEditor : public QWidget
{
Q_OBJECT
diff --git a/src/designer/src/lib/shared/iconselector.cpp b/src/designer/src/lib/shared/iconselector.cpp
index c250842dd..60c404423 100644
--- a/src/designer/src/lib/shared/iconselector.cpp
+++ b/src/designer/src/lib/shared/iconselector.cpp
@@ -14,7 +14,9 @@
#include <QtDesigner/abstractlanguage.h>
#include <QtDesigner/abstractintegration.h>
#include <QtDesigner/qextensionmanager.h>
+#include <QtDesigner/private/resourcebuilder_p.h>
+#include <QtWidgets/qabstractitemview.h>
#include <QtWidgets/qtoolbutton.h>
#include <QtWidgets/qcombobox.h>
#include <QtWidgets/qdialogbuttonbox.h>
@@ -34,12 +36,38 @@
#include <QtCore/qdebug.h>
#include <QtCore/qlist.h>
+#include <utility>
+
QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
namespace qdesigner_internal {
+using ThemeIconEnumEntry = std::pair<QString, QIcon>;
+
+static const QList<ThemeIconEnumEntry> &themeEnumIcons()
+{
+ static QList<ThemeIconEnumEntry> result;
+ if (result.isEmpty()) {
+ const QStringList &names = QResourceBuilder::themeIconNames();
+ result.reserve(names.size());
+ for (qsizetype i = 0, size = names.size(); i < size; ++i)
+ result.append({names.at(i), QIcon::fromTheme(QIcon::ThemeIcon(i))});
+ }
+ return result;
+}
+
+static void initThemeCombo(QComboBox *cb)
+{
+ cb->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
+
+ for (const auto &te : themeEnumIcons())
+ cb->addItem(te.second, te.first);
+
+ cb->setCurrentIndex(-1);
+}
+
// Validator for theme line edit, accepts empty or non-blank strings.
class BlankSuppressingValidator : public QValidator {
public:
@@ -519,40 +547,40 @@ static const QMap<QString, QIcon> &themeIcons()
}
struct IconThemeEditorPrivate {
- IconThemeEditorPrivate();
+ void create(QWidget *topLevel, bool wantResetButton);
- QComboBox *m_themeComboBox;
+ QComboBox *m_themeComboBox{};
+ QToolButton *m_themeResetButton{};
};
-IconThemeEditorPrivate::IconThemeEditorPrivate() :
- m_themeComboBox(new QComboBox)
+void IconThemeEditorPrivate::create(QWidget *topLevel, bool wantResetButton)
{
+ m_themeComboBox = new QComboBox();
+ QHBoxLayout *mainHLayout = new QHBoxLayout(topLevel);
+ mainHLayout->setContentsMargins({});
+ mainHLayout->addWidget(m_themeComboBox);
+ if (wantResetButton) {
+ m_themeResetButton = new QToolButton;
+ m_themeResetButton->setIcon(createIconSet("resetproperty.png"_L1));
+ mainHLayout->addWidget(m_themeResetButton);
+ }
+ topLevel->setFocusProxy(m_themeComboBox);
}
IconThemeEditor::IconThemeEditor(QWidget *parent, bool wantResetButton) :
QWidget (parent), d(new IconThemeEditorPrivate)
{
- QHBoxLayout *mainHLayout = new QHBoxLayout;
- mainHLayout->setContentsMargins(QMargins());
+ d->create(this, wantResetButton);
+ d->m_themeComboBox->setEditable(true);
const auto icons = themeIcons();
for (auto i = icons.constBegin(); i != icons.constEnd(); ++i)
d->m_themeComboBox->addItem(i.value(), i.key());
d->m_themeComboBox->setCurrentIndex(-1);
- d->m_themeComboBox->setEditable(true);
d->m_themeComboBox->lineEdit()->setValidator(new BlankSuppressingValidator(this));
connect(d->m_themeComboBox, &QComboBox::currentTextChanged, this, &IconThemeEditor::edited);
- mainHLayout->addWidget(d->m_themeComboBox);
-
- if (wantResetButton) {
- QToolButton *themeResetButton = new QToolButton;
- themeResetButton->setIcon(createIconSet("resetproperty.png"_L1));
- connect(themeResetButton, &QAbstractButton::clicked, this, &IconThemeEditor::reset);
- mainHLayout->addWidget(themeResetButton);
- }
-
- setLayout(mainHLayout);
- setFocusProxy(d->m_themeComboBox);
+ if (wantResetButton)
+ connect(d->m_themeResetButton, &QAbstractButton::clicked, this, &IconThemeEditor::reset);
}
IconThemeEditor::~IconThemeEditor() = default;
@@ -573,6 +601,49 @@ void IconThemeEditor::setTheme(const QString &t)
d->m_themeComboBox->setCurrentText(t);
}
+IconThemeEnumEditor::IconThemeEnumEditor(QWidget *parent, bool wantResetButton) :
+ QWidget (parent), d(new IconThemeEditorPrivate)
+{
+ d->create(this, wantResetButton);
+ initThemeCombo(d->m_themeComboBox);
+
+ connect(d->m_themeComboBox, &QComboBox::currentIndexChanged,
+ this, &IconThemeEnumEditor::edited);
+ if (wantResetButton)
+ connect(d->m_themeResetButton, &QAbstractButton::clicked, this, &IconThemeEnumEditor::reset);
+}
+
+IconThemeEnumEditor::~IconThemeEnumEditor() = default;
+
+void IconThemeEnumEditor::reset()
+{
+ d->m_themeComboBox->setCurrentIndex(-1);
+ emit edited(-1);
+}
+
+int IconThemeEnumEditor::themeEnum() const
+{
+ return d->m_themeComboBox->currentIndex();
+}
+
+void IconThemeEnumEditor::setThemeEnum(int t)
+{
+ Q_ASSERT(t >= -1 && t < int(QIcon::ThemeIcon::NThemeIcons));
+ d->m_themeComboBox->setCurrentIndex(t);
+}
+
+QString IconThemeEnumEditor::iconName(int e)
+{
+ return QResourceBuilder::themeIconNames().value(e);
+}
+
+QComboBox *IconThemeEnumEditor::createComboBox(QWidget *parent)
+{
+ auto *result = new QComboBox(parent);
+ initThemeCombo(result);
+ return result;
+}
+
} // qdesigner_internal
QT_END_NAMESPACE
diff --git a/src/designer/src/lib/shared/iconselector_p.h b/src/designer/src/lib/shared/iconselector_p.h
index 6315314e6..4a4238e66 100644
--- a/src/designer/src/lib/shared/iconselector_p.h
+++ b/src/designer/src/lib/shared/iconselector_p.h
@@ -25,6 +25,8 @@
QT_BEGIN_NAMESPACE
+class QComboBox;
+
class QtResourceModel;
class QDesignerFormEditorInterface;
class QDesignerDialogGuiInterface;
@@ -111,6 +113,31 @@ private:
QScopedPointer<IconThemeEditorPrivate> d;
};
+// IconThemeEnumEditor: Let's the user input theme icon enum values
+// (QIcon::ThemeIcon) and shows a preview label. -1 means nothing selected.
+class QDESIGNER_SHARED_EXPORT IconThemeEnumEditor : public QWidget
+{
+ Q_OBJECT
+public:
+ explicit IconThemeEnumEditor(QWidget *parent = nullptr, bool wantResetButton = true);
+ ~IconThemeEnumEditor() override;
+
+ int themeEnum() const;
+ void setThemeEnum(int);
+
+ static QString iconName(int e);
+ static QComboBox *createComboBox(QWidget *parent = nullptr);
+
+signals:
+ void edited(int);
+
+public slots:
+ void reset();
+
+private:
+ QScopedPointer<IconThemeEditorPrivate> d;
+};
+
} // namespace qdesigner_internal
QT_END_NAMESPACE