on 3ecd374 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
234 lines 6.7 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_parentSurface(nullptr)
42 , m_children(new MirSurfaceItemModel(this))
43 , m_qmlItem(nullptr)
44 , m_screenshotUrl(screenshot)
46 qDebug() << "MirSurfaceItem::MirSurfaceItem() " << this->name();
48 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
50 connect(this, &QQuickItem::focusChanged,
51 this, &MirSurfaceItem::onFocusChanged);
53 // The assumptions I make here really should hold.
54 QQuickView *quickView =
55 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
57 QString qmlComponentFilePath;
58 if (!qmlFilePath.isEmpty()) {
59 qmlComponentFilePath.append(qmlFilePath);
60 } else {
61 qmlComponentFilePath = QString("%1/Unity/Application/MirSurfaceItem.qml")
62 .arg(mockPluginsDir());
63 }
65 m_qmlContentComponent = new QQmlComponent(quickView->engine(), qmlComponentFilePath);
67 switch (m_qmlContentComponent->status()) {
68 case QQmlComponent::Ready:
69 createQmlContentItem();
70 break;
71 case QQmlComponent::Loading:
72 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
73 this, &MirSurfaceItem::onComponentStatusChanged);
74 break;
75 case QQmlComponent::Error:
76 printComponentErrors();
77 qFatal("MirSurfaceItem: failed to create content component.");
78 break;
79 default:
80 qFatal("MirSurfaceItem: Unhandled component status");
81 }
84MirSurfaceItem::~MirSurfaceItem()
86 qDebug() << "MirSurfaceItem::~MirSurfaceItem() " << name();
87 Q_EMIT aboutToBeDestroyed();
89 QList<MirSurfaceItem*> children(m_children->list());
90 for (MirSurfaceItem* child : children) {
91 delete child;
92 }
93 if (m_parentSurface) {
94 m_parentSurface->removeChildSurface(this);
95 }
96 if (m_session) {
97 m_session->setSurface(nullptr);
98 }
99 delete m_children;
102void MirSurfaceItem::printComponentErrors()
104 QList<QQmlError> errors = m_qmlContentComponent->errors();
105 for (int i = 0; i < errors.count(); ++i) {
106 qDebug() << errors[i];
107 }
110void MirSurfaceItem::release()
112 qDebug() << "MirSurfaceItem::release " << name();
114 if (m_parentSurface) {
115 m_parentSurface->removeChildSurface(this);
116 }
117 if (m_session) {
118 m_session->setSurface(nullptr);
119 }
120 if (!parent()) {
121 deleteLater();
122 }
125void MirSurfaceItem::setSession(Session* session)
127 m_session = session;
130void MirSurfaceItem::setScreenshot(const QUrl& screenshot)
132 m_screenshotUrl = screenshot;
133 if (m_qmlItem) {
134 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
135 screenshotSource.write(QVariant::fromValue(m_screenshotUrl));
136 }
139void MirSurfaceItem::setParentSurface(MirSurfaceItem* surface)
141 if (m_parentSurface == surface || surface == this)
142 return;
144 m_parentSurface = surface;
145 Q_EMIT parentSurfaceChanged(surface);
148void MirSurfaceItem::addChildSurface(MirSurfaceItem* surface)
150 insertChildSurface(m_children->rowCount(), surface);
153void MirSurfaceItem::insertChildSurface(uint index, MirSurfaceItem* surface)
155 qDebug() << "MirSurfaceItem::insertChildSurface - " << surface->name() << " to " << name() << " @ " << index;
157 surface->setParentSurface(this);
158 m_children->insert(index, surface);
161void MirSurfaceItem::removeChildSurface(MirSurfaceItem* surface)
163 qDebug() << "MirSurfaceItem::removeChildSurface - " << surface->name() << " from " << name();
165 if (m_children->contains(surface)) {
166 m_children->remove(surface);
167 surface->setParentSurface(nullptr);
168 }
171MirSurfaceItemModel* MirSurfaceItem::childSurfaces() const
173 return m_children;
176void MirSurfaceItem::onQmlWantInputMethodChanged()
178 QQmlProperty prop(m_qmlItem, "wantInputMethod");
179 bool wantInputMethod = prop.read().toBool();
181 if (hasFocus() && wantInputMethod) {
182 Q_EMIT inputMethodRequested();
183 }
186void MirSurfaceItem::onFocusChanged()
188 QQmlProperty prop(m_qmlItem, "wantInputMethod");
189 bool wantInputMethod = prop.read().toBool();
191 if (!hasFocus() && wantInputMethod) {
192 Q_EMIT inputMethodDismissed();
193 prop.write(QVariant::fromValue(false));
194 }
197void MirSurfaceItem::setState(MirSurfaceItem::State newState)
199 if (newState != m_state) {
200 m_state = newState;
201 Q_EMIT stateChanged(newState);
202 }
205void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
207 if (status == QQmlComponent::Ready) {
208 createQmlContentItem();
209 }
212void MirSurfaceItem::createQmlContentItem()
214 qDebug() << "MirSurfaceItem::createQmlContentItem()";
216 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
217 m_qmlItem->setParentItem(this);
219 setImplicitWidth(m_qmlItem->implicitWidth());
220 setImplicitHeight(m_qmlItem->implicitHeight());
222 {
223 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
224 screenshotSource.write(QVariant::fromValue(m_screenshotUrl));
225 }
227 {
228 QQmlProperty prop(m_qmlItem, "wantInputMethod");
229 if (prop.type() == QQmlProperty::Property) {
230 bool ok = prop.connectNotifySignal(this, SLOT(onQmlWantInputMethodChanged()));
231 if (!ok) qCritical("MirSurfaceItem: failed to connect to wantInputMethod notify signal");
232 }
233 }