on fa03a39 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
172 lines 4.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 "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 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
39 // The virtual keyboard (input method) has a big transparent area so that
40 // content behind it show through
41 setFillColor(Qt::transparent);
43 connect(this, &QQuickItem::focusChanged,
44 this, &MirSurfaceItem::onFocusChanged);
47MirSurfaceItem::~MirSurfaceItem()
49 QList<MirSurfaceItem*> children(m_children);
50 for (MirSurfaceItem* child : children) {
51 child->setParentSurface(nullptr);
52 }
53 if (m_parentSurface)
54 m_parentSurface->removeChildSurface(this);
56 if (m_application)
57 m_application->setSurface(nullptr);
60void MirSurfaceItem::paint(QPainter * painter)
62 if (!m_img.isNull()) {
63 painter->drawImage(contentsBoundingRect(), m_img, QRect(QPoint(0,0), m_img.size()));
64 }
67void MirSurfaceItem::release()
69 QList<MirSurfaceItem*> children(m_children);
70 for (MirSurfaceItem* child : children) {
71 child->release();
72 }
73 if (m_parentSurface) {
74 m_parentSurface->removeChildSurface(this);
75 }
77 if (m_application) {
78 m_application->setSurface(nullptr);
79 }
80 if (!parent()) {
81 deleteLater();
82 }
85void MirSurfaceItem::setApplication(ApplicationInfo* application)
87 m_application = application;
90void MirSurfaceItem::setParentSurface(MirSurfaceItem* surface)
92 if (m_parentSurface == surface || surface == this)
93 return;
95 if (m_parentSurface) {
96 m_parentSurface->removeChildSurface(this);
97 }
99 m_parentSurface = surface;
101 if (m_parentSurface) {
102 m_parentSurface->addChildSurface(this);
103 }
104 Q_EMIT parentSurfaceChanged(surface);
107void MirSurfaceItem::removeChildSurface(MirSurfaceItem* surface)
109 if (m_children.contains(surface)) {
110 m_children.removeOne(surface);
111 Q_EMIT childSurfacesChanged();
112 }
115void MirSurfaceItem::addChildSurface(MirSurfaceItem* surface)
117 m_children.append(surface);
118 Q_EMIT childSurfacesChanged();
121QList<MirSurfaceItem*> MirSurfaceItem::childSurfaceList()
123 return m_children;
126QQmlListProperty<MirSurfaceItem> MirSurfaceItem::childSurfaces()
128 return QQmlListProperty<MirSurfaceItem>(this,
129 0,
130 MirSurfaceItem::childSurfaceCount,
131 MirSurfaceItem::childSurfaceAt);
134int MirSurfaceItem::childSurfaceCount(QQmlListProperty<MirSurfaceItem> *prop)
136 MirSurfaceItem *p = qobject_cast<MirSurfaceItem*>(prop->object);
137 return p->m_children.count();
140MirSurfaceItem* MirSurfaceItem::childSurfaceAt(QQmlListProperty<MirSurfaceItem> *prop, int index)
142 MirSurfaceItem *p = qobject_cast<MirSurfaceItem*>(prop->object);
144 if (index < 0 || index >= p->m_children.count())
145 return nullptr;
146 return p->m_children[index];
150void MirSurfaceItem::touchEvent(QTouchEvent * event)
152 if (event->type() == QEvent::TouchBegin && hasFocus()) {
153 Q_EMIT inputMethodRequested();
154 m_haveInputMethod = true;
155 }
158void MirSurfaceItem::onFocusChanged()
160 if (!hasFocus() && m_haveInputMethod) {
161 Q_EMIT inputMethodDismissed();
162 m_haveInputMethod = false;
163 }
166void MirSurfaceItem::setState(MirSurfaceItem::State newState)
168 if (newState != m_state) {
169 m_state = newState;
170 Q_EMIT stateChanged(newState);
171 }