on fcccc72 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
184 lines 5.2 kB history raw
1/*
2 * Copyright (C) 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"
18#include "Session.h"
20#include <paths.h>
22#include <QGuiApplication>
23#include <QQuickView>
24#include <QQmlProperty>
25#include <QQmlEngine>
26#include <QString>
28#include <QDebug>
30MirSurfaceItem::MirSurfaceItem(const QString& name,
31 MirSurfaceItem::Type type,
32 MirSurfaceItem::State state,
33 const QUrl& screenshot,
34 const QString &qmlFilePath,
35 QQuickItem *parent)
36 : QQuickItem(parent)
37 , m_session(nullptr)
38 , m_name(name)
39 , m_type(type)
40 , m_state(state)
41 , m_live(true)
42 , m_orientationAngle(Angle0)
43 , m_qmlItem(nullptr)
44 , m_screenshotUrl(screenshot)
45 , m_touchPressCount(0)
46 , m_touchReleaseCount(0)
48 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
50 // The assumptions I make here really should hold.
51 QQuickView *quickView =
52 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
54 QString qmlComponentFilePath;
55 if (!qmlFilePath.isEmpty()) {
56 qmlComponentFilePath.append(qmlFilePath);
57 } else {
58 qmlComponentFilePath = QString("%1/Unity/Application/MirSurfaceItem.qml")
59 .arg(mockPluginsDir());
60 }
62 m_qmlContentComponent = new QQmlComponent(quickView->engine(), qmlComponentFilePath);
64 switch (m_qmlContentComponent->status()) {
65 case QQmlComponent::Ready:
66 createQmlContentItem();
67 break;
68 case QQmlComponent::Loading:
69 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
70 this, &MirSurfaceItem::onComponentStatusChanged);
71 break;
72 case QQmlComponent::Error:
73 printComponentErrors();
74 qFatal("MirSurfaceItem: failed to create content component.");
75 break;
76 default:
77 qFatal("MirSurfaceItem: Unhandled component status");
78 }
81MirSurfaceItem::~MirSurfaceItem()
83 qDebug() << "MirSurfaceItem::~MirSurfaceItem() " << name();
84 if (m_session) {
85 m_session->setSurface(nullptr);
86 }
89void MirSurfaceItem::printComponentErrors()
91 QList<QQmlError> errors = m_qmlContentComponent->errors();
92 for (int i = 0; i < errors.count(); ++i) {
93 qDebug() << errors[i];
94 }
97void MirSurfaceItem::release()
99 qDebug() << "MirSurfaceItem::release " << name();
101 if (m_session) {
102 m_session->setSurface(nullptr);
103 }
104 if (!parent()) {
105 deleteLater();
106 }
109void MirSurfaceItem::setOrientationAngle(OrientationAngle angle)
111 if (m_orientationAngle == angle)
112 return;
114 m_orientationAngle = angle;
116 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
117 orientationProp.write(QVariant::fromValue(m_orientationAngle));
119 Q_EMIT orientationAngleChanged(m_orientationAngle);
122void MirSurfaceItem::setSession(Session* session)
124 m_session = session;
127void MirSurfaceItem::setScreenshot(const QUrl& screenshot)
129 m_screenshotUrl = screenshot;
130 if (m_qmlItem) {
131 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
132 screenshotSource.write(QVariant::fromValue(m_screenshotUrl));
133 }
136void MirSurfaceItem::setLive(bool live)
138 if (m_live != live) {
139 m_live = live;
140 Q_EMIT liveChanged(m_live);
141 }
144void MirSurfaceItem::setState(MirSurfaceItem::State newState)
146 if (newState != m_state) {
147 m_state = newState;
148 Q_EMIT stateChanged(newState);
149 }
152void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
154 if (status == QQmlComponent::Ready) {
155 createQmlContentItem();
156 }
159void MirSurfaceItem::createQmlContentItem()
161 qDebug() << "MirSurfaceItem::createQmlContentItem()";
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_screenshotUrl));
172 }
175void MirSurfaceItem::touchEvent(QTouchEvent * event)
177 if (event->touchPointStates() & Qt::TouchPointPressed) {
178 ++m_touchPressCount;
179 Q_EMIT touchPressCountChanged(m_touchPressCount);
180 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
181 ++m_touchReleaseCount;
182 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
183 }