14
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#include "MirSurfaceItem.h"
17
#include "GenericApp.h"
18
18
#include "Session.h"
22
#include <QGuiApplication>
24
#include <QQmlProperty>
30
MirSurfaceItem::MirSurfaceItem(const QString& name,
31
MirSurfaceItem::Type type,
32
MirSurfaceItem::State state,
26
GenericApp::GenericApp(const QString& name,
27
GenericApp::Type type,
28
GenericApp::State state,
33
29
const QUrl& screenshot,
34
30
const QString &qmlFilePath,
35
31
QQuickItem *parent)
42
, m_orientationAngle(Angle0)
32
: MirSurfaceItem(name, type, state, screenshot, qmlFilePath, parent)
43
33
, m_touchPressCount(0)
44
34
, m_touchReleaseCount(0)
46
, m_screenshotUrl(screenshot)
48
qDebug() << "MirSurfaceItem::MirSurfaceItem() " << this->name();
50
QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
52
connect(this, &QQuickItem::focusChanged,
53
this, &MirSurfaceItem::onFocusChanged);
55
// The assumptions I make here really should hold.
56
QQuickView *quickView =
57
qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
59
QString qmlComponentFilePath;
60
if (!qmlFilePath.isEmpty()) {
61
qmlComponentFilePath.append(qmlFilePath);
35
, m_requestedInputMethod(false)
37
connect(this, &QQuickItem::activeFocusChanged,
38
this, &GenericApp::onActiveFocusChanged);
41
GenericApp::~GenericApp()
45
void GenericApp::onActiveFocusChanged()
47
if (hasActiveFocus()) {
48
if (m_requestedInputMethod) {
49
Q_EMIT inputMethodRequested();
63
qmlComponentFilePath = QString("%1/Unity/Application/MirSurfaceItem.qml")
64
.arg(mockPluginsDir());
67
m_qmlContentComponent = new QQmlComponent(quickView->engine(), qmlComponentFilePath);
69
switch (m_qmlContentComponent->status()) {
70
case QQmlComponent::Ready:
71
createQmlContentItem();
73
case QQmlComponent::Loading:
74
connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
75
this, &MirSurfaceItem::onComponentStatusChanged);
77
case QQmlComponent::Error:
78
printComponentErrors();
79
qFatal("MirSurfaceItem: failed to create content component.");
82
qFatal("MirSurfaceItem: Unhandled component status");
86
MirSurfaceItem::~MirSurfaceItem()
88
qDebug() << "MirSurfaceItem::~MirSurfaceItem() " << name();
90
m_session->setSurface(nullptr);
94
void MirSurfaceItem::printComponentErrors()
96
QList<QQmlError> errors = m_qmlContentComponent->errors();
97
for (int i = 0; i < errors.count(); ++i) {
98
qDebug() << errors[i];
102
void MirSurfaceItem::release()
104
qDebug() << "MirSurfaceItem::release " << name();
107
m_session->setSurface(nullptr);
114
void MirSurfaceItem::setOrientationAngle(OrientationAngle angle)
116
if (m_orientationAngle == angle)
119
m_orientationAngle = angle;
121
QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
122
orientationProp.write(QVariant::fromValue(m_orientationAngle));
124
Q_EMIT orientationAngleChanged(m_orientationAngle);
127
void MirSurfaceItem::setSession(Session* session)
132
void MirSurfaceItem::setScreenshot(const QUrl& screenshot)
134
m_screenshotUrl = screenshot;
136
QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
137
screenshotSource.write(QVariant::fromValue(m_screenshotUrl));
141
void MirSurfaceItem::setLive(bool live)
143
if (m_live != live) {
145
Q_EMIT liveChanged(m_live);
149
void MirSurfaceItem::onFocusChanged()
152
// Causes a crash in tst_Shell.qml, inside the mock Unity.Application itself.
153
// Didn't have time to debug yet.
154
//Q_EMIT inputMethodDismissed();
158
void MirSurfaceItem::setState(MirSurfaceItem::State newState)
160
if (newState != m_state) {
162
Q_EMIT stateChanged(newState);
166
void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
168
if (status == QQmlComponent::Ready) {
169
createQmlContentItem();
173
void MirSurfaceItem::createQmlContentItem()
175
qDebug() << "MirSurfaceItem::createQmlContentItem()";
177
m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
178
m_qmlItem->setParentItem(this);
180
setImplicitWidth(m_qmlItem->implicitWidth());
181
setImplicitHeight(m_qmlItem->implicitHeight());
184
QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
185
screenshotSource.write(QVariant::fromValue(m_screenshotUrl));
189
void MirSurfaceItem::touchEvent(QTouchEvent * event)
52
Q_EMIT inputMethodDismissed();
56
void GenericApp::touchEvent(QTouchEvent * event)
191
58
if (event->touchPointStates() & Qt::TouchPointPressed) {
192
59
++m_touchPressCount;
193
60
Q_EMIT touchPressCountChanged(m_touchPressCount);
194
// Causes a crash in tst_Shell.qml, inside the mock Unity.Application itself.
195
// Didn't have time to debug yet.
196
// Q_EMIT inputMethodRequested();
62
if (hasActiveFocus()) {
63
if (m_requestedInputMethod) {
64
Q_EMIT inputMethodDismissed();
66
Q_EMIT inputMethodRequested();
68
m_requestedInputMethod = !m_requestedInputMethod;
197
71
} else if (event->touchPointStates() & Qt::TouchPointReleased) {
198
72
++m_touchReleaseCount;
199
73
Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);