on d2ce140 help sign in
unity8/plugins/Dash/croppedimagesizer.cpp
121 lines 3.1 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()
27 : m_width(0),
28 m_height(0),
29 m_sourceSize(QSize(-1, -1))
31 connect(this, &CroppedImageSizer::inputParamsChanged, this, &CroppedImageSizer::calculateSourceSize);
34CroppedImageSizer::~CroppedImageSizer()
36 if (m_reply) {
37 m_reply->deleteLater();
38 }
41QUrl CroppedImageSizer::source() const
43 return m_source;
46void CroppedImageSizer::setSource(const QUrl &source)
48 if (source != m_source) {
49 m_source = source;
50 Q_EMIT inputParamsChanged();
51 }
54qreal CroppedImageSizer::width() const
56 return m_width;
59void CroppedImageSizer::setWidth(qreal width)
61 if (width != m_width) {
62 m_width = width;
63 Q_EMIT inputParamsChanged();
64 }
67qreal CroppedImageSizer::height() const
69 return m_height;
72void CroppedImageSizer::setHeight(qreal height)
74 if (height != m_height) {
75 m_height = height;
76 Q_EMIT inputParamsChanged();
77 }
80QSize CroppedImageSizer::sourceSize() const
82 return m_sourceSize;
85void CroppedImageSizer::calculateSourceSize()
87 if (m_source.isValid() && m_width > 0 && m_height > 0 && qmlEngine(this) && qmlEngine(this)->networkAccessManager()) {
88 if (m_reply) {
89 m_reply->abort();
90 m_reply->deleteLater();
91 }
92 QNetworkRequest request(m_source);
93 m_reply = qmlEngine(this)->networkAccessManager()->get(request);
94 connect(m_reply, &QNetworkReply::finished, this, &CroppedImageSizer::requestFinished);
95 } else if (m_sourceSize != QSize(-1, -1)) {
96 m_sourceSize = QSize(-1, -1);
97 sourceSizeChanged();
98 }
101void CroppedImageSizer::requestFinished()
103 QImageReader m_reader(m_reply);
104 const QSize imageSize = m_reader.size();
105 if (imageSize.isValid()) {
106 const qreal ar = width() / height();
107 const qreal ssar = imageSize.width() / imageSize.height();
108 if (ar > ssar) {
109 m_sourceSize = QSize(m_width, 0);
110 } else {
111 m_sourceSize = QSize(0, m_height);
112 }
113 } else {
114 qWarning() << "Could not find size of" << m_source;
115 m_sourceSize = QSize(0, 0);
116 }
117 sourceSizeChanged();
119 m_reply->deleteLater();
120 m_reply = nullptr;