on beafaa2 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItem.cpp
423 lines 13 kB history raw
1/*
2 * Copyright (C) 2014-2016 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 <QQmlContext>
24#include <QQmlProperty>
25#include <QQmlEngine>
26#include <QString>
28#include <QDebug>
30using namespace unity::shell::application;
32#define MIRSURFACEITEM_DEBUG 0
34#if MIRSURFACEITEM_DEBUG
35#define DEBUG_MSG(params) qDebug().nospace() << "MirSurfaceItem::" << __func__ << " " << params
36#else
37#define DEBUG_MSG(params) ((void)0)
38#endif
40MirSurfaceItem::MirSurfaceItem(QQuickItem *parent)
41 : MirSurfaceItemInterface(parent)
42 , m_qmlSurface(nullptr)
43 , m_qmlContentComponent(nullptr)
44 , m_qmlItem(nullptr)
45 , m_consumesInput(false)
46 , m_orientationAngle(nullptr)
47 , m_surfaceWidth(0)
48 , m_surfaceHeight(0)
49 , m_touchPressCount(0)
50 , m_touchReleaseCount(0)
51 , m_mousePressCount(0)
52 , m_mouseReleaseCount(0)
54 DEBUG_MSG((void*)(this) << name());
55 setAcceptedMouseButtons(Qt::LeftButton | Qt::MiddleButton | Qt::RightButton |
56 Qt::ExtraButton1 | Qt::ExtraButton2 | Qt::ExtraButton3 | Qt::ExtraButton4 |
57 Qt::ExtraButton5 | Qt::ExtraButton6 | Qt::ExtraButton7 | Qt::ExtraButton8 |
58 Qt::ExtraButton9 | Qt::ExtraButton10 | Qt::ExtraButton11 |
59 Qt::ExtraButton12 | Qt::ExtraButton13);
61 connect(this, &QQuickItem::activeFocusChanged, this, &MirSurfaceItem::updateMirSurfaceActiveFocus);
62 connect(this, &QQuickItem::visibleChanged, this, &MirSurfaceItem::updateMirSurfaceExposure);
64 connect(this, &MirSurfaceItem::consumesInputChanged, this, [this]() {
65 updateMirSurfaceActiveFocus(hasActiveFocus());
66 });
68 // We're just clipping contents in the mock. The real QtMir would copy only relevant buffer instead
69 setClip(true);
72MirSurfaceItem::~MirSurfaceItem()
74 DEBUG_MSG((void*)(this) << name());
75 setSurface(nullptr);
76 delete m_orientationAngle;
79void MirSurfaceItem::printComponentErrors()
81 QList<QQmlError> errors = m_qmlContentComponent->errors();
82 for (int i = 0; i < errors.count(); ++i) {
83 qDebug() << errors[i];
84 }
87Mir::Type MirSurfaceItem::type() const
89 if (m_qmlSurface) {
90 return m_qmlSurface->type();
91 } else {
92 return Mir::UnknownType;
93 }
96Mir::State MirSurfaceItem::surfaceState() const
98 if (m_qmlSurface) {
99 return m_qmlSurface->state();
100 } else {
101 return Mir::UnknownState;
102 }
105QString MirSurfaceItem::name() const
107 if (m_qmlSurface) {
108 return m_qmlSurface->name();
109 } else {
110 return QString();
111 }
114bool MirSurfaceItem::live() const
116 if (m_qmlSurface) {
117 return m_qmlSurface->live();
118 } else {
119 return false;
120 }
123Mir::ShellChrome MirSurfaceItem::shellChrome() const
125 if (m_qmlSurface) {
126 return m_qmlSurface->shellChrome();
127 } else {
128 return Mir::NormalChrome;
129 }
132Mir::OrientationAngle MirSurfaceItem::orientationAngle() const
134 if (m_orientationAngle) {
135 Q_ASSERT(!m_qmlSurface);
136 return *m_orientationAngle;
137 } else if (m_qmlSurface) {
138 return m_qmlSurface->orientationAngle();
139 } else {
140 return Mir::Angle0;
141 }
144void MirSurfaceItem::setOrientationAngle(Mir::OrientationAngle angle)
146 DEBUG_MSG(angle);
148 if (m_qmlSurface) {
149 Q_ASSERT(!m_orientationAngle);
150 m_qmlSurface->setOrientationAngle(angle);
151 } else if (!m_orientationAngle) {
152 m_orientationAngle = new Mir::OrientationAngle;
153 *m_orientationAngle = angle;
154 Q_EMIT orientationAngleChanged(angle);
155 } else if (*m_orientationAngle != angle) {
156 *m_orientationAngle = angle;
157 Q_EMIT orientationAngleChanged(angle);
158 }
160 if (m_qmlItem) {
161 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
162 orientationProp.write(QVariant::fromValue(orientationAngle()));
163 }
166void MirSurfaceItem::updateScreenshot(QUrl screenshotUrl)
168 if (m_qmlItem) {
169 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
170 screenshotSource.write(QVariant::fromValue(screenshotUrl));
171 }
174void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
176 if (status == QQmlComponent::Ready) {
177 createQmlContentItem();
178 }
181void MirSurfaceItem::createQmlContentItem()
183 DEBUG_MSG("");
185 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
186 m_qmlItem->setParentItem(this);
188 if (m_fillMode == FillMode::Stretch && width() != 0 && height() != 0) {
189 m_qmlItem->setWidth(width());
190 m_qmlItem->setHeight(height());
191 } else {
192 m_qmlItem->setWidth(m_surfaceWidth);
193 m_qmlItem->setHeight(m_surfaceHeight);
194 }
196 qDebug() << "set size to" << m_surfaceWidth;
197 setImplicitWidth(m_qmlItem->implicitWidth());
198 setImplicitHeight(m_qmlItem->implicitHeight());
200 {
201 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
202 screenshotSource.write(QVariant::fromValue(m_qmlSurface->screenshotUrl()));
203 }
205 {
206 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
207 orientationProp.write(QVariant::fromValue(orientationAngle()));
208 }
211void MirSurfaceItem::touchEvent(QTouchEvent * event)
213 if (event->type() == QEvent::TouchBegin) {
214 m_touchTrail.clear();
215 }
217 if (event->touchPointStates() & Qt::TouchPointPressed) {
218 ++m_touchPressCount;
219 Q_EMIT touchPressCountChanged(m_touchPressCount);
220 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
221 ++m_touchReleaseCount;
222 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
223 }
225 Q_FOREACH(const QTouchEvent::TouchPoint &touchPoint, event->touchPoints()) {
226 QString id(touchPoint.id());
227 QVariantList list = m_touchTrail[id].toList();
228 list.append(QVariant::fromValue(touchPoint.pos()));
229 if (list.count() > 100) list.pop_front();
230 m_touchTrail[id] = list;
231 }
233 if (m_qmlItem) {
234 QQmlProperty touchTrail(m_qmlItem, "touchTrail");
235 touchTrail.write(m_touchTrail);
236 }
239void MirSurfaceItem::mousePressEvent(QMouseEvent * event)
241 m_mousePressCount++;
242 Q_EMIT mousePressCountChanged(m_mousePressCount);
243 event->accept();
246void MirSurfaceItem::mouseMoveEvent(QMouseEvent * event)
248 event->accept();
251void MirSurfaceItem::mouseReleaseEvent(QMouseEvent * event)
253 m_mouseReleaseCount++;
254 Q_EMIT mouseReleaseCountChanged(m_mouseReleaseCount);
255 event->accept();
258void MirSurfaceItem::setSurface(MirSurfaceInterface* surface)
260 DEBUG_MSG("this=" << (void*)(this) << " name=" << name() << " surface=" << surface);
262 if (m_qmlSurface == surface) {
263 return;
264 }
266 if (m_qmlSurface) {
267 delete m_qmlItem;
268 m_qmlItem = nullptr;
270 delete m_qmlContentComponent;
271 m_qmlContentComponent = nullptr;
273 if (hasActiveFocus() && m_consumesInput && m_qmlSurface->live()) {
274 m_qmlSurface->setActiveFocus(false);
275 }
277 disconnect(m_qmlSurface, nullptr, this, nullptr);
278 m_qmlSurface->unregisterView((qintptr)this);
279 }
281 m_qmlSurface = static_cast<MirSurface*>(surface);
283 if (m_qmlSurface) {
284 m_qmlSurface->registerView((qintptr)this);
286 updateSurfaceSize();
287 updateMirSurfaceExposure();
289 if (m_orientationAngle) {
290 m_qmlSurface->setOrientationAngle(*m_orientationAngle);
291 connect(m_qmlSurface, &MirSurfaceInterface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
292 delete m_orientationAngle;
293 m_orientationAngle = nullptr;
294 } else {
295 connect(m_qmlSurface, &MirSurfaceInterface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
296 Q_EMIT orientationAngleChanged(m_qmlSurface->orientationAngle());
297 }
299 connect(m_qmlSurface, &MirSurface::screenshotUrlChanged, this, &MirSurfaceItem::updateScreenshot);
300 connect(m_qmlSurface, &MirSurface::liveChanged, this, &MirSurfaceItem::liveChanged);
301 connect(m_qmlSurface, &MirSurface::stateChanged, this, &MirSurfaceItem::surfaceStateChanged);
302 connect(m_qmlSurface, &MirSurface::sizeChanged, this, [this] () {
303 if (m_fillMode == FillMode::Stretch) {
304 m_qmlItem->setSize(QSize(this->width(), this->height()));
305 } else {
306 m_qmlItem->setSize(m_qmlSurface->size());
307 }
308 setImplicitSize(m_qmlSurface->width(), m_qmlSurface->height());
309 });
310 m_surfaceWidth = surface->size().width();
311 m_surfaceHeight = surface->size().height();
313 QUrl qmlComponentFilePath;
314 if (!m_qmlSurface->qmlFilePath().isEmpty()) {
315 qmlComponentFilePath = m_qmlSurface->qmlFilePath();
316 } else {
317 qmlComponentFilePath = QUrl("qrc:///Unity/Application/MirSurfaceItem.qml");
318 }
320 m_qmlContentComponent = new QQmlComponent(QQmlEngine::contextForObject(parent())->engine(), qmlComponentFilePath);
322 switch (m_qmlContentComponent->status()) {
323 case QQmlComponent::Ready:
324 createQmlContentItem();
325 qDebug() << "content created" << m_surfaceWidth << implicitWidth() << width();
326 break;
327 case QQmlComponent::Loading:
328 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
329 this, &MirSurfaceItem::onComponentStatusChanged);
330 break;
331 case QQmlComponent::Error:
332 printComponentErrors();
333 qFatal("MirSurfaceItem: failed to create content component.");
334 break;
335 default:
336 qFatal("MirSurfaceItem: Unhandled component status");
337 }
339 if (m_consumesInput) {
340 m_qmlSurface->setActiveFocus(hasActiveFocus());
341 }
342 }
344 Q_EMIT surfaceChanged(m_qmlSurface);
348void MirSurfaceItem::updateMirSurfaceActiveFocus(bool focused)
350 if (m_qmlSurface && m_consumesInput && m_qmlSurface->live()) {
351 m_qmlSurface->setActiveFocus(focused);
352 }
355void MirSurfaceItem::updateMirSurfaceExposure()
357 if (!m_qmlSurface) return;
359 m_qmlSurface->setViewExposure((qintptr)this, isVisible());
362void MirSurfaceItem::setConsumesInput(bool value)
364 if (m_consumesInput != value) {
365 m_consumesInput = value;
366 Q_EMIT consumesInputChanged(value);
367 }
370int MirSurfaceItem::surfaceWidth() const
372 return m_surfaceWidth;
375void MirSurfaceItem::setSurfaceWidth(int value)
377// qDebug() << "setSurfaceWidth called" << value;
378 if (value != -1 && m_surfaceWidth != value) {
379 m_surfaceWidth = value;
380 Q_EMIT surfaceWidthChanged(m_surfaceWidth);
381 updateSurfaceSize();
382 }
385int MirSurfaceItem::surfaceHeight() const
387 return m_surfaceHeight;
390void MirSurfaceItem::setSurfaceHeight(int value)
392 if (value != -1 && m_surfaceHeight != value) {
393 m_surfaceHeight = value;
394 Q_EMIT surfaceHeightChanged(m_surfaceHeight);
395 updateSurfaceSize();
396 }
399void MirSurfaceItem::updateSurfaceSize()
401 if (m_qmlSurface && m_surfaceWidth > 0 && m_surfaceHeight > 0) {
402 m_qmlSurface->resize(m_surfaceWidth, m_surfaceHeight);
403 if (m_qmlItem) {
404 if (m_fillMode == FillMode::Stretch) {
405 m_qmlItem->setWidth(width());
406 m_qmlItem->setHeight(height());
407 } else {
408 m_qmlItem->setWidth(m_surfaceWidth);
409 m_qmlItem->setHeight(m_surfaceHeight);
410 }
411 }
412 qDebug() << this << "setting implicitsize" << m_surfaceWidth << m_surfaceHeight;
413 setImplicitSize(m_surfaceWidth, m_surfaceHeight);
414 }
417void MirSurfaceItem::setFillMode(FillMode value)
419 if (value != m_fillMode) {
420 m_fillMode = value;
421 Q_EMIT fillModeChanged(m_fillMode);
422 }