From aa334d0110e3c34f01dfab67c367ba20c4a0f140 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 22 Dec 2018 08:18:17 +0100 Subject: [PATCH] Can enlarge conversation images. --- ComunicMessages.pro | 8 +++-- widgets/clickablelabel.cpp | 11 +++++++ widgets/clickablelabel.h | 26 ++++++++++++++++ widgets/conversationmessagewidget.cpp | 9 +++--- widgets/remoteimagemanager.cpp | 42 +++++++++++++++++++++++++ widgets/remoteimagemanager.h | 45 +++++++++++++++++++++++++++ 6 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 widgets/clickablelabel.cpp create mode 100644 widgets/clickablelabel.h create mode 100644 widgets/remoteimagemanager.cpp create mode 100644 widgets/remoteimagemanager.h diff --git a/ComunicMessages.pro b/ComunicMessages.pro index 60e4a1f..3df1fad 100644 --- a/ComunicMessages.pro +++ b/ComunicMessages.pro @@ -33,7 +33,9 @@ SOURCES += \ widgets/conversationmessagewidget.cpp \ data/conversationmessageslist.cpp \ helpers/imageloadhelper.cpp \ - utils/filesutils.cpp + utils/filesutils.cpp \ + widgets/remoteimagemanager.cpp \ + widgets/clickablelabel.cpp HEADERS += \ helpers/accounthelper.h \ @@ -69,7 +71,9 @@ HEADERS += \ data/conversationmessageslist.h \ helpers/imageloadhelper.h \ utils/filesutils.h \ - data/qlabelholder.h + data/qlabelholder.h \ + widgets/remoteimagemanager.h \ + widgets/clickablelabel.h FORMS += \ widgets/loginwidget.ui \ diff --git a/widgets/clickablelabel.cpp b/widgets/clickablelabel.cpp new file mode 100644 index 0000000..7f0f2ca --- /dev/null +++ b/widgets/clickablelabel.cpp @@ -0,0 +1,11 @@ +#include "clickablelabel.h" + +ClickableLabel::ClickableLabel(QWidget *parent) : QLabel (parent) +{ + +} + +void ClickableLabel::mousePressEvent(QMouseEvent *) +{ + emit clicked(); +} diff --git a/widgets/clickablelabel.h b/widgets/clickablelabel.h new file mode 100644 index 0000000..5825f7d --- /dev/null +++ b/widgets/clickablelabel.h @@ -0,0 +1,26 @@ +#ifndef CLICKABLELABEL_H +#define CLICKABLELABEL_H + +#include + +class ClickableLabel : public QLabel +{ + Q_OBJECT + +public: + ClickableLabel(QWidget *parent=nullptr); + +signals: + + /** + * This signal is emitted when the a click is made o the view + */ + void clicked(); + +protected: + + void mousePressEvent(QMouseEvent*) override; + +}; + +#endif // CLICKABLELABEL_H diff --git a/widgets/conversationmessagewidget.cpp b/widgets/conversationmessagewidget.cpp index 6dd048e..87d561b 100644 --- a/widgets/conversationmessagewidget.cpp +++ b/widgets/conversationmessagewidget.cpp @@ -2,7 +2,8 @@ #include "ui_conversationmessagewidget.h" #include "../data/user.h" #include "../data/conversationmessage.h" -#include "../helpers/imageloadhelper.h" +#include "remoteimagemanager.h" +#include "clickablelabel.h" ConversationMessageWidget::ConversationMessageWidget(QWidget *parent) : QWidget(parent), @@ -20,15 +21,15 @@ void ConversationMessageWidget::setMessage(const ConversationMessage &message, c { ui->nameLabel->setText(user.displayName()); ui->messageLabel->setText(message.message()); - ImageLoadHelper::Load(ui->accountImageLabel, user.accountImage()); + new RemoteImageManager(ui->accountImageLabel, user.accountImage(), this); //Add message image (if any) if(message.hasImage()){ - QLabel *label = new QLabel; + ClickableLabel *label = new ClickableLabel; label->setParent(ui->messageContentContainer); label->setScaledContents(true); label->setMaximumSize(200, 200); ui->messageLayout->addWidget(label); - ImageLoadHelper::Load(label, message.imagePath()); + (new RemoteImageManager(label, message.imagePath(), this))->setEnlargeable(true); } } diff --git a/widgets/remoteimagemanager.cpp b/widgets/remoteimagemanager.cpp new file mode 100644 index 0000000..5f9c7d0 --- /dev/null +++ b/widgets/remoteimagemanager.cpp @@ -0,0 +1,42 @@ +#include + +#include "remoteimagemanager.h" +#include "clickablelabel.h" +#include "../helpers/imageloadhelper.h" + +RemoteImageManager::RemoteImageManager(QLabel *label, const QString &url, QObject *parent) : QObject(parent) +{ + //Load remote image + ImageLoadHelper::Load(label, url); + mUrl = url; +} + +RemoteImageManager::RemoteImageManager(ClickableLabel *label, const QString &url, QObject *parent) : QObject (parent) +{ + //Load remote image + ImageLoadHelper::Load(label, url); + mUrl = url; + + connect(label, &ClickableLabel::clicked, this, &RemoteImageManager::enlarge); +} + +bool RemoteImageManager::enlargeable() const +{ + return mEnlargeable; +} + +void RemoteImageManager::setEnlargeable(bool enlargeable) +{ + mEnlargeable = enlargeable; +} + +void RemoteImageManager::enlarge() +{ + if(!enlargeable()) + return; + + //Open the image in a bigger label (to improve) + QLabel *label = new QLabel; + ImageLoadHelper::Load(label, mUrl); + label->show(); +} diff --git a/widgets/remoteimagemanager.h b/widgets/remoteimagemanager.h new file mode 100644 index 0000000..1a19136 --- /dev/null +++ b/widgets/remoteimagemanager.h @@ -0,0 +1,45 @@ +/** + * This object has to be used in all classes + * in order to manage remote image management + * + * @author Pierre HUBERT + */ +#ifndef REMOTEIMAGEMANAGER_H +#define REMOTEIMAGEMANAGER_H + +#include + +class QLabel; +class ClickableLabel; + +class RemoteImageManager : public QObject +{ + Q_OBJECT +public: + explicit RemoteImageManager(QLabel *label, const QString &url, QObject *parent); //Avoid loss of memory : force parent = nullptr); + explicit RemoteImageManager(ClickableLabel *label, const QString &url, QObject *parent); + + /** + * Note that the enlargable feature is currently available only with + * the ClickableLabel widgets + */ + bool enlargeable() const; + void setEnlargeable(bool enlargeable); + +signals: + +public slots: + + /** + * This slot is triggered when we want to enlarge the image + */ + void enlarge(); + +private: + + //Private URL + QString mUrl; + bool mEnlargeable = false; +}; + +#endif // REMOTEIMAGEMANAGER_H