@@ -0,0 +1,138 @@
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+ */
16+
17+#include "croppedimagesizer.h"
18+
19+#include <QDebug>
20+#include <QNetworkReply>
21+#include <QNetworkRequest>
22+#include <QQmlEngine>
23+#include <QQuickItem>
24+#include <QQuickView>
25+
26+CroppedImageSizer::CroppedImageSizer()
27+{
28+ connect(this, &CroppedImageSizer::inputParamsChanged, this, &CroppedImageSizer::calculateSourceSize);
29+ connect(this, &CroppedImageSizer::sourceChanged, this, &CroppedImageSizer::requestImage);
30+}
31+
32+CroppedImageSizer::~CroppedImageSizer()
33+{
34+ if (m_reply) {
35+ m_reply->deleteLater();
36+ }
37+}
38+
39+QUrl CroppedImageSizer::source() const
40+{
41+ return m_source;
42+}
43+
44+void CroppedImageSizer::setSource(const QUrl &source)
45+{
46+ if (source != m_source) {
47+ m_source = source;
48+ Q_EMIT sourceChanged();
49+ }
50+}
51+
52+qreal CroppedImageSizer::width() const
53+{
54+ return m_width;
55+}
56+
57+void CroppedImageSizer::setWidth(qreal width)
58+{
59+ if (width != m_width) {
60+ m_width = width;
61+ Q_EMIT inputParamsChanged();
62+ }
63+}
64+
65+qreal CroppedImageSizer::height() const
66+{
67+ return m_height;
68+}
69+
70+void CroppedImageSizer::setHeight(qreal height)
71+{
72+ if (height != m_height) {
73+ m_height = height;
74+ Q_EMIT inputParamsChanged();
75+ }
76+}
77+
78+QSize CroppedImageSizer::sourceSize() const
79+{
80+ return m_sourceSize;
81+}
82+
83+void CroppedImageSizer::setSourceSize(const QSize &sourceSize)
84+{
85+ if (sourceSize != m_sourceSize) {
86+ m_sourceSize = sourceSize;
87+ Q_EMIT sourceSizeChanged();
88+ }
89+}
90+
91+void CroppedImageSizer::requestImage()
92+{
93+ if (m_reply) {
94+ m_reply->disconnect();
95+ m_reply->abort();
96+ m_reply->deleteLater();
97+ m_reply = nullptr;
98+ }
99+
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);
104+ } else {
105+ setSourceSize(QSize(-1, -1));
106+ }
107+}
108+
109+void CroppedImageSizer::requestFinished()
110+{
111+ QImageReader m_reader(m_reply);
112+ m_imageSize = m_reader.size();
113+
114+ m_reply->deleteLater();
115+ m_reply = nullptr;
116+
117+ calculateSourceSize();
118+}
119+
120+void CroppedImageSizer::calculateSourceSize()
121+{
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();
126+ if (ar > ssar) {
127+ setSourceSize(QSize(m_width, 0));
128+ } else {
129+ setSourceSize(QSize(0, m_height));
130+ }
131+ } else {
132+ qWarning() << "Invalid size for " << m_source << m_imageSize;
133+ setSourceSize(QSize(0, 0));
134+ }
135+ } else {
136+ setSourceSize(QSize(-1, -1));
137+ }
138+}