on 2c2899a help sign in
unity8/plugins/Dash/croppedimagesizer.cpp
115 lines 2.9 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 <QDebug>
20#include <QNetworkReply>
21#include <QNetworkRequest>
22#include <QQmlEngine>
23#include <QQuickItem>
24#include <QQuickView>
26CroppedImageSizer::CroppedImageSizer()
28 connect(this, &CroppedImageSizer::inputParamsChanged, this, &CroppedImageSizer::calculateSourceSize);
31CroppedImageSizer::~CroppedImageSizer()
33 if (m_reply) {
34 m_reply->deleteLater();
35 }
38QUrl CroppedImageSizer::source() const
40 return m_source;
43void CroppedImageSizer::setSource(const QUrl &source)
45 if (source != m_source) {
46 m_source = source;
47 Q_EMIT inputParamsChanged();
48 }
51qreal CroppedImageSizer::width() const
53 return m_width;
56void CroppedImageSizer::setWidth(qreal width)
58 if (width != m_width) {
59 m_width = width;
60 Q_EMIT inputParamsChanged();
61 }
64qreal CroppedImageSizer::height() const
66 return m_height;
69void CroppedImageSizer::setHeight(qreal height)
71 if (height != m_height) {
72 m_height = height;
73 Q_EMIT inputParamsChanged();
74 }
77QSize CroppedImageSizer::sourceSize() const
79 return m_sourceSize;
82void CroppedImageSizer::calculateSourceSize()
84 if (m_source.isValid() && m_width > 0 && m_height > 0 && qmlEngine(this) && qmlEngine(this)->networkAccessManager()) {
85 if (m_reply) {
86 m_reply->abort();
87 m_reply->deleteLater();
88 }
89 QNetworkRequest request(m_source);
90 m_reply = qmlEngine(this)->networkAccessManager()->get(request);
91 connect(m_reply, &QNetworkReply::finished, this, &CroppedImageSizer::requestFinished);
92 }
95void CroppedImageSizer::requestFinished()
97 QImageReader m_reader(m_reply);
98 const QSize imageSize = m_reader.size();
99 if (imageSize.isValid()) {
100 const qreal ar = width() / height();
101 const qreal ssar = imageSize.width() / imageSize.height();
102 if (ar > ssar) {
103 m_sourceSize = QSize(m_width, 0);
104 } else {
105 m_sourceSize = QSize(0, m_height);
106 }
107 } else {
108 qWarning() << "Could not find size of" << m_source;
109 m_sourceSize = QSize(0, 0);
110 }
111 sourceSizeChanged();
113 m_reply->deleteLater();
114 m_reply = nullptr;