on 1663e5c help sign in
unity8/tests/mocks/Unity/Application/ApplicationImage.cpp
123 lines 3.2 kB history raw
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 */
17#include "ApplicationImage.h"
18#include "ApplicationInfo.h"
20#include <QQmlEngine>
21#include <QQmlComponent>
22#include <QQmlContext>
24ApplicationImage::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)
34void ApplicationImage::setSource(ApplicationInfo* source)
36 if (m_source != source) {
37 if (m_source)
38 disconnect(m_source, &ApplicationInfo::imageQmlChanged,
39 this, &ApplicationImage::updateImage);
41 m_source = source;
43 if (m_source) {
44 connect(m_source, &ApplicationInfo::imageQmlChanged,
45 this, &ApplicationImage::updateImage);
46 }
47 updateImage();
49 Q_EMIT sourceChanged();
50 }
53void ApplicationImage::setFillMode(FillMode newFillMode)
55 if (m_fillMode != newFillMode) {
56 m_fillMode = newFillMode;
57 Q_EMIT fillModeChanged();
58 }
61void ApplicationImage::setReady(bool value)
63 if (value != m_ready) {
64 m_ready = value;
65 Q_EMIT readyChanged();
66 }
69void ApplicationImage::destroyImage()
71 delete m_imageItem;
72 m_imageItem = 0;
73 delete m_imageComponent;
74 m_imageComponent = 0;
75 m_qmlUsed.clear();
76 setReady(false);
79void ApplicationImage::updateImage()
81 if (!m_source || m_source->imageQml().isEmpty()) {
82 destroyImage();
83 } else if (m_source->imageQml() != m_qmlUsed) {
84 destroyImage();
85 createImageItem();
86 }
89void ApplicationImage::createImageItem()
92 if (!m_imageComponent)
93 createImageComponent();
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 }
105void ApplicationImage::createImageComponent()
107 QQmlEngine *engine = QQmlEngine::contextForObject(this)->engine();
108 m_imageComponent = new QQmlComponent(engine, this);
109 m_imageComponent->setData(m_source->imageQml().toLatin1(), QUrl());
112void ApplicationImage::doCreateImageItem()
114 m_imageItem = qobject_cast<QQuickItem *>(m_imageComponent->create());
115 m_imageItem->setParentItem(this);
116 setReady(true);
119void ApplicationImage::onImageComponentStatusChanged(QQmlComponent::Status status)
121 if (status == QQmlComponent::Ready && !m_imageItem)
122 doCreateImageItem();