on 2917119 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
157 lines 4.3 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 "MirSurfaceItemModel.h"
19#include "ApplicationInfo.h"
21#include <QPainter>
22#include <QQmlEngine>
24MirSurfaceItem::MirSurfaceItem(const QString& name,
25 MirSurfaceItem::Type type,
26 MirSurfaceItem::State state,
27 const QUrl& screenshot,
28 QQuickItem *parent)
29 : QQuickPaintedItem(parent)
30 , m_application(nullptr)
31 , m_name(name)
32 , m_type(type)
33 , m_state(state)
34 , m_img(screenshot.isLocalFile() ? screenshot.toLocalFile() : screenshot.toString())
35 , m_parentSurface(nullptr)
36 , m_children(new MirSurfaceItemModel(this))
37 , m_haveInputMethod(false)
39 qDebug() << "MirSurfaceItem::MirSurfaceItem() " << this->name();
41 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
43 // The virtual keyboard (input method) has a big transparent area so that
44 // content behind it show through
45 setFillColor(Qt::transparent);
47 connect(this, &QQuickItem::focusChanged,
48 this, &MirSurfaceItem::onFocusChanged);
51MirSurfaceItem::~MirSurfaceItem()
53 qDebug() << "MirSurfaceItem::~MirSurfaceItem() " << name();
54 Q_EMIT aboutToBeDestroyed();
56 QList<MirSurfaceItem*> children(m_children->list());
57 for (MirSurfaceItem* child : children) {
58 delete child;
59 }
60 if (m_parentSurface) {
61 m_parentSurface->removeChildSurface(this);
62 }
63 if (m_application) {
64 m_application->removeSurface(this);
65 }
66 delete m_children;
69void MirSurfaceItem::paint(QPainter * painter)
71 if (!m_img.isNull()) {
72 painter->drawImage(contentsBoundingRect(), m_img, QRect(QPoint(0,0), m_img.size()));
73 }
76void MirSurfaceItem::release()
78 qDebug() << "MirSurfaceItem::release " << name();
80 if (m_parentSurface) {
81 m_parentSurface->removeChildSurface(this);
82 }
84 if (m_application) {
85 m_application->removeSurface(this);
86 }
87 if (!parent()) {
88 deleteLater();
89 }
92void MirSurfaceItem::setApplication(ApplicationInfo* application)
94 m_application = application;
97void MirSurfaceItem::setParentSurface(MirSurfaceItem* surface)
99 if (m_parentSurface == surface || surface == this)
100 return;
102 m_parentSurface = surface;
103 Q_EMIT parentSurfaceChanged(surface);
106void MirSurfaceItem::addChildSurface(MirSurfaceItem* surface)
108 insertChildSurface(m_children->count(), surface);
111void MirSurfaceItem::insertChildSurface(uint index, MirSurfaceItem* surface)
113 qDebug() << "MirSurfaceItem::insertChildSurface - " << surface->name() << " to " << name() << " @ " << index;
115 surface->setParentSurface(this);
116 m_children->insertSurface(index, surface);
119void MirSurfaceItem::removeChildSurface(MirSurfaceItem* surface)
121 qDebug() << "MirSurfaceItem::removeChildSurface - " << surface->name() << " from " << name();
123 if (m_children->contains(surface)) {
124 m_children->removeSurface(surface);
125 surface->setParentSurface(nullptr);
126 }
129MirSurfaceItemModel* MirSurfaceItem::childSurfaces() const
131 return m_children;
135void MirSurfaceItem::touchEvent(QTouchEvent * event)
137 if (event->type() == QEvent::TouchBegin && hasFocus()) {
138 Q_EMIT inputMethodRequested();
139 m_haveInputMethod = true;
140 }
143void MirSurfaceItem::onFocusChanged()
145 if (!hasFocus() && m_haveInputMethod) {
146 Q_EMIT inputMethodDismissed();
147 m_haveInputMethod = false;
148 }
151void MirSurfaceItem::setState(MirSurfaceItem::State newState)
153 if (newState != m_state) {
154 m_state = newState;
155 Q_EMIT stateChanged(newState);
156 }