on 1bc5ffd help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
190 lines 5.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"
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 setAcceptedMouseButtons(Qt::LeftButton | Qt::MiddleButton | Qt::RightButton |
49 Qt::ExtraButton1 | Qt::ExtraButton2 | Qt::ExtraButton3 | Qt::ExtraButton4 |
50 Qt::ExtraButton5 | Qt::ExtraButton6 | Qt::ExtraButton7 | Qt::ExtraButton8 |
51 Qt::ExtraButton9 | Qt::ExtraButton10 | Qt::ExtraButton11 |
52 Qt::ExtraButton12 | Qt::ExtraButton13);
54 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
56 // The assumptions I make here really should hold.
57 QQuickView *quickView =
58 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
60 QString qmlComponentFilePath;
61 if (!qmlFilePath.isEmpty()) {
62 qmlComponentFilePath.append(qmlFilePath);
63 } else {
64 qmlComponentFilePath = QString("%1/Unity/Application/MirSurfaceItem.qml")
65 .arg(mockPluginsDir());
66 }
68 m_qmlContentComponent = new QQmlComponent(quickView->engine(), qmlComponentFilePath);
70 switch (m_qmlContentComponent->status()) {
71 case QQmlComponent::Ready:
72 createQmlContentItem();
73 break;
74 case QQmlComponent::Loading:
75 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
76 this, &MirSurfaceItem::onComponentStatusChanged);
77 break;
78 case QQmlComponent::Error:
79 printComponentErrors();
80 qFatal("MirSurfaceItem: failed to create content component.");
81 break;
82 default:
83 qFatal("MirSurfaceItem: Unhandled component status");
84 }
87MirSurfaceItem::~MirSurfaceItem()
89 qDebug() << "MirSurfaceItem::~MirSurfaceItem() " << name();
90 if (m_session) {
91 m_session->setSurface(nullptr);
92 }
95void MirSurfaceItem::printComponentErrors()
97 QList<QQmlError> errors = m_qmlContentComponent->errors();
98 for (int i = 0; i < errors.count(); ++i) {
99 qDebug() << errors[i];
100 }
103void MirSurfaceItem::release()
105 qDebug() << "MirSurfaceItem::release " << name();
107 if (m_session) {
108 m_session->setSurface(nullptr);
109 }
110 if (!parent()) {
111 deleteLater();
112 }
115void MirSurfaceItem::setOrientationAngle(OrientationAngle angle)
117 if (m_orientationAngle == angle)
118 return;
120 m_orientationAngle = angle;
122 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
123 orientationProp.write(QVariant::fromValue(m_orientationAngle));
125 Q_EMIT orientationAngleChanged(m_orientationAngle);
128void MirSurfaceItem::setSession(Session* session)
130 m_session = session;
133void MirSurfaceItem::setScreenshot(const QUrl& screenshot)
135 m_screenshotUrl = screenshot;
136 if (m_qmlItem) {
137 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
138 screenshotSource.write(QVariant::fromValue(m_screenshotUrl));
139 }
142void MirSurfaceItem::setLive(bool live)
144 if (m_live != live) {
145 m_live = live;
146 Q_EMIT liveChanged(m_live);
147 }
150void MirSurfaceItem::setState(MirSurfaceItem::State newState)
152 if (newState != m_state) {
153 m_state = newState;
154 Q_EMIT stateChanged(newState);
155 }
158void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
160 if (status == QQmlComponent::Ready) {
161 createQmlContentItem();
162 }
165void MirSurfaceItem::createQmlContentItem()
167 qDebug() << "MirSurfaceItem::createQmlContentItem()";
169 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
170 m_qmlItem->setParentItem(this);
172 setImplicitWidth(m_qmlItem->implicitWidth());
173 setImplicitHeight(m_qmlItem->implicitHeight());
175 {
176 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
177 screenshotSource.write(QVariant::fromValue(m_screenshotUrl));
178 }
181void MirSurfaceItem::touchEvent(QTouchEvent * event)
183 if (event->touchPointStates() & Qt::TouchPointPressed) {
184 ++m_touchPressCount;
185 Q_EMIT touchPressCountChanged(m_touchPressCount);
186 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
187 ++m_touchReleaseCount;
188 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
189 }