on 28836cb help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
182 lines 5.3 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 QUrl& qmlFileUrl,
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 m_qmlContentComponent = new QQmlComponent(quickView->engine(), qmlFileUrl);
62 switch (m_qmlContentComponent->status()) {
63 case QQmlComponent::Ready:
64 createQmlContentItem();
65 break;
66 case QQmlComponent::Loading:
67 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
68 this, &MirSurfaceItem::onComponentStatusChanged);
69 break;
70 case QQmlComponent::Error:
71 printComponentErrors();
72 qFatal("MirSurfaceItem: failed to create content component.");
73 break;
74 default:
75 qFatal("MirSurfaceItem: Unhandled component status");
76 }
79MirSurfaceItem::~MirSurfaceItem()
81 qDebug() << "MirSurfaceItem::~MirSurfaceItem() " << name();
82 if (m_session) {
83 m_session->setSurface(nullptr);
84 }
87void MirSurfaceItem::printComponentErrors()
89 QList<QQmlError> errors = m_qmlContentComponent->errors();
90 for (int i = 0; i < errors.count(); ++i) {
91 qDebug() << errors[i];
92 }
95void MirSurfaceItem::release()
97 qDebug() << "MirSurfaceItem::release " << name();
99 if (m_session) {
100 m_session->setSurface(nullptr);
101 }
102 if (!parent()) {
103 deleteLater();
104 }
107void MirSurfaceItem::setOrientationAngle(OrientationAngle angle)
109 if (m_orientationAngle == angle)
110 return;
112 m_orientationAngle = angle;
114 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
115 orientationProp.write(QVariant::fromValue(m_orientationAngle));
117 Q_EMIT orientationAngleChanged(m_orientationAngle);
120void MirSurfaceItem::setSession(Session* session)
122 m_session = session;
125void MirSurfaceItem::setScreenshot(const QUrl& screenshot)
127 m_screenshotUrl = screenshot;
128 if (m_qmlItem) {
129 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
130 screenshotSource.write(QVariant::fromValue(m_screenshotUrl));
131 }
134void MirSurfaceItem::setLive(bool live)
136 if (m_live != live) {
137 m_live = live;
138 Q_EMIT liveChanged(m_live);
139 }
142void MirSurfaceItem::setState(MirSurfaceItem::State newState)
144 if (newState != m_state) {
145 m_state = newState;
146 Q_EMIT stateChanged(newState);
147 }
150void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
152 if (status == QQmlComponent::Ready) {
153 createQmlContentItem();
154 }
157void MirSurfaceItem::createQmlContentItem()
159 qDebug() << "MirSurfaceItem::createQmlContentItem()";
161 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
162 m_qmlItem->setParentItem(this);
164 setImplicitWidth(m_qmlItem->implicitWidth());
165 setImplicitHeight(m_qmlItem->implicitHeight());
167 {
168 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
169 screenshotSource.write(QVariant::fromValue(m_screenshotUrl));
170 }
173void MirSurfaceItem::touchEvent(QTouchEvent * event)
175 if (event->touchPointStates() & Qt::TouchPointPressed) {
176 ++m_touchPressCount;
177 Q_EMIT touchPressCountChanged(m_touchPressCount);
178 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
179 ++m_touchReleaseCount;
180 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
181 }