~paulliu/unity8/lp1378469_MessageMenu

« back to all changes in this revision

Viewing changes to tests/mocks/Ubuntu/Application/ApplicationImage.cpp

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 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
 */
 
16
 
 
17
#include "ApplicationImage.h"
 
18
#include "ApplicationInfo.h"
 
19
 
 
20
#include <QQmlEngine>
 
21
#include <QQmlComponent>
 
22
#include <QQmlContext>
 
23
 
 
24
ApplicationImage::ApplicationImage(QQuickItem* parent)
 
25
    : QQuickItem(parent),
 
26
    m_source(NULL),
 
27
    m_fillMode(Stretch),
 
28
    m_ready(false),
 
29
    m_imageComponent(0),
 
30
    m_imageItem(0)
 
31
{
 
32
}
 
33
 
 
34
void ApplicationImage::setSource(ApplicationInfo* source)
 
35
{
 
36
    if (m_source != source) {
 
37
        if (m_source)
 
38
            disconnect(m_source, &ApplicationInfo::imageQmlChanged,
 
39
                       this, &ApplicationImage::updateImage);
 
40
 
 
41
        m_source = source;
 
42
 
 
43
        if (m_source) {
 
44
            connect(m_source, &ApplicationInfo::imageQmlChanged,
 
45
                   this, &ApplicationImage::updateImage);
 
46
        }
 
47
        updateImage();
 
48
 
 
49
        Q_EMIT sourceChanged();
 
50
    }
 
51
}
 
52
 
 
53
void ApplicationImage::setFillMode(FillMode newFillMode)
 
54
{
 
55
    if (m_fillMode != newFillMode) {
 
56
        m_fillMode = newFillMode;
 
57
        Q_EMIT fillModeChanged();
 
58
    }
 
59
}
 
60
 
 
61
void ApplicationImage::setReady(bool value)
 
62
{
 
63
    if (value != m_ready) {
 
64
        m_ready = value;
 
65
        Q_EMIT readyChanged();
 
66
    }
 
67
}
 
68
 
 
69
void ApplicationImage::destroyImage()
 
70
{
 
71
    delete m_imageItem;
 
72
    m_imageItem = 0;
 
73
    delete m_imageComponent;
 
74
    m_imageComponent = 0;
 
75
    m_qmlUsed.clear();
 
76
    setReady(false);
 
77
}
 
78
 
 
79
void ApplicationImage::updateImage()
 
80
{
 
81
    if (!m_source || m_source->imageQml().isEmpty()) {
 
82
        destroyImage();
 
83
    } else if (m_source->imageQml() != m_qmlUsed) {
 
84
        destroyImage();
 
85
        createImageItem();
 
86
    }
 
87
}
 
88
 
 
89
void ApplicationImage::createImageItem()
 
90
{
 
91
 
 
92
    if (!m_imageComponent)
 
93
        createImageComponent();
 
94
 
 
95
    // only create the windowItem one the component is ready
 
96
    if (!m_imageComponent->isReady()) {
 
97
        connect(m_imageComponent, &QQmlComponent::statusChanged,
 
98
                this, &ApplicationImage::onImageComponentStatusChanged);
 
99
    } else {
 
100
        doCreateImageItem();
 
101
    }
 
102
 
 
103
}
 
104
 
 
105
void ApplicationImage::createImageComponent()
 
106
{
 
107
    QQmlEngine *engine = QQmlEngine::contextForObject(this)->engine();
 
108
    m_imageComponent = new QQmlComponent(engine, this);
 
109
    m_imageComponent->setData(m_source->imageQml().toLatin1(), QUrl());
 
110
}
 
111
 
 
112
void ApplicationImage::doCreateImageItem()
 
113
{
 
114
    m_imageItem = qobject_cast<QQuickItem *>(m_imageComponent->create());
 
115
    m_imageItem->setParentItem(this);
 
116
    setReady(true);
 
117
}
 
118
 
 
119
void ApplicationImage::onImageComponentStatusChanged(QQmlComponent::Status status)
 
120
{
 
121
    if (status == QQmlComponent::Ready && !m_imageItem)
 
122
        doCreateImageItem();
 
123
}