@@ -0,0 +1,115 @@
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);
31+CroppedImageSizer::~CroppedImageSizer()
34+ m_reply->deleteLater();
38+QUrl CroppedImageSizer::source() const
43+void CroppedImageSizer::setSource(const QUrl &source)
45+ if (source != m_source) {
47+ Q_EMIT inputParamsChanged();
51+qreal CroppedImageSizer::width() const
56+void CroppedImageSizer::setWidth(qreal width)
58+ if (width != m_width) {
60+ Q_EMIT inputParamsChanged();
64+qreal CroppedImageSizer::height() const
69+void CroppedImageSizer::setHeight(qreal height)
71+ if (height != m_height) {
73+ Q_EMIT inputParamsChanged();
77+QSize CroppedImageSizer::sourceSize() const
82+void CroppedImageSizer::calculateSourceSize()
84+ if (m_source.isValid() && m_width > 0 && m_height > 0 && qmlEngine(this) && qmlEngine(this)->networkAccessManager()) {
87+ m_reply->deleteLater();
89+ QNetworkRequest request(m_source);
90+ m_reply = qmlEngine(this)->networkAccessManager()->get(request);
91+ connect(m_reply, &QNetworkReply::finished, this, &CroppedImageSizer::requestFinished);
95+void 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();
103+ m_sourceSize = QSize(m_width, 0);
105+ m_sourceSize = QSize(0, m_height);
108+ qWarning() << "Could not find size of" << m_source;
109+ m_sourceSize = QSize(0, 0);
111+ sourceSizeChanged();
113+ m_reply->deleteLater();