on xenial help sign in
Merge
commit383d3cb12a2b49c764e4d5d4ed49f51dd034c8e6 authorAlbert Astals <albert.astals@canonical.com> dateWed 12 Nov 2014, 13:29 (12 years ago) parent5509d1ae65178d
5 files changed+56−35
plugins/Dash/CroppedImageMinimumSourceSize.qml+6−6 plugins/Dash/croppedimagesizer.cpp+39−9 plugins/Dash/croppedimagesizer.h+7−2 plugins/Dash/croppedimagesizerasyncworker.cpp+3−17 plugins/Dash/croppedimagesizerasyncworker.h+1−1
plugins/Dash/CroppedImageMinimumSourceSize.qml+6−6
@@ -21,11 +21,11 @@ Item {
2121 id: root
2222
2323 property string source
24− property alias image: image
25− property alias asynchronous: image.asynchronous
26− property alias verticalAlignment: image.verticalAlignment
27− property alias horizontalAlignment: image.horizontalAlignment
28− property alias fillMode: image.fillMode
24+ property alias image: innerImage
25+ property alias asynchronous: innerImage.asynchronous
26+ property alias verticalAlignment: innerImage.verticalAlignment
27+ property alias horizontalAlignment: innerImage.horizontalAlignment
28+ property alias fillMode: innerImage.fillMode
2929
3030 CroppedImageSizer {
3131 id: sizer
@@ -35,7 +35,7 @@ Item {
3535 }
3636
3737 Image {
38− id: image
38+ id: innerImage
3939 anchors.fill: parent
4040 fillMode: Image.PreserveAspectCrop
4141 sourceSize: sizer.sourceSize.width == 0 && sizer.sourceSize.height == 0 ? undefined : sizer.sourceSize
plugins/Dash/croppedimagesizer.cpp+39−9
@@ -30,6 +30,7 @@ CroppedImageSizer::CroppedImageSizer()
3030 m_worker(nullptr)
3131{
3232 connect(this, &CroppedImageSizer::inputParamsChanged, this, &CroppedImageSizer::calculateSourceSize);
33+ connect(this, &CroppedImageSizer::sourceChanged, this, &CroppedImageSizer::updateImageSize);
3334}
3435
3536CroppedImageSizer::~CroppedImageSizer()
@@ -48,7 +49,7 @@ void CroppedImageSizer::setSource(const QUrl &source)
4849{
4950 if (source != m_source) {
5051 m_source = source;
51− Q_EMIT inputParamsChanged();
52+ Q_EMIT sourceChanged();
5253 }
5354}
5455
@@ -91,18 +92,47 @@ void CroppedImageSizer::setSourceSize(const QSize &size)
9192 }
9293}
9394
94−void CroppedImageSizer::calculateSourceSize()
95+void CroppedImageSizer::setImageSize(const QSize &imageSize)
9596{
96− if (m_source.isValid() && m_width > 0 && m_height > 0 && qmlEngine(this) && qmlEngine(this)->networkAccessManager()) {
97− if (m_worker) {
98− m_worker->abort();
99− }
97+ if (imageSize != m_imageSize) {
98+ m_imageSize = imageSize;
99+ m_worker = nullptr;
100+ calculateSourceSize();
101+ }
102+}
103+
104+void CroppedImageSizer::updateImageSize()
105+{
106+ if (m_worker) {
107+ m_worker->abort();
108+ m_worker = nullptr;
109+ }
100110
111+ if (m_source.isValid() && qmlEngine(this) && qmlEngine(this)->networkAccessManager()) {
101112 QNetworkRequest request(m_source);
102113 QNetworkReply *reply = qmlEngine(this)->networkAccessManager()->get(request);
103114 m_worker = new CroppedImageSizerAsyncWorker(this, reply);
104− } else if (m_sourceSize != QSize(-1, -1)) {
105− m_sourceSize = QSize(-1, -1);
106− sourceSizeChanged();
115+ } else {
116+ setSourceSize(QSize(-1, -1));
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));
107137 }
108138}
plugins/Dash/croppedimagesizer.h+7−2
@@ -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,25 @@ public:
4747 void setHeight(qreal height);
4848
4949 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);
5153
5254Q_SIGNALS:
5355 void inputParamsChanged();
56+ void sourceChanged();
5457 void sourceSizeChanged();
5558
5659private Q_SLOT:
5760 void calculateSourceSize();
61+ void updateImageSize();
5862
5963private:
6064 QUrl m_source;
6165 qreal m_width;
6266 qreal m_height;
6367 QSize m_sourceSize;
68+ QSize m_imageSize;
6469 QPointer<CroppedImageSizerAsyncWorker> m_worker;
6570};
6671
plugins/Dash/croppedimagesizerasyncworker.cpp+3−17
@@ -47,7 +47,7 @@ void CroppedImageSizerAsyncWorker::requestFinished()
4747 QMutexLocker locker(&m_mutex);
4848 if (m_sizer) {
4949 m_ignoreAbort = true;
50− QtConcurrent::run(processRequestFinished, m_sizer->width(), m_sizer->height(), m_sizer->source(), this);
50+ QtConcurrent::run(processRequestFinished, this);
5151 } else {
5252 // We were aborted delete ourselves
5353 m_reply->deleteLater();
@@ -55,30 +55,16 @@ void CroppedImageSizerAsyncWorker::requestFinished()
5555 }
5656}
5757
58−void CroppedImageSizerAsyncWorker::processRequestFinished(qreal width, qreal height, QUrl source, CroppedImageSizerAsyncWorker *worker)
58+void CroppedImageSizerAsyncWorker::processRequestFinished(CroppedImageSizerAsyncWorker *worker)
5959{
6060 // This runs on non main thread
6161 // m_reply has finished at this point and is protected against change by m_ignoreAbort
6262 QImageReader reader(worker->m_reply);
6363 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− }
7864
7965 worker->m_mutex.lock();
8066 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));
8268 }
8369 worker->m_mutex.unlock();
8470
plugins/Dash/croppedimagesizerasyncworker.h+1−1
@@ -36,7 +36,7 @@ private Q_SLOTS:
3636 void requestFinished();
3737
3838private:
39− static void processRequestFinished(qreal width, qreal height, QUrl source, CroppedImageSizerAsyncWorker *worker);
39+ static void processRequestFinished(CroppedImageSizerAsyncWorker *worker);
4040
4141 QMutex m_mutex;
4242 CroppedImageSizer *m_sizer;