KIconThemes

icondialog.cpp
1/*
2 SPDX-FileCopyrightText: 2015 Kai Uwe Broulik <kde@privat.broulik.de>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "icondialog_p.h"
8
9#include <QQuickItem>
10#include <QQuickWindow>
11
12#include <KIconDialog>
13#include <KIconLoader>
14#include <QApplication>
15
16IconDialog::IconDialog(QObject *parent)
17 : QObject(parent)
18 , m_dialog(nullptr)
19 , m_iconSize(0)
20 , m_user(false)
21 , m_modality(Qt::WindowModal)
22{
23 if (qobject_cast<QApplication *>(QCoreApplication::instance())) {
24 m_dialog.reset(new KIconDialog());
25 connect(m_dialog.data(), &KIconDialog::newIconName, this, [this](const QString &newIconName) {
26 if (m_iconName != newIconName) {
27 m_iconName = newIconName;
28 Q_EMIT iconNameChanged(newIconName);
29 }
30 });
31
32 m_dialog->installEventFilter(this);
33 }
34}
35
36IconDialog::~IconDialog()
37{
38 if (m_dialog) {
39 m_dialog->close();
40 }
41}
42
43QString IconDialog::iconName() const
44{
45 return m_iconName;
46}
47
48int IconDialog::iconSize() const
49{
50 return m_iconSize;
51}
52
53void IconDialog::setIconSize(int iconSize)
54{
55 if (m_dialog->iconSize() != iconSize) {
56 m_iconSize = iconSize;
57 Q_EMIT iconSizeChanged(iconSize);
58 }
59}
60
61QString IconDialog::title() const
62{
63 return m_dialog->windowTitle();
64}
65
66void IconDialog::setTitle(const QString &title)
67{
68 if (m_dialog->windowTitle() != title) {
69 m_dialog->setWindowTitle(title);
70 Q_EMIT titleChanged(title);
71 }
72}
73
74bool IconDialog::user() const
75{
76 return m_user;
77}
78
79void IconDialog::setUser(bool user)
80{
81 if (m_user != user) {
82 m_user = user;
83 Q_EMIT userChanged(user);
84 }
85}
86
87QString IconDialog::customLocation() const
88{
89 return m_customLocation;
90}
91
92void IconDialog::setCustomLocation(const QString &customLocation)
93{
94 if (m_customLocation != customLocation) {
95 m_dialog->setCustomLocation(customLocation);
96
97 m_customLocation = customLocation;
98 Q_EMIT customLocationChanged(customLocation);
99 }
100}
101
102Qt::WindowModality IconDialog::modality() const
103{
104 return m_modality;
105}
106
107void IconDialog::setModality(Qt::WindowModality modality)
108{
109 if (m_modality != modality) {
110 m_modality = modality;
111 Q_EMIT modalityChanged(modality);
112 }
113}
114
115bool IconDialog::visible() const
116{
117 return m_dialog->isVisible();
118}
119
120void IconDialog::setVisible(bool visible)
121{
122 if (visible) {
123 open();
124 } else {
125 close();
126 }
127}
128
129void IconDialog::open()
130{
131 if (m_dialog->isVisible()) {
132 return;
133 }
134
135 QQuickItem *parentItem = qobject_cast<QQuickItem *>(parent());
136 QQuickWindow *parentWindow = (parentItem ? parentItem->window() : qobject_cast<QQuickWindow *>(parent()));
137
138 if (m_modality == Qt::NonModal) {
139 m_dialog->setModal(false);
140 } else if (m_modality == Qt::WindowModal) {
141 m_dialog->winId(); // needed to get the windowHandle prior to showing
142 m_dialog->windowHandle()->setTransientParent(parentWindow);
143 m_dialog->setModal(false); // WindowModal does not unset the overall modality
144 } else if (m_modality == Qt::ApplicationModal) {
145 m_dialog->setModal(true);
146 }
147
148 m_dialog->setWindowModality(m_modality);
149
150 m_dialog->setup(KIconLoader::Desktop, KIconLoader::Application, false, m_iconSize, m_user);
151
152 m_dialog->show();
153}
154
155void IconDialog::close()
156{
157 if (!m_dialog->isVisible()) {
158 return;
159 }
160
161 m_dialog->hide();
162}
163
164bool IconDialog::eventFilter(QObject *watched, QEvent *event)
165{
166 if (watched == m_dialog.data() && (event->type() == QEvent::Show || event->type() == QEvent::Hide)) {
167 Q_EMIT visibleChanged();
168 }
169
170 return false;
171}
172
173#include "moc_icondialog_p.cpp"
Dialog for interactive selection of icons.
Definition kicondialog.h:33
@ Desktop
Desktop icons.
@ Application
An icon that represents an application.
Definition kiconloader.h:83
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
const QList< QKeySequence > & close()
const QList< QKeySequence > & open()
QCoreApplication * instance()
void installEventFilter(QObject *filterObj)
QQuickWindow * window() const const
WindowModality
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void show()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.