on 713e430 help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
162 lines 4.5 kB history raw
1/*
2 * Copyright (C) 2013-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 "ApplicationInfo.h"
18#include "MirSurfaceItem.h"
19#include "SurfaceManager.h"
21#include <QGuiApplication>
22#include <QQuickItem>
23#include <QQuickView>
24#include <QQmlComponent>
25#include <QTimer>
27ApplicationInfo::ApplicationInfo(const QString &appId, QObject *parent)
28 : ApplicationInfoInterface(appId, parent)
29 , m_appId(appId)
30 , m_stage(MainStage)
31 , m_state(Starting)
32 , m_focused(false)
33 , m_fullscreen(false)
34 , m_windowItem(0)
35 , m_windowComponent(0)
36 , m_parentItem(0)
37 , m_surface(0)
39 connect(this, &ApplicationInfo::stateChanged, this, &ApplicationInfo::onStateChanged);
42ApplicationInfo::ApplicationInfo(QObject *parent)
43 : ApplicationInfoInterface(QString(), parent)
44 , m_stage(MainStage)
45 , m_state(Starting)
46 , m_focused(false)
47 , m_fullscreen(false)
48 , m_windowItem(0)
49 , m_windowComponent(0)
50 , m_parentItem(0)
51 , m_surface(0)
53 connect(this, &ApplicationInfo::stateChanged, this, &ApplicationInfo::onStateChanged);
56ApplicationInfo::~ApplicationInfo()
58 if (m_surface) {
59 Q_EMIT SurfaceManager::singleton()->surfaceDestroyed(m_surface);
60 m_surface->deleteLater();
61 }
64void ApplicationInfo::onWindowComponentStatusChanged(QQmlComponent::Status status)
66 if (status == QQmlComponent::Ready && !m_windowItem)
67 doCreateWindowItem();
70void ApplicationInfo::onStateChanged(State state)
72 if (state == ApplicationInfo::Running) {
73 QTimer::singleShot(1000, this, SLOT(createSurface()));
74 } else if (state == ApplicationInfo::Stopped) {
75 setSurface(nullptr);
76 }
79void ApplicationInfo::createSurface()
81 if (m_surface || state() == ApplicationInfo::Stopped) return;
83 setSurface(new MirSurfaceItem(name(),
84 MirSurfaceItem::Normal,
85 fullscreen() ? MirSurfaceItem::Fullscreen : MirSurfaceItem::Maximized,
86 screenshot()));
89void ApplicationInfo::setSurface(MirSurfaceItem* surface)
91 if (m_surface == surface)
92 return;
94 if (m_surface) {
95 m_surface->setApplication(nullptr);
96 m_surface->setParent(nullptr);
97 SurfaceManager::singleton()->unregisterSurface(m_surface);
98 }
100 m_surface = surface;
102 if (m_surface) {
103 m_surface->setApplication(this);
104 m_surface->setParent(this);
105 SurfaceManager::singleton()->registerSurface(m_surface);
106 }
108 Q_EMIT surfaceChanged(m_surface);
109 SurfaceManager::singleton()->registerSurface(m_surface);
112void ApplicationInfo::createWindowComponent()
114 // The assumptions I make here really should hold.
115 QQuickView *quickView =
116 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
118 QQmlEngine *engine = quickView->engine();
120 m_windowComponent = new QQmlComponent(engine, this);
121 m_windowComponent->setData(m_windowQml.toLatin1(), QUrl());
124void ApplicationInfo::doCreateWindowItem()
126 m_windowItem = qobject_cast<QQuickItem *>(m_windowComponent->create());
127 m_windowItem->setParentItem(m_parentItem);
130void ApplicationInfo::createWindowItem()
132 if (!m_windowComponent)
133 createWindowComponent();
135 // only create the windowItem once the component is ready
136 if (!m_windowComponent->isReady()) {
137 connect(m_windowComponent, &QQmlComponent::statusChanged,
138 this, &ApplicationInfo::onWindowComponentStatusChanged);
139 } else {
140 doCreateWindowItem();
141 }
144void ApplicationInfo::showWindow(QQuickItem *parent)
146 m_parentItem = parent;
148 if (!m_windowItem)
149 createWindowItem();
151 if (m_windowItem) {
152 m_windowItem->setVisible(true);
153 }
156void ApplicationInfo::hideWindow()
158 if (!m_windowItem)
159 return;
161 m_windowItem->setVisible(false);