2 * Copyright (C) 2014-2016 Canonical, Ltd.
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.
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.
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/>.
17#include "MirSurfaceItem.h"
21#include <QGuiApplication>
24#include <QQmlProperty>
30using namespace unity::shell::application;
32#define MIRSURFACEITEM_DEBUG 0
34#if MIRSURFACEITEM_DEBUG
35#define DEBUG_MSG(params) qDebug().nospace() << "MirSurfaceItem::" << __func__ << " " << params
37#define DEBUG_MSG(params) ((void)0)
40MirSurfaceItem::MirSurfaceItem(QQuickItem *parent)
41 : MirSurfaceItemInterface(parent)
42 , m_qmlSurface(nullptr)
43 , m_qmlContentComponent(nullptr)
45 , m_consumesInput(false)
48 , m_touchPressCount(0)
49 , m_touchReleaseCount(0)
50 , m_mousePressCount(0)
51 , m_mouseReleaseCount(0)
53 DEBUG_MSG((void*)(this) << name());
54 setAcceptedMouseButtons(Qt::LeftButton | Qt::MiddleButton | Qt::RightButton |
55 Qt::ExtraButton1 | Qt::ExtraButton2 | Qt::ExtraButton3 | Qt::ExtraButton4 |
56 Qt::ExtraButton5 | Qt::ExtraButton6 | Qt::ExtraButton7 | Qt::ExtraButton8 |
57 Qt::ExtraButton9 | Qt::ExtraButton10 | Qt::ExtraButton11 |
58 Qt::ExtraButton12 | Qt::ExtraButton13);
60 connect(this, &QQuickItem::activeFocusChanged, this, &MirSurfaceItem::updateMirSurfaceActiveFocus);
61 connect(this, &QQuickItem::visibleChanged, this, &MirSurfaceItem::updateMirSurfaceVisibility);
62 connect(this, &MirSurfaceItem::consumesInputChanged, this, [this]() {
63 updateMirSurfaceActiveFocus(hasActiveFocus());
67MirSurfaceItem::~MirSurfaceItem()
69 DEBUG_MSG((void*)(this) << name());
73void MirSurfaceItem::printComponentErrors()
75 QList<QQmlError> errors = m_qmlContentComponent->errors();
76 for (int i = 0; i < errors.count(); ++i) {
77 qDebug() << errors[i];
81Mir::Type MirSurfaceItem::type() const
84 return m_qmlSurface->type();
86 return Mir::UnknownType;
90Mir::State MirSurfaceItem::surfaceState() const
93 return m_qmlSurface->state();
95 return Mir::UnknownState;
99QString MirSurfaceItem::name() const
102 return m_qmlSurface->name();
108bool MirSurfaceItem::live() const
111 return m_qmlSurface->live();
117Mir::ShellChrome MirSurfaceItem::shellChrome() const
120 return m_qmlSurface->shellChrome();
122 return Mir::NormalChrome;
126Mir::OrientationAngle MirSurfaceItem::orientationAngle() const
129 return m_qmlSurface->orientationAngle();
135void MirSurfaceItem::setOrientationAngle(Mir::OrientationAngle angle)
140 if (m_qmlSurface->orientationAngle() == angle)
143 m_qmlSurface->setOrientationAngle(angle);
145 QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
146 orientationProp.write(QVariant::fromValue(m_qmlSurface->orientationAngle()));
149void MirSurfaceItem::updateScreenshot(QUrl screenshotUrl)
152 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
153 screenshotSource.write(QVariant::fromValue(screenshotUrl));
157void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
159 if (status == QQmlComponent::Ready) {
160 createQmlContentItem();
164void MirSurfaceItem::createQmlContentItem()
168 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
169 m_qmlItem->setParentItem(this);
171 setImplicitWidth(m_qmlItem->implicitWidth());
172 setImplicitHeight(m_qmlItem->implicitHeight());
175 QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
176 screenshotSource.write(QVariant::fromValue(m_qmlSurface->screenshotUrl()));
180void MirSurfaceItem::touchEvent(QTouchEvent * event)
182 if (event->type() == QEvent::TouchBegin) {
183 m_touchTrail.clear();
186 if (event->touchPointStates() & Qt::TouchPointPressed) {
188 Q_EMIT touchPressCountChanged(m_touchPressCount);
189 } else if (event->touchPointStates() & Qt::TouchPointReleased) {
190 ++m_touchReleaseCount;
191 Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);
194 Q_FOREACH(QTouchEvent::TouchPoint touchPoint, event->touchPoints()) {
195 QString id(touchPoint.id());
196 QVariantList list = m_touchTrail[id].toList();
197 list.append(QVariant::fromValue(touchPoint.pos()));
198 if (list.count() > 100) list.pop_front();
199 m_touchTrail[id] = list;
203 QQmlProperty touchTrail(m_qmlItem, "touchTrail");
204 touchTrail.write(m_touchTrail);
208void MirSurfaceItem::mousePressEvent(QMouseEvent * event)
211 Q_EMIT mousePressCountChanged(m_mousePressCount);
215void MirSurfaceItem::mouseMoveEvent(QMouseEvent * event)
220void MirSurfaceItem::mouseReleaseEvent(QMouseEvent * event)
222 m_mouseReleaseCount++;
223 Q_EMIT mouseReleaseCountChanged(m_mouseReleaseCount);
227void MirSurfaceItem::setSurface(MirSurfaceInterface* surface)
229 DEBUG_MSG("this=" << (void*)(this) << " name=" << name() << " surface=" << surface);
231 if (m_qmlSurface == surface) {
239 delete m_qmlContentComponent;
240 m_qmlContentComponent = nullptr;
242 if (hasActiveFocus() && m_consumesInput && m_qmlSurface->live()) {
243 m_qmlSurface->setActiveFocus(false);
246 disconnect(m_qmlSurface, nullptr, this, nullptr);
247 m_qmlSurface->unregisterView((qintptr)this);
250 m_qmlSurface = static_cast<MirSurface*>(surface);
253 m_qmlSurface->registerView((qintptr)this);
256 updateMirSurfaceVisibility();
258 connect(m_qmlSurface, &MirSurface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged);
259 connect(m_qmlSurface, &MirSurface::screenshotUrlChanged, this, &MirSurfaceItem::updateScreenshot);
260 connect(m_qmlSurface, &MirSurface::liveChanged, this, &MirSurfaceItem::liveChanged);
261 connect(m_qmlSurface, &MirSurface::stateChanged, this, &MirSurfaceItem::surfaceStateChanged);
263 QUrl qmlComponentFilePath;
264 if (!m_qmlSurface->qmlFilePath().isEmpty()) {
265 qmlComponentFilePath = m_qmlSurface->qmlFilePath();
267 qmlComponentFilePath = QUrl("qrc:///Unity/Application/MirSurfaceItem.qml");
270 m_qmlContentComponent = new QQmlComponent(QQmlEngine::contextForObject(parent())->engine(), qmlComponentFilePath);
272 switch (m_qmlContentComponent->status()) {
273 case QQmlComponent::Ready:
274 createQmlContentItem();
276 case QQmlComponent::Loading:
277 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
278 this, &MirSurfaceItem::onComponentStatusChanged);
280 case QQmlComponent::Error:
281 printComponentErrors();
282 qFatal("MirSurfaceItem: failed to create content component.");
285 qFatal("MirSurfaceItem: Unhandled component status");
288 if (m_consumesInput) {
289 m_qmlSurface->setActiveFocus(hasActiveFocus());
293 Q_EMIT surfaceChanged(m_qmlSurface);
297void MirSurfaceItem::updateMirSurfaceActiveFocus(bool focused)
299 if (m_qmlSurface && m_consumesInput && m_qmlSurface->live()) {
300 m_qmlSurface->setActiveFocus(focused);
304void MirSurfaceItem::updateMirSurfaceVisibility()
306 if (!m_qmlSurface) return;
308 m_qmlSurface->setViewVisibility((qintptr)this, isVisible());
311void MirSurfaceItem::setConsumesInput(bool value)
313 if (m_consumesInput != value) {
314 m_consumesInput = value;
315 Q_EMIT consumesInputChanged(value);
319int MirSurfaceItem::surfaceWidth() const
321 return m_surfaceWidth;
324void MirSurfaceItem::setSurfaceWidth(int value)
326 if (value != -1 && m_surfaceWidth != value) {
327 m_surfaceWidth = value;
328 Q_EMIT surfaceWidthChanged(m_surfaceWidth);
333int MirSurfaceItem::surfaceHeight() const
335 return m_surfaceHeight;
338void MirSurfaceItem::setSurfaceHeight(int value)
340 if (value != -1 && m_surfaceHeight != value) {
341 m_surfaceHeight = value;
342 Q_EMIT surfaceHeightChanged(m_surfaceHeight);
347void MirSurfaceItem::updateSurfaceSize()
349 if (m_qmlSurface && m_surfaceWidth > 0 && m_surfaceHeight > 0) {
350 m_qmlSurface->resize(m_surfaceWidth, m_surfaceHeight);
354void MirSurfaceItem::setFillMode(FillMode value)
356 if (value != m_fillMode) {
358 Q_EMIT fillModeChanged(m_fillMode);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list