on e65178d help sign in
unity8/plugins/Dash/croppedimagesizer.cpp
141 lines 3.5 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);
32 connect(this, &CroppedImageSizer::sourceChanged, this, &CroppedImageSizer::updateImageSize);
35CroppedImageSizer::~CroppedImageSizer()
37 if (m_reply) {
38 m_reply->deleteLater();
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 sourceChanged();
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 &sourceSize)
88 if (sourceSize != m_sourceSize) {
89 m_sourceSize = sourceSize;
90 Q_EMIT sourceSizeChanged();
91 }
94void CroppedImageSizer::updateImageSize()
96 if (m_reply) {
97 m_reply->disconnect();
98 m_reply->abort();
99 m_reply->deleteLater();
100 m_reply = nullptr;
101 }
103 if (m_source.isValid() && qmlEngine(this) && qmlEngine(this)->networkAccessManager()) {
104 QNetworkRequest request(m_source);
105 m_reply = qmlEngine(this)->networkAccessManager()->get(request);
106 connect(m_reply, &QNetworkReply::finished, this, &CroppedImageSizer::requestFinished);
107 } else {
108 setSourceSize(QSize(-1, -1));
109 }
112void CroppedImageSizer::requestFinished()
114 QImageReader m_reader(m_reply);
115 m_imageSize = m_reader.size();
117 m_reply->deleteLater();
118 m_reply = nullptr;
120 calculateSourceSize();
123void CroppedImageSizer::calculateSourceSize()
125 if (m_source.isValid() && m_width > 0 && m_height > 0 && !m_reply) {
126 if (!m_imageSize.isEmpty()) {
127 const qreal ar = m_width / m_height;
128 const qreal ssar = m_imageSize.width() / (qreal)m_imageSize.height();
129 if (ar > ssar) {
130 setSourceSize(QSize(m_width, 0));
131 } else {
132 setSourceSize(QSize(0, m_height));
133 }
134 } else {
135 qWarning() << "Invalid size for " << m_source << m_imageSize << m_reply;
136 setSourceSize(QSize(0, 0));
137 }
138 } else {
139 setSourceSize(QSize(-1, -1));
140 }