2012-05-14 14:01:57 +00:00
|
|
|
#include "imagehandler.h"
|
|
|
|
#include <QGraphicsObject>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QStyleOptionGraphicsItem>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
ImageHandler::ImageHandler(QObject *parent) :
|
|
|
|
QObject(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage ImageHandler::extractQImage(QObject *imageObj,
|
|
|
|
const double offsetX, const double offsetY,
|
|
|
|
const double width, const double height)
|
|
|
|
{
|
|
|
|
QGraphicsObject *item = qobject_cast<QGraphicsObject*>(imageObj);
|
|
|
|
|
2015-06-11 07:36:08 +00:00
|
|
|
if (!item) {
|
2012-05-14 14:01:57 +00:00
|
|
|
qDebug() << "Item is NULL";
|
|
|
|
return QImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage img(item->boundingRect().size().toSize(), QImage::Format_RGB32);
|
|
|
|
img.fill(QColor(255, 255, 255).rgb());
|
|
|
|
QPainter painter(&img);
|
|
|
|
QStyleOptionGraphicsItem styleOption;
|
|
|
|
item->paint(&painter, &styleOption);
|
|
|
|
|
|
|
|
if(offsetX == 0 && offsetY == 0 && width == 0 && height == 0)
|
|
|
|
return img;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return img.copy(offsetX, offsetY, width, height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageHandler::save(QObject *imageObj, const QString &path,
|
|
|
|
const double offsetX, const double offsetY,
|
|
|
|
const double width, const double height)
|
|
|
|
{
|
|
|
|
QImage img = extractQImage(imageObj, offsetX, offsetY, width, height);
|
|
|
|
img.save(path);
|
|
|
|
}
|
2015-10-31 17:14:59 +00:00
|
|
|
|