on e7c8b2c help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
360 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_qmlContentComponent(nullptr)
44 , m_qmlItem(nullptr)
45 , m_consumesInput(false)
46 , m_surfaceWidth(0)
47 , m_surfaceHeight(0)
48 , m_touchPressCount(0)
49 , m_touchReleaseCount(0)
50 , m_mousePressCount(0)
51 , m_mouseReleaseCount(0)
53 DEBUG_MSG((void*)(this) << name());
54 setAcceptedMouseButtons(Qt::LeftButton | Qt::MiddleButton | Qt::RightButton |
55 Qt::ExtraButton1 | Qt::ExtraButton2 | Qt::ExtraButton3 | Qt::ExtraButton4 |
56 Qt::ExtraButton5 | Qt::ExtraButton6 | Qt::ExtraButton7 | Qt::ExtraButton8 |
57 Qt::ExtraButton9 | Qt::ExtraButton10 | Qt::ExtraButton11 |
58 Qt::ExtraButton12 | Qt::ExtraButton13);
60 connect(this, &QQuickItem::activeFocusChanged, this, &MirSurfaceItem::updateMirSurfaceActiveFocus);
61 connect(this, &QQuickItem::visibleChanged, this, &MirSurfaceItem::updateMirSurfaceVisibility);
62 connect(this, &MirSurfaceItem::consumesInputChanged, this, [this]() {
63 updateMirSurfaceActiveFocus(hasActiveFocus());
64 });
67MirSurfaceItem::~MirSurfaceItem()
69 DEBUG_MSG((void*)(this) << name());
70 setSurface(nullptr);
73void MirSurfaceItem::printComponentErrors()
75 QList<QQmlError> errors = m_qmlContentComponent->errors();
76 for (int i = 0; i < errors.count(); ++i) {
77 qDebug() << errors[i];
78 }
81Mir::Type MirSurfaceItem::type() const
83 if (m_qmlSurface) {
84 return m_qmlSurface->type();
85 } else {
86 return Mir::UnknownType;
87 }
90Mir::State MirSurfaceItem::surfaceState() const
92 if (m_qmlSurface) {
93 return m_qmlSurface->state();
94 } else {
95 return Mir::UnknownState;
96 }
99QString MirSurfaceItem::name() const
101 if (m_qmlSurface) {
102 return m_qmlSurface->name();
103 } else {
104 return QString();
105 }
108bool MirSurfaceItem::live() const
110 if (m_qmlSurface) {
111 return m_qmlSurface->live();
112 } else {
113 return false;
114 }
117Mir::ShellChrome MirSurfaceItem::shellChrome() const
119 if (m_qmlSurface) {
120 return m_qmlSurface->shellChrome();
121 } else {
122 return Mir::NormalChrome;
123 }
126Mir::OrientationAngle MirSurfaceItem::orientationAngle() const
128 if (m_qmlSurface) {
129 return m_qmlSurface->orientationAngle();
130 } else {
131 return Mir::Angle0;
132 }
135void MirSurfaceItem::setOrientationAngle(Mir::OrientationAngle angle)
137 if (!m_qmlSurface)
138 return;
140 if (m_qmlSurface->orientationAngle() == angle)
141 return;
143 m_qmlSurface->setOrientationAngle(angle);
145 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
146 orientationProp.write(QVariant::fromValue(m_qmlSurface->orientationAngle()));
149void MirSurfaceItem::updateScreenshot(QUrl screenshotUrl)
151 if (m_qmlItem) {
152 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
153 screenshotSource.write(QVariant::fromValue(screenshotUrl));
154 }
157void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
159 if (status == QQmlComponent::Ready) {
160 createQmlContentItem();
161 }
164void MirSurfaceItem::createQmlContentItem()
166 DEBUG_MSG("");
168 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
169 m_qmlItem->setParentItem(this);
171 setImplicitWidth(m_qmlItem->implicitWidth());
172 setImplicitHeight(m_qmlItem->implicitHeight());
174 {
175 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
176 screenshotSource.write(QVariant::fromValue(m_qmlSurface->screenshotUrl()));
177 }
180void MirSurfaceItem::touchEvent(QTouchEvent * event)
182 if (event->type() == QEvent::TouchBegin) {
183 m_touchTrail.clear();
184 }
186 if (event->touchPointStates() & Qt::TouchPointPressed) {
187 ++m_touchPressCount;
188 Q_EMIT touchPressCountChanged(m_touchPressCount);
189 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
190 ++m_touchReleaseCount;
191 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
192 }
194 Q_FOREACH(QTouchEvent::TouchPoint touchPoint, event->touchPoints()) {
195 QString id(touchPoint.id());
196 QVariantList list = m_touchTrail[id].toList();
197 list.append(QVariant::fromValue(touchPoint.pos()));
198 if (list.count() > 100) list.pop_front();
199 m_touchTrail[id] = list;
200 }
202 if (m_qmlItem) {
203 QQmlProperty touchTrail(m_qmlItem, "touchTrail");
204 touchTrail.write(m_touchTrail);
205 }
208void MirSurfaceItem::mousePressEvent(QMouseEvent * event)
210 m_mousePressCount++;
211 Q_EMIT mousePressCountChanged(m_mousePressCount);
212 event->accept();
215void MirSurfaceItem::mouseMoveEvent(QMouseEvent * event)
217 event->accept();
220void MirSurfaceItem::mouseReleaseEvent(QMouseEvent * event)
222 m_mouseReleaseCount++;
223 Q_EMIT mouseReleaseCountChanged(m_mouseReleaseCount);
224 event->accept();
227void MirSurfaceItem::setSurface(MirSurfaceInterface* surface)
229 DEBUG_MSG("this=" << (void*)(this) << " name=" << name() << " surface=" << surface);
231 if (m_qmlSurface == surface) {
232 return;
233 }
235 if (m_qmlSurface) {
236 delete m_qmlItem;
237 m_qmlItem = nullptr;
239 delete m_qmlContentComponent;
240 m_qmlContentComponent = nullptr;
242 if (hasActiveFocus() && m_consumesInput && m_qmlSurface->live()) {
243 m_qmlSurface->setActiveFocus(false);
244 }
246 disconnect(m_qmlSurface, nullptr, this, nullptr);
247 m_qmlSurface->unregisterView((qintptr)this);
248 }
250 m_qmlSurface = static_cast<MirSurface*>(surface);
252 if (m_qmlSurface) {
253 m_qmlSurface->registerView((qintptr)this);
255 updateSurfaceSize();
256 updateMirSurfaceVisibility();
258 connect(m_qmlSurface, &MirSurface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
259 connect(m_qmlSurface, &MirSurface::screenshotUrlChanged, this, &MirSurfaceItem::updateScreenshot);
260 connect(m_qmlSurface, &MirSurface::liveChanged, this, &MirSurfaceItem::liveChanged);
261 connect(m_qmlSurface, &MirSurface::stateChanged, this, &MirSurfaceItem::surfaceStateChanged);
263 QUrl qmlComponentFilePath;
264 if (!m_qmlSurface->qmlFilePath().isEmpty()) {
265 qmlComponentFilePath = m_qmlSurface->qmlFilePath();
266 } else {
267 qmlComponentFilePath = QUrl("qrc:///Unity/Application/MirSurfaceItem.qml");
268 }
270 m_qmlContentComponent = new QQmlComponent(QQmlEngine::contextForObject(parent())->engine(), qmlComponentFilePath);
272 switch (m_qmlContentComponent->status()) {
273 case QQmlComponent::Ready:
274 createQmlContentItem();
275 break;
276 case QQmlComponent::Loading:
277 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
278 this, &MirSurfaceItem::onComponentStatusChanged);
279 break;
280 case QQmlComponent::Error:
281 printComponentErrors();
282 qFatal("MirSurfaceItem: failed to create content component.");
283 break;
284 default:
285 qFatal("MirSurfaceItem: Unhandled component status");
286 }
288 if (m_consumesInput) {
289 m_qmlSurface->setActiveFocus(hasActiveFocus());
290 }
291 }
293 Q_EMIT surfaceChanged(m_qmlSurface);
297void MirSurfaceItem::updateMirSurfaceActiveFocus(bool focused)
299 if (m_qmlSurface && m_consumesInput && m_qmlSurface->live()) {
300 m_qmlSurface->setActiveFocus(focused);
301 }
304void MirSurfaceItem::updateMirSurfaceVisibility()
306 if (!m_qmlSurface) return;
308 m_qmlSurface->setViewVisibility((qintptr)this, isVisible());
311void MirSurfaceItem::setConsumesInput(bool value)
313 if (m_consumesInput != value) {
314 m_consumesInput = value;
315 Q_EMIT consumesInputChanged(value);
316 }
319int MirSurfaceItem::surfaceWidth() const
321 return m_surfaceWidth;
324void MirSurfaceItem::setSurfaceWidth(int value)
326 if (value != -1 && m_surfaceWidth != value) {
327 m_surfaceWidth = value;
328 Q_EMIT surfaceWidthChanged(m_surfaceWidth);
329 updateSurfaceSize();
330 }
333int MirSurfaceItem::surfaceHeight() const
335 return m_surfaceHeight;
338void MirSurfaceItem::setSurfaceHeight(int value)
340 if (value != -1 && m_surfaceHeight != value) {
341 m_surfaceHeight = value;
342 Q_EMIT surfaceHeightChanged(m_surfaceHeight);
343 updateSurfaceSize();
344 }
347void MirSurfaceItem::updateSurfaceSize()
349 if (m_qmlSurface && m_surfaceWidth > 0 && m_surfaceHeight > 0) {
350 m_qmlSurface->resize(m_surfaceWidth, m_surfaceHeight);
351 }
354void MirSurfaceItem::setFillMode(FillMode value)
356 if (value != m_fillMode) {
357 m_fillMode = value;
358 Q_EMIT fillModeChanged(m_fillMode);
359 }