on 28f8655 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
207 lines 6.1 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 setAcceptedMouseButtons(Qt::LeftButton | Qt::MiddleButton | Qt::RightButton |
51 Qt::ExtraButton1 | Qt::ExtraButton2 | Qt::ExtraButton3 | Qt::ExtraButton4 |
52 Qt::ExtraButton5 | Qt::ExtraButton6 | Qt::ExtraButton7 | Qt::ExtraButton8 |
53 Qt::ExtraButton9 | Qt::ExtraButton10 | Qt::ExtraButton11 |
54 Qt::ExtraButton12 | Qt::ExtraButton13);
56 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
58 connect(this, &QQuickItem::focusChanged,
59 this, &MirSurfaceItem::onFocusChanged);
61 // The assumptions I make here really should hold.
62 QQuickView *quickView =
63 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
65 QString qmlComponentFilePath;
66 if (!qmlFilePath.isEmpty()) {
67 qmlComponentFilePath.append(qmlFilePath);
68 } else {
69 qmlComponentFilePath = QString("%1/Unity/Application/MirSurfaceItem.qml")
70 .arg(mockPluginsDir());
71 }
73 m_qmlContentComponent = new QQmlComponent(quickView->engine(), qmlComponentFilePath);
75 switch (m_qmlContentComponent->status()) {
76 case QQmlComponent::Ready:
77 createQmlContentItem();
78 break;
79 case QQmlComponent::Loading:
80 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
81 this, &MirSurfaceItem::onComponentStatusChanged);
82 break;
83 case QQmlComponent::Error:
84 printComponentErrors();
85 qFatal("MirSurfaceItem: failed to create content component.");
86 break;
87 default:
88 qFatal("MirSurfaceItem: Unhandled component status");
89 }
92MirSurfaceItem::~MirSurfaceItem()
94 qDebug() << "MirSurfaceItem::~MirSurfaceItem() " << name();
95 if (m_session) {
96 m_session->setSurface(nullptr);
97 }
100void MirSurfaceItem::printComponentErrors()
102 QList<QQmlError> errors = m_qmlContentComponent->errors();
103 for (int i = 0; i < errors.count(); ++i) {
104 qDebug() << errors[i];
105 }
108void MirSurfaceItem::release()
110 qDebug() << "MirSurfaceItem::release " << name();
112 if (m_session) {
113 m_session->setSurface(nullptr);
114 }
115 if (!parent()) {
116 deleteLater();
117 }
120void MirSurfaceItem::setOrientation(const Qt::ScreenOrientation orientation)
122 if (m_orientation == orientation)
123 return;
125 m_orientation = orientation;
127 QQmlProperty orientationProp(m_qmlItem, "orientation");
128 orientationProp.write(QVariant::fromValue(orientation));
130 Q_EMIT orientationChanged();
133void MirSurfaceItem::setSession(Session* session)
135 m_session = session;
138void MirSurfaceItem::setScreenshot(const QUrl& screenshot)
140 m_screenshotUrl = screenshot;
141 if (m_qmlItem) {
142 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
143 screenshotSource.write(QVariant::fromValue(m_screenshotUrl));
144 }
147void MirSurfaceItem::setLive(bool live)
149 if (m_live != live) {
150 m_live = live;
151 Q_EMIT liveChanged(m_live);
152 }
155void MirSurfaceItem::onFocusChanged()
157 if (!hasFocus()) {
158 // Causes a crash in tst_Shell.qml, inside the mock Unity.Application itself.
159 // Didn't have time to debug yet.
160 //Q_EMIT inputMethodDismissed();
161 }
164void MirSurfaceItem::setState(MirSurfaceItem::State newState)
166 if (newState != m_state) {
167 m_state = newState;
168 Q_EMIT stateChanged(newState);
169 }
172void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
174 if (status == QQmlComponent::Ready) {
175 createQmlContentItem();
176 }
179void MirSurfaceItem::createQmlContentItem()
181 qDebug() << "MirSurfaceItem::createQmlContentItem()";
183 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
184 m_qmlItem->setParentItem(this);
186 setImplicitWidth(m_qmlItem->implicitWidth());
187 setImplicitHeight(m_qmlItem->implicitHeight());
189 {
190 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
191 screenshotSource.write(QVariant::fromValue(m_screenshotUrl));
192 }
195void MirSurfaceItem::touchEvent(QTouchEvent * event)
197 if (event->touchPointStates() & Qt::TouchPointPressed) {
198 ++m_touchPressCount;
199 Q_EMIT touchPressCountChanged(m_touchPressCount);
200 // Causes a crash in tst_Shell.qml, inside the mock Unity.Application itself.
201 // Didn't have time to debug yet.
202 // Q_EMIT inputMethodRequested();
203 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
204 ++m_touchReleaseCount;
205 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
206 }