on f7cdbb2 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
251 lines 7.2 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 "ApplicationInfo.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_application(nullptr)
38 , m_name(name)
39 , m_type(type)
40 , m_state(state)
41 , m_parentSurface(nullptr)
42 , m_qmlItem(nullptr)
43 , m_screenshotUrl(screenshot)
45 qDebug() << "MirSurfaceItem::MirSurfaceItem() " << this->name();
47 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
49 connect(this, &QQuickItem::focusChanged,
50 this, &MirSurfaceItem::onFocusChanged);
52 // The assumptions I make here really should hold.
53 QQuickView *quickView =
54 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
56 QString qmlComponentFilePath;
57 if (!qmlFilePath.isEmpty()) {
58 qmlComponentFilePath.append(qmlFilePath);
59 } else {
60 qmlComponentFilePath = QString("%1/Unity/Application/MirSurfaceItem.qml")
61 .arg(mockPluginsDir());
62 }
64 m_qmlContentComponent = new QQmlComponent(quickView->engine(), qmlComponentFilePath);
66 switch (m_qmlContentComponent->status()) {
67 case QQmlComponent::Ready:
68 createQmlContentItem();
69 break;
70 case QQmlComponent::Loading:
71 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
72 this, &MirSurfaceItem::onComponentStatusChanged);
73 break;
74 case QQmlComponent::Error:
75 printComponentErrors();
76 qFatal("MirSurfaceItem: failed to create content component.");
77 break;
78 default:
79 qFatal("MirSurfaceItem: Unhandled component status");
80 }
83MirSurfaceItem::~MirSurfaceItem()
85 qDebug() << "MirSurfaceItem::~MirSurfaceItem() " << name();
87 QList<MirSurfaceItem*> children(m_children);
88 for (MirSurfaceItem* child : children) {
89 child->setParentSurface(nullptr);
90 }
91 if (m_parentSurface)
92 m_parentSurface->removeChildSurface(this);
94 if (m_application)
95 m_application->removeSurface(this);
98void MirSurfaceItem::printComponentErrors()
100 QList<QQmlError> errors = m_qmlContentComponent->errors();
101 for (int i = 0; i < errors.count(); ++i) {
102 qDebug() << errors[i];
103 }
106void MirSurfaceItem::release()
108 QList<MirSurfaceItem*> children(m_children);
109 for (MirSurfaceItem* child : children) {
110 child->release();
111 }
112 if (m_parentSurface) {
113 m_parentSurface->removeChildSurface(this);
114 }
116 if (m_application) {
117 m_application->removeSurface(this);
118 }
119 if (!parent()) {
120 deleteLater();
121 }
124void MirSurfaceItem::setApplication(ApplicationInfo* application)
126 m_application = application;
129void MirSurfaceItem::setParentSurface(MirSurfaceItem* surface)
131 if (m_parentSurface == surface || surface == this)
132 return;
134 if (m_parentSurface) {
135 m_parentSurface->removeChildSurface(this);
136 }
138 m_parentSurface = surface;
140 if (m_parentSurface) {
141 m_parentSurface->addChildSurface(this);
142 }
143 Q_EMIT parentSurfaceChanged(surface);
146void MirSurfaceItem::addChildSurface(MirSurfaceItem* surface)
148 qDebug() << "MirSurfaceItem::addChildSurface " << surface->name() << " to " << name();
150 m_children.append(surface);
151 Q_EMIT childSurfacesChanged();
154void MirSurfaceItem::removeChildSurface(MirSurfaceItem* surface)
156 qDebug() << "MirSurfaceItem::removeChildSurface " << surface->name() << " from " << name();
158 if (m_children.contains(surface)) {
159 m_children.removeOne(surface);
160 Q_EMIT childSurfacesChanged();
161 }
164QList<MirSurfaceItem*> MirSurfaceItem::childSurfaceList()
166 return m_children;
169QQmlListProperty<MirSurfaceItem> MirSurfaceItem::childSurfaces()
171 return QQmlListProperty<MirSurfaceItem>(this,
172 0,
173 MirSurfaceItem::childSurfaceCount,
174 MirSurfaceItem::childSurfaceAt);
177int MirSurfaceItem::childSurfaceCount(QQmlListProperty<MirSurfaceItem> *prop)
179 MirSurfaceItem *p = qobject_cast<MirSurfaceItem*>(prop->object);
180 return p->m_children.count();
183MirSurfaceItem* MirSurfaceItem::childSurfaceAt(QQmlListProperty<MirSurfaceItem> *prop, int index)
185 MirSurfaceItem *p = qobject_cast<MirSurfaceItem*>(prop->object);
187 if (index < 0 || index >= p->m_children.count())
188 return nullptr;
189 return p->m_children[index];
193void MirSurfaceItem::onQmlWantInputMethodChanged()
195 QQmlProperty prop(m_qmlItem, "wantInputMethod");
196 bool wantInputMethod = prop.read().toBool();
198 if (hasFocus() && wantInputMethod) {
199 Q_EMIT inputMethodRequested();
200 }
203void MirSurfaceItem::onFocusChanged()
205 QQmlProperty prop(m_qmlItem, "wantInputMethod");
206 bool wantInputMethod = prop.read().toBool();
208 if (!hasFocus() && wantInputMethod) {
209 Q_EMIT inputMethodDismissed();
210 prop.write(QVariant::fromValue(false));
211 }
214void MirSurfaceItem::setState(MirSurfaceItem::State newState)
216 if (newState != m_state) {
217 m_state = newState;
218 Q_EMIT stateChanged(newState);
219 }
222void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
224 if (status == QQmlComponent::Ready) {
225 createQmlContentItem();
226 }
229void MirSurfaceItem::createQmlContentItem()
231 qDebug() << "MirSurfaceItem::createQmlContentItem()";
233 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
234 m_qmlItem->setParentItem(this);
236 setImplicitWidth(m_qmlItem->implicitWidth());
237 setImplicitHeight(m_qmlItem->implicitHeight());
239 {
240 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
241 screenshotSource.write(QVariant::fromValue(m_screenshotUrl));
242 }
244 {
245 QQmlProperty prop(m_qmlItem, "wantInputMethod");
246 if (prop.type() == QQmlProperty::Property) {
247 bool ok = prop.connectNotifySignal(this, SLOT(onQmlWantInputMethodChanged()));
248 if (!ok) qCritical("MirSurfaceItem: failed to connect to wantInputMethod notify signal");
249 }
250 }