on d345593 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
318 lines 8.9 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::OrientationAngle MirSurfaceItem::orientationAngle() const
106 if (m_qmlSurface) {
107 return m_qmlSurface->orientationAngle();
108 } else {
109 return Mir::Angle0;
110 }
113void MirSurfaceItem::setOrientationAngle(Mir::OrientationAngle angle)
115 if (!m_qmlSurface)
116 return;
118 if (m_qmlSurface->orientationAngle() == angle)
119 return;
121 m_qmlSurface->setOrientationAngle(angle);
123 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
124 orientationProp.write(QVariant::fromValue(m_qmlSurface->orientationAngle()));
127void MirSurfaceItem::updateScreenshot(QUrl screenshotUrl)
129 if (m_qmlItem) {
130 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
131 screenshotSource.write(QVariant::fromValue(screenshotUrl));
132 }
135void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
137 if (status == QQmlComponent::Ready) {
138 createQmlContentItem();
139 }
142void MirSurfaceItem::createQmlContentItem()
144// qDebug() << "MirSurfaceItem::createQmlContentItem()";
146 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
147 m_qmlItem->setParentItem(this);
149 setImplicitWidth(m_qmlItem->implicitWidth());
150 setImplicitHeight(m_qmlItem->implicitHeight());
152 {
153 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
154 screenshotSource.write(QVariant::fromValue(m_qmlSurface->screenshotUrl()));
155 }
158void MirSurfaceItem::touchEvent(QTouchEvent * event)
160 if (event->touchPointStates() & Qt::TouchPointPressed) {
161 ++m_touchPressCount;
162 Q_EMIT touchPressCountChanged(m_touchPressCount);
163 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
164 ++m_touchReleaseCount;
165 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
166 }
169void MirSurfaceItem::mousePressEvent(QMouseEvent * event)
171 m_mousePressCount++;
172 Q_EMIT mousePressCountChanged(m_mousePressCount);
173 event->accept();
176void MirSurfaceItem::mouseMoveEvent(QMouseEvent * event)
178 event->accept();
181void MirSurfaceItem::mouseReleaseEvent(QMouseEvent * event)
183 m_mouseReleaseCount++;
184 Q_EMIT mouseReleaseCountChanged(m_mouseReleaseCount);
185 event->accept();
188void MirSurfaceItem::setSurface(MirSurfaceInterface* surface)
190// qDebug().nospace() << "MirSurfaceItem::setSurface() this=" << (void*)(this)
191// << " name=" << name()
192// << " surface=" << surface;
194 if (m_qmlSurface == surface) {
195 return;
196 }
198 if (m_qmlSurface) {
199 delete m_qmlItem;
200 m_qmlItem = nullptr;
202 delete m_qmlContentComponent;
203 m_qmlContentComponent = nullptr;
205 disconnect(m_qmlSurface, nullptr, this, nullptr);
206 m_qmlSurface->unregisterView((qintptr)this);
207 }
209 m_qmlSurface = static_cast<MirSurface*>(surface);
211 if (m_qmlSurface) {
212 m_qmlSurface->registerView((qintptr)this);
214 m_qmlSurface->setActiveFocus(hasActiveFocus());
216 updateSurfaceSize();
217 updateMirSurfaceVisibility();
219 connect(m_qmlSurface, &MirSurface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
220 connect(m_qmlSurface, &MirSurface::screenshotUrlChanged, this, &MirSurfaceItem::updateScreenshot);
221 connect(m_qmlSurface, &MirSurface::liveChanged, this, &MirSurfaceItem::liveChanged);
222 connect(m_qmlSurface, &MirSurface::stateChanged, this, &MirSurfaceItem::surfaceStateChanged);
224 QUrl qmlComponentFilePath;
225 if (!m_qmlSurface->qmlFilePath().isEmpty()) {
226 qmlComponentFilePath = m_qmlSurface->qmlFilePath();
227 } else {
228 qmlComponentFilePath = QUrl("qrc:///Unity/Application/MirSurfaceItem.qml");
229 }
231 m_qmlContentComponent = new QQmlComponent(QQmlEngine::contextForObject(parent())->engine(), qmlComponentFilePath);
233 switch (m_qmlContentComponent->status()) {
234 case QQmlComponent::Ready:
235 createQmlContentItem();
236 break;
237 case QQmlComponent::Loading:
238 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
239 this, &MirSurfaceItem::onComponentStatusChanged);
240 break;
241 case QQmlComponent::Error:
242 printComponentErrors();
243 qFatal("MirSurfaceItem: failed to create content component.");
244 break;
245 default:
246 qFatal("MirSurfaceItem: Unhandled component status");
247 }
248 }
250 Q_EMIT surfaceChanged(m_qmlSurface);
253void MirSurfaceItem::itemChange(ItemChange change, const ItemChangeData & value)
255 if (change == QQuickItem::ItemActiveFocusHasChanged) {
256 if (m_qmlSurface) {
257 m_qmlSurface->setActiveFocus(value.boolValue);
258 }
259 }
262void MirSurfaceItem::updateMirSurfaceVisibility()
264 if (!m_qmlSurface) return;
266 m_qmlSurface->setViewVisibility((qintptr)this, isVisible());
269void MirSurfaceItem::setConsumesInput(bool value)
271 if (m_consumesInput != value) {
272 m_consumesInput = value;
273 Q_EMIT consumesInputChanged(value);
274 }
277int MirSurfaceItem::surfaceWidth() const
279 return m_surfaceWidth;
282void MirSurfaceItem::setSurfaceWidth(int value)
284 if (value != -1 && m_surfaceWidth != value) {
285 m_surfaceWidth = value;
286 Q_EMIT surfaceWidthChanged(m_surfaceWidth);
287 updateSurfaceSize();
288 }
291int MirSurfaceItem::surfaceHeight() const
293 return m_surfaceHeight;
296void MirSurfaceItem::setSurfaceHeight(int value)
298 if (value != -1 && m_surfaceHeight != value) {
299 m_surfaceHeight = value;
300 Q_EMIT surfaceHeightChanged(m_surfaceHeight);
301 updateSurfaceSize();
302 }
305void MirSurfaceItem::updateSurfaceSize()
307 if (m_qmlSurface && m_surfaceWidth > 0 && m_surfaceHeight > 0) {
308 m_qmlSurface->resize(m_surfaceWidth, m_surfaceHeight);
309 }
312void MirSurfaceItem::setFillMode(FillMode value)
314 if (value != m_fillMode) {
315 m_fillMode = value;
316 Q_EMIT fillModeChanged(m_fillMode);
317 }