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 | |
16 | IconDialog::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 *>(object: QCoreApplication::instance())) { |
24 | m_dialog.reset(other: new KIconDialog()); |
25 | connect(sender: m_dialog.data(), signal: &KIconDialog::newIconName, context: this, slot: [this](const QString &newIconName) { |
26 | if (m_iconName != newIconName) { |
27 | m_iconName = newIconName; |
28 | Q_EMIT iconNameChanged(iconName: newIconName); |
29 | } |
30 | }); |
31 | |
32 | m_dialog->installEventFilter(filterObj: this); |
33 | } |
34 | } |
35 | |
36 | IconDialog::~IconDialog() |
37 | { |
38 | if (m_dialog) { |
39 | m_dialog->close(); |
40 | } |
41 | } |
42 | |
43 | QString IconDialog::iconName() const |
44 | { |
45 | return m_iconName; |
46 | } |
47 | |
48 | int IconDialog::iconSize() const |
49 | { |
50 | return m_iconSize; |
51 | } |
52 | |
53 | void IconDialog::setIconSize(int iconSize) |
54 | { |
55 | if (m_dialog->iconSize() != iconSize) { |
56 | m_iconSize = iconSize; |
57 | Q_EMIT iconSizeChanged(iconSize); |
58 | } |
59 | } |
60 | |
61 | QString IconDialog::title() const |
62 | { |
63 | return m_dialog->windowTitle(); |
64 | } |
65 | |
66 | void 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 | |
74 | bool IconDialog::user() const |
75 | { |
76 | return m_user; |
77 | } |
78 | |
79 | void IconDialog::setUser(bool user) |
80 | { |
81 | if (m_user != user) { |
82 | m_user = user; |
83 | Q_EMIT userChanged(user); |
84 | } |
85 | } |
86 | |
87 | QString IconDialog::customLocation() const |
88 | { |
89 | return m_customLocation; |
90 | } |
91 | |
92 | void 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 | |
102 | Qt::WindowModality IconDialog::modality() const |
103 | { |
104 | return m_modality; |
105 | } |
106 | |
107 | void IconDialog::setModality(Qt::WindowModality modality) |
108 | { |
109 | if (m_modality != modality) { |
110 | m_modality = modality; |
111 | Q_EMIT modalityChanged(modality); |
112 | } |
113 | } |
114 | |
115 | bool IconDialog::visible() const |
116 | { |
117 | return m_dialog->isVisible(); |
118 | } |
119 | |
120 | void IconDialog::setVisible(bool visible) |
121 | { |
122 | if (visible) { |
123 | open(); |
124 | } else { |
125 | close(); |
126 | } |
127 | } |
128 | |
129 | void IconDialog::open() |
130 | { |
131 | if (m_dialog->isVisible()) { |
132 | return; |
133 | } |
134 | |
135 | QQuickItem *parentItem = qobject_cast<QQuickItem *>(o: parent()); |
136 | QQuickWindow *parentWindow = (parentItem ? parentItem->window() : qobject_cast<QQuickWindow *>(object: 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(group: KIconLoader::Desktop, context: KIconLoader::Application, strictIconSize: false, iconSize: m_iconSize, user: m_user); |
151 | |
152 | m_dialog->show(); |
153 | } |
154 | |
155 | void IconDialog::close() |
156 | { |
157 | if (!m_dialog->isVisible()) { |
158 | return; |
159 | } |
160 | |
161 | m_dialog->hide(); |
162 | } |
163 | |
164 | bool 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" |
174 | |