Added save image function into chat

This commit is contained in:
hunbernd 2015-12-06 23:00:29 +01:00
parent 0b92c79eb0
commit ff44965e2c
6 changed files with 87 additions and 3 deletions

View File

@ -51,6 +51,7 @@
#include "util/HandleRichText.h"
#include "gui/chat/ChatUserNotify.h"//For BradCast
#include "util/DateTime.h"
#include "util/imageutil.h"
#include <retroshare/rsstatus.h>
#include <retroshare/rsidentity.h>
@ -126,6 +127,7 @@ ChatWidget::ChatWidget(QWidget *parent) :
connect(ui->actionChooseColor, SIGNAL(triggered()), this, SLOT(chooseColor()));
connect(ui->actionResetFont, SIGNAL(triggered()), this, SLOT(resetFont()));
connect(ui->actionQuote, SIGNAL(triggered()), this, SLOT(quote()));
connect(ui->actionSave_image, SIGNAL(triggered()), this, SLOT(saveImage()));
connect(ui->hashBox, SIGNAL(fileHashingFinished(QList<HashedFile>)), this, SLOT(fileHashingFinished(QList<HashedFile>)));
@ -977,6 +979,9 @@ void ChatWidget::contextMenuTextBrowser(QPoint point)
contextMnu->addAction(ui->actionClearChatHistory);
contextMnu->addAction(ui->actionQuote);
ui->actionSave_image->setData(point);
contextMnu->addAction(ui->actionSave_image);
contextMnu->exec(ui->textBrowser->viewport()->mapToGlobal(point));
delete(contextMnu);
}
@ -1674,3 +1679,10 @@ void ChatWidget::quote()
emit ui->chatTextEdit->append(QString(">") + text);
}
}
void ChatWidget::saveImage()
{
QPoint point = ui->actionSave_image->data().toPoint();
QTextCursor cursor = ui->textBrowser->cursorForPosition(point);
ImageUtil::extractImage(window(), cursor);
}

View File

@ -133,7 +133,6 @@ private slots:
void messageHistory();
void resetStatusBar() ;
void searchHistory();
void quote();
signals:
@ -186,6 +185,9 @@ private slots:
bool fileSave();
bool fileSaveAs();
void quote();
void saveImage();
private:
bool findText(const QString& qsStringToFind);
bool findText(const QString& qsStringToFind, bool bBackWard, bool bForceMove);

View File

@ -979,6 +979,15 @@ border-image: url(:/images/closepressed.png)
<property name="toolTip">
<string>Quotes the selected text</string>
</property>
</action>
<action name="actionSave_image">
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/images/document_save.png</normaloff>:/images/document_save.png</iconset>
</property>
<property name="text">
<string>Save image</string>
</property>
</action>
</widget>
<customwidgets>

View File

@ -524,7 +524,8 @@ HEADERS += rshare.h \
gui/GetStartedDialog.h \
gui/settings/WebuiPage.h \
gui/statistics/BWGraph.h \
util/RsSyntaxHighlighter.h
util/RsSyntaxHighlighter.h \
util/imageutil.h
# gui/ForumsDialog.h \
# gui/forums/ForumDetails.h \
@ -873,7 +874,8 @@ SOURCES += main.cpp \
gui/statistics/BwCtrlWindow.cpp \
gui/statistics/RttStatistics.cpp \
gui/statistics/BWGraph.cpp \
util/RsSyntaxHighlighter.cpp
util/RsSyntaxHighlighter.cpp \
util/imageutil.cpp
# gui/ForumsDialog.cpp \
# gui/forums/ForumDetails.cpp \

View File

@ -0,0 +1,43 @@
#include "imageutil.h"
#include "util/misc.h"
#include <QMessageBox>
#include <QTextCursor>
#include <QByteArray>
#include <QString>
#include <QImage>
#include <QTextDocumentFragment>
#include <QApplication>
ImageUtil::ImageUtil() {}
void ImageUtil::extractImage(QWidget *window, QTextCursor cursor)
{
cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 1);
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2);
QString imagestr = cursor.selection().toHtml();
bool success = false;
int start = imagestr.indexOf("base64,") + 7;
int stop = imagestr.indexOf("\"", start);
int length = stop - start;
if((start >= 0) && (length > 0))
{
QByteArray ba = QByteArray::fromBase64(imagestr.mid(start, length).toLatin1());
QImage image = QImage::fromData(ba);
if(!image.isNull())
{
QString file;
success = true;
if(misc::getSaveFileName(window, RshareSettings::LASTDIR_IMAGES, "Save Picture File", "Pictures (*.png *.xpm *.jpg)", file))
{
if(!image.save(file, 0, 100))
if(!image.save(file + ".png", 0, 100))
QMessageBox::warning(window, QApplication::translate("ImageUtil", "Save image"), QApplication::translate("ImageUtil", "Cannot save the image, invalid filename"));
}
}
}
if(!success)
{
QMessageBox::warning(window, QApplication::translate("ImageUtil", "Save image"), QApplication::translate("ImageUtil", "Not an image"));
}
}

View File

@ -0,0 +1,16 @@
#ifndef IMAGEUTIL_H
#define IMAGEUTIL_H
#include <QTextCursor>
#include <QWidget>
class ImageUtil
{
public:
ImageUtil();
static void extractImage(QWidget *window, QTextCursor cursor);
};
#endif // IMAGEUTIL_H