on cfcde21 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
400 lines 11 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::updateMirSurfaceVisibility);
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 orientationProp.write(QVariant::fromValue(orientationAngle()));
163 }
166void MirSurfaceItem::updateScreenshot(QUrl screenshotUrl)
168 if (m_qmlItem) {
169 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
170 screenshotSource.write(QVariant::fromValue(screenshotUrl));
171 }
174void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
176 if (status == QQmlComponent::Ready) {
177 createQmlContentItem();
178 }
181void MirSurfaceItem::createQmlContentItem()
183 DEBUG_MSG("");
185 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
186 m_qmlItem->setParentItem(this);
188 m_qmlItem->setWidth(m_surfaceWidth);
189 m_qmlItem->setHeight(m_surfaceHeight);
191 setImplicitWidth(m_qmlItem->implicitWidth());
192 setImplicitHeight(m_qmlItem->implicitHeight());
194 {
195 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
196 screenshotSource.write(QVariant::fromValue(m_qmlSurface->screenshotUrl()));
197 }
199 {
200 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
201 orientationProp.write(QVariant::fromValue(orientationAngle()));
202 }
205void MirSurfaceItem::touchEvent(QTouchEvent * event)
207 if (event->type() == QEvent::TouchBegin) {
208 m_touchTrail.clear();
209 }
211 if (event->touchPointStates() & Qt::TouchPointPressed) {
212 ++m_touchPressCount;
213 Q_EMIT touchPressCountChanged(m_touchPressCount);
214 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
215 ++m_touchReleaseCount;
216 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
217 }
219 Q_FOREACH(QTouchEvent::TouchPoint touchPoint, event->touchPoints()) {
220 QString id(touchPoint.id());
221 QVariantList list = m_touchTrail[id].toList();
222 list.append(QVariant::fromValue(touchPoint.pos()));
223 if (list.count() > 100) list.pop_front();
224 m_touchTrail[id] = list;
225 }
227 if (m_qmlItem) {
228 QQmlProperty touchTrail(m_qmlItem, "touchTrail");
229 touchTrail.write(m_touchTrail);
230 }
233void MirSurfaceItem::mousePressEvent(QMouseEvent * event)
235 m_mousePressCount++;
236 Q_EMIT mousePressCountChanged(m_mousePressCount);
237 event->accept();
240void MirSurfaceItem::mouseMoveEvent(QMouseEvent * event)
242 event->accept();
245void MirSurfaceItem::mouseReleaseEvent(QMouseEvent * event)
247 m_mouseReleaseCount++;
248 Q_EMIT mouseReleaseCountChanged(m_mouseReleaseCount);
249 event->accept();
252void MirSurfaceItem::setSurface(MirSurfaceInterface* surface)
254 DEBUG_MSG("this=" << (void*)(this) << " name=" << name() << " surface=" << surface);
256 if (m_qmlSurface == surface) {
257 return;
258 }
260 if (m_qmlSurface) {
261 delete m_qmlItem;
262 m_qmlItem = nullptr;
264 delete m_qmlContentComponent;
265 m_qmlContentComponent = nullptr;
267 if (hasActiveFocus() && m_consumesInput && m_qmlSurface->live()) {
268 m_qmlSurface->setActiveFocus(false);
269 }
271 disconnect(m_qmlSurface, nullptr, this, nullptr);
272 m_qmlSurface->unregisterView((qintptr)this);
273 }
275 m_qmlSurface = static_cast<MirSurface*>(surface);
277 if (m_qmlSurface) {
278 m_qmlSurface->registerView((qintptr)this);
280 updateSurfaceSize();
281 updateMirSurfaceVisibility();
283 if (m_orientationAngle) {
284 m_qmlSurface->setOrientationAngle(*m_orientationAngle);
285 connect(m_qmlSurface, &MirSurfaceInterface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
286 delete m_orientationAngle;
287 m_orientationAngle = nullptr;
288 } else {
289 connect(m_qmlSurface, &MirSurfaceInterface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
290 Q_EMIT orientationAngleChanged(m_qmlSurface->orientationAngle());
291 }
293 connect(m_qmlSurface, &MirSurface::screenshotUrlChanged, this, &MirSurfaceItem::updateScreenshot);
294 connect(m_qmlSurface, &MirSurface::liveChanged, this, &MirSurfaceItem::liveChanged);
295 connect(m_qmlSurface, &MirSurface::stateChanged, this, &MirSurfaceItem::surfaceStateChanged);
297 QUrl qmlComponentFilePath;
298 if (!m_qmlSurface->qmlFilePath().isEmpty()) {
299 qmlComponentFilePath = m_qmlSurface->qmlFilePath();
300 } else {
301 qmlComponentFilePath = QUrl("qrc:///Unity/Application/MirSurfaceItem.qml");
302 }
304 m_qmlContentComponent = new QQmlComponent(QQmlEngine::contextForObject(parent())->engine(), qmlComponentFilePath);
306 switch (m_qmlContentComponent->status()) {
307 case QQmlComponent::Ready:
308 createQmlContentItem();
309 break;
310 case QQmlComponent::Loading:
311 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
312 this, &MirSurfaceItem::onComponentStatusChanged);
313 break;
314 case QQmlComponent::Error:
315 printComponentErrors();
316 qFatal("MirSurfaceItem: failed to create content component.");
317 break;
318 default:
319 qFatal("MirSurfaceItem: Unhandled component status");
320 }
322 if (m_consumesInput) {
323 m_qmlSurface->setActiveFocus(hasActiveFocus());
324 }
325 }
327 Q_EMIT surfaceChanged(m_qmlSurface);
331void MirSurfaceItem::updateMirSurfaceActiveFocus(bool focused)
333 if (m_qmlSurface && m_consumesInput && m_qmlSurface->live()) {
334 m_qmlSurface->setActiveFocus(focused);
335 }
338void MirSurfaceItem::updateMirSurfaceVisibility()
340 if (!m_qmlSurface) return;
342 m_qmlSurface->setViewVisibility((qintptr)this, isVisible());
345void MirSurfaceItem::setConsumesInput(bool value)
347 if (m_consumesInput != value) {
348 m_consumesInput = value;
349 Q_EMIT consumesInputChanged(value);
350 }
353int MirSurfaceItem::surfaceWidth() const
355 return m_surfaceWidth;
358void MirSurfaceItem::setSurfaceWidth(int value)
360// qDebug() << "setSurfaceWidth called" << value;
361 if (value != -1 && m_surfaceWidth != value) {
362 m_surfaceWidth = value;
363 Q_EMIT surfaceWidthChanged(m_surfaceWidth);
364 updateSurfaceSize();
365 }
368int MirSurfaceItem::surfaceHeight() const
370 return m_surfaceHeight;
373void MirSurfaceItem::setSurfaceHeight(int value)
375 if (value != -1 && m_surfaceHeight != value) {
376 m_surfaceHeight = value;
377 Q_EMIT surfaceHeightChanged(m_surfaceHeight);
378 updateSurfaceSize();
379 }
382void MirSurfaceItem::updateSurfaceSize()
384 if (m_qmlSurface && m_surfaceWidth > 0 && m_surfaceHeight > 0) {
385 m_qmlSurface->resize(m_surfaceWidth, m_surfaceHeight);
386 if (m_qmlItem) {
387 m_qmlItem->setWidth(m_surfaceWidth);
388 m_qmlItem->setHeight(m_surfaceHeight);
389 }
390 setImplicitSize(m_surfaceWidth, m_surfaceHeight);
391 }
394void MirSurfaceItem::setFillMode(FillMode value)
396 if (value != m_fillMode) {
397 m_fillMode = value;
398 Q_EMIT fillModeChanged(m_fillMode);
399 }