@@ -0,0 +1,115 @@
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+}
30+
31+CroppedImageSizer::~CroppedImageSizer()
32+{
33+ if (m_reply) {
34+ m_reply->deleteLater();
35+ }
36+}
37+
38+QUrl CroppedImageSizer::source() const
39+{
40+ return m_source;
41+}
42+
43+void CroppedImageSizer::setSource(const QUrl &source)
44+{
45+ if (source != m_source) {
46+ m_source = source;
47+ Q_EMIT inputParamsChanged();
48+ }
49+}
50+
51+qreal CroppedImageSizer::width() const
52+{
53+ return m_width;
54+}
55+
56+void CroppedImageSizer::setWidth(qreal width)
57+{
58+ if (width != m_width) {
59+ m_width = width;
60+ Q_EMIT inputParamsChanged();
61+ }
62+}
63+
64+qreal CroppedImageSizer::height() const
65+{
66+ return m_height;
67+}
68+
69+void CroppedImageSizer::setHeight(qreal height)
70+{
71+ if (height != m_height) {
72+ m_height = height;
73+ Q_EMIT inputParamsChanged();
74+ }
75+}
76+
77+QSize CroppedImageSizer::sourceSize() const
78+{
79+ return m_sourceSize;
80+}
81+
82+void CroppedImageSizer::calculateSourceSize()
83+{
84+ if (m_source.isValid() && m_width > 0 && m_height > 0 && qmlEngine(this) && qmlEngine(this)->networkAccessManager()) {
85+ if (m_reply) {
86+ m_reply->abort();
87+ m_reply->deleteLater();
88+ }
89+ QNetworkRequest request(m_source);
90+ m_reply = qmlEngine(this)->networkAccessManager()->get(request);
91+ connect(m_reply, &QNetworkReply::finished, this, &CroppedImageSizer::requestFinished);
92+ }
93+}
94+
95+void CroppedImageSizer::requestFinished()
96+{
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();
102+ if (ar > ssar) {
103+ m_sourceSize = QSize(m_width, 0);
104+ } else {
105+ m_sourceSize = QSize(0, m_height);
106+ }
107+ } else {
108+ qWarning() << "Could not find size of" << m_source;
109+ m_sourceSize = QSize(0, 0);
110+ }
111+ sourceSizeChanged();
112+
113+ m_reply->deleteLater();
114+ m_reply = nullptr;
115+}