on e84df84 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
297 lines 8.3 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::mousePressEvent(QMouseEvent * event)
166 event->accept();
169void MirSurfaceItem::mouseMoveEvent(QMouseEvent * event)
171 event->accept();
174void MirSurfaceItem::mouseReleaseEvent(QMouseEvent * event)
176 event->accept();
179void MirSurfaceItem::setSurface(MirSurfaceInterface* surface)
181// qDebug().nospace() << "MirSurfaceItem::setSurface() this=" << (void*)(this)
182// << " name=" << name()
183// << " surface=" << surface;
185 if (m_qmlSurface == surface) {
186 return;
187 }
189 if (m_qmlSurface) {
190 delete m_qmlItem;
191 m_qmlItem = nullptr;
193 delete m_qmlContentComponent;
194 m_qmlContentComponent = nullptr;
196 disconnect(m_qmlSurface, nullptr, this, nullptr);
197 m_qmlSurface->decrementViewCount();
198 }
200 m_qmlSurface = static_cast<MirSurface*>(surface);
202 if (m_qmlSurface) {
203 m_qmlSurface->incrementViewCount();
205 m_qmlSurface->setActiveFocus(hasActiveFocus());
207 updateSurfaceSize();
209 connect(m_qmlSurface, &MirSurface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
210 connect(m_qmlSurface, &MirSurface::screenshotUrlChanged, this, &MirSurfaceItem::updateScreenshot);
211 connect(m_qmlSurface, &MirSurface::liveChanged, this, &MirSurfaceItem::liveChanged);
212 connect(m_qmlSurface, &MirSurface::stateChanged, this, &MirSurfaceItem::surfaceStateChanged);
214 // The assumptions I make here really should hold.
215 QQuickView *quickView =
216 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
218 QUrl qmlComponentFilePath;
219 if (!m_qmlSurface->qmlFilePath().isEmpty()) {
220 qmlComponentFilePath = m_qmlSurface->qmlFilePath();
221 } else {
222 qmlComponentFilePath = QUrl("qrc:///Unity/Application/MirSurfaceItem.qml");
223 }
225 m_qmlContentComponent = new QQmlComponent(quickView->engine(), qmlComponentFilePath);
227 switch (m_qmlContentComponent->status()) {
228 case QQmlComponent::Ready:
229 createQmlContentItem();
230 break;
231 case QQmlComponent::Loading:
232 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
233 this, &MirSurfaceItem::onComponentStatusChanged);
234 break;
235 case QQmlComponent::Error:
236 printComponentErrors();
237 qFatal("MirSurfaceItem: failed to create content component.");
238 break;
239 default:
240 qFatal("MirSurfaceItem: Unhandled component status");
241 }
242 }
244 Q_EMIT surfaceChanged(m_qmlSurface);
247void MirSurfaceItem::itemChange(ItemChange change, const ItemChangeData & value)
249 if (change == QQuickItem::ItemActiveFocusHasChanged) {
250 if (m_qmlSurface) {
251 m_qmlSurface->setActiveFocus(value.boolValue);
252 }
253 }
256void MirSurfaceItem::setConsumesInput(bool value)
258 if (m_consumesInput != value) {
259 m_consumesInput = value;
260 Q_EMIT consumesInputChanged(value);
261 }
264int MirSurfaceItem::surfaceWidth() const
266 return m_surfaceWidth;
269void MirSurfaceItem::setSurfaceWidth(int value)
271 if (value != -1 && m_surfaceWidth != value) {
272 m_surfaceWidth = value;
273 Q_EMIT surfaceWidthChanged(m_surfaceWidth);
274 updateSurfaceSize();
275 }
278int MirSurfaceItem::surfaceHeight() const
280 return m_surfaceHeight;
283void MirSurfaceItem::setSurfaceHeight(int value)
285 if (value != -1 && m_surfaceHeight != value) {
286 m_surfaceHeight = value;
287 Q_EMIT surfaceHeightChanged(m_surfaceHeight);
288 updateSurfaceSize();
289 }
292void MirSurfaceItem::updateSurfaceSize()
294 if (m_qmlSurface && m_surfaceWidth > 0 && m_surfaceHeight > 0) {
295 m_qmlSurface->resize(m_surfaceWidth, m_surfaceHeight);
296 }