on b60e2fc help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
201 lines 5.8 kB history raw
1/*
2 * Copyright (C) 2014 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_orientation(Qt::PortraitOrientation)
43 , m_touchPressCount(0)
44 , m_touchReleaseCount(0)
45 , m_qmlItem(nullptr)
46 , m_screenshotUrl(screenshot)
48 qDebug() << "MirSurfaceItem::MirSurfaceItem() " << this->name();
50 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
52 connect(this, &QQuickItem::focusChanged,
53 this, &MirSurfaceItem::onFocusChanged);
55 // The assumptions I make here really should hold.
56 QQuickView *quickView =
57 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
59 QString qmlComponentFilePath;
60 if (!qmlFilePath.isEmpty()) {
61 qmlComponentFilePath.append(qmlFilePath);
62 } else {
63 qmlComponentFilePath = QString("%1/Unity/Application/MirSurfaceItem.qml")
64 .arg(mockPluginsDir());
65 }
67 m_qmlContentComponent = new QQmlComponent(quickView->engine(), qmlComponentFilePath);
69 switch (m_qmlContentComponent->status()) {
70 case QQmlComponent::Ready:
71 createQmlContentItem();
72 break;
73 case QQmlComponent::Loading:
74 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
75 this, &MirSurfaceItem::onComponentStatusChanged);
76 break;
77 case QQmlComponent::Error:
78 printComponentErrors();
79 qFatal("MirSurfaceItem: failed to create content component.");
80 break;
81 default:
82 qFatal("MirSurfaceItem: Unhandled component status");
83 }
86MirSurfaceItem::~MirSurfaceItem()
88 qDebug() << "MirSurfaceItem::~MirSurfaceItem() " << name();
89 if (m_session) {
90 m_session->setSurface(nullptr);
91 }
94void MirSurfaceItem::printComponentErrors()
96 QList<QQmlError> errors = m_qmlContentComponent->errors();
97 for (int i = 0; i < errors.count(); ++i) {
98 qDebug() << errors[i];
99 }
102void MirSurfaceItem::release()
104 qDebug() << "MirSurfaceItem::release " << name();
106 if (m_session) {
107 m_session->setSurface(nullptr);
108 }
109 if (!parent()) {
110 deleteLater();
111 }
114void MirSurfaceItem::setOrientation(const Qt::ScreenOrientation orientation)
116 if (m_orientation == orientation)
117 return;
119 m_orientation = orientation;
121 QQmlProperty orientationProp(m_qmlItem, "orientation");
122 orientationProp.write(QVariant::fromValue(orientation));
124 Q_EMIT orientationChanged();
127void MirSurfaceItem::setSession(Session* session)
129 m_session = session;
132void MirSurfaceItem::setScreenshot(const QUrl& screenshot)
134 m_screenshotUrl = screenshot;
135 if (m_qmlItem) {
136 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
137 screenshotSource.write(QVariant::fromValue(m_screenshotUrl));
138 }
141void MirSurfaceItem::setLive(bool live)
143 if (m_live != live) {
144 m_live = live;
145 Q_EMIT liveChanged(m_live);
146 }
149void MirSurfaceItem::onFocusChanged()
151 if (!hasFocus()) {
152 // Causes a crash in tst_Shell.qml, inside the mock Unity.Application itself.
153 // Didn't have time to debug yet.
154 //Q_EMIT inputMethodDismissed();
155 }
158void MirSurfaceItem::setState(MirSurfaceItem::State newState)
160 if (newState != m_state) {
161 m_state = newState;
162 Q_EMIT stateChanged(newState);
163 }
166void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
168 if (status == QQmlComponent::Ready) {
169 createQmlContentItem();
170 }
173void MirSurfaceItem::createQmlContentItem()
175 qDebug() << "MirSurfaceItem::createQmlContentItem()";
177 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
178 m_qmlItem->setParentItem(this);
180 setImplicitWidth(m_qmlItem->implicitWidth());
181 setImplicitHeight(m_qmlItem->implicitHeight());
183 {
184 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
185 screenshotSource.write(QVariant::fromValue(m_screenshotUrl));
186 }
189void MirSurfaceItem::touchEvent(QTouchEvent * event)
191 if (event->touchPointStates() & Qt::TouchPointPressed) {
192 ++m_touchPressCount;
193 Q_EMIT touchPressCountChanged(m_touchPressCount);
194 // Causes a crash in tst_Shell.qml, inside the mock Unity.Application itself.
195 // Didn't have time to debug yet.
196 // Q_EMIT inputMethodRequested();
197 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
198 ++m_touchReleaseCount;
199 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
200 }