KDeclarative

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

KDE's Doxygen guidelines are available online.