on xenial help sign in
Merge
383d3cbAlbert Astals
plugins/Dash/CroppedImageMinimumSourceSize.qml+6−6
@@ -21,11 +21,11 @@ Item {
21 id: root
21 id: root
22
22
23 property string source
23 property string source
24− property alias image: image
24+ property alias image: innerImage
25− property alias asynchronous: image.asynchronous
25+ property alias asynchronous: innerImage.asynchronous
26− property alias verticalAlignment: image.verticalAlignment
26+ property alias verticalAlignment: innerImage.verticalAlignment
27− property alias horizontalAlignment: image.horizontalAlignment
27+ property alias horizontalAlignment: innerImage.horizontalAlignment
28− property alias fillMode: image.fillMode
28+ property alias fillMode: innerImage.fillMode
29
29
30 CroppedImageSizer {
30 CroppedImageSizer {
31 id: sizer
31 id: sizer
@@ -35,7 +35,7 @@ Item {
35 }
35 }
36
36
37 Image {
37 Image {
38− id: image
38+ id: innerImage
39 anchors.fill: parent
39 anchors.fill: parent
40 fillMode: Image.PreserveAspectCrop
40 fillMode: Image.PreserveAspectCrop
41 sourceSize: sizer.sourceSize.width == 0 && sizer.sourceSize.height == 0 ? undefined : sizer.sourceSize
41 sourceSize: sizer.sourceSize.width == 0 && sizer.sourceSize.height == 0 ? undefined : sizer.sourceSize
plugins/Dash/croppedimagesizer.cpp+39−9
@@ -30,6 +30,7 @@ CroppedImageSizer::CroppedImageSizer()
30 m_worker(nullptr)
30 m_worker(nullptr)
31{
31{
32 connect(this, &CroppedImageSizer::inputParamsChanged, this, &CroppedImageSizer::calculateSourceSize);
32 connect(this, &CroppedImageSizer::inputParamsChanged, this, &CroppedImageSizer::calculateSourceSize);
33+ connect(this, &CroppedImageSizer::sourceChanged, this, &CroppedImageSizer::updateImageSize);
33}
34}
34
35
35CroppedImageSizer::~CroppedImageSizer()
36CroppedImageSizer::~CroppedImageSizer()
@@ -48,7 +49,7 @@ void CroppedImageSizer::setSource(const QUrl &source)
48{
49{
49 if (source != m_source) {
50 if (source != m_source) {
50 m_source = source;
51 m_source = source;
51− Q_EMIT inputParamsChanged();
52+ Q_EMIT sourceChanged();
52 }
53 }
53}
54}
54
55
@@ -91,18 +92,47 @@ void CroppedImageSizer::setSourceSize(const QSize &size)
91 }
92 }
92}
93}
93
94
94−void CroppedImageSizer::calculateSourceSize()
95+void CroppedImageSizer::setImageSize(const QSize &imageSize)
95{
96{
96− if (m_source.isValid() && m_width > 0 && m_height > 0 && qmlEngine(this) && qmlEngine(this)->networkAccessManager()) {
97+ if (imageSize != m_imageSize) {
98+ m_imageSize = imageSize;
99+ m_worker = nullptr;
100+ calculateSourceSize();
101+ }
102+}
103+
104+void CroppedImageSizer::updateImageSize()
105+{
97− if (m_worker) {
106+ if (m_worker) {
98− m_worker->abort();
107+ m_worker->abort();
108+ m_worker = nullptr;
99− }
109+ }
100
110
111+ if (m_source.isValid() && qmlEngine(this) && qmlEngine(this)->networkAccessManager()) {
101 QNetworkRequest request(m_source);
112 QNetworkRequest request(m_source);
102 QNetworkReply *reply = qmlEngine(this)->networkAccessManager()->get(request);
113 QNetworkReply *reply = qmlEngine(this)->networkAccessManager()->get(request);
103 m_worker = new CroppedImageSizerAsyncWorker(this, reply);
114 m_worker = new CroppedImageSizerAsyncWorker(this, reply);
104− } else if (m_sourceSize != QSize(-1, -1)) {
115+ } else {
105− m_sourceSize = QSize(-1, -1);
116+ setSourceSize(QSize(-1, -1));
106− sourceSizeChanged();
117+ }
118+}
119+
120+void CroppedImageSizer::calculateSourceSize()
121+{
122+ if (m_source.isValid() && m_width > 0 && m_height > 0 && !m_worker) {
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));
107 }
137 }
108}
138}
plugins/Dash/croppedimagesizer.h+7−2
@@ -28,7 +28,7 @@ class CroppedImageSizer : public QObject
28{
28{
29 Q_OBJECT
29 Q_OBJECT
30
30
31− Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY inputParamsChanged)
31+ Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
32 Q_PROPERTY(qreal width READ width WRITE setWidth NOTIFY inputParamsChanged)
32 Q_PROPERTY(qreal width READ width WRITE setWidth NOTIFY inputParamsChanged)
33 Q_PROPERTY(qreal height READ height WRITE setHeight NOTIFY inputParamsChanged)
33 Q_PROPERTY(qreal height READ height WRITE setHeight NOTIFY inputParamsChanged)
34 Q_PROPERTY(QSize sourceSize READ sourceSize NOTIFY sourceSizeChanged)
34 Q_PROPERTY(QSize sourceSize READ sourceSize NOTIFY sourceSizeChanged)
@@ -47,20 +47,25 @@ public:
47 void setHeight(qreal height);
47 void setHeight(qreal height);
48
48
49 QSize sourceSize() const;
49 QSize sourceSize() const;
50− Q_INVOKABLE void setSourceSize(const QSize &sourceSize);
50+ void setSourceSize(const QSize &sourceSize);
51+
52+ Q_INVOKABLE void setImageSize(const QSize &imageSize);
51
53
52Q_SIGNALS:
54Q_SIGNALS:
53 void inputParamsChanged();
55 void inputParamsChanged();
56+ void sourceChanged();
54 void sourceSizeChanged();
57 void sourceSizeChanged();
55
58
56private Q_SLOT:
59private Q_SLOT:
57 void calculateSourceSize();
60 void calculateSourceSize();
61+ void updateImageSize();
58
62
59private:
63private:
60 QUrl m_source;
64 QUrl m_source;
61 qreal m_width;
65 qreal m_width;
62 qreal m_height;
66 qreal m_height;
63 QSize m_sourceSize;
67 QSize m_sourceSize;
68+ QSize m_imageSize;
64 QPointer<CroppedImageSizerAsyncWorker> m_worker;
69 QPointer<CroppedImageSizerAsyncWorker> m_worker;
65};
70};
66
71
plugins/Dash/croppedimagesizerasyncworker.cpp+3−17
@@ -47,7 +47,7 @@ void CroppedImageSizerAsyncWorker::requestFinished()
47 QMutexLocker locker(&m_mutex);
47 QMutexLocker locker(&m_mutex);
48 if (m_sizer) {
48 if (m_sizer) {
49 m_ignoreAbort = true;
49 m_ignoreAbort = true;
50− QtConcurrent::run(processRequestFinished, m_sizer->width(), m_sizer->height(), m_sizer->source(), this);
50+ QtConcurrent::run(processRequestFinished, this);
51 } else {
51 } else {
52 // We were aborted delete ourselves
52 // We were aborted delete ourselves
53 m_reply->deleteLater();
53 m_reply->deleteLater();
@@ -55,30 +55,16 @@ void CroppedImageSizerAsyncWorker::requestFinished()
55 }
55 }
56}
56}
57
57
58−void CroppedImageSizerAsyncWorker::processRequestFinished(qreal width, qreal height, QUrl source, CroppedImageSizerAsyncWorker *worker)
58+void CroppedImageSizerAsyncWorker::processRequestFinished(CroppedImageSizerAsyncWorker *worker)
59{
59{
60 // This runs on non main thread
60 // This runs on non main thread
61 // m_reply has finished at this point and is protected against change by m_ignoreAbort
61 // m_reply has finished at this point and is protected against change by m_ignoreAbort
62 QImageReader reader(worker->m_reply);
62 QImageReader reader(worker->m_reply);
63 const QSize imageSize = reader.size();
63 const QSize imageSize = reader.size();
64− QSize sourceSize = QSize(0, 0);
65− if (imageSize.isValid()) {
66− if (height > 0 && imageSize.height() > 0) {
67− const qreal ar = width / height;
68− const qreal ssar = imageSize.width() / (qreal)imageSize.height();
69− if (ar > ssar) {
70− sourceSize = QSize(width, 0);
71− } else {
72− sourceSize = QSize(0, height);
73− }
74− }
75− } else {
76− qWarning() << "Could not find size of" << source << worker;
77− }
78
64
79 worker->m_mutex.lock();
65 worker->m_mutex.lock();
80 if (worker->m_sizer) {
66 if (worker->m_sizer) {
81− QMetaObject::invokeMethod(worker->m_sizer, "setSourceSize", Qt::QueuedConnection, Q_ARG(QSize, sourceSize));
67+ QMetaObject::invokeMethod(worker->m_sizer, "setImageSize", Qt::QueuedConnection, Q_ARG(QSize, imageSize));
82 }
68 }
83 worker->m_mutex.unlock();
69 worker->m_mutex.unlock();
84
70
plugins/Dash/croppedimagesizerasyncworker.h+1−1
@@ -36,7 +36,7 @@ private Q_SLOTS:
36 void requestFinished();
36 void requestFinished();
37
37
38private:
38private:
39− static void processRequestFinished(qreal width, qreal height, QUrl source, CroppedImageSizerAsyncWorker *worker);
39+ static void processRequestFinished(CroppedImageSizerAsyncWorker *worker);
40
40
41 QMutex m_mutex;
41 QMutex m_mutex;
42 CroppedImageSizer *m_sizer;
42 CroppedImageSizer *m_sizer;