RetroShare/retroshare-gui/src/util/imageutil.cpp

44 lines
1.4 KiB
C++
Raw Normal View History

2015-12-06 17:00:29 -05:00
#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"));
}
}