Added patch from braindead

Embedded images in private chat and messages (only for QT version 4.7.0 and higher)

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@5569 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2012-09-18 22:59:23 +00:00
parent 4d452b4034
commit 02892d4ae5
7 changed files with 3524 additions and 159 deletions

View File

@ -30,6 +30,7 @@
#include <QTextStream> #include <QTextStream>
#include <QTextCodec> #include <QTextCodec>
#include <QTimer> #include <QTimer>
#include <QTextDocumentFragment>
#include "ChatWidget.h" #include "ChatWidget.h"
#include "ui_ChatWidget.h" #include "ui_ChatWidget.h"
@ -117,7 +118,8 @@ ChatWidget::ChatWidget(QWidget *parent) :
ui->chatTextEdit->installEventFilter(this); ui->chatTextEdit->installEventFilter(this);
#ifdef RS_RELEASE_VERSION #if QT_VERSION < 0x040700
// embedded images are not supported before QT 4.7.0
ui->attachPictureButton->setVisible(false); ui->attachPictureButton->setVisible(false);
#endif #endif
@ -585,7 +587,11 @@ void ChatWidget::addExtraPicture()
// select a picture file // select a picture file
QString file; QString file;
if (misc::getOpenFileName(window(), RshareSettings::LASTDIR_IMAGES, tr("Load Picture File"), "Pictures (*.png *.xpm *.jpg)", file)) { if (misc::getOpenFileName(window(), RshareSettings::LASTDIR_IMAGES, tr("Load Picture File"), "Pictures (*.png *.xpm *.jpg)", file)) {
ui->hashBox->addAttachments(QStringList(file), HashedFile::Picture); QString encodedImage;
if (RsHtml::makeEmbeddedImage(file, encodedImage, 640*480)) {
QTextDocumentFragment fragment = QTextDocumentFragment::fromHtml(encodedImage);
ui->chatTextEdit->textCursor().insertFragment(fragment);
}
} }
} }

View File

