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