From fbf56fcf03684a9d0a57ac5ebfdd5d535ead37a8 Mon Sep 17 00:00:00 2001 From: drbob Date: Mon, 18 May 2020 17:35:08 +1000 Subject: [PATCH] Add image handling to PulseAddDialog and PulseMessage --- .../src/gui/TheWire/PulseAddDialog.cpp | 158 +++++++++++++++++- .../src/gui/TheWire/PulseAddDialog.h | 20 +++ .../src/gui/TheWire/PulseAddDialog.ui | 84 +++++++++- .../src/gui/TheWire/PulseMessage.cpp | 40 ++++- .../src/gui/TheWire/PulseMessage.ui | 32 ++-- 5 files changed, 319 insertions(+), 15 deletions(-) diff --git a/retroshare-gui/src/gui/TheWire/PulseAddDialog.cpp b/retroshare-gui/src/gui/TheWire/PulseAddDialog.cpp index 10e76f173..edf95df35 100644 --- a/retroshare-gui/src/gui/TheWire/PulseAddDialog.cpp +++ b/retroshare-gui/src/gui/TheWire/PulseAddDialog.cpp @@ -19,6 +19,7 @@ *******************************************************************************/ #include +#include #include "PulseDetails.h" @@ -37,6 +38,8 @@ PulseAddDialog::PulseAddDialog(QWidget *parent) connect(ui.pushButton_ClearDisplayAs, SIGNAL( clicked( void ) ), this, SLOT( clearDisplayAs( void ) ) ); connect(ui.pushButton_Cancel, SIGNAL( clicked( void ) ), this, SLOT( cancelPulse( void ) ) ); connect(ui.textEdit_Pulse, SIGNAL( textChanged( void ) ), this, SLOT( pulseTextChanged( void ) ) ); + + setAcceptDrops(true); } void PulseAddDialog::setGroup(RsWireGroup &group) @@ -84,7 +87,7 @@ void PulseAddDialog::cleanup() } // then finally delete layout; - mIsReply = false; + mIsReply = false; } ui.frame_reply->setVisible(false); ui.comboBox_sentiment->setCurrentIndex(0); @@ -98,6 +101,23 @@ void PulseAddDialog::cleanup() ui.pushButton_Post->setText("Post Pulse to Wire"); ui.frame_input->setVisible(true); ui.widget_sentiment->setVisible(true); + + // cleanup images. + mImage1.clear(); + ui.label_image1->clear(); + ui.label_image1->setText("Drag and Drop Image"); + + mImage2.clear(); + ui.label_image2->clear(); + ui.label_image2->setText("Drag and Drop Image"); + + mImage3.clear(); + ui.label_image3->clear(); + ui.label_image3->setText("Drag and Drop Image"); + + mImage4.clear(); + ui.label_image4->clear(); + ui.label_image4->setText("Drag and Drop Image"); } void PulseAddDialog::pulseTextChanged() @@ -221,6 +241,10 @@ void PulseAddDialog::postOriginalPulse() pPulse->mSentiment = WIRE_PULSE_SENTIMENT_NO_SENTIMENT; pPulse->mPulseText = ui.textEdit_Pulse->toPlainText().toStdString(); // set images here too. + pPulse->mImage1 = mImage1; + pPulse->mImage2 = mImage2; + pPulse->mImage3 = mImage3; + pPulse->mImage4 = mImage4; // this should be in async thread, so doesn't block UI thread. if (!rsWire->createOriginalPulse(mGroup.mMeta.mGroupId, pPulse)) @@ -265,7 +289,11 @@ void PulseAddDialog::postReplyPulse() pPulse->mSentiment = toPulseSentiment(ui.comboBox_sentiment->currentIndex()); pPulse->mPulseText = ui.textEdit_Pulse->toPlainText().toStdString(); - // set images too. + // set images here too. + pPulse->mImage1 = mImage1; + pPulse->mImage2 = mImage2; + pPulse->mImage3 = mImage3; + pPulse->mImage4 = mImage4; // this should be in async thread, so doesn't block UI thread. if (!rsWire->createReplyPulse(mReplyToPulse.mMeta.mGroupId, @@ -288,5 +316,131 @@ void PulseAddDialog::clearDialog() ui.textEdit_Pulse->setPlainText(""); } +//--------------------------------------------------------------------- +// Drag and Drop Images. + +void PulseAddDialog::dragEnterEvent(QDragEnterEvent *event) +{ + std::cerr << "PulseAddDialog::dragEnterEvent()"; + std::cerr << std::endl; + + if (event->mimeData()->hasUrls()) + { + std::cerr << "PulseAddDialog::dragEnterEvent() Accepting"; + std::cerr << std::endl; + event->accept(); + } + else + { + std::cerr << "PulseAddDialog::dragEnterEvent() Ignoring"; + std::cerr << std::endl; + event->ignore(); + } +} + +void PulseAddDialog::dragLeaveEvent(QDragLeaveEvent *event) +{ + std::cerr << "PulseAddDialog::dragLeaveEvent()"; + std::cerr << std::endl; + + event->ignore(); +} + +void PulseAddDialog::dragMoveEvent(QDragMoveEvent *event) +{ + std::cerr << "PulseAddDialog::dragMoveEvent()"; + std::cerr << std::endl; + + event->accept(); +} + +void PulseAddDialog::dropEvent(QDropEvent *event) +{ + std::cerr << "PulseAddDialog::dropEvent()"; + std::cerr << std::endl; + + if (event->mimeData()->hasUrls()) + { + std::cerr << "PulseAddDialog::dropEvent() Urls:" << std::endl; + + QList urls = event->mimeData()->urls(); + QList::iterator uit; + for (uit = urls.begin(); uit != urls.end(); ++uit) + { + QString localpath = uit->toLocalFile(); + std::cerr << "Whole URL: " << uit->toString().toStdString() << std::endl; + std::cerr << "or As Local File: " << localpath.toStdString() << std::endl; + + addImage(localpath); + } + event->setDropAction(Qt::CopyAction); + event->accept(); + } + else + { + std::cerr << "PulseAddDialog::dropEvent Ignoring"; + std::cerr << std::endl; + event->ignore(); + } +} +void PulseAddDialog::addImage(const QString &path) +{ + std::cerr << "PulseAddDialog::addImage() loading image from: " << path.toStdString(); + std::cerr << std::endl; + + QPixmap qtn = QPixmap(path); + if (qtn.isNull()) { + std::cerr << "PulseAddDialog::addImage() Invalid Image"; + std::cerr << std::endl; + return; + } + + QPixmap image; + if ((qtn.width() <= 512) && (qtn.height() <= 512)) { + image = qtn; + } else { + image = qtn.scaled(512, 512, Qt::KeepAspectRatio, Qt::SmoothTransformation); + } + + // scaled down for display, allow wide images. + QPixmap icon = qtn.scaled(256, 128, Qt::KeepAspectRatio, Qt::SmoothTransformation); + QByteArray ba; + QBuffer buffer(&ba); + + buffer.open(QIODevice::WriteOnly); + image.save(&buffer, "JPG"); + + if (mImage1.empty()) { + std::cerr << "PulseAddDialog::addImage() Installing in Image1"; + std::cerr << std::endl; + ui.label_image1->setPixmap(icon); + mImage1.copy((uint8_t *) ba.data(), ba.size()); + std::cerr << "PulseAddDialog::addImage() Installing in Image1 Size: " << mImage1.mSize; + std::cerr << std::endl; + } + else if (mImage2.empty()) { + ui.label_image2->setPixmap(icon); + mImage2.copy((uint8_t *) ba.data(), ba.size()); + std::cerr << "PulseAddDialog::addImage() Installing in Image2 Size: " << mImage2.mSize; + std::cerr << std::endl; + } + else if (mImage3.empty()) { + ui.label_image3->setPixmap(icon); + mImage3.copy((uint8_t *) ba.data(), ba.size()); + std::cerr << "PulseAddDialog::addImage() Installing in Image3 Size: " << mImage3.mSize; + std::cerr << std::endl; + } + else if (mImage4.empty()) { + ui.label_image4->setPixmap(icon); + mImage4.copy((uint8_t *) ba.data(), ba.size()); + std::cerr << "PulseAddDialog::addImage() Installing in Image4 Size: " << mImage4.mSize; + std::cerr << std::endl; + } + else { + std::cerr << "PulseAddDialog::addImage() Images all full"; + std::cerr << std::endl; + } +} + diff --git a/retroshare-gui/src/gui/TheWire/PulseAddDialog.h b/retroshare-gui/src/gui/TheWire/PulseAddDialog.h index 88b0bb815..881b0c1a7 100644 --- a/retroshare-gui/src/gui/TheWire/PulseAddDialog.h +++ b/retroshare-gui/src/gui/TheWire/PulseAddDialog.h @@ -25,6 +25,14 @@ #include + +QT_BEGIN_NAMESPACE +class QDragEnterEvent; +class QDropEvent; +class QMouseEvent; +QT_END_NAMESPACE + + class PulseAddDialog : public QWidget { Q_OBJECT @@ -56,6 +64,12 @@ private: uint32_t toPulseSentiment(int index); protected: + void dragEnterEvent(QDragEnterEvent *event); + void dragLeaveEvent(QDragLeaveEvent *event); + void dragMoveEvent(QDragMoveEvent *event); + void dropEvent(QDropEvent *event); + + void addImage(const QString &path); RsWireGroup mGroup; // replyWith. @@ -64,6 +78,12 @@ protected: RsWirePulse mReplyToPulse; uint32_t mReplyType; + // images + RsGxsImage mImage1; + RsGxsImage mImage2; + RsGxsImage mImage3; + RsGxsImage mImage4; + Ui::PulseAddDialog ui; }; diff --git a/retroshare-gui/src/gui/TheWire/PulseAddDialog.ui b/retroshare-gui/src/gui/TheWire/PulseAddDialog.ui index 6e88f6219..0b6c4a70c 100644 --- a/retroshare-gui/src/gui/TheWire/PulseAddDialog.ui +++ b/retroshare-gui/src/gui/TheWire/PulseAddDialog.ui @@ -7,7 +7,7 @@ 0 0 720 - 586 + 633 @@ -161,6 +161,88 @@ + + + + + 0 + 100 + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + + + + 80 + 80 + + + + Drag and Drop Image + + + Qt::AlignCenter + + + + + + + + 80 + 80 + + + + Drag and Drop Image + + + Qt::AlignCenter + + + + + + + + 80 + 80 + + + + Drag and Drop Image + + + Qt::AlignCenter + + + + + + + + 80 + 80 + + + + Drag and Drop Image + + + Qt::AlignCenter + + + + + + diff --git a/retroshare-gui/src/gui/TheWire/PulseMessage.cpp b/retroshare-gui/src/gui/TheWire/PulseMessage.cpp index c7d884d9c..cd666a408 100644 --- a/retroshare-gui/src/gui/TheWire/PulseMessage.cpp +++ b/retroshare-gui/src/gui/TheWire/PulseMessage.cpp @@ -37,30 +37,66 @@ void PulseMessage::setup(RsWirePulseSPtr pulse) setMessage(QString::fromStdString(pulse->mPulseText)); // setup images. + int width = 256; + int height = 128; + bool imagesShown = false; + + if (pulse->mImage2.empty()) { + // allow wider space for image 1. + width = 512; + } + if (!pulse->mImage1.empty()) { // install image. + QPixmap qtn; + qtn.loadFromData(pulse->mImage1.mData, pulse->mImage1.mSize); + label_image1->setPixmap(qtn.scaled(width, height, + Qt::KeepAspectRatio, Qt::SmoothTransformation)); + imagesShown = true; } else { - // leave this visible for a bit. - // label_image1->setVisible(false); + label_image1->setVisible(false); } if (!pulse->mImage2.empty()) { // install image. + QPixmap qtn; + qtn.loadFromData(pulse->mImage2.mData, pulse->mImage2.mSize); + label_image2->setPixmap(qtn.scaled(width, height, + Qt::KeepAspectRatio, Qt::SmoothTransformation)); + imagesShown = true; } else { label_image2->setVisible(false); } + width = 256; + if (pulse->mImage4.empty()) { + // allow wider space for image 3. + width = 512; + } + if (!pulse->mImage3.empty()) { // install image. + QPixmap qtn; + qtn.loadFromData(pulse->mImage3.mData, pulse->mImage3.mSize); + label_image3->setPixmap(qtn.scaled(width, height, + Qt::KeepAspectRatio, Qt::SmoothTransformation)); + imagesShown = true; } else { label_image3->setVisible(false); } if (!pulse->mImage4.empty()) { // install image. + QPixmap qtn; + qtn.loadFromData(pulse->mImage4.mData, pulse->mImage4.mSize); + label_image4->setPixmap(qtn.scaled(width, height, + Qt::KeepAspectRatio, Qt::SmoothTransformation)); + imagesShown = true; } else { label_image4->setVisible(false); } + + frame_expand->setVisible(imagesShown); } void PulseMessage::setMessage(QString msg) diff --git a/retroshare-gui/src/gui/TheWire/PulseMessage.ui b/retroshare-gui/src/gui/TheWire/PulseMessage.ui index 17d88eec3..a85f602c6 100644 --- a/retroshare-gui/src/gui/TheWire/PulseMessage.ui +++ b/retroshare-gui/src/gui/TheWire/PulseMessage.ui @@ -6,8 +6,8 @@ 0 0 - 553 - 281 + 570 + 376 @@ -30,52 +30,64 @@ - 80 - 80 + 128 + 128 Image + + Qt::AlignCenter + - 80 - 80 + 128 + 128 Image + + Qt::AlignCenter + - 80 - 80 + 128 + 128 Image + + Qt::AlignCenter + - 80 - 80 + 128 + 128 Image + + Qt::AlignCenter +