on e700c19 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
180 lines 5.0 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 <QPainter>
21#include <QQmlEngine>
23MirSurfaceItem::MirSurfaceItem(const QString& name,
24 MirSurfaceItem::Type type,
25 MirSurfaceItem::State state,
26 const QUrl& screenshot,
27 QQuickItem *parent)
28 : QQuickPaintedItem(parent)
29 , m_application(nullptr)
30 , m_name(name)
31 , m_type(type)
32 , m_state(state)
33 , m_img(screenshot.isLocalFile() ? screenshot.toLocalFile() : screenshot.toString())
34 , m_parentSurface(nullptr)
35 , m_haveInputMethod(false)
37 qDebug() << "MirSurfaceItem::MirSurfaceItem() " << this->name();
39 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
41 // The virtual keyboard (input method) has a big transparent area so that
42 // content behind it show through
43 setFillColor(Qt::transparent);
45 connect(this, &QQuickItem::focusChanged,
46 this, &MirSurfaceItem::onFocusChanged);
49MirSurfaceItem::~MirSurfaceItem()
51 qDebug() << "MirSurfaceItem::~MirSurfaceItem() " << name();
53 QList<MirSurfaceItem*> children(m_children);
54 for (MirSurfaceItem* child : children) {
55 child->setParentSurface(nullptr);
56 }
57 if (m_parentSurface)
58 m_parentSurface->removeChildSurface(this);
60 if (m_application)
61 m_application->removeSurface(this);
64void MirSurfaceItem::paint(QPainter * painter)
66 if (!m_img.isNull()) {
67 painter->drawImage(contentsBoundingRect(), m_img, QRect(QPoint(0,0), m_img.size()));
68 }
71void MirSurfaceItem::release()
73 QList<MirSurfaceItem*> children(m_children);
74 for (MirSurfaceItem* child : children) {
75 child->release();
76 }
77 if (m_parentSurface) {
78 m_parentSurface->removeChildSurface(this);
79 }
81 if (m_application) {
82 m_application->removeSurface(this);
83 }
84 if (!parent()) {
85 deleteLater();
86 }
89void MirSurfaceItem::setApplication(ApplicationInfo* application)
91 m_application = application;
94void MirSurfaceItem::setParentSurface(MirSurfaceItem* surface)
96 if (m_parentSurface == surface || surface == this)
97 return;
99 if (m_parentSurface) {
100 m_parentSurface->removeChildSurface(this);
101 }
103 m_parentSurface = surface;
105 if (m_parentSurface) {
106 m_parentSurface->addChildSurface(this);
107 }
108 Q_EMIT parentSurfaceChanged(surface);
111void MirSurfaceItem::addChildSurface(MirSurfaceItem* surface)
113 qDebug() << "MirSurfaceItem::addChildSurface " << surface->name() << " to " << name();
115 m_children.append(surface);
116 Q_EMIT childSurfacesChanged();
119void MirSurfaceItem::removeChildSurface(MirSurfaceItem* surface)
121 qDebug() << "MirSurfaceItem::removeChildSurface " << surface->name() << " from " << name();
123 if (m_children.contains(surface)) {
124 m_children.removeOne(surface);
125 Q_EMIT childSurfacesChanged();
126 }
129QList<MirSurfaceItem*> MirSurfaceItem::childSurfaceList()
131 return m_children;
134QQmlListProperty<MirSurfaceItem> MirSurfaceItem::childSurfaces()
136 return QQmlListProperty<MirSurfaceItem>(this,
137 0,
138 MirSurfaceItem::childSurfaceCount,
139 MirSurfaceItem::childSurfaceAt);
142int MirSurfaceItem::childSurfaceCount(QQmlListProperty<MirSurfaceItem> *prop)
144 MirSurfaceItem *p = qobject_cast<MirSurfaceItem*>(prop->object);
145 return p->m_children.count();
148MirSurfaceItem* MirSurfaceItem::childSurfaceAt(QQmlListProperty<MirSurfaceItem> *prop, int index)
150 MirSurfaceItem *p = qobject_cast<MirSurfaceItem*>(prop->object);
152 if (index < 0 || index >= p->m_children.count())
153 return nullptr;
154 return p->m_children[index];
158void MirSurfaceItem::touchEvent(QTouchEvent * event)
160 if (event->type() == QEvent::TouchBegin && hasFocus()) {
161 Q_EMIT inputMethodRequested();
162 m_haveInputMethod = true;
163 }
166void MirSurfaceItem::onFocusChanged()
168 if (!hasFocus() && m_haveInputMethod) {
169 Q_EMIT inputMethodDismissed();
170 m_haveInputMethod = false;
171 }
174void MirSurfaceItem::setState(MirSurfaceItem::State newState)
176 if (newState != m_state) {
177 m_state = newState;
178 Q_EMIT stateChanged(newState);
179 }