on 55ecae8 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
321 lines 9.0 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 <QQmlProperty>
24#include <QQmlEngine>
25#include <QString>
27#include <QDebug>
29using namespace unity::shell::application;
31MirSurfaceItem::MirSurfaceItem(QQuickItem *parent)
32 : MirSurfaceItemInterface(parent)
33 , m_qmlSurface(nullptr)
34 , m_qmlItem(nullptr)
35 , m_consumesInput(false)
36 , m_surfaceWidth(0)
37 , m_surfaceHeight(0)
38 , m_touchPressCount(0)
39 , m_touchReleaseCount(0)
40 , m_mousePressCount(0)
41 , m_mouseReleaseCount(0)
43// qDebug() << "MirSurfaceItem::MirSurfaceItem() " << (void*)(this) << name();
44 setAcceptedMouseButtons(Qt::LeftButton | Qt::MiddleButton | Qt::RightButton |
45 Qt::ExtraButton1 | Qt::ExtraButton2 | Qt::ExtraButton3 | Qt::ExtraButton4 |
46 Qt::ExtraButton5 | Qt::ExtraButton6 | Qt::ExtraButton7 | Qt::ExtraButton8 |
47 Qt::ExtraButton9 | Qt::ExtraButton10 | Qt::ExtraButton11 |
48 Qt::ExtraButton12 | Qt::ExtraButton13);
50 connect(this, &QQuickItem::visibleChanged, this, &MirSurfaceItem::updateMirSurfaceVisibility);
53MirSurfaceItem::~MirSurfaceItem()
55// qDebug() << "MirSurfaceItem::~MirSurfaceItem() " << (void*)(this) << name();
56 setSurface(nullptr);
59void MirSurfaceItem::printComponentErrors()
61 QList<QQmlError> errors = m_qmlContentComponent->errors();
62 for (int i = 0; i < errors.count(); ++i) {
63 qDebug() << errors[i];
64 }
67Mir::Type MirSurfaceItem::type() const
69 if (m_qmlSurface) {
70 return m_qmlSurface->type();
71 } else {
72 return Mir::UnknownType;
73 }
76Mir::State MirSurfaceItem::surfaceState() const
78 if (m_qmlSurface) {
79 return m_qmlSurface->state();
80 } else {
81 return Mir::UnknownState;
82 }
85QString MirSurfaceItem::name() const
87 if (m_qmlSurface) {
88 return m_qmlSurface->name();
89 } else {
90 return QString();
91 }
94bool MirSurfaceItem::live() const
96 if (m_qmlSurface) {
97 return m_qmlSurface->live();
98 } else {
99 return false;
100 }
103Mir::OrientationAngle MirSurfaceItem::orientationAngle() const
105 if (m_qmlSurface) {
106 return m_qmlSurface->orientationAngle();
107 } else {
108 return Mir::Angle0;
109 }
112void MirSurfaceItem::setOrientationAngle(Mir::OrientationAngle angle)
114 if (!m_qmlSurface)
115 return;
117 if (m_qmlSurface->orientationAngle() == angle)
118 return;
120 m_qmlSurface->setOrientationAngle(angle);
122 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
123 orientationProp.write(QVariant::fromValue(m_qmlSurface->orientationAngle()));
126void MirSurfaceItem::updateScreenshot(QUrl screenshotUrl)
128 if (m_qmlItem) {
129 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
130 screenshotSource.write(QVariant::fromValue(screenshotUrl));
131 }
134void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
136 if (status == QQmlComponent::Ready) {
137 createQmlContentItem();
138 }
141void MirSurfaceItem::createQmlContentItem()
143// qDebug() << "MirSurfaceItem::createQmlContentItem()";
145 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
146 m_qmlItem->setParentItem(this);
148 setImplicitWidth(m_qmlItem->implicitWidth());
149 setImplicitHeight(m_qmlItem->implicitHeight());
151 {
152 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
153 screenshotSource.write(QVariant::fromValue(m_qmlSurface->screenshotUrl()));
154 }
157void MirSurfaceItem::touchEvent(QTouchEvent * event)
159 if (event->touchPointStates() & Qt::TouchPointPressed) {
160 ++m_touchPressCount;
161 Q_EMIT touchPressCountChanged(m_touchPressCount);
162 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
163 ++m_touchReleaseCount;
164 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
165 }
168void MirSurfaceItem::mousePressEvent(QMouseEvent * event)
170 m_mousePressCount++;
171 Q_EMIT mousePressCountChanged(m_mousePressCount);
172 event->accept();
175void MirSurfaceItem::mouseMoveEvent(QMouseEvent * event)
177 event->accept();
180void MirSurfaceItem::mouseReleaseEvent(QMouseEvent * event)
182 m_mouseReleaseCount++;
183 Q_EMIT mouseReleaseCountChanged(m_mouseReleaseCount);
184 event->accept();
187void MirSurfaceItem::setSurface(MirSurfaceInterface* surface)
189// qDebug().nospace() << "MirSurfaceItem::setSurface() this=" << (void*)(this)
190// << " name=" << name()
191// << " surface=" << surface;
193 if (m_qmlSurface == surface) {
194 return;
195 }
197 if (m_qmlSurface) {
198 delete m_qmlItem;
199 m_qmlItem = nullptr;
201 delete m_qmlContentComponent;
202 m_qmlContentComponent = nullptr;
204 disconnect(m_qmlSurface, nullptr, this, nullptr);
205 m_qmlSurface->unregisterView((qintptr)this);
206 }
208 m_qmlSurface = static_cast<MirSurface*>(surface);
210 if (m_qmlSurface) {
211 m_qmlSurface->registerView((qintptr)this);
213 m_qmlSurface->setActiveFocus(hasActiveFocus());
215 updateSurfaceSize();
216 updateMirSurfaceVisibility();
218 connect(m_qmlSurface, &MirSurface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
219 connect(m_qmlSurface, &MirSurface::screenshotUrlChanged, this, &MirSurfaceItem::updateScreenshot);
220 connect(m_qmlSurface, &MirSurface::liveChanged, this, &MirSurfaceItem::liveChanged);
221 connect(m_qmlSurface, &MirSurface::stateChanged, this, &MirSurfaceItem::surfaceStateChanged);
223 // The assumptions I make here really should hold.
224 QQuickView *quickView =
225 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
227 QUrl qmlComponentFilePath;
228 if (!m_qmlSurface->qmlFilePath().isEmpty()) {
229 qmlComponentFilePath = m_qmlSurface->qmlFilePath();
230 } else {
231 qmlComponentFilePath = QUrl("qrc:///Unity/Application/MirSurfaceItem.qml");
232 }
234 m_qmlContentComponent = new QQmlComponent(quickView->engine(), qmlComponentFilePath);
236 switch (m_qmlContentComponent->status()) {
237 case QQmlComponent::Ready:
238 createQmlContentItem();
239 break;
240 case QQmlComponent::Loading:
241 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
242 this, &MirSurfaceItem::onComponentStatusChanged);
243 break;
244 case QQmlComponent::Error:
245 printComponentErrors();
246 qFatal("MirSurfaceItem: failed to create content component.");
247 break;
248 default:
249 qFatal("MirSurfaceItem: Unhandled component status");
250 }
251 }
253 Q_EMIT surfaceChanged(m_qmlSurface);
256void MirSurfaceItem::itemChange(ItemChange change, const ItemChangeData & value)
258 if (change == QQuickItem::ItemActiveFocusHasChanged) {
259 if (m_qmlSurface) {
260 m_qmlSurface->setActiveFocus(value.boolValue);
261 }
262 }
265void MirSurfaceItem::updateMirSurfaceVisibility()
267 if (!m_qmlSurface) return;
269 m_qmlSurface->setViewVisibility((qintptr)this, isVisible());
272void MirSurfaceItem::setConsumesInput(bool value)
274 if (m_consumesInput != value) {
275 m_consumesInput = value;
276 Q_EMIT consumesInputChanged(value);
277 }
280int MirSurfaceItem::surfaceWidth() const
282 return m_surfaceWidth;
285void MirSurfaceItem::setSurfaceWidth(int value)
287 if (value != -1 && m_surfaceWidth != value) {
288 m_surfaceWidth = value;
289 Q_EMIT surfaceWidthChanged(m_surfaceWidth);
290 updateSurfaceSize();
291 }
294int MirSurfaceItem::surfaceHeight() const
296 return m_surfaceHeight;
299void MirSurfaceItem::setSurfaceHeight(int value)
301 if (value != -1 && m_surfaceHeight != value) {
302 m_surfaceHeight = value;
303 Q_EMIT surfaceHeightChanged(m_surfaceHeight);
304 updateSurfaceSize();
305 }
308void MirSurfaceItem::updateSurfaceSize()
310 if (m_qmlSurface && m_surfaceWidth > 0 && m_surfaceHeight > 0) {
311 m_qmlSurface->resize(m_surfaceWidth, m_surfaceHeight);
312 }
315void MirSurfaceItem::setFillMode(FillMode value)
317 if (value != m_fillMode) {
318 m_fillMode = value;
319 Q_EMIT fillModeChanged(m_fillMode);
320 }