on 24b9982 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
330 lines 9.2 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::ShellChrome MirSurfaceItem::shellChrome() const
105 if (m_qmlSurface) {
106 return m_qmlSurface->shellChrome();
107 } else {
108 return Mir::NormalChrome;
109 }
112Mir::OrientationAngle MirSurfaceItem::orientationAngle() const
114 if (m_qmlSurface) {
115 return m_qmlSurface->orientationAngle();
116 } else {
117 return Mir::Angle0;
118 }
121void MirSurfaceItem::setOrientationAngle(Mir::OrientationAngle angle)
123 if (!m_qmlSurface)
124 return;
126 if (m_qmlSurface->orientationAngle() == angle)
127 return;
129 m_qmlSurface->setOrientationAngle(angle);
131 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
132 orientationProp.write(QVariant::fromValue(m_qmlSurface->orientationAngle()));
135void MirSurfaceItem::updateScreenshot(QUrl screenshotUrl)
137 if (m_qmlItem) {
138 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
139 screenshotSource.write(QVariant::fromValue(screenshotUrl));
140 }
143void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
145 if (status == QQmlComponent::Ready) {
146 createQmlContentItem();
147 }
150void MirSurfaceItem::createQmlContentItem()
152// qDebug() << "MirSurfaceItem::createQmlContentItem()";
154 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
155 m_qmlItem->setParentItem(this);
157 setImplicitWidth(m_qmlItem->implicitWidth());
158 setImplicitHeight(m_qmlItem->implicitHeight());
160 {
161 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
162 screenshotSource.write(QVariant::fromValue(m_qmlSurface->screenshotUrl()));
163 }
166void MirSurfaceItem::touchEvent(QTouchEvent * event)
168 if (event->touchPointStates() & Qt::TouchPointPressed) {
169 ++m_touchPressCount;
170 Q_EMIT touchPressCountChanged(m_touchPressCount);
171 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
172 ++m_touchReleaseCount;
173 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
174 }
177void MirSurfaceItem::mousePressEvent(QMouseEvent * event)
179 m_mousePressCount++;
180 Q_EMIT mousePressCountChanged(m_mousePressCount);
181 event->accept();
184void MirSurfaceItem::mouseMoveEvent(QMouseEvent * event)
186 event->accept();
189void MirSurfaceItem::mouseReleaseEvent(QMouseEvent * event)
191 m_mouseReleaseCount++;
192 Q_EMIT mouseReleaseCountChanged(m_mouseReleaseCount);
193 event->accept();
196void MirSurfaceItem::setSurface(MirSurfaceInterface* surface)
198// qDebug().nospace() << "MirSurfaceItem::setSurface() this=" << (void*)(this)
199// << " name=" << name()
200// << " surface=" << surface;
202 if (m_qmlSurface == surface) {
203 return;
204 }
206 if (m_qmlSurface) {
207 delete m_qmlItem;
208 m_qmlItem = nullptr;
210 delete m_qmlContentComponent;
211 m_qmlContentComponent = nullptr;
213 disconnect(m_qmlSurface, nullptr, this, nullptr);
214 m_qmlSurface->unregisterView((qintptr)this);
215 }
217 m_qmlSurface = static_cast<MirSurface*>(surface);
219 if (m_qmlSurface) {
220 m_qmlSurface->registerView((qintptr)this);
222 m_qmlSurface->setActiveFocus(hasActiveFocus());
224 updateSurfaceSize();
225 updateMirSurfaceVisibility();
227 connect(m_qmlSurface, &MirSurface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
228 connect(m_qmlSurface, &MirSurface::screenshotUrlChanged, this, &MirSurfaceItem::updateScreenshot);
229 connect(m_qmlSurface, &MirSurface::liveChanged, this, &MirSurfaceItem::liveChanged);
230 connect(m_qmlSurface, &MirSurface::stateChanged, this, &MirSurfaceItem::surfaceStateChanged);
232 // The assumptions I make here really should hold.
233 QQuickView *quickView =
234 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
236 QUrl qmlComponentFilePath;
237 if (!m_qmlSurface->qmlFilePath().isEmpty()) {
238 qmlComponentFilePath = m_qmlSurface->qmlFilePath();
239 } else {
240 qmlComponentFilePath = QUrl("qrc:///Unity/Application/MirSurfaceItem.qml");
241 }
243 m_qmlContentComponent = new QQmlComponent(quickView->engine(), qmlComponentFilePath);
245 switch (m_qmlContentComponent->status()) {
246 case QQmlComponent::Ready:
247 createQmlContentItem();
248 break;
249 case QQmlComponent::Loading:
250 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
251 this, &MirSurfaceItem::onComponentStatusChanged);
252 break;
253 case QQmlComponent::Error:
254 printComponentErrors();
255 qFatal("MirSurfaceItem: failed to create content component.");
256 break;
257 default:
258 qFatal("MirSurfaceItem: Unhandled component status");
259 }
260 }
262 Q_EMIT surfaceChanged(m_qmlSurface);
265void MirSurfaceItem::itemChange(ItemChange change, const ItemChangeData & value)
267 if (change == QQuickItem::ItemActiveFocusHasChanged) {
268 if (m_qmlSurface) {
269 m_qmlSurface->setActiveFocus(value.boolValue);
270 }
271 }
274void MirSurfaceItem::updateMirSurfaceVisibility()
276 if (!m_qmlSurface) return;
278 m_qmlSurface->setViewVisibility((qintptr)this, isVisible());
281void MirSurfaceItem::setConsumesInput(bool value)
283 if (m_consumesInput != value) {
284 m_consumesInput = value;
285 Q_EMIT consumesInputChanged(value);
286 }
289int MirSurfaceItem::surfaceWidth() const
291 return m_surfaceWidth;
294void MirSurfaceItem::setSurfaceWidth(int value)
296 if (value != -1 && m_surfaceWidth != value) {
297 m_surfaceWidth = value;
298 Q_EMIT surfaceWidthChanged(m_surfaceWidth);
299 updateSurfaceSize();
300 }
303int MirSurfaceItem::surfaceHeight() const
305 return m_surfaceHeight;
308void MirSurfaceItem::setSurfaceHeight(int value)
310 if (value != -1 && m_surfaceHeight != value) {
311 m_surfaceHeight = value;
312 Q_EMIT surfaceHeightChanged(m_surfaceHeight);
313 updateSurfaceSize();
314 }
317void MirSurfaceItem::updateSurfaceSize()
319 if (m_qmlSurface && m_surfaceWidth > 0 && m_surfaceHeight > 0) {
320 m_qmlSurface->resize(m_surfaceWidth, m_surfaceHeight);
321 }
324void MirSurfaceItem::setFillMode(FillMode value)
326 if (value != m_fillMode) {
327 m_fillMode = value;
328 Q_EMIT fillModeChanged(m_fillMode);
329 }