@@ -0,0 +1,138 @@
2+ * Copyright (C) 2014 Canonical, Ltd.
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.
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.
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/>.
17+#include "croppedimagesizer.h"
20+#include <QNetworkReply>
21+#include <QNetworkRequest>
26+CroppedImageSizer::CroppedImageSizer()
28+ connect(this, &CroppedImageSizer::inputParamsChanged, this, &CroppedImageSizer::calculateSourceSize);
29+ connect(this, &CroppedImageSizer::sourceChanged, this, &CroppedImageSizer::requestImage);
32+CroppedImageSizer::~CroppedImageSizer()
35+ m_reply->deleteLater();
39+QUrl CroppedImageSizer::source() const
44+void CroppedImageSizer::setSource(const QUrl &source)
46+ if (source != m_source) {
48+ Q_EMIT sourceChanged();
52+qreal CroppedImageSizer::width() const
57+void CroppedImageSizer::setWidth(qreal width)
59+ if (width != m_width) {
61+ Q_EMIT inputParamsChanged();
65+qreal CroppedImageSizer::height() const
70+void CroppedImageSizer::setHeight(qreal height)
72+ if (height != m_height) {
74+ Q_EMIT inputParamsChanged();
78+QSize CroppedImageSizer::sourceSize() const
83+void CroppedImageSizer::setSourceSize(const QSize &sourceSize)
85+ if (sourceSize != m_sourceSize) {
86+ m_sourceSize = sourceSize;
87+ Q_EMIT sourceSizeChanged();
91+void CroppedImageSizer::requestImage()
94+ m_reply->disconnect();
96+ m_reply->deleteLater();
100+ if (m_source.isValid() && qmlEngine(this) && qmlEngine(this)->networkAccessManager()) {
101+ QNetworkRequest request(m_source);
102+ m_reply = qmlEngine(this)->networkAccessManager()->get(request);
103+ connect(m_reply, &QNetworkReply::finished, this, &CroppedImageSizer::requestFinished);
105+ setSourceSize(QSize(-1, -1));
109+void CroppedImageSizer::requestFinished()
111+ QImageReader m_reader(m_reply);
112+ m_imageSize = m_reader.size();
114+ m_reply->deleteLater();
117+ calculateSourceSize();
120+void CroppedImageSizer::calculateSourceSize()
122+ if (m_source.isValid() && m_width > 0 && m_height > 0 && !m_reply) {
123+ if (!m_imageSize.isEmpty()) {
124+ const qreal ar = m_width / m_height;
125+ const qreal ssar = m_imageSize.width() / (qreal)m_imageSize.height();
127+ setSourceSize(QSize(m_width, 0));
129+ setSourceSize(QSize(0, m_height));
132+ qWarning() << "Invalid size for " << m_source << m_imageSize;
133+ setSourceSize(QSize(0, 0));
136+ setSourceSize(QSize(-1, -1));