on fa190e6 help sign in
unity8/plugins/Dash/croppedimagesizer.cpp
108 lines 2.6 kB history raw
1/*
2 * Copyright (C) 2014 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 "croppedimagesizer.h"
19#include "croppedimagesizerasyncworker.h"
21#include <QNetworkAccessManager>
22#include <QNetworkRequest>
23#include <QQmlEngine>
24#include <QQuickItem>
26CroppedImageSizer::CroppedImageSizer()
27 : m_width(0),
28 m_height(0),
29 m_sourceSize(QSize(-1, -1)),
30 m_worker(nullptr)
32 connect(this, &CroppedImageSizer::inputParamsChanged, this, &CroppedImageSizer::calculateSourceSize);
35CroppedImageSizer::~CroppedImageSizer()
37 if (m_worker) {
38 m_worker->abort();
39 }
42QUrl CroppedImageSizer::source() const
44 return m_source;
47void CroppedImageSizer::setSource(const QUrl &source)
49 if (source != m_source) {
50 m_source = source;
51 Q_EMIT inputParamsChanged();
52 }
55qreal CroppedImageSizer::width() const
57 return m_width;
60void CroppedImageSizer::setWidth(qreal width)
62 if (width != m_width) {
63 m_width = width;
64 Q_EMIT inputParamsChanged();
65 }
68qreal CroppedImageSizer::height() const
70 return m_height;
73void CroppedImageSizer::setHeight(qreal height)
75 if (height != m_height) {
76 m_height = height;
77 Q_EMIT inputParamsChanged();
78 }
81QSize CroppedImageSizer::sourceSize() const
83 return m_sourceSize;
86void CroppedImageSizer::setSourceSize(const QSize &size)
88 if (size != m_sourceSize) {
89 m_sourceSize = size;
90 Q_EMIT sourceSizeChanged();
91 }
94void CroppedImageSizer::calculateSourceSize()
96 if (m_source.isValid() && m_width > 0 && m_height > 0 && qmlEngine(this) && qmlEngine(this)->networkAccessManager()) {
97 if (m_worker) {
98 m_worker->abort();
99 }
101 QNetworkRequest request(m_source);
102 QNetworkReply *reply = qmlEngine(this)->networkAccessManager()->get(request);
103 m_worker = new CroppedImageSizerAsyncWorker(this, reply);
104 } else if (m_sourceSize != QSize(-1, -1)) {
105 m_sourceSize = QSize(-1, -1);
106 sourceSizeChanged();
107 }