~brendan-donegan/unity8/shellRotationInvestigation

« back to all changes in this revision

Viewing changes to tests/mocks/Unity/Application/GenericApp.cpp

  • Committer: Mirco Müller
  • Date: 2015-03-06 14:22:41 UTC
  • mfrom: (1589.1.1 unity8)
  • Revision ID: mirco.mueller@ubuntu.com-20150306142241-wse3a2j2yghqfwoh
Merged kgunn's conflict-resolving branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2014 Canonical, Ltd.
 
2
 * Copyright (C) 2014-2015 Canonical, Ltd.
3
3
 *
4
4
 * This program is free software; you can redistribute it and/or modify
5
5
 * it under the terms of the GNU General Public License as published by
14
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
15
 */
16
16
 
17
 
#include "MirSurfaceItem.h"
 
17
#include "GenericApp.h"
18
18
#include "Session.h"
19
19
 
20
20
#include <paths.h>
21
21
 
22
 
#include <QGuiApplication>
23
 
#include <QQuickView>
24
 
#include <QQmlProperty>
25
 
#include <QQmlEngine>
26
22
#include <QString>
27
23
 
28
24
#include <QDebug>
29
25
 
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)
36
 
    : QQuickItem(parent)
37
 
    , m_session(nullptr)
38
 
    , m_name(name)
39
 
    , m_type(type)
40
 
    , m_state(state)
41
 
    , m_live(true)
42
 
    , m_orientationAngle(Angle0)
 
32
    : MirSurfaceItem(name, type, state, screenshot, qmlFilePath, parent)
43
33
    , m_touchPressCount(0)
44
34
    , m_touchReleaseCount(0)
45
 
    , m_qmlItem(nullptr)
46
 
    , m_screenshotUrl(screenshot)
47
 
