on 6bb8a90 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
350 lines 9.5 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::visibleChanged, this, &MirSurfaceItem::updateMirSurfaceVisibility);
62MirSurfaceItem::~MirSurfaceItem()
64 DEBUG_MSG((void*)(this) << name());
65 setSurface(nullptr);
68void MirSurfaceItem::printComponentErrors()
70 QList<QQmlError> errors = m_qmlContentComponent->errors();
71 for (int i = 0; i < errors.count(); ++i) {
72 qDebug() << errors[i];
73 }
76Mir::Type MirSurfaceItem::type() const
78 if (m_qmlSurface) {
79 return m_qmlSurface->type();
80 } else {
81 return Mir::UnknownType;
82 }
85Mir::State MirSurfaceItem::surfaceState() const
87 if (m_qmlSurface) {
88 return m_qmlSurface->state();
89 } else {
90 return Mir::UnknownState;
91 }
94QString MirSurfaceItem::name() const
96 if (m_qmlSurface) {
97 return m_qmlSurface->name();
98 } else {
99 return QString();
100 }
103bool MirSurfaceItem::live() const
105 if (m_qmlSurface) {
106 return m_qmlSurface->live();
107 } else {
108 return false;
109 }
112Mir::ShellChrome MirSurfaceItem::shellChrome() const
114 if (m_qmlSurface) {
115 return m_qmlSurface->shellChrome();
116 } else {
117 return Mir::NormalChrome;
118 }
121Mir::OrientationAngle MirSurfaceItem::orientationAngle() const
123 if (m_qmlSurface) {
124 return m_qmlSurface->orientationAngle();
125 } else {
126 return Mir::Angle0;
127 }
130void MirSurfaceItem::setOrientationAngle(Mir::OrientationAngle angle)
132 if (!m_qmlSurface)
133 return;
135 if (m_qmlSurface->orientationAngle() == angle)
136 return;
138 m_qmlSurface->setOrientationAngle(angle);
140 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
141 orientationProp.write(QVariant::fromValue(m_qmlSurface->orientationAngle()));
144void MirSurfaceItem::updateScreenshot(QUrl screenshotUrl)
146 if (m_qmlItem) {
147 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
148 screenshotSource.write(QVariant::fromValue(screenshotUrl));
149 }
152void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
154 if (status == QQmlComponent::Ready) {
155 createQmlContentItem();
156 }
159void MirSurfaceItem::createQmlContentItem()
161 DEBUG_MSG("");
163 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
164 m_qmlItem->setParentItem(this);
166 setImplicitWidth(m_qmlItem->implicitWidth());
167 setImplicitHeight(m_qmlItem->implicitHeight());
169 {
170 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
171 screenshotSource.write(QVariant::fromValue(m_qmlSurface->screenshotUrl()));
172 }
175void MirSurfaceItem::touchEvent(QTouchEvent * event)
177 if (event->type() == QEvent::TouchBegin) {
178 m_touchTrail.clear();
179 }
181 if (event->touchPointStates() & Qt::TouchPointPressed) {
182 ++m_touchPressCount;
183 Q_EMIT touchPressCountChanged(m_touchPressCount);
184 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
185 ++m_touchReleaseCount;
186 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
187 }
189 Q_FOREACH(QTouchEvent::TouchPoint touchPoint, event->touchPoints()) {
190 QString id(touchPoint.id());
191 QVariantList list = m_touchTrail[id].toList();
192 list.append(QVariant::fromValue(touchPoint.pos()));
193 if (list.count() > 100) list.pop_front();
194 m_touchTrail[id] = list;
195 }
197 if (m_qmlItem) {
198 QQmlProperty touchTrail(m_qmlItem, "touchTrail");
199 touchTrail.write(m_touchTrail);
200 }
203void MirSurfaceItem::mousePressEvent(QMouseEvent * event)
205 m_mousePressCount++;
206 Q_EMIT mousePressCountChanged(m_mousePressCount);
207 event->accept();
210void MirSurfaceItem::mouseMoveEvent(QMouseEvent * event)
212 event->accept();
215void MirSurfaceItem::mouseReleaseEvent(QMouseEvent * event)
217 m_mouseReleaseCount++;
218 Q_EMIT mouseReleaseCountChanged(m_mouseReleaseCount);
219 event->accept();
222void MirSurfaceItem::setSurface(MirSurfaceInterface* surface)
224 DEBUG_MSG("this=" << (void*)(this) << " name=" << name() << " surface=" << surface);
226 if (m_qmlSurface == surface) {
227 return;
228 }
230 if (m_qmlSurface) {
231 delete m_qmlItem;
232 m_qmlItem = nullptr;
234 delete m_qmlContentComponent;
235 m_qmlContentComponent = nullptr;
237 disconnect(m_qmlSurface, nullptr, this, nullptr);
238 m_qmlSurface->unregisterView((qintptr)this);
239 }
241 m_qmlSurface = static_cast<MirSurface*>(surface);
243 if (m_qmlSurface) {
244 m_qmlSurface->registerView((qintptr)this);
246 m_qmlSurface->setActiveFocus(hasActiveFocus());
248 updateSurfaceSize();
249 updateMirSurfaceVisibility();
251 connect(m_qmlSurface, &MirSurface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
252 connect(m_qmlSurface, &MirSurface::screenshotUrlChanged, this, &MirSurfaceItem::updateScreenshot);
253 connect(m_qmlSurface, &MirSurface::liveChanged, this, &MirSurfaceItem::liveChanged);
254 connect(m_qmlSurface, &MirSurface::stateChanged, this, &MirSurfaceItem::surfaceStateChanged);
256 QUrl qmlComponentFilePath;
257 if (!m_qmlSurface->qmlFilePath().isEmpty()) {
258 qmlComponentFilePath = m_qmlSurface->qmlFilePath();
259 } else {
260 qmlComponentFilePath = QUrl("qrc:///Unity/Application/MirSurfaceItem.qml");
261 }
263 m_qmlContentComponent = new QQmlComponent(QQmlEngine::contextForObject(parent())->engine(), qmlComponentFilePath);
265 switch (m_qmlContentComponent->status()) {
266 case QQmlComponent::Ready:
267 createQmlContentItem();
268 break;
269 case QQmlComponent::Loading:
270 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
271 this, &MirSurfaceItem::onComponentStatusChanged);
272 break;
273 case QQmlComponent::Error:
274 printComponentErrors();
275 qFatal("MirSurfaceItem: failed to create content component.");
276 break;
277 default:
278 qFatal("MirSurfaceItem: Unhandled component status");
279 }
280 }
282 Q_EMIT surfaceChanged(m_qmlSurface);
285void MirSurfaceItem::itemChange(ItemChange change, const ItemChangeData & value)
287 if (change == QQuickItem::ItemActiveFocusHasChanged) {
288 if (m_qmlSurface) {
289 m_qmlSurface->setActiveFocus(value.boolValue);
290 }
291 }
294void MirSurfaceItem::updateMirSurfaceVisibility()
296 if (!m_qmlSurface) return;
298 m_qmlSurface->setViewVisibility((qintptr)this, isVisible());
301void MirSurfaceItem::setConsumesInput(bool value)
303 if (m_consumesInput != value) {
304 m_consumesInput = value;
305 Q_EMIT consumesInputChanged(value);
306 }
309int MirSurfaceItem::surfaceWidth() const
311 return m_surfaceWidth;
314void MirSurfaceItem::setSurfaceWidth(int value)
316 if (value != -1 && m_surfaceWidth != value) {
317 m_surfaceWidth = value;
318 Q_EMIT surfaceWidthChanged(m_surfaceWidth);
319 updateSurfaceSize();
320 }
323int MirSurfaceItem::surfaceHeight() const
325 return m_surfaceHeight;
328void MirSurfaceItem::setSurfaceHeight(int value)
330 if (value != -1 && m_surfaceHeight != value) {
331 m_surfaceHeight = value;
332 Q_EMIT surfaceHeightChanged(m_surfaceHeight);
333 updateSurfaceSize();
334 }
337void MirSurfaceItem::updateSurfaceSize()
339 if (m_qmlSurface && m_surfaceWidth > 0 && m_surfaceHeight > 0) {
340 m_qmlSurface->resize(m_surfaceWidth, m_surfaceHeight);
341 }
344void MirSurfaceItem::setFillMode(FillMode value)
346 if (value != m_fillMode) {
347 m_fillMode = value;
348 Q_EMIT fillModeChanged(m_fillMode);
349 }