on cd4bc68 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
283 lines 8.1 kB history raw
1/*
2 * Copyright (C) 2014-2015 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"
19#include <paths.h>
21#include <QGuiApplication>
22#include <QQuickView>
23#include <QQmlProperty>
24#include <QQmlEngine>
25#include <QString>
27#include <QDebug>
29using namespace unity::shell::application;
31MirSurfaceItem::MirSurfaceItem(QQuickItem *parent)
32 : MirSurfaceItemInterface(parent)
33 , m_qmlSurface(nullptr)
34 , m_qmlItem(nullptr)
35 , m_consumesInput(false)
36 , m_surfaceWidth(0)
37 , m_surfaceHeight(0)
38 , m_touchPressCount(0)
39 , m_touchReleaseCount(0)
41 qDebug() << "MirSurfaceItem::MirSurfaceItem() " << (void*)(this) << name();
42 setAcceptedMouseButtons(Qt::LeftButton | Qt::MiddleButton | Qt::RightButton |
43 Qt::ExtraButton1 | Qt::ExtraButton2 | Qt::ExtraButton3 | Qt::ExtraButton4 |
44 Qt::ExtraButton5 | Qt::ExtraButton6 | Qt::ExtraButton7 | Qt::ExtraButton8 |
45 Qt::ExtraButton9 | Qt::ExtraButton10 | Qt::ExtraButton11 |
46 Qt::ExtraButton12 | Qt::ExtraButton13);
49MirSurfaceItem::~MirSurfaceItem()
51 qDebug() << "MirSurfaceItem::~MirSurfaceItem() " << (void*)(this) << name();
52 setSurface(nullptr);
55void MirSurfaceItem::printComponentErrors()
57 QList<QQmlError> errors = m_qmlContentComponent->errors();
58 for (int i = 0; i < errors.count(); ++i) {
59 qDebug() << errors[i];
60 }
63Mir::Type MirSurfaceItem::type() const
65 if (m_qmlSurface) {
66 return m_qmlSurface->type();
67 } else {
68 return Mir::UnknownType;
69 }
72Mir::State MirSurfaceItem::surfaceState() const
74 if (m_qmlSurface) {
75 return m_qmlSurface->state();
76 } else {
77 return Mir::UnknownState;
78 }
81QString MirSurfaceItem::name() const
83 if (m_qmlSurface) {
84 return m_qmlSurface->name();
85 } else {
86 return QString();
87 }
90bool MirSurfaceItem::live() const
92 if (m_qmlSurface) {
93 return m_qmlSurface->live();
94 } else {
95 return false;
96 }
99Mir::OrientationAngle MirSurfaceItem::orientationAngle() const
101 if (m_qmlSurface) {
102 return m_qmlSurface->orientationAngle();
103 } else {
104 return Mir::Angle0;
105 }
108void MirSurfaceItem::setOrientationAngle(Mir::OrientationAngle angle)
110 if (!m_qmlSurface)
111 return;
113 if (m_qmlSurface->orientationAngle() == angle)
114 return;
116 m_qmlSurface->setOrientationAngle(angle);
118 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
119 orientationProp.write(QVariant::fromValue(m_qmlSurface->orientationAngle()));
122void MirSurfaceItem::updateScreenshot(QUrl screenshotUrl)
124 if (m_qmlItem) {
125 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
126 screenshotSource.write(QVariant::fromValue(screenshotUrl));
127 }
130void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
132 if (status == QQmlComponent::Ready) {
133 createQmlContentItem();
134 }
137void MirSurfaceItem::createQmlContentItem()
139 qDebug() << "MirSurfaceItem::createQmlContentItem()";
141 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
142 m_qmlItem->setParentItem(this);
144 setImplicitWidth(m_qmlItem->implicitWidth());
145 setImplicitHeight(m_qmlItem->implicitHeight());
147 {
148 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
149 screenshotSource.write(QVariant::fromValue(m_qmlSurface->screenshotUrl()));
150 }
153void MirSurfaceItem::touchEvent(QTouchEvent * event)
155 if (event->touchPointStates() & Qt::TouchPointPressed) {
156 ++m_touchPressCount;
157 Q_EMIT touchPressCountChanged(m_touchPressCount);
158 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
159 ++m_touchReleaseCount;
160 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
161 }
164void MirSurfaceItem::setSurface(MirSurfaceInterface* surface)
166 qDebug().nospace() << "MirSurfaceItem::setSurface() this=" << (void*)(this)
167 << " name=" << name()
168 << " surface=" << surface;
170 if (m_qmlSurface == surface) {
171 return;
172 }
174 if (m_qmlSurface) {
175 delete m_qmlItem;
176 m_qmlItem = nullptr;
178 delete m_qmlContentComponent;
179 m_qmlContentComponent = nullptr;
181 disconnect(m_qmlSurface, nullptr, this, nullptr);
182 m_qmlSurface->decrementViewCount();
183 }
185 m_qmlSurface = static_cast<MirSurface*>(surface);
187 if (m_qmlSurface) {
188 m_qmlSurface->incrementViewCount();
190 m_qmlSurface->setActiveFocus(hasActiveFocus());
192 updateSurfaceSize();
194 connect(m_qmlSurface, &MirSurface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
195 connect(m_qmlSurface, &MirSurface::screenshotUrlChanged, this, &MirSurfaceItem::updateScreenshot);
196 connect(m_qmlSurface, &MirSurface::liveChanged, this, &MirSurfaceItem::liveChanged);
197 connect(m_qmlSurface, &MirSurface::stateChanged, this, &MirSurfaceItem::surfaceStateChanged);
199 // The assumptions I make here really should hold.
200 QQuickView *quickView =
201 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
203 QString qmlComponentFilePath;
204 if (!m_qmlSurface->qmlFilePath().isEmpty()) {
205 qmlComponentFilePath.append(m_qmlSurface->qmlFilePath());
206 } else {
207 qmlComponentFilePath = QString("%1/Unity/Application/MirSurfaceItem.qml")
208 .arg(mockPluginsDir());
209 }
211 m_qmlContentComponent = new QQmlComponent(quickView->engine(), qmlComponentFilePath);
213 switch (m_qmlContentComponent->status()) {
214 case QQmlComponent::Ready:
215 createQmlContentItem();
216 break;
217 case QQmlComponent::Loading:
218 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
219 this, &MirSurfaceItem::onComponentStatusChanged);
220 break;
221 case QQmlComponent::Error:
222 printComponentErrors();
223 qFatal("MirSurfaceItem: failed to create content component.");
224 break;
225 default:
226 qFatal("MirSurfaceItem: Unhandled component status");
227 }
228 }
230 Q_EMIT surfaceChanged(m_qmlSurface);
233void MirSurfaceItem::itemChange(ItemChange change, const ItemChangeData & value)
235 if (change == QQuickItem::ItemActiveFocusHasChanged) {
236 if (m_qmlSurface) {
237 m_qmlSurface->setActiveFocus(value.boolValue);
238 }
239 }
242void MirSurfaceItem::setConsumesInput(bool value)
244 if (m_consumesInput != value) {
245 m_consumesInput = value;
246 Q_EMIT consumesInputChanged(value);
247 }
250int MirSurfaceItem::surfaceWidth() const
252 return m_surfaceWidth;
255void MirSurfaceItem::setSurfaceWidth(int value)
257 if (m_surfaceWidth != value) {
258 m_surfaceWidth = value;
259 Q_EMIT surfaceWidthChanged(m_surfaceWidth);
260 updateSurfaceSize();
261 }
264int MirSurfaceItem::surfaceHeight() const
266 return m_surfaceHeight;
269void MirSurfaceItem::setSurfaceHeight(int value)
271 if (m_surfaceHeight != value) {
272 m_surfaceHeight = value;
273 Q_EMIT surfaceHeightChanged(m_surfaceHeight);
274 updateSurfaceSize();
275 }
278void MirSurfaceItem::updateSurfaceSize()
280 if (m_qmlSurface && m_surfaceWidth > 0 && m_surfaceHeight > 0) {
281 m_qmlSurface->resize(m_surfaceWidth, m_surfaceHeight);
282 }