on f87ef56 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
359 lines 9.9 kB history raw
1/*
2 * Copyright (C) 2014-2016 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "MirSurfaceItem.h"
19#include <paths.h>
21#include <QGuiApplication>
22#include <QQuickView>
23#include <QQmlContext>
24#include <QQmlProperty>
25#include <QQmlEngine>
26#include <QString>
28#include <QDebug>
30using namespace unity::shell::application;
32#define MIRSURFACEITEM_DEBUG 0
34#if MIRSURFACEITEM_DEBUG
35#define DEBUG_MSG(params) qDebug().nospace() << "MirSurfaceItem::" << __func__ << " " << params
36#else
37#define DEBUG_MSG(params) ((void)0)
38#endif
40MirSurfaceItem::MirSurfaceItem(QQuickItem *parent)
41 : MirSurfaceItemInterface(parent)
42 , m_qmlSurface(nullptr)
43 , m_qmlItem(nullptr)
44 , m_consumesInput(false)
45 , m_surfaceWidth(0)
46 , m_surfaceHeight(0)
47 , m_touchPressCount(0)
48 , m_touchReleaseCount(0)
49 , m_mousePressCount(0)
50 , m_mouseReleaseCount(0)
52 DEBUG_MSG((void*)(this) << name());
53 setAcceptedMouseButtons(Qt::LeftButton | Qt::MiddleButton | Qt::RightButton |
54 Qt::ExtraButton1 | Qt::ExtraButton2 | Qt::ExtraButton3 | Qt::ExtraButton4 |
55 Qt::ExtraButton5 | Qt::ExtraButton6 | Qt::ExtraButton7 | Qt::ExtraButton8 |
56 Qt::ExtraButton9 | Qt::ExtraButton10 | Qt::ExtraButton11 |
57 Qt::ExtraButton12 | Qt::ExtraButton13);
59 connect(this, &QQuickItem::activeFocusChanged, this, &MirSurfaceItem::updateMirSurfaceActiveFocus);
60 connect(this, &QQuickItem::visibleChanged, this, &MirSurfaceItem::updateMirSurfaceVisibility);
61 connect(this, &MirSurfaceItem::consumesInputChanged, this, [this]() {
62 updateMirSurfaceActiveFocus(hasActiveFocus());
63 });
66MirSurfaceItem::~MirSurfaceItem()
68 DEBUG_MSG((void*)(this) << name());
69 setSurface(nullptr);
72void MirSurfaceItem::printComponentErrors()
74 QList<QQmlError> errors = m_qmlContentComponent->errors();
75 for (int i = 0; i < errors.count(); ++i) {
76 qDebug() << errors[i];
77 }
80Mir::Type MirSurfaceItem::type() const
82 if (m_qmlSurface) {
83 return m_qmlSurface->type();
84 } else {
85 return Mir::UnknownType;
86 }
89Mir::State MirSurfaceItem::surfaceState() const
91 if (m_qmlSurface) {
92 return m_qmlSurface->state();
93 } else {
94 return Mir::UnknownState;
95 }
98QString MirSurfaceItem::name() const
100 if (m_qmlSurface) {
101 return m_qmlSurface->name();
102 } else {
103 return QString();
104 }
107bool MirSurfaceItem::live() const
109 if (m_qmlSurface) {
110 return m_qmlSurface->live();
111 } else {
112 return false;
113 }
116Mir::ShellChrome MirSurfaceItem::shellChrome() const
118 if (m_qmlSurface) {
119 return m_qmlSurface->shellChrome();
120 } else {
121 return Mir::NormalChrome;
122 }
125Mir::OrientationAngle MirSurfaceItem::orientationAngle() const
127 if (m_qmlSurface) {
128 return m_qmlSurface->orientationAngle();
129 } else {
130 return Mir::Angle0;
131 }
134void MirSurfaceItem::setOrientationAngle(Mir::OrientationAngle angle)
136 if (!m_qmlSurface)
137 return;
139 if (m_qmlSurface->orientationAngle() == angle)
140 return;
142 m_qmlSurface->setOrientationAngle(angle);
144 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
145 orientationProp.write(QVariant::fromValue(m_qmlSurface->orientationAngle()));
148void MirSurfaceItem::updateScreenshot(QUrl screenshotUrl)
150 if (m_qmlItem) {
151 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
152 screenshotSource.write(QVariant::fromValue(screenshotUrl));
153 }
156void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
158 if (status == QQmlComponent::Ready) {
159 createQmlContentItem();
160 }
163void MirSurfaceItem::createQmlContentItem()
165 DEBUG_MSG("");
167 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
168 m_qmlItem->setParentItem(this);
170 setImplicitWidth(m_qmlItem->implicitWidth());
171 setImplicitHeight(m_qmlItem->implicitHeight());
173 {
174 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
175 screenshotSource.write(QVariant::fromValue(m_qmlSurface->screenshotUrl()));
176 }
179void MirSurfaceItem::touchEvent(QTouchEvent * event)
181 if (event->type() == QEvent::TouchBegin) {
182 m_touchTrail.clear();
183 }
185 if (event->touchPointStates() & Qt::TouchPointPressed) {
186 ++m_touchPressCount;
187 Q_EMIT touchPressCountChanged(m_touchPressCount);
188 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
189 ++m_touchReleaseCount;
190 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
191 }
193 Q_FOREACH(QTouchEvent::TouchPoint touchPoint, event->touchPoints()) {
194 QString id(touchPoint.id());
195 QVariantList list = m_touchTrail[id].toList();
196 list.append(QVariant::fromValue(touchPoint.pos()));
197 if (list.count() > 100) list.pop_front();
198 m_touchTrail[id] = list;
199 }
201 if (m_qmlItem) {
202 QQmlProperty touchTrail(m_qmlItem, "touchTrail");
203 touchTrail.write(m_touchTrail);
204 }
207void MirSurfaceItem::mousePressEvent(QMouseEvent * event)
209 m_mousePressCount++;
210 Q_EMIT mousePressCountChanged(m_mousePressCount);
211 event->accept();
214void MirSurfaceItem::mouseMoveEvent(QMouseEvent * event)
216 event->accept();
219void MirSurfaceItem::mouseReleaseEvent(QMouseEvent * event)
221 m_mouseReleaseCount++;
222 Q_EMIT mouseReleaseCountChanged(m_mouseReleaseCount);
223 event->accept();
226void MirSurfaceItem::setSurface(MirSurfaceInterface* surface)
228 DEBUG_MSG("this=" << (void*)(this) << " name=" << name() << " surface=" << surface);
230 if (m_qmlSurface == surface) {
231 return;
232 }
234 if (m_qmlSurface) {
235 delete m_qmlItem;
236 m_qmlItem = nullptr;
238 delete m_qmlContentComponent;
239 m_qmlContentComponent = nullptr;
241 if (hasActiveFocus() && m_consumesInput && m_qmlSurface->live()) {
242 m_qmlSurface->setActiveFocus(false);
243 }
245 disconnect(m_qmlSurface, nullptr, this, nullptr);
246 m_qmlSurface->unregisterView((qintptr)this);
247 }
249 m_qmlSurface = static_cast<MirSurface*>(surface);
251 if (m_qmlSurface) {
252 m_qmlSurface->registerView((qintptr)this);
254 updateSurfaceSize();
255 updateMirSurfaceVisibility();
257 connect(m_qmlSurface, &MirSurface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
258 connect(m_qmlSurface, &MirSurface::screenshotUrlChanged, this, &MirSurfaceItem::updateScreenshot);
259 connect(m_qmlSurface, &MirSurface::liveChanged, this, &MirSurfaceItem::liveChanged);
260 connect(m_qmlSurface, &MirSurface::stateChanged, this, &MirSurfaceItem::surfaceStateChanged);
262 QUrl qmlComponentFilePath;
263 if (!m_qmlSurface->qmlFilePath().isEmpty()) {
264 qmlComponentFilePath = m_qmlSurface->qmlFilePath();
265 } else {
266 qmlComponentFilePath = QUrl("qrc:///Unity/Application/MirSurfaceItem.qml");
267 }
269 m_qmlContentComponent = new QQmlComponent(QQmlEngine::contextForObject(parent())->engine(), qmlComponentFilePath);
271 switch (m_qmlContentComponent->status()) {
272 case QQmlComponent::Ready:
273 createQmlContentItem();
274 break;
275 case QQmlComponent::Loading:
276 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
277 this, &MirSurfaceItem::onComponentStatusChanged);
278 break;
279 case QQmlComponent::Error:
280 printComponentErrors();
281 qFatal("MirSurfaceItem: failed to create content component.");
282 break;
283 default:
284 qFatal("MirSurfaceItem: Unhandled component status");
285 }
287 if (m_consumesInput) {
288 m_qmlSurface->setActiveFocus(hasActiveFocus());
289 }
290 }
292 Q_EMIT surfaceChanged(m_qmlSurface);
296void MirSurfaceItem::updateMirSurfaceActiveFocus(bool focused)
298 if (m_qmlSurface && m_consumesInput && m_qmlSurface->live()) {
299 m_qmlSurface->setActiveFocus(focused);
300 }
303void MirSurfaceItem::updateMirSurfaceVisibility()
305 if (!m_qmlSurface) return;
307 m_qmlSurface->setViewVisibility((qintptr)this, isVisible());
310void MirSurfaceItem::setConsumesInput(bool value)
312 if (m_consumesInput != value) {
313 m_consumesInput = value;
314 Q_EMIT consumesInputChanged(value);
315 }
318int MirSurfaceItem::surfaceWidth() const
320 return m_surfaceWidth;
323void MirSurfaceItem::setSurfaceWidth(int value)
325 if (value != -1 && m_surfaceWidth != value) {
326 m_surfaceWidth = value;
327 Q_EMIT surfaceWidthChanged(m_surfaceWidth);
328 updateSurfaceSize();
329 }
332int MirSurfaceItem::surfaceHeight() const
334 return m_surfaceHeight;
337void MirSurfaceItem::setSurfaceHeight(int value)
339 if (value != -1 && m_surfaceHeight != value) {
340 m_surfaceHeight = value;
341 Q_EMIT surfaceHeightChanged(m_surfaceHeight);
342 updateSurfaceSize();
343 }
346void MirSurfaceItem::updateSurfaceSize()
348 if (m_qmlSurface && m_surfaceWidth > 0 && m_surfaceHeight > 0) {
349 m_qmlSurface->resize(m_surfaceWidth, m_surfaceHeight);
350 }
353void MirSurfaceItem::setFillMode(FillMode value)
355 if (value != m_fillMode) {
356 m_fillMode = value;
357 Q_EMIT fillModeChanged(m_fillMode);
358 }