on 98c1952 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
315 lines 8.8 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);
48 connect(this, &QQuickItem::visibleChanged, this, &MirSurfaceItem::updateMirSurfaceVisibility);
51MirSurfaceItem::~MirSurfaceItem()
53// qDebug() << "MirSurfaceItem::~MirSurfaceItem() " << (void*)(this) << name();
54 setSurface(nullptr);
57void MirSurfaceItem::printComponentErrors()
59 QList<QQmlError> errors = m_qmlContentComponent->errors();
60 for (int i = 0; i < errors.count(); ++i) {
61 qDebug() << errors[i];
62 }
65Mir::Type MirSurfaceItem::type() const
67 if (m_qmlSurface) {
68 return m_qmlSurface->type();
69 } else {
70 return Mir::UnknownType;
71 }
74Mir::State MirSurfaceItem::surfaceState() const
76 if (m_qmlSurface) {
77 return m_qmlSurface->state();
78 } else {
79 return Mir::UnknownState;
80 }
83QString MirSurfaceItem::name() const
85 if (m_qmlSurface) {
86 return m_qmlSurface->name();
87 } else {
88 return QString();
89 }
92bool MirSurfaceItem::live() const
94 if (m_qmlSurface) {
95 return m_qmlSurface->live();
96 } else {
97 return false;
98 }
101Mir::OrientationAngle MirSurfaceItem::orientationAngle() const
103 if (m_qmlSurface) {
104 return m_qmlSurface->orientationAngle();
105 } else {
106 return Mir::Angle0;
107 }
110void MirSurfaceItem::setOrientationAngle(Mir::OrientationAngle angle)
112 if (!m_qmlSurface)
113 return;
115 if (m_qmlSurface->orientationAngle() == angle)
116 return;
118 m_qmlSurface->setOrientationAngle(angle);
120 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
121 orientationProp.write(QVariant::fromValue(m_qmlSurface->orientationAngle()));
124void MirSurfaceItem::updateScreenshot(QUrl screenshotUrl)
126 if (m_qmlItem) {
127 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
128 screenshotSource.write(QVariant::fromValue(screenshotUrl));
129 }
132void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
134 if (status == QQmlComponent::Ready) {
135 createQmlContentItem();
136 }
139void MirSurfaceItem::createQmlContentItem()
141// qDebug() << "MirSurfaceItem::createQmlContentItem()";
143 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
144 m_qmlItem->setParentItem(this);
146 setImplicitWidth(m_qmlItem->implicitWidth());
147 setImplicitHeight(m_qmlItem->implicitHeight());
149 {
150 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
151 screenshotSource.write(QVariant::fromValue(m_qmlSurface->screenshotUrl()));
152 }
155void MirSurfaceItem::touchEvent(QTouchEvent * event)
157 if (event->touchPointStates() & Qt::TouchPointPressed) {
158 ++m_touchPressCount;
159 Q_EMIT touchPressCountChanged(m_touchPressCount);
160 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
161 ++m_touchReleaseCount;
162 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
163 }
166void MirSurfaceItem::mousePressEvent(QMouseEvent * event)
168 event->accept();
171void MirSurfaceItem::mouseMoveEvent(QMouseEvent * event)
173 event->accept();
176void MirSurfaceItem::mouseReleaseEvent(QMouseEvent * event)
178 event->accept();
181void MirSurfaceItem::setSurface(MirSurfaceInterface* surface)
183// qDebug().nospace() << "MirSurfaceItem::setSurface() this=" << (void*)(this)
184// << " name=" << name()
185// << " surface=" << surface;
187 if (m_qmlSurface == surface) {
188 return;
189 }
191 if (m_qmlSurface) {
192 delete m_qmlItem;
193 m_qmlItem = nullptr;
195 delete m_qmlContentComponent;
196 m_qmlContentComponent = nullptr;
198 disconnect(m_qmlSurface, nullptr, this, nullptr);
199 m_qmlSurface->unregisterView((qintptr)this);
200 }
202 m_qmlSurface = static_cast<MirSurface*>(surface);
204 if (m_qmlSurface) {
205 m_qmlSurface->registerView((qintptr)this);
207 m_qmlSurface->setActiveFocus(hasActiveFocus());
209 updateSurfaceSize();
210 updateMirSurfaceVisibility();
212 connect(m_qmlSurface, &MirSurface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
213 connect(m_qmlSurface, &MirSurface::screenshotUrlChanged, this, &MirSurfaceItem::updateScreenshot);
214 connect(m_qmlSurface, &MirSurface::liveChanged, this, &MirSurfaceItem::liveChanged);
215 connect(m_qmlSurface, &MirSurface::stateChanged, this, &MirSurfaceItem::surfaceStateChanged);
217 // The assumptions I make here really should hold.
218 QQuickView *quickView =
219 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
221 QUrl qmlComponentFilePath;
222 if (!m_qmlSurface->qmlFilePath().isEmpty()) {
223 qmlComponentFilePath = m_qmlSurface->qmlFilePath();
224 } else {
225 qmlComponentFilePath = QUrl("qrc:///Unity/Application/MirSurfaceItem.qml");
226 }
228 m_qmlContentComponent = new QQmlComponent(quickView->engine(), qmlComponentFilePath);
230 switch (m_qmlContentComponent->status()) {
231 case QQmlComponent::Ready:
232 createQmlContentItem();
233 break;
234 case QQmlComponent::Loading:
235 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
236 this, &MirSurfaceItem::onComponentStatusChanged);
237 break;
238 case QQmlComponent::Error:
239 printComponentErrors();
240 qFatal("MirSurfaceItem: failed to create content component.");
241 break;
242 default:
243 qFatal("MirSurfaceItem: Unhandled component status");
244 }
245 }
247 Q_EMIT surfaceChanged(m_qmlSurface);
250void MirSurfaceItem::itemChange(ItemChange change, const ItemChangeData & value)
252 if (change == QQuickItem::ItemActiveFocusHasChanged) {
253 if (m_qmlSurface) {
254 m_qmlSurface->setActiveFocus(value.boolValue);
255 }
256 }
259void MirSurfaceItem::updateMirSurfaceVisibility()
261 if (!m_qmlSurface) return;
263 m_qmlSurface->setViewVisibility((qintptr)this, isVisible());
266void MirSurfaceItem::setConsumesInput(bool value)
268 if (m_consumesInput != value) {
269 m_consumesInput = value;
270 Q_EMIT consumesInputChanged(value);
271 }
274int MirSurfaceItem::surfaceWidth() const
276 return m_surfaceWidth;
279void MirSurfaceItem::setSurfaceWidth(int value)
281 if (value != -1 && m_surfaceWidth != value) {
282 m_surfaceWidth = value;
283 Q_EMIT surfaceWidthChanged(m_surfaceWidth);
284 updateSurfaceSize();
285 }
288int MirSurfaceItem::surfaceHeight() const
290 return m_surfaceHeight;
293void MirSurfaceItem::setSurfaceHeight(int value)
295 if (value != -1 && m_surfaceHeight != value) {
296 m_surfaceHeight = value;
297 Q_EMIT surfaceHeightChanged(m_surfaceHeight);
298 updateSurfaceSize();
299 }
302void MirSurfaceItem::updateSurfaceSize()
304 if (m_qmlSurface && m_surfaceWidth > 0 && m_surfaceHeight > 0) {
305 m_qmlSurface->resize(m_surfaceWidth, m_surfaceHeight);
306 }
309void MirSurfaceItem::setFillMode(FillMode value)
311 if (value != m_fillMode) {
312 m_fillMode = value;
313 Q_EMIT fillModeChanged(m_fillMode);
314 }