on fe71e21 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
415 lines 12 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_orientationAngle(nullptr)
47 , m_surfaceWidth(0)
48 , m_surfaceHeight(0)
49 , m_touchPressCount(0)
50 , m_touchReleaseCount(0)
51 , m_mousePressCount(0)
52 , m_mouseReleaseCount(0)
54 DEBUG_MSG((void*)(this) << name());
55 setAcceptedMouseButtons(Qt::LeftButton | Qt::MiddleButton | Qt::RightButton |
56 Qt::ExtraButton1 | Qt::ExtraButton2 | Qt::ExtraButton3 | Qt::ExtraButton4 |
57 Qt::ExtraButton5 | Qt::ExtraButton6 | Qt::ExtraButton7 | Qt::ExtraButton8 |
58 Qt::ExtraButton9 | Qt::ExtraButton10 | Qt::ExtraButton11 |
59 Qt::ExtraButton12 | Qt::ExtraButton13);
61 connect(this, &QQuickItem::activeFocusChanged, this, &MirSurfaceItem::updateMirSurfaceActiveFocus);
62 connect(this, &QQuickItem::visibleChanged, this, &MirSurfaceItem::updateMirSurfaceExposure);
64 connect(this, &MirSurfaceItem::consumesInputChanged, this, [this]() {
65 updateMirSurfaceActiveFocus(hasActiveFocus());
66 });
68 // We're just clipping contents in the mock. The real QtMir would copy only relevant buffer instead
69 setClip(true);
72MirSurfaceItem::~MirSurfaceItem()
74 DEBUG_MSG((void*)(this) << name());
75 setSurface(nullptr);
76 delete m_orientationAngle;
79void MirSurfaceItem::printComponentErrors()
81 QList<QQmlError> errors = m_qmlContentComponent->errors();
82 for (int i = 0; i < errors.count(); ++i) {
83 qDebug() << errors[i];
84 }
87Mir::Type MirSurfaceItem::type() const
89 if (m_qmlSurface) {
90 return m_qmlSurface->type();
91 } else {
92 return Mir::UnknownType;
93 }
96Mir::State MirSurfaceItem::surfaceState() const
98 if (m_qmlSurface) {
99 return m_qmlSurface->state();
100 } else {
101 return Mir::UnknownState;
102 }
105QString MirSurfaceItem::name() const
107 if (m_qmlSurface) {
108 return m_qmlSurface->name();
109 } else {
110 return QString();
111 }
114bool MirSurfaceItem::live() const
116 if (m_qmlSurface) {
117 return m_qmlSurface->live();
118 } else {
119 return false;
120 }
123Mir::ShellChrome MirSurfaceItem::shellChrome() const
125 if (m_qmlSurface) {
126 return m_qmlSurface->shellChrome();
127 } else {
128 return Mir::NormalChrome;
129 }
132Mir::OrientationAngle MirSurfaceItem::orientationAngle() const
134 if (m_orientationAngle) {
135 Q_ASSERT(!m_qmlSurface);
136 return *m_orientationAngle;
137 } else if (m_qmlSurface) {
138 return m_qmlSurface->orientationAngle();
139 } else {
140 return Mir::Angle0;
141 }
144void MirSurfaceItem::setOrientationAngle(Mir::OrientationAngle angle)
146 DEBUG_MSG(angle);
148 if (m_qmlSurface) {
149 Q_ASSERT(!m_orientationAngle);
150 m_qmlSurface->setOrientationAngle(angle);
151 } else if (!m_orientationAngle) {
152 m_orientationAngle = new Mir::OrientationAngle;
153 *m_orientationAngle = angle;
154 Q_EMIT orientationAngleChanged(angle);
155 } else if (*m_orientationAngle != angle) {
156 *m_orientationAngle = angle;
157 Q_EMIT orientationAngleChanged(angle);
158 }
160 if (m_qmlItem) {
161 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
162 if (orientationProp.isValid()) {
163 orientationProp.write(QVariant::fromValue(orientationAngle()));
164 }
165 }
168void MirSurfaceItem::updateScreenshot(QUrl screenshotUrl)
170 if (m_qmlItem) {
171 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
172 if (screenshotSource.isValid()) {
173 screenshotSource.write(QVariant::fromValue(screenshotUrl));
174 }
175 }
178void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
180 if (status == QQmlComponent::Ready) {
181 createQmlContentItem();
182 }
185void MirSurfaceItem::createQmlContentItem()
187 DEBUG_MSG("");
189 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
190 m_qmlItem->setParentItem(this);
192 m_qmlItem->setWidth(m_surfaceWidth);
193 m_qmlItem->setHeight(m_surfaceHeight);
195 setImplicitWidth(m_qmlItem->implicitWidth());
196 setImplicitHeight(m_qmlItem->implicitHeight());
198 {
199 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
200 if (screenshotSource.isValid()) {
201 screenshotSource.write(QVariant::fromValue(m_qmlSurface->screenshotUrl()));
202 }
203 }
205 {
206 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
207 if (orientationProp.isValid()) {
208 orientationProp.write(QVariant::fromValue(orientationAngle()));
209 }
210 }
212 {
213 QQmlProperty surfaceProperty(m_qmlItem, "surface");
214 if (surfaceProperty.isValid()) {
215 surfaceProperty.write(QVariant::fromValue(m_qmlSurface));
216 }
217 }
220void MirSurfaceItem::touchEvent(QTouchEvent * event)
222 if (event->type() == QEvent::TouchBegin) {
223 m_touchTrail.clear();
224 }
226 if (event->touchPointStates() & Qt::TouchPointPressed) {
227 ++m_touchPressCount;
228 Q_EMIT touchPressCountChanged(m_touchPressCount);
229 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
230 ++m_touchReleaseCount;
231 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
232 }
234 Q_FOREACH(const QTouchEvent::TouchPoint &touchPoint, event->touchPoints()) {
235 QString id(touchPoint.id());
236 QVariantList list = m_touchTrail[id].toList();
237 list.append(QVariant::fromValue(touchPoint.pos()));
238 if (list.count() > 100) list.pop_front();
239 m_touchTrail[id] = list;
240 }
242 if (m_qmlItem) {
243 QQmlProperty touchTrail(m_qmlItem, "touchTrail");
244 touchTrail.write(m_touchTrail);
245 }
248void MirSurfaceItem::mousePressEvent(QMouseEvent * event)
250 m_mousePressCount++;
251 Q_EMIT mousePressCountChanged(m_mousePressCount);
252 event->accept();
255void MirSurfaceItem::mouseMoveEvent(QMouseEvent * event)
257 event->accept();
260void MirSurfaceItem::mouseReleaseEvent(QMouseEvent * event)
262 m_mouseReleaseCount++;
263 Q_EMIT mouseReleaseCountChanged(m_mouseReleaseCount);
264 event->accept();
267void MirSurfaceItem::setSurface(MirSurfaceInterface* surface)
269 DEBUG_MSG("this=" << (void*)(this) << " name=" << name() << " surface=" << surface);
271 if (m_qmlSurface == surface) {
272 return;
273 }
275 if (m_qmlSurface) {
276 delete m_qmlItem;
277 m_qmlItem = nullptr;
279 delete m_qmlContentComponent;
280 m_qmlContentComponent = nullptr;
282 if (hasActiveFocus() && m_consumesInput && m_qmlSurface->live()) {
283 m_qmlSurface->setActiveFocus(false);
284 }
286 disconnect(m_qmlSurface, nullptr, this, nullptr);
287 m_qmlSurface->unregisterView((qintptr)this);
288 }
290 m_qmlSurface = static_cast<MirSurface*>(surface);
292 if (m_qmlSurface) {
293 m_qmlSurface->registerView((qintptr)this);
295 updateSurfaceSize();
296 updateMirSurfaceExposure();
298 if (m_orientationAngle) {
299 m_qmlSurface->setOrientationAngle(*m_orientationAngle);
300 connect(m_qmlSurface, &MirSurfaceInterface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
301 delete m_orientationAngle;
302 m_orientationAngle = nullptr;
303 } else {
304 connect(m_qmlSurface, &MirSurfaceInterface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
305 Q_EMIT orientationAngleChanged(m_qmlSurface->orientationAngle());
306 }
308 connect(m_qmlSurface, &MirSurface::screenshotUrlChanged, this, &MirSurfaceItem::updateScreenshot);
309 connect(m_qmlSurface, &MirSurface::liveChanged, this, &MirSurfaceItem::liveChanged);
310 connect(m_qmlSurface, &MirSurface::stateChanged, this, &MirSurfaceItem::surfaceStateChanged);
312 QUrl qmlComponentFilePath;
313 if (!m_qmlSurface->qmlFilePath().isEmpty()) {
314 qmlComponentFilePath = m_qmlSurface->qmlFilePath();
315 } else {
316 qmlComponentFilePath = QUrl("qrc:///Unity/Application/MirSurfaceItem.qml");
317 }
319 m_qmlContentComponent = new QQmlComponent(QQmlEngine::contextForObject(parent())->engine(), qmlComponentFilePath);
321 switch (m_qmlContentComponent->status()) {
322 case QQmlComponent::Ready:
323 createQmlContentItem();
324 break;
325 case QQmlComponent::Loading:
326 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
327 this, &MirSurfaceItem::onComponentStatusChanged);
328 break;
329 case QQmlComponent::Error:
330 printComponentErrors();
331 qFatal("MirSurfaceItem: failed to create content component.");
332 break;
333 default:
334 qFatal("MirSurfaceItem: Unhandled component status");
335 }
337 if (m_consumesInput) {
338 m_qmlSurface->setActiveFocus(hasActiveFocus());
339 }
340 }
342 Q_EMIT surfaceChanged(m_qmlSurface);
346void MirSurfaceItem::updateMirSurfaceActiveFocus(bool focused)
348 if (m_qmlSurface && m_consumesInput && m_qmlSurface->live()) {
349 m_qmlSurface->setActiveFocus(focused);
350 }
353void MirSurfaceItem::updateMirSurfaceExposure()
355 if (!m_qmlSurface) return;
357 m_qmlSurface->setViewExposure((qintptr)this, isVisible());
360void MirSurfaceItem::setConsumesInput(bool value)
362 if (m_consumesInput != value) {
363 m_consumesInput = value;
364 Q_EMIT consumesInputChanged(value);
365 }
368int MirSurfaceItem::surfaceWidth() const
370 return m_surfaceWidth;
373void MirSurfaceItem::setSurfaceWidth(int value)
375// qDebug() << "setSurfaceWidth called" << value;
376 if (value != -1 && m_surfaceWidth != value) {
377 m_surfaceWidth = value;
378 Q_EMIT surfaceWidthChanged(m_surfaceWidth);
379 updateSurfaceSize();
380 }
383int MirSurfaceItem::surfaceHeight() const
385 return m_surfaceHeight;
388void MirSurfaceItem::setSurfaceHeight(int value)
390 if (value != -1 && m_surfaceHeight != value) {
391 m_surfaceHeight = value;
392 Q_EMIT surfaceHeightChanged(m_surfaceHeight);
393 updateSurfaceSize();
394 }
397void MirSurfaceItem::updateSurfaceSize()
399 if (m_qmlSurface && m_surfaceWidth > 0 && m_surfaceHeight > 0) {
400 m_qmlSurface->resize(m_surfaceWidth, m_surfaceHeight);
401 if (m_qmlItem) {
402 m_qmlItem->setWidth(m_surfaceWidth);
403 m_qmlItem->setHeight(m_surfaceHeight);
404 }
405 setImplicitSize(m_surfaceWidth, m_surfaceHeight);
406 }
409void MirSurfaceItem::setFillMode(FillMode value)
411 if (value != m_fillMode) {
412 m_fillMode = value;
413 Q_EMIT fillModeChanged(m_fillMode);
414 }