on xenial help sign in
It's the source being valid only that should trigger the network request
commite8a9d98036de6c3ac75b3c37cde3236cd24043af authorAlbert Astals <albert.astals@canonical.com> dateWed 12 Nov 2014, 13:06 (12 years ago) parentf96138d
2 files changed+44−23
plugins/Dash/croppedimagesizer.cpp+39−22 plugins/Dash/croppedimagesizer.h+5−1
plugins/Dash/croppedimagesizer.cpp+39−22
@@ -26,6 +26,7 @@
2626CroppedImageSizer::CroppedImageSizer()
2727{
2828 connect(this, &CroppedImageSizer::inputParamsChanged, this, &CroppedImageSizer::calculateSourceSize);
29+ connect(this, &CroppedImageSizer::sourceChanged, this, &CroppedImageSizer::updateImageSize);
2930}
3031
3132CroppedImageSizer::~CroppedImageSizer()
@@ -44,7 +45,7 @@ void CroppedImageSizer::setSource(const QUrl &source)
4445{
4546 if (source != m_source) {
4647 m_source = source;
47− Q_EMIT inputParamsChanged();
48+ Q_EMIT sourceChanged();
4849 }
4950}
5051
@@ -79,43 +80,59 @@ QSize CroppedImageSizer::sourceSize() const
7980 return m_sourceSize;
8081}
8182
82−void CroppedImageSizer::calculateSourceSize()
83+void CroppedImageSizer::setSourceSize(const QSize &sourceSize)
8384{
84− if (m_source.isValid() && m_width > 0 && m_height > 0 && qmlEngine(this) && qmlEngine(this)->networkAccessManager()) {
85− if (m_reply) {
86− m_reply->disconnect();
87− m_reply->abort();
88− m_reply->deleteLater();
89− }
85+ if (sourceSize != m_sourceSize) {
86+ m_sourceSize = sourceSize;
87+ Q_EMIT sourceSizeChanged();
88+ }
89+}
90+
91+void CroppedImageSizer::updateImageSize()
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()) {
90101 QNetworkRequest request(m_source);
91102 m_reply = qmlEngine(this)->networkAccessManager()->get(request);
92103 connect(m_reply, &QNetworkReply::finished, this, &CroppedImageSizer::requestFinished);
104+ } else {
105+ setSourceSize(QSize(-1, -1));
93106 }
94107}
95108
96109void CroppedImageSizer::requestFinished()
97110{
98111 QImageReader m_reader(m_reply);
99− const QSize imageSize = m_reader.size();
100− if (imageSize.isValid()) {
101− if (m_height > 0 && imageSize.height() > 0) {
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()) {
102124 const qreal ar = m_width / m_height;
103− const qreal ssar = imageSize.width() / (qreal)imageSize.height();
125+ const qreal ssar = m_imageSize.width() / (qreal)m_imageSize.height();
104126 if (ar > ssar) {
105− m_sourceSize = QSize(m_width, 0);
127+ setSourceSize(QSize(m_width, 0));
106128 } else {
107− m_sourceSize = QSize(0, m_height);
129+ setSourceSize(QSize(0, m_height));
108130 }
109131 } else {
110− qWarning() << "Invalid height parameter or image height" << m_height << imageSize.height();
111− m_sourceSize = QSize(0, 0);
132+ qWarning() << "Invalid size for " << m_source << m_imageSize << m_reply;
133+ setSourceSize(QSize(0, 0));
112134 }
113135 } else {
114− qWarning() << "Could not find size of" << m_source;
115− m_sourceSize = QSize(0, 0);
136+ setSourceSize(QSize(-1, -1));
116137 }
117− sourceSizeChanged();
118−
119− m_reply->deleteLater();
120− m_reply = nullptr;
121138}
plugins/Dash/croppedimagesizer.h+5−1
@@ -28,7 +28,7 @@ class CroppedImageSizer : public QObject
2828{
2929 Q_OBJECT
3030
31− Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY inputParamsChanged)
31+ Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
3232 Q_PROPERTY(qreal width READ width WRITE setWidth NOTIFY inputParamsChanged)
3333 Q_PROPERTY(qreal height READ height WRITE setHeight NOTIFY inputParamsChanged)
3434 Q_PROPERTY(QSize sourceSize READ sourceSize NOTIFY sourceSizeChanged)
@@ -47,20 +47,24 @@ public:
4747 void setHeight(qreal height);
4848
4949 QSize sourceSize() const;
50+ void setSourceSize(const QSize &sourceSize);
5051
5152Q_SIGNALS:
5253 void inputParamsChanged();
54+ void sourceChanged();
5355 void sourceSizeChanged();
5456
5557private Q_SLOT:
5658 void calculateSourceSize();
5759 void requestFinished();
60+ void updateImageSize();
5861
5962private:
6063 QUrl m_source;
6164 qreal m_width = 0;
6265 qreal m_height = 0;
6366 QSize m_sourceSize = QSize(-1, -1);
67+ QSize m_imageSize;
6468 QNetworkReply *m_reply = nullptr;
6569};
6670