{
48
 
    qDebug() << "MirSurfaceItem::MirSurfaceItem() " << this->name();
49
 
 
50
 
    QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
51
 
 
52
 
    connect(this, &QQuickItem::focusChanged,
53
 
            this, &MirSurfaceItem::onFocusChanged);
54
 
 
55
 
    // The assumptions I make here really should hold.
56
 
    QQuickView *quickView =
57
 
        qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
58
 
 
59
 
    QString qmlComponentFilePath;
60
 
    if (!qmlFilePath.isEmpty()) {
61
 
        qmlComponentFilePath.append(qmlFilePath);
 
35
    , m_requestedInputMethod(false)
 
36
{
 
37
    connect(this, &QQuickItem::activeFocusChanged,
 
38
            this, &GenericApp::onActiveFocusChanged);
 
39
}
 
40
 
 
41
GenericApp::~GenericApp()
 
42
{
 
43
}
 
44
 
 
45
void GenericApp::onActiveFocusChanged()
 
46
{
 
47
    if (hasActiveFocus()) {
 
48
        if (m_requestedInputMethod) {
 
49
            Q_EMIT inputMethodRequested();
 
50
        }
62
51
    } else {
63
 
        qmlComponentFilePath = QString("%1/Unity/Application/MirSurfaceItem.qml")
64
 
            .arg(mockPluginsDir());
65
 
    }
66
 
 
67
 
    m_qmlContentComponent = new QQmlComponent(quickView->engine(), qmlComponentFilePath);
68
 
 
69
 
    switch (m_qmlContentComponent->status()) {
70
 
        case QQmlComponent::Ready:
71
 
            createQmlContentItem();
72
 
            break;
73
 
        case QQmlComponent::Loading:
74
 
            connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
75
 
                    this, &MirSurfaceItem::onComponentStatusChanged);
76
 
            break;
77
 
        case QQmlComponent::Error:
78
 
            printComponentErrors();
79
 
            qFatal("MirSurfaceItem: failed to create content component.");
80
 
            break;
81
 
        default:
82
 
            qFatal("MirSurfaceItem: Unhandled component status");
83
 
    }
84
 
}
85
 
 
86
 
MirSurfaceItem::~MirSurfaceItem()
87
 
{
88
 
    qDebug() << "MirSurfaceItem::~MirSurfaceItem() " << name();
89
 
    if (m_session) {
90
 
        m_session->setSurface(nullptr);
91
 
    }
92
 
}
93
 
 
94
 
void MirSurfaceItem::printComponentErrors()
95
 
{
96
 
    QList<QQmlError> errors = m_qmlContentComponent->errors();
97
 
    for (int i = 0; i < errors.count(); ++i) {
98
 
        qDebug() << errors[i];
99
 
    }
100
 
}
101
 
 
102
 
void MirSurfaceItem::release()
103
 
{
104
 
    qDebug() << "MirSurfaceItem::release " << name();
105
 
 
106
 
    if (m_session) {
107
 
        m_session->setSurface(nullptr);
108
 
    }
109
 
    if (!parent()) {
110
 
        deleteLater();
111
 
    }
112
 
}
113
 
 
114
 
void MirSurfaceItem::setOrientationAngle(OrientationAngle angle)
115
 
{
116
 
    if (m_orientationAngle == angle)
117
 
        return;
118
 
 
119
 
    m_orientationAngle = angle;
120
 
 
121
 
    QQmlProperty orientationProp(m_qmlItem, "orientationAngle");
122
 
    orientationProp.write(QVariant::fromValue(m_orientationAngle));
123
 
 
124
 
    Q_EMIT orientationAngleChanged(m_orientationAngle);
125
 
}
126
 
 
127
 
void MirSurfaceItem::setSession(Session* session)
128
 
{
129
 
    m_session = session;
130
 
}
131
 
 
132
 
void MirSurfaceItem::setScreenshot(const QUrl& screenshot)
133
 
{
134
 
    m_screenshotUrl = screenshot;
135
 
    if (m_qmlItem) {
136
 
        QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
137
 
        screenshotSource.write(QVariant::fromValue(m_screenshotUrl));
138
 
    }
139
 
}
140
 
 
141
 
void MirSurfaceItem::setLive(bool live)
142
 
{
143
 
    if (m_live != live) {
144
 
        m_live = live;
145
 
        Q_EMIT liveChanged(m_live);
146
 
    }
147
 
}
148
 
 
149
 
void MirSurfaceItem::onFocusChanged()
150
 
{
151
 
    if (!hasFocus()) {
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();
155
 
    }
156
 
}
157
 
 
158
 
void MirSurfaceItem::setState(MirSurfaceItem::State newState)
159
 
{
160
 
    if (newState != m_state) {
161
 
        m_state = newState;
162
 
        Q_EMIT stateChanged(newState);
163
 
    }
164
 
}
165
 
 
166
 
void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status)
167
 
{
168
 
    if (status == QQmlComponent::Ready) {
169
 
        createQmlContentItem();
170
 
    }
171
 
}
172
 
 
173
 
void MirSurfaceItem::createQmlContentItem()
174
 
{
175
 
    qDebug() << "MirSurfaceItem::createQmlContentItem()";
176
 
 
177
 
    m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
178
 
    m_qmlItem->setParentItem(this);
179
 
 
180
 
    setImplicitWidth(m_qmlItem->implicitWidth());
181
 
    setImplicitHeight(m_qmlItem->implicitHeight());
182
 
 
183
 
    {
184
 
        QQmlProperty screenshotSource(m_qmlItem, "screenshotSource");
185
 
        screenshotSource.write(QVariant::fromValue(m_screenshotUrl));
186
 
    }
187
 
}
188
 
 
189
 
void MirSurfaceItem::touchEvent(QTouchEvent * event)
 
52
        Q_EMIT inputMethodDismissed();
 
53
    }
 
54
}
 
55
 
 
56
void GenericApp::touchEvent(QTouchEvent * event)
190
57
{
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();
 
61
 
 
62
        if (hasActiveFocus()) {
 
63
            if (m_requestedInputMethod) {
 
64
                Q_EMIT inputMethodDismissed();
 
65
            } else {
 
66
                Q_EMIT inputMethodRequested();
 
67
            }
 
68
            m_requestedInputMethod = !m_requestedInputMethod;
 
69
        }
 
70
 
197
71
    } else if (event->touchPointStates() & Qt::TouchPointReleased) {
198
72
        ++m_touchReleaseCount;
199
73
        Q_EMIT touchReleaseCountChanged(m_touchReleaseCount);