on fea8816 help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
154 lines 4.7 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_parentItem(0)
35 , m_surface(0)
37 connect(this, &ApplicationInfo::stateChanged, this, &ApplicationInfo::onStateChanged);
40ApplicationInfo::ApplicationInfo(QObject *parent)
41 : ApplicationInfoInterface(QString(), parent)
42 , m_stage(MainStage)
43 , m_state(Starting)
44 , m_focused(false)
45 , m_fullscreen(false)
46 , m_parentItem(0)
47 , m_surface(0)
49 connect(this, &ApplicationInfo::stateChanged, this, &ApplicationInfo::onStateChanged);
52ApplicationInfo::~ApplicationInfo()
54 if (m_surface) {
55 Q_EMIT SurfaceManager::singleton()->surfaceDestroyed(m_surface);
56 m_surface->deleteLater();
57 setSurface(nullptr);
58 }
61void ApplicationInfo::onStateChanged(State state)
63 if (state == ApplicationInfo::Running) {
64 QTimer::singleShot(1000, this, SLOT(createSurface()));
65 } else if (state == ApplicationInfo::Stopped) {
66 setSurface(nullptr);
67 }
70void ApplicationInfo::createSurface()
72 if (m_surface || state() == ApplicationInfo::Stopped) return;
74 setSurface(new MirSurfaceItem(name(),
75 MirSurfaceItem::Normal,
76 fullscreen() ? MirSurfaceItem::Fullscreen : MirSurfaceItem::Maximized,
77 screenshot()));
80void ApplicationInfo::setSurface(MirSurfaceItem* surface)
82 qDebug() << "Application::setSurface - appId=" << appId() << "surface=" << surface;
83 if (m_surface == surface)
84 return;
86 if (m_surface) {
87 m_surface->setApplication(nullptr);
88 m_surface->setParent(nullptr);
89 SurfaceManager::singleton()->unregisterSurface(m_surface);
90 }
92 m_surface = surface;
94 if (m_surface) {
95 m_surface->setApplication(this);
96 m_surface->setParent(this);
97 SurfaceManager::singleton()->registerSurface(m_surface);
98 }
100 Q_EMIT surfaceChanged(m_surface);
101 SurfaceManager::singleton()->registerSurface(m_surface);
104void ApplicationInfo::addPromptSurface(MirSurfaceItem* surface)
106 qDebug() << "ApplicationInfo::addPromptSurface " << surface->name() << " to " << name();
107 if (surface == m_surface || m_promptSurfaces.contains(surface)) return;
109 surface->setApplication(this);
110 m_promptSurfaces.append(surface);
111 Q_EMIT promptSurfacesChanged();
114void ApplicationInfo::removeSurface(MirSurfaceItem* surface)
116 if (m_surface == surface) {
117 setSurface(nullptr);
118 } else if (m_promptSurfaces.contains(surface)) {
119 qDebug() << "Application::removeSurface " << surface->name() << " from " << name();
121 m_promptSurfaces.removeAll(surface);
122 surface->setApplication(nullptr);
124 Q_EMIT promptSurfacesChanged();
125 }
128QList<MirSurfaceItem*> ApplicationInfo::promptSurfaceList() const
130 return m_promptSurfaces;
133QQmlListProperty<MirSurfaceItem> ApplicationInfo::promptSurfaces()
135 return QQmlListProperty<MirSurfaceItem>(this,
136 0,
137 ApplicationInfo::promptSurfaceCount,
138 ApplicationInfo::promptSurfaceAt);
141int ApplicationInfo::promptSurfaceCount(QQmlListProperty<MirSurfaceItem> *prop)
143 ApplicationInfo *p = qobject_cast<ApplicationInfo*>(prop->object);
144 return p->m_promptSurfaces.count();
147MirSurfaceItem* ApplicationInfo::promptSurfaceAt(QQmlListProperty<MirSurfaceItem> *prop, int index)
149 ApplicationInfo *p = qobject_cast<ApplicationInfo*>(prop->object);
151 if (index < 0 || index >= p->m_promptSurfaces.count())
152 return nullptr;
153 return p->m_promptSurfaces[index];