on 1aaa701 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
344 lines 9.6 kB history raw
1/*
2 * Copyright (C) 2014-2015 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;
32MirSurfaceItem::MirSurfaceItem(QQuickItem *parent)
33 : MirSurfaceItemInterface(parent)
34 , m_qmlSurface(nullptr)
35 , m_qmlItem(nullptr)
36 , m_consumesInput(false)
37 , m_surfaceWidth(0)
38 , m_surfaceHeight(0)
39 , m_touchPressCount(0)
40 , m_touchReleaseCount(0)
41 , m_mousePressCount(0)
42 , m_mouseReleaseCount(0)
44// qDebug() << "MirSurfaceItem::MirSurfaceItem() " << (void*)(this) << name();
45 setAcceptedMouseButtons(Qt::LeftButton | Qt::MiddleButton | Qt::RightButton |
46 Qt::ExtraButton1 | Qt::ExtraButton2 | Qt::ExtraButton3 | Qt::ExtraButton4 |
47 Qt::ExtraButton5 | Qt::ExtraButton6 | Qt::ExtraButton7 | Qt::ExtraButton8 |
48 Qt::ExtraButton9 | Qt::ExtraButton10 | Qt::ExtraButton11 |
49 Qt::ExtraButton12 | Qt::ExtraButton13);
51 connect(this, &QQuickItem::visibleChanged, this, &MirSurfaceItem::updateMirSurfaceVisibility);
54MirSurfaceItem::~MirSurfaceItem()
56// qDebug() << "MirSurfaceItem::~MirSurfaceItem() " << (void*)(this) << name();
57 setSurface(nullptr);
60void MirSurfaceItem::printComponentErrors()
62 QList<QQmlError> errors = m_qmlContentComponent->errors();
63 for (int i = 0; i < errors.count(); ++i) {
64 qDebug() << errors[i];
65 }
68Mir::Type MirSurfaceItem::type() const
70 if (m_qmlSurface) {
71 return m_qmlSurface->type();
72 } else {
73 return Mir::UnknownType;
74 }
77Mir::State MirSurfaceItem::surfaceState() const
79 if (m_qmlSurface) {
80 return m_qmlSurface->state();
81 } else {
82 return Mir::UnknownState;
83 }
86QString MirSurfaceItem::name() const
88 if (m_qmlSurface) {
89 return m_qmlSurface->name();
90 } else {
91 return QString();
92 }
95bool MirSurfaceItem::live() const
97 if (m_qmlSurface) {
98 return m_qmlSurface->live();
99 } else {
100 return false;
101 }
104Mir::ShellChrome MirSurfaceItem::shellChrome() const
106 if (m_qmlSurface) {
107 return m_qmlSurface->shellChrome();
108 } else {
109 return Mir::NormalChrome;
110 }
113Mir::OrientationAngle MirSurfaceItem::orientationAngle() const
115 if (m_qmlSurface) {
116 return m_qmlSurface->orientationAngle();
117 } else {
118 return Mir::Angle0;
119 }
122void MirSurfaceItem::setOrientationAngle(Mir::OrientationAngle angle)
124 if (!m_qmlSurface)
125 return;
127 if (m_qmlSurface->orientationAngle() == angle)
128 return;
130 m_qmlSurface->setOrientationAngle(angle);
132 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
133 orientationProp.write(QVariant::fromValue(m_qmlSurface->orientationAngle()));
136void MirSurfaceItem::updateScreenshot(QUrl screenshotUrl)
138 if (m_qmlItem) {
139 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
140 screenshotSource.write(QVariant::fromValue(screenshotUrl));
141 }
144void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
146 if (status == QQmlComponent::Ready) {
147 createQmlContentItem();
148 }
151void MirSurfaceItem::createQmlContentItem()
153// qDebug() << "MirSurfaceItem::createQmlContentItem()";
155 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
156 m_qmlItem->setParentItem(this);
158 setImplicitWidth(m_qmlItem->implicitWidth());
159 setImplicitHeight(m_qmlItem->implicitHeight());
161 {
162 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
163 screenshotSource.write(QVariant::fromValue(m_qmlSurface->screenshotUrl()));
164 }
167void MirSurfaceItem::touchEvent(QTouchEvent * event)
169 if (event->type() == QEvent::TouchBegin) {
170 m_touchTrail.clear();
171 }
173 if (event->touchPointStates() & Qt::TouchPointPressed) {
174 ++m_touchPressCount;
175 Q_EMIT touchPressCountChanged(m_touchPressCount);
176 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
177 ++m_touchReleaseCount;
178 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
179 }
181 Q_FOREACH(QTouchEvent::TouchPoint touchPoint, event->touchPoints()) {
182 QString id(touchPoint.id());
183 QVariantList list = m_touchTrail[id].toList();
184 list.append(QVariant::fromValue(touchPoint.pos()));
185 if (list.count() > 100) list.pop_front();
186 m_touchTrail[id] = list;
187 }
189 if (m_qmlItem) {
190 QQmlProperty touchTrail(m_qmlItem, "touchTrail");
191 touchTrail.write(m_touchTrail);
192 }
195void MirSurfaceItem::mousePressEvent(QMouseEvent * event)
197 m_mousePressCount++;
198 Q_EMIT mousePressCountChanged(m_mousePressCount);
199 event->accept();
202void MirSurfaceItem::mouseMoveEvent(QMouseEvent * event)
204 event->accept();
207void MirSurfaceItem::mouseReleaseEvent(QMouseEvent * event)
209 m_mouseReleaseCount++;
210 Q_EMIT mouseReleaseCountChanged(m_mouseReleaseCount);
211 event->accept();
214void MirSurfaceItem::setSurface(MirSurfaceInterface* surface)
216// qDebug().nospace() << "MirSurfaceItem::setSurface() this=" << (void*)(this)
217// << " name=" << name()
218// << " surface=" << surface;
220 if (m_qmlSurface == surface) {
221 return;
222 }
224 if (m_qmlSurface) {
225 delete m_qmlItem;
226 m_qmlItem = nullptr;
228 delete m_qmlContentComponent;
229 m_qmlContentComponent = nullptr;
231 disconnect(m_qmlSurface, nullptr, this, nullptr);
232 m_qmlSurface->unregisterView((qintptr)this);
233 }
235 m_qmlSurface = static_cast<MirSurface*>(surface);
237 if (m_qmlSurface) {
238 m_qmlSurface->registerView((qintptr)this);
240 m_qmlSurface->setActiveFocus(hasActiveFocus());
242 updateSurfaceSize();
243 updateMirSurfaceVisibility();
245 connect(m_qmlSurface, &MirSurface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
246 connect(m_qmlSurface, &MirSurface::screenshotUrlChanged, this, &MirSurfaceItem::updateScreenshot);
247 connect(m_qmlSurface, &MirSurface::liveChanged, this, &MirSurfaceItem::liveChanged);
248 connect(m_qmlSurface, &MirSurface::stateChanged, this, &MirSurfaceItem::surfaceStateChanged);
250 QUrl qmlComponentFilePath;
251 if (!m_qmlSurface->qmlFilePath().isEmpty()) {
252 qmlComponentFilePath = m_qmlSurface->qmlFilePath();
253 } else {
254 qmlComponentFilePath = QUrl("qrc:///Unity/Application/MirSurfaceItem.qml");
255 }
257 m_qmlContentComponent = new QQmlComponent(QQmlEngine::contextForObject(parent())->engine(), qmlComponentFilePath);
259 switch (m_qmlContentComponent->status()) {
260 case QQmlComponent::Ready:
261 createQmlContentItem();
262 break;
263 case QQmlComponent::Loading:
264 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
265 this, &MirSurfaceItem::onComponentStatusChanged);
266 break;
267 case QQmlComponent::Error:
268 printComponentErrors();
269 qFatal("MirSurfaceItem: failed to create content component.");
270 break;
271 default:
272 qFatal("MirSurfaceItem: Unhandled component status");
273 }
274 }
276 Q_EMIT surfaceChanged(m_qmlSurface);
279void MirSurfaceItem::itemChange(ItemChange change, const ItemChangeData & value)
281 if (change == QQuickItem::ItemActiveFocusHasChanged) {
282 if (m_qmlSurface) {
283 m_qmlSurface->setActiveFocus(value.boolValue);
284 }
285 }
288void MirSurfaceItem::updateMirSurfaceVisibility()
290 if (!m_qmlSurface) return;
292 m_qmlSurface->setViewVisibility((qintptr)this, isVisible());
295void MirSurfaceItem::setConsumesInput(bool value)
297 if (m_consumesInput != value) {
298 m_consumesInput = value;
299 Q_EMIT consumesInputChanged(value);
300 }
303int MirSurfaceItem::surfaceWidth() const
305 return m_surfaceWidth;
308void MirSurfaceItem::setSurfaceWidth(int value)
310 if (value != -1 && m_surfaceWidth != value) {
311 m_surfaceWidth = value;
312 Q_EMIT surfaceWidthChanged(m_surfaceWidth);
313 updateSurfaceSize();
314 }
317int MirSurfaceItem::surfaceHeight() const
319 return m_surfaceHeight;
322void MirSurfaceItem::setSurfaceHeight(int value)
324 if (value != -1 && m_surfaceHeight != value) {
325 m_surfaceHeight = value;
326 Q_EMIT surfaceHeightChanged(m_surfaceHeight);
327 updateSurfaceSize();
328 }
331void MirSurfaceItem::updateSurfaceSize()
333 if (m_qmlSurface && m_surfaceWidth > 0 && m_surfaceHeight > 0) {
334 m_qmlSurface->resize(m_surfaceWidth, m_surfaceHeight);
335 }
338void MirSurfaceItem::setFillMode(FillMode value)
340 if (value != m_fillMode) {
341 m_fillMode = value;
342 Q_EMIT fillModeChanged(m_fillMode);
343 }