KDeclarative

quickviewsharedengine.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2015 Marco Martin <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "quickviewsharedengine.h"
9 
10 #include <QQmlEngine>
11 #include <QQmlContext>
12 #include <QQuickItem>
13 
14 #include <KLocalizedString>
15 #include <kdeclarative/qmlobjectsharedengine.h>
16 
17 #include <KPackage/Package>
18 #include <KPackage/PackageLoader>
19 
20 namespace KQuickAddons {
21 
22 class QuickViewSharedEnginePrivate
23 {
24 public:
25  QuickViewSharedEnginePrivate(QuickViewSharedEngine *module)
26  : q(module),
27  resizeMode(QuickViewSharedEngine::ResizeMode::SizeRootObjectToView),
28  initialSize(0, 0)
29  {
30  qmlObject = new KDeclarative::QmlObjectSharedEngine(q);
31  QObject::connect(qmlObject, &KDeclarative::QmlObject::statusChanged,
32  q, &QuickViewSharedEngine::statusChanged);
33  QObject::connect(qmlObject, SIGNAL(finished()),
34  q, SLOT(executionFinished()));
35  }
36 
37  void executionFinished();
38  void syncResizeMode();
39  void syncWidth();
40  void syncHeight();
41 
42 
43  QuickViewSharedEngine *q;
45  QuickViewSharedEngine::ResizeMode resizeMode;
46  QSize initialSize;
47 
48 };
49 
50 void QuickViewSharedEnginePrivate::executionFinished()
51 {
52  if (!qmlObject->rootObject()) {
53  return;
54  }
55 
56  QQuickItem *item = qobject_cast<QQuickItem *>(qmlObject->rootObject());
57 
58  if (!item) {
59  return;
60  }
61 
62  item->setParentItem(q->contentItem());
63  initialSize = QSize(item->width(), item ->height());
64 
65  if(q->size().isEmpty()) {
66  q->resize(initialSize);
67  q->contentItem()->setSize(initialSize);
68  }
69 
70  syncResizeMode();
71 }
72 
73 void QuickViewSharedEnginePrivate::syncResizeMode()
74 {
75  QQuickItem *item = qobject_cast<QQuickItem *>(qmlObject->rootObject());
76 
77  if (!item) {
78  return;
79  }
80 
81  if (resizeMode == QuickViewSharedEngine::SizeRootObjectToView) {
82  item->setSize(QSize(q->width(), q->height()));
83 
84  QObject::disconnect(item, SIGNAL(widthChanged()),
85  q, SLOT(syncWidth()));
86  QObject::disconnect(item, SIGNAL(heightChanged()),
87  q, SLOT(syncHeight()));
88 
89  } else {
90 
91  QObject::connect(item, SIGNAL(widthChanged()),
92  q, SLOT(syncWidth()));
93  QObject::connect(item, SIGNAL(heightChanged()),
94  q, SLOT(syncHeight()));
95 
96  syncWidth();
97  syncHeight();
98  }
99 }
100 
101 void QuickViewSharedEnginePrivate::syncWidth()
102 {
103  QQuickItem *item = qobject_cast<QQuickItem *>(qmlObject->rootObject());
104 
105  if (!item) {
106  return;
107  }
108 
109  q->setWidth(item->width());
110 }
111 
112 void QuickViewSharedEnginePrivate::syncHeight()
113 {
114  QQuickItem *item = qobject_cast<QQuickItem *>(qmlObject->rootObject());
115 
116  if (!item) {
117  return;
118  }
119 
120  q->setHeight(item->height());
121 }
122 
123 
124 
125 QuickViewSharedEngine::QuickViewSharedEngine(QWindow *parent)
126  : QQuickWindow(parent),
127  d(new QuickViewSharedEnginePrivate(this))
128 {
129 }
130 
131 QuickViewSharedEngine::~QuickViewSharedEngine()
132 {
133 }
134 
135 
137 {
138  d->qmlObject->setTranslationDomain(translationDomain);
139 }
140 
142 {
143  return d->qmlObject->translationDomain();
144 }
145 
146 QQmlEngine *QuickViewSharedEngine::engine() const
147 {
148  return d->qmlObject->engine();
149 }
150 
151 QList<QQmlError> QuickViewSharedEngine::errors() const
152 {
153  QList<QQmlError> errs;
154 
155  if (d->qmlObject->mainComponent()) {
156  errs = d->qmlObject->mainComponent()->errors();
157  }
158 
159  return errs;
160 }
161 
162 QSize QuickViewSharedEngine::sizeHint() const
163 {
164  QQuickItem *item = qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
165  if (!item) {
166  return QSize();
167  }
168 
169  const QSizeF implicitSize(item->implicitWidth(), item->implicitHeight());
170 
171  if (!implicitSize.isEmpty()) {
172  return implicitSize.toSize();
173  }
174 
175  return QSize(item->width(), item->height());
176 }
177 
178 QSize QuickViewSharedEngine::initialSize() const
179 {
180  return d->initialSize;
181 }
182 
183 QuickViewSharedEngine::ResizeMode QuickViewSharedEngine::resizeMode() const
184 {
185  return d->resizeMode;
186 }
187 
188 QQmlContext *QuickViewSharedEngine::rootContext() const
189 {
190  return d->qmlObject->rootContext();
191 }
192 
193 QQuickItem *QuickViewSharedEngine::rootObject() const
194 {
195  return qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
196 }
197 
198 void QuickViewSharedEngine::setResizeMode(ResizeMode mode)
199 {
200  if (d->resizeMode == mode) {
201  return;
202  }
203 
204  d->resizeMode = mode;
205 
206  Q_EMIT resizeModeChanged(mode);
207 
208  QQuickItem *item = qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
209  if (!item) {
210  return;
211  }
212 
213  d->syncResizeMode();
214 }
215 
216 void QuickViewSharedEngine::setSource(const QUrl &url)
217 {
218  if (d->qmlObject->source() == url) {
219  return;
220  }
221 
222  d->qmlObject->setSource(url);
223  Q_EMIT sourceChanged(url);
224 }
225 
226 QUrl QuickViewSharedEngine::source() const
227 {
228  return d->qmlObject->source();
229 }
230 
231 QQmlComponent::Status QuickViewSharedEngine::status() const
232 {
233  if (!d->qmlObject->mainComponent()) {
234  return QQmlComponent::Null;
235  }
236 
237  return QQmlComponent::Status(d->qmlObject->status());
238 }
239 
240 void QuickViewSharedEngine::resizeEvent(QResizeEvent *e)
241 {
242  QQuickItem *item = qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
243  if (item && d->resizeMode == SizeRootObjectToView) {
244  item->setSize(e->size());
245  }
246 
248 }
249 
250 }
251 
252 #include "moc_quickviewsharedengine.cpp"
253 
void setParentItem(QQuickItem *parent)
virtual void resizeEvent(QResizeEvent *ev) override
void setTranslationDomain(const QString &translationDomain)
Installs a translation domain for all i18n global functions.
An object that instantiates an entire QML context, with its own declarative engine.
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QSize toSize() const const
const QSize & size() const const
void setSize(const QSizeF &size)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jan 27 2021 22:38:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.