KDeclarative

quickviewsharedengine.cpp
1 /*
2  This file is part of the KDE libraries
3 
4  Copyright (C) 2015 Marco Martin <[email protected]>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 
21 */
22 
23 #include "quickviewsharedengine.h"
24 
25 #include <QUrl>
26 #include <QQmlEngine>
27 #include <QQmlContext>
28 #include <QQuickItem>
29 
30 #include <KLocalizedString>
31 #include <kdeclarative/qmlobjectsharedengine.h>
32 
33 #include <KPackage/Package>
34 #include <KPackage/PackageLoader>
35 
36 namespace KQuickAddons {
37 
38 class QuickViewSharedEnginePrivate
39 {
40 public:
41  QuickViewSharedEnginePrivate(QuickViewSharedEngine *module)
42  : q(module),
43  resizeMode(QuickViewSharedEngine::ResizeMode::SizeRootObjectToView),
44  initialSize(0, 0)
45  {
46  qmlObject = new KDeclarative::QmlObjectSharedEngine(q);
47  QObject::connect(qmlObject, &KDeclarative::QmlObject::statusChanged,
48  q, &QuickViewSharedEngine::statusChanged);
49  QObject::connect(qmlObject, SIGNAL(finished()),
50  q, SLOT(executionFinished()));
51  }
52 
53  void executionFinished();
54  void syncResizeMode();
55  void syncWidth();
56  void syncHeight();
57 
58 
59  QuickViewSharedEngine *q;
61  QuickViewSharedEngine::ResizeMode resizeMode;
62  QSize initialSize;
63 
64 };
65 
66 void QuickViewSharedEnginePrivate::executionFinished()
67 {
68  if (!qmlObject->rootObject()) {
69  return;
70  }
71 
72  QQuickItem *item = qobject_cast<QQuickItem *>(qmlObject->rootObject());
73 
74  if (!item) {
75  return;
76  }
77 
78  item->setParentItem(q->contentItem());
79  initialSize = QSize(item->width(), item ->height());
80 
81  if(q->size().isEmpty()) {
82  q->resize(initialSize);
83  q->contentItem()->setSize(initialSize);
84  }
85 
86  syncResizeMode();
87 }
88 
89 void QuickViewSharedEnginePrivate::syncResizeMode()
90 {
91  QQuickItem *item = qobject_cast<QQuickItem *>(qmlObject->rootObject());
92 
93  if (!item) {
94  return;
95  }
96 
97  if (resizeMode == QuickViewSharedEngine::SizeRootObjectToView) {
98  item->setSize(QSize(q->width(), q->height()));
99 
100  QObject::disconnect(item, SIGNAL(widthChanged()),
101  q, SLOT(syncWidth()));
102  QObject::disconnect(item, SIGNAL(heightChanged()),
103  q, SLOT(syncHeight()));
104 
105  } else {
106 
107  QObject::connect(item, SIGNAL(widthChanged()),
108  q, SLOT(syncWidth()));
109  QObject::connect(item, SIGNAL(heightChanged()),
110  q, SLOT(syncHeight()));
111 
112  syncWidth();
113  syncHeight();
114  }
115 }
116 
117 void QuickViewSharedEnginePrivate::syncWidth()
118 {
119  QQuickItem *item = qobject_cast<QQuickItem *>(qmlObject->rootObject());
120 
121  if (!item) {
122  return;
123  }
124 
125  q->setWidth(item->width());
126 }
127 
128 void QuickViewSharedEnginePrivate::syncHeight()
129 {
130  QQuickItem *item = qobject_cast<QQuickItem *>(qmlObject->rootObject());
131 
132  if (!item) {
133  return;
134  }
135 
136  q->setHeight(item->height());
137 }
138 
139 
140 
141 QuickViewSharedEngine::QuickViewSharedEngine(QWindow *parent)
142  : QQuickWindow(parent),
143  d(new QuickViewSharedEnginePrivate(this))
144 {
145 }
146 
147 QuickViewSharedEngine::~QuickViewSharedEngine()
148 {
149 }
150 
151 
153 {
154  d->qmlObject->setTranslationDomain(translationDomain);
155 }
156 
158 {
159  return d->qmlObject->translationDomain();
160 }
161 
162 QQmlEngine *QuickViewSharedEngine::engine() const
163 {
164  return d->qmlObject->engine();
165 }
166 
167 QList<QQmlError> QuickViewSharedEngine::errors() const
168 {
169  QList<QQmlError> errs;
170 
171  if (d->qmlObject->mainComponent()) {
172  errs = d->qmlObject->mainComponent()->errors();
173  }
174 
175  return errs;
176 }
177 
178 QSize QuickViewSharedEngine::sizeHint() const
179 {
180  QQuickItem *item = qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
181  if (!item) {
182  return QSize();
183  }
184 
185  const QSizeF implicitSize(item->implicitWidth(), item->implicitHeight());
186 
187  if (!implicitSize.isEmpty()) {
188  return implicitSize.toSize();
189  }
190 
191  return QSize(item->width(), item->height());
192 }
193 
194 QSize QuickViewSharedEngine::initialSize() const
195 {
196  return d->initialSize;
197 }
198 
199 QuickViewSharedEngine::ResizeMode QuickViewSharedEngine::resizeMode() const
200 {
201  return d->resizeMode;
202 }
203 
204 QQmlContext *QuickViewSharedEngine::rootContext() const
205 {
206  return d->qmlObject->rootContext();
207 }
208 
209 QQuickItem *QuickViewSharedEngine::rootObject() const
210 {
211  return qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
212 }
213 
214 void QuickViewSharedEngine::setResizeMode(ResizeMode mode)
215 {
216  if (d->resizeMode == mode) {
217  return;
218  }
219 
220  d->resizeMode = mode;
221 
222  emit resizeModeChanged(mode);
223 
224  QQuickItem *item = qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
225  if (!item) {
226  return;
227  }
228 
229  d->syncResizeMode();
230 }
231 
232 void QuickViewSharedEngine::setSource(const QUrl &url)
233 {
234  if (d->qmlObject->source() == url) {
235  return;
236  }
237 
238  d->qmlObject->setSource(url);
239  emit sourceChanged(url);
240 }
241 
242 QUrl QuickViewSharedEngine::source() const
243 {
244  return d->qmlObject->source();
245 }
246 
247 QQmlComponent::Status QuickViewSharedEngine::status() const
248 {
249  if (!d->qmlObject->mainComponent()) {
250  return QQmlComponent::Null;
251  }
252 
253  return QQmlComponent::Status(d->qmlObject->status());
254 }
255 
256 void QuickViewSharedEngine::resizeEvent(QResizeEvent *e)
257 {
258  QQuickItem *item = qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
259  if (item && d->resizeMode == SizeRootObjectToView) {
260  item->setSize(e->size());
261  }
262 
264 }
265 
266 }
267 
268 #include "moc_quickviewsharedengine.cpp"
269 
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-2020 The KDE developers.
Generated on Fri Aug 7 2020 22:37:05 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.