@ -307,8 +307,8 @@ MessageComposer::MessageComposer(QWidget *parent, Qt::WFlags flags)
ui.hashBox->setDropWidget(this); ui.hashBox->setDropWidget(this);
ui.hashBox->setAutoHide(true); ui.hashBox->setAutoHide(true);
#ifdef RS_RELEASE_VERSION #if QT_VERSION < 0x040700
ui.imagebtn->setVisible(false); // embedded images are not supported before QT 4.7.0
ui.imagebtn->setVisible(false); ui.imagebtn->setVisible(false);
#endif #endif
@ -1693,7 +1693,8 @@ void MessageComposer::setupInsertActions()
QAction *a; QAction *a;
#ifndef RS_RELEASE_VERSION #if QT_VERSION >= 0x040700
// embedded images are not supported before QT 4.7.0
a = new QAction(QIcon(""), tr("&Image"), this); a = new QAction(QIcon(""), tr("&Image"), this);
connect(a, SIGNAL(triggered()), this, SLOT(addImage())); connect(a, SIGNAL(triggered()), this, SLOT(addImage()));
menu->addAction(a); menu->addAction(a);
@ -2094,26 +2095,14 @@ void MessageComposer::on_contactsdockWidget_visibilityChanged(bool visible)
void MessageComposer::addImage() void MessageComposer::addImage()
{ {
QString fileimg; QString file;
if (misc::getOpenFileName(this, RshareSettings::LASTDIR_IMAGES, tr("Choose Image"), tr("Image Files supported (*.png *.jpeg *.jpg *.gif)"), fileimg)) { if (misc::getOpenFileName(this, RshareSettings::LASTDIR_IMAGES, tr("Choose Image"), tr("Image Files supported (*.png *.jpeg *.jpg *.gif)"), file)) {
QImage base(fileimg); QString encodedImage;
if (RsHtml::makeEmbeddedImage(file, encodedImage, 640*480)) {
QString pathimage = fileimg.left(fileimg.lastIndexOf("/"))+"/"; QTextDocumentFragment fragment = QTextDocumentFragment::fromHtml(encodedImage);
Settings->setValueToGroup("MessageComposer", "LastDir", pathimage);
Create_New_Image_Tag(fileimg);
}
}
void MessageComposer::Create_New_Image_Tag( const QString urlremoteorlocal )
{
/*if (image_extension(urlremoteorlocal)) {*/
QString subtext = QString("<p><img src=\"%1\">").arg(urlremoteorlocal);
///////////subtext.append("<br><br>Description on image.</p>");
QTextDocumentFragment fragment = QTextDocumentFragment::fromHtml(subtext);
ui.msgText->textCursor().insertFragment(fragment); ui.msgText->textCursor().insertFragment(fragment);
//emit statusMessage(QString("Image new :").arg(urlremoteorlocal)); }
//} }
} }
void MessageComposer::fontSizeIncrease() void MessageComposer::fontSizeIncrease()

View File

@ -176,8 +176,6 @@ private:
void clearTagLabels(); void clearTagLabels();
void showTagLabels(); void showTagLabels();
void Create_New_Image_Tag(const QString urlremoteorlocal);
QAction *actionSave, QAction *actionSave,
*actionAlignLeft, *actionAlignLeft,
*actionAlignCenter, *actionAlignCenter,

File diff suppressed because it is too large Load Diff

View File

@ -19,8 +19,12 @@
* Boston, MA 02110-1301, USA. * Boston, MA 02110-1301, USA.
****************************************************************/ ****************************************************************/
#include <QApplication>
#include <QTextBrowser> #include <QTextBrowser>
#include <QtXml> #include <QtXml>
#include <QBuffer>
#include <QMessageBox>
#include <qmath.h>
#include "HandleRichText.h" #include "HandleRichText.h"
#include "gui/RetroShareLink.h" #include "gui/RetroShareLink.h"
@ -625,3 +629,62 @@ QString RsHtml::toHtml(QString text, bool realHtml)
doc.setHtml(text); doc.setHtml(text);
return doc.toHtml(); return doc.toHtml();
} }
/** Loads image and converts image to embedded image HTML fragment **/
bool RsHtml::makeEmbeddedImage(const QString &fileName, QString &embeddedImage, const int maxPixels)
{
QImage image;
if (image.load (fileName) == false) {
fprintf (stderr, "RsHtml::makeEmbeddedImage() - image \"%s\" can't be load", fileName.toAscii().constData());
return false;
}
return RsHtml::makeEmbeddedImage(image, embeddedImage, maxPixels);
}
/** Converts image to embedded image HTML fragment **/
bool RsHtml::makeEmbeddedImage(const QImage &originalImage, QString &embeddedImage, const int maxPixels)
{
QByteArray bytearray;
QBuffer buffer(&bytearray);
QImage resizedImage;
const QImage *image = &originalImage;
if (maxPixels > 0) {
QSize imgSize = originalImage.size();
if ((imgSize.height() * imgSize.width()) > maxPixels) {
// image is too large - resize keeping aspect ratio
QSize newSize;
newSize.setWidth(int(qSqrt((maxPixels * imgSize.width()) / imgSize.height())));
newSize.setHeight(int((imgSize.height() * newSize.width()) / imgSize.width()));
// ask user
QMessageBox msgBox;
msgBox.setText(QString(QApplication::translate("RsHtml", "Image is oversized for transmission.\nReducing image to %1x%2 pixels?", 0, QApplication::UnicodeUTF8)).arg(newSize.width()).arg(newSize.height()));
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Ok);
if (msgBox.exec() != QMessageBox::Ok) {
return false;
}
resizedImage = originalImage.scaled(newSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
image = &resizedImage;
}
}
if (buffer.open(QIODevice::WriteOnly)) {
if (image->save(&buffer, "PNG")) {
QByteArray encodedByteArray = bytearray.toBase64();
embeddedImage = "<img src=\"data:image/png;base64,";
embeddedImage.append(encodedByteArray);
embeddedImage.append("\">");
} else {
fprintf (stderr, "RsHtml::makeEmbeddedImage() - image can't be saved to buffer");
return false;
}
} else {
fprintf (stderr, "RsHtml::makeEmbeddedImage() - buffer can't be opened");
return false;
}
return true;
}

View File

@ -64,6 +64,9 @@ public:
static void optimizeHtml(QString &text, unsigned int flag = 0); static void optimizeHtml(QString &text, unsigned int flag = 0);
static QString toHtml(QString text, bool realHtml = true); static QString toHtml(QString text, bool realHtml = true);
static bool makeEmbeddedImage(const QString &fileName, QString &embeddedImage, const int maxPixels);
static bool makeEmbeddedImage(const QImage &originalImage, QString &embeddedImage, const int maxPixels);
protected: protected:
void embedHtml(QTextDocument *textDocument, QDomDocument &doc, QDomElement &currentElement, EmbedInHtml& embedInfos, ulong flag); void embedHtml(QTextDocument *textDocument, QDomDocument &doc, QDomElement &currentElement, EmbedInHtml& embedInfos, ulong flag);
void replaceAnchorWithImg(QDomDocument& doc, QDomElement &element, QTextDocument *textDocument, const RetroShareLink &link); void replaceAnchorWithImg(QDomDocument& doc, QDomElement &element, QTextDocument *textDocument, const RetroShareLink &link);