2016-02-17 10:11:00 +03:00
|
|
|
/***************************************************************************
|
|
|
|
* This file is part of the Lime Report project *
|
2021-08-18 20:21:36 +03:00
|
|
|
* Copyright (C) 2021 by Alexander Arin *
|
2016-02-17 10:11:00 +03:00
|
|
|
* arin_a@bk.ru *
|
|
|
|
* *
|
|
|
|
** GNU General Public License Usage **
|
|
|
|
* *
|
|
|
|
* This library is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
** GNU Lesser General Public License **
|
|
|
|
* *
|
|
|
|
* This library is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public *
|
|
|
|
* License along with this library. *
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
* This library is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
****************************************************************************/
|
|
|
|
#include "lrimageitem.h"
|
|
|
|
#include "lrdesignelementsfactory.h"
|
|
|
|
#include "lrglobal.h"
|
|
|
|
#include "lrdatasourcemanager.h"
|
2018-03-22 20:26:07 +03:00
|
|
|
#include "lrpagedesignintf.h"
|
2019-08-07 21:06:47 +03:00
|
|
|
#include "lrimageitemeditor.h"
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
namespace{
|
|
|
|
|
|
|
|
const QString xmlTag = "ImageItem";
|
|
|
|
|
|
|
|
LimeReport::BaseDesignIntf * createImageItem(QObject* owner, LimeReport::BaseDesignIntf* parent){
|
|
|
|
return new LimeReport::ImageItem(owner,parent);
|
|
|
|
}
|
2016-10-18 15:00:26 +03:00
|
|
|
bool VARIABLE_IS_NOT_USED registred = LimeReport::DesignElementsFactory::instance().registerCreator(
|
2016-02-17 10:11:00 +03:00
|
|
|
xmlTag, LimeReport::ItemAttribs(QObject::tr("Image Item"),"Item"), createImageItem
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace LimeReport{
|
|
|
|
|
|
|
|
ImageItem::ImageItem(QObject* owner,QGraphicsItem* parent)
|
2018-12-12 22:55:03 +03:00
|
|
|
:ItemDesignIntf(xmlTag,owner,parent), m_useExternalPainter(false), m_externalPainter(0),
|
|
|
|
m_autoSize(false), m_scale(true),
|
|
|
|
m_keepAspectRatio(true), m_center(true), m_format(Binary){}
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
BaseDesignIntf *ImageItem::createSameTypeItem(QObject *owner, QGraphicsItem *parent)
|
|
|
|
{
|
2018-12-12 22:55:03 +03:00
|
|
|
ImageItem* result = new ImageItem(owner,parent);
|
|
|
|
result->setExternalPainter(m_externalPainter);
|
|
|
|
return result;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2018-03-16 19:12:14 +03:00
|
|
|
void ImageItem::loadPictureFromVariant(QVariant& data){
|
2022-07-19 00:43:54 +03:00
|
|
|
//TODO: Migrate to QMetaType
|
2018-03-16 19:12:14 +03:00
|
|
|
if (data.isValid()){
|
2022-08-23 01:31:12 +03:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
2022-07-19 00:43:54 +03:00
|
|
|
if (data.typeId() == QMetaType::QImage){
|
|
|
|
#else
|
|
|
|
if (data.type() == QVariant::Image){
|
|
|
|
#endif
|
2018-03-16 19:12:14 +03:00
|
|
|
m_picture = data.value<QImage>();
|
|
|
|
} else {
|
|
|
|
switch (m_format) {
|
|
|
|
default:
|
|
|
|
case Binary:
|
|
|
|
m_picture.loadFromData(data.toByteArray());
|
|
|
|
break;
|
|
|
|
case Hex:
|
|
|
|
m_picture.loadFromData(QByteArray::fromHex(data.toByteArray()));
|
|
|
|
break;
|
|
|
|
case Base64:
|
|
|
|
m_picture.loadFromData(QByteArray::fromBase64(data.toByteArray()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 20:26:07 +03:00
|
|
|
void ImageItem::preparePopUpMenu(QMenu &menu)
|
|
|
|
{
|
2019-08-07 21:06:47 +03:00
|
|
|
QAction* editAction = menu.addAction(QIcon(":/report/images/edit_pecil2.png"),tr("Edit"));
|
|
|
|
menu.insertAction(menu.actions().at(0),editAction);
|
|
|
|
menu.insertSeparator(menu.actions().at(1));
|
|
|
|
|
|
|
|
menu.addSeparator();
|
2018-03-22 20:26:07 +03:00
|
|
|
QAction* action = menu.addAction(tr("Watermark"));
|
|
|
|
action->setCheckable(true);
|
|
|
|
action->setChecked(isWatermark());
|
2019-08-07 21:06:47 +03:00
|
|
|
|
2018-03-22 20:26:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ImageItem::processPopUpAction(QAction *action)
|
|
|
|
{
|
|
|
|
if (action->text().compare(tr("Watermark")) == 0){
|
|
|
|
page()->setPropertyToSelectedItems("watermark",action->isChecked());
|
|
|
|
}
|
2019-08-07 21:06:47 +03:00
|
|
|
if (action->text().compare(tr("Edit")) == 0){
|
|
|
|
this->showEditorDialog();
|
|
|
|
}
|
2019-06-28 20:12:47 +03:00
|
|
|
ItemDesignIntf::processPopUpAction(action);
|
2018-03-22 20:26:07 +03:00
|
|
|
}
|
|
|
|
|
2019-08-07 21:06:47 +03:00
|
|
|
QImage getFileByResourcePath(QString resourcePath){
|
|
|
|
QFileInfo resourceFile(resourcePath);
|
|
|
|
if (resourceFile.exists())
|
|
|
|
return QImage(resourcePath);
|
|
|
|
return QImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage ImageItem::drawImage()
|
|
|
|
{
|
|
|
|
if (image().isNull())
|
|
|
|
return getFileByResourcePath(m_resourcePath);
|
|
|
|
return image();
|
|
|
|
}
|
|
|
|
|
2018-12-12 22:55:03 +03:00
|
|
|
bool ImageItem::useExternalPainter() const
|
|
|
|
{
|
|
|
|
return m_useExternalPainter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageItem::setUseExternalPainter(bool value)
|
|
|
|
{
|
|
|
|
if (m_useExternalPainter != value){
|
|
|
|
m_useExternalPainter = value;
|
|
|
|
notify("useExternalPainter",!value, value);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 21:06:47 +03:00
|
|
|
QWidget *ImageItem::defaultEditor()
|
|
|
|
{
|
|
|
|
ImageItemEditor* editor = new ImageItemEditor(this);
|
|
|
|
editor->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
return editor;
|
|
|
|
}
|
|
|
|
|
2020-03-19 20:09:35 +03:00
|
|
|
QByteArray ImageItem::imageAsByteArray() const
|
|
|
|
{
|
|
|
|
QByteArray result;
|
|
|
|
QBuffer buffer(&result);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
m_picture.save(&buffer,"PNG");
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageItem::setImageAsByteArray(QByteArray image)
|
|
|
|
{
|
|
|
|
QImage value;
|
|
|
|
value.loadFromData(image);
|
2020-03-19 20:59:24 +03:00
|
|
|
setImage(value);
|
2020-03-19 20:09:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString ImageItem::fileFilter() const
|
|
|
|
{
|
|
|
|
return tr("Images (*.gif *.icns *.ico *.jpeg *.tga *.tiff *.wbmp *.webp *.png *.jpg *.bmp);;All(*.*)");
|
|
|
|
}
|
|
|
|
|
2016-02-17 10:28:27 +03:00
|
|
|
void ImageItem::updateItemSize(DataSourceManager* dataManager, RenderPass pass, int maxHeight)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2016-12-05 22:58:52 +03:00
|
|
|
|
2018-12-12 22:55:03 +03:00
|
|
|
if (m_picture.isNull()){
|
|
|
|
if (!m_datasource.isEmpty() && !m_field.isEmpty()){
|
|
|
|
IDataSource* ds = dataManager->dataSource(m_datasource);
|
2016-12-05 22:58:52 +03:00
|
|
|
if (ds) {
|
|
|
|
QVariant data = ds->data(m_field);
|
2018-03-16 19:12:14 +03:00
|
|
|
loadPictureFromVariant(data);
|
2016-12-05 22:58:52 +03:00
|
|
|
}
|
|
|
|
} else if (!m_resourcePath.isEmpty()){
|
2019-04-29 10:44:59 +03:00
|
|
|
m_resourcePath = expandUserVariables(m_resourcePath, pass, NoEscapeSymbols, dataManager);
|
|
|
|
m_resourcePath = expandDataFields(m_resourcePath, NoEscapeSymbols, dataManager);
|
2016-12-05 22:58:52 +03:00
|
|
|
m_picture = QImage(m_resourcePath);
|
2018-03-16 19:12:14 +03:00
|
|
|
} else if (!m_variable.isEmpty()){
|
2022-07-19 00:43:54 +03:00
|
|
|
//TODO: Migrate to QMetaType
|
2018-03-16 19:12:14 +03:00
|
|
|
QVariant data = dataManager->variable(m_variable);
|
2022-08-23 01:31:12 +03:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
2022-07-19 00:43:54 +03:00
|
|
|
if (data.typeId() == QMetaType::QString){
|
|
|
|
#else
|
2019-01-08 18:02:12 +03:00
|
|
|
if (data.type() == QVariant::String){
|
2022-07-19 00:43:54 +03:00
|
|
|
#endif
|
2019-01-08 18:02:12 +03:00
|
|
|
m_picture = QImage(data.toString());
|
2022-07-19 00:43:54 +03:00
|
|
|
} else {
|
2022-08-23 01:31:12 +03:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
2022-07-19 00:43:54 +03:00
|
|
|
if (data.typeId() == QMetaType::QImage){
|
|
|
|
#else
|
|
|
|
if (data.type() == QVariant::Image){
|
|
|
|
#endif
|
|
|
|
loadPictureFromVariant(data);
|
|
|
|
}
|
2019-01-08 18:02:12 +03:00
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m_autoSize){
|
|
|
|
setWidth(m_picture.width());
|
|
|
|
setHeight(m_picture.height());
|
|
|
|
}
|
2016-02-17 10:28:27 +03:00
|
|
|
BaseDesignIntf::updateItemSize(dataManager, pass, maxHeight);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ImageItem::isNeedUpdateSize(RenderPass) const
|
|
|
|
{
|
|
|
|
return m_picture.isNull() || m_autoSize;
|
|
|
|
}
|
|
|
|
|
2016-12-05 22:58:52 +03:00
|
|
|
QString ImageItem::resourcePath() const
|
|
|
|
{
|
|
|
|
return m_resourcePath;
|
|
|
|
}
|
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
qreal ImageItem::minHeight() const{
|
|
|
|
if (!m_picture.isNull() && autoSize())
|
|
|
|
{
|
|
|
|
return m_picture.height();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 19:12:14 +03:00
|
|
|
void ImageItem::setVariable(const QString& content)
|
|
|
|
{
|
|
|
|
if (m_variable!=content){
|
|
|
|
QString oldValue = m_variable;
|
|
|
|
m_variable=content;
|
|
|
|
update();
|
2019-01-08 18:02:12 +03:00
|
|
|
notify("variable", oldValue, m_variable);
|
2018-03-16 19:12:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
bool ImageItem::center() const
|
|
|
|
{
|
|
|
|
return m_center;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageItem::setCenter(bool center)
|
|
|
|
{
|
|
|
|
if (m_center != center){
|
|
|
|
m_center = center;
|
|
|
|
update();
|
|
|
|
notify("center",!center,center);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImageItem::keepAspectRatio() const
|
|
|
|
{
|
|
|
|
return m_keepAspectRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageItem::setKeepAspectRatio(bool keepAspectRatio)
|
|
|
|
{
|
|
|
|
if (m_keepAspectRatio != keepAspectRatio){
|
|
|
|
m_keepAspectRatio = keepAspectRatio;
|
|
|
|
update();
|
|
|
|
notify("keepAspectRatio",!keepAspectRatio,keepAspectRatio);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImageItem::scale() const
|
|
|
|
{
|
|
|
|
return m_scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageItem::setScale(bool scale)
|
|
|
|
{
|
|
|
|
if (m_scale != scale){
|
|
|
|
m_scale = scale;
|
|
|
|
update();
|
|
|
|
notify("scale",!scale,scale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool ImageItem::autoSize() const
|
|
|
|
{
|
|
|
|
return m_autoSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageItem::setAutoSize(bool autoSize)
|
|
|
|
{
|
|
|
|
if (m_autoSize != autoSize){
|
|
|
|
m_autoSize = autoSize;
|
|
|
|
if (m_autoSize && !m_picture.isNull()){
|
2019-08-07 21:06:47 +03:00
|
|
|
setWidth(drawImage().width());
|
|
|
|
setHeight(drawImage().height());
|
2016-09-16 02:02:32 +03:00
|
|
|
setPossibleResizeDirectionFlags(Fixed);
|
2016-02-17 10:11:00 +03:00
|
|
|
} else {
|
2016-09-16 02:02:32 +03:00
|
|
|
setPossibleResizeDirectionFlags(AllDirections);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
update();
|
|
|
|
notify("autoSize",!autoSize,autoSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ImageItem::field() const
|
|
|
|
{
|
|
|
|
return m_field;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageItem::setField(const QString &field)
|
|
|
|
{
|
|
|
|
if (m_field != field){
|
|
|
|
QString oldValue = m_field;
|
|
|
|
m_field = field;
|
|
|
|
update();
|
|
|
|
notify("field",oldValue,field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ImageItem::datasource() const
|
|
|
|
{
|
|
|
|
return m_datasource;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageItem::setDatasource(const QString &datasource)
|
|
|
|
{
|
|
|
|
if (m_datasource != datasource){
|
|
|
|
QString oldValue = m_datasource;
|
|
|
|
m_datasource = datasource;
|
|
|
|
update();
|
|
|
|
notify("datasource",oldValue,datasource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-18 13:34:11 +03:00
|
|
|
void ImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2020-03-18 13:34:11 +03:00
|
|
|
painter->save();
|
|
|
|
if (isSelected()) painter->setOpacity(Const::SELECTION_OPACITY);
|
|
|
|
else painter->setOpacity(qreal(opacity())/100);
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
QPointF point = rect().topLeft();
|
|
|
|
QImage img;
|
|
|
|
|
2019-08-07 21:06:47 +03:00
|
|
|
if (m_scale && !drawImage().isNull()){
|
|
|
|
img = drawImage().scaled(rect().width(), rect().height(), keepAspectRatio() ? Qt::KeepAspectRatio : Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
2016-02-17 10:11:00 +03:00
|
|
|
} else {
|
2019-08-07 21:06:47 +03:00
|
|
|
img = drawImage();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal shiftHeight = rect().height() - img.height();
|
|
|
|
qreal shiftWidth = rect().width() - img.width();
|
|
|
|
|
|
|
|
if (m_center){
|
|
|
|
if (shiftHeight<0 || shiftWidth<0){
|
|
|
|
qreal cutX = 0;
|
|
|
|
qreal cutY = 0;
|
|
|
|
qreal cutWidth = img.width();
|
|
|
|
qreal cutHeigth = img.height();
|
|
|
|
|
|
|
|
if (shiftWidth > 0){
|
|
|
|
point.setX(point.x()+shiftWidth/2);
|
|
|
|
} else {
|
2016-05-20 18:14:48 +03:00
|
|
|
cutX = fabs(shiftWidth/2);
|
2016-02-17 10:11:00 +03:00
|
|
|
cutWidth += shiftWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shiftHeight > 0){
|
|
|
|
point.setY(point.x()+shiftHeight/2);
|
|
|
|
} else {
|
2016-05-20 18:14:48 +03:00
|
|
|
cutY = fabs(shiftHeight/2);
|
2016-02-17 10:11:00 +03:00
|
|
|
cutHeigth += shiftHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
img = img.copy(cutX,cutY,cutWidth,cutHeigth);
|
|
|
|
} else {
|
|
|
|
point.setX(point.x()+shiftWidth/2);
|
|
|
|
point.setY(point.y()+shiftHeight/2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 13:34:11 +03:00
|
|
|
if (img.isNull() && itemMode() == DesignMode){
|
2016-02-17 10:11:00 +03:00
|
|
|
QString text;
|
2020-03-18 13:34:11 +03:00
|
|
|
painter->setFont(transformToSceneFont(QFont("Arial",10)));
|
|
|
|
painter->setPen(Qt::black);
|
2016-02-17 10:11:00 +03:00
|
|
|
if (!datasource().isEmpty() && !field().isEmpty())
|
|
|
|
text = datasource()+"."+field();
|
2018-12-12 22:55:03 +03:00
|
|
|
else if (m_useExternalPainter) text = tr("Ext."); else text = tr("Image");
|
2020-03-18 13:34:11 +03:00
|
|
|
painter->drawText(rect().adjusted(4,4,-4,-4), Qt::AlignCenter, text );
|
2016-02-17 10:11:00 +03:00
|
|
|
} else {
|
2018-12-12 22:55:03 +03:00
|
|
|
if (m_externalPainter && m_useExternalPainter)
|
2020-03-18 13:34:11 +03:00
|
|
|
m_externalPainter->paintByExternalPainter(this->patternName(), painter, option);
|
2018-12-12 22:55:03 +03:00
|
|
|
else
|
2020-03-18 13:34:11 +03:00
|
|
|
painter->drawImage(point,img);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
2020-03-18 13:34:11 +03:00
|
|
|
|
|
|
|
ItemDesignIntf::paint(painter,option,widget);
|
|
|
|
painter->restore();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ImageItem::setImage(QImage value)
|
|
|
|
{
|
2019-08-07 21:06:47 +03:00
|
|
|
if (m_picture != value){
|
2016-02-17 10:11:00 +03:00
|
|
|
QImage oldValue = m_picture;
|
2019-08-07 21:06:47 +03:00
|
|
|
m_picture = value;
|
2016-02-17 10:11:00 +03:00
|
|
|
if (m_autoSize){
|
|
|
|
setWidth(m_picture.width());
|
|
|
|
setHeight(m_picture.height());
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
notify("image",oldValue,value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 12:40:42 +03:00
|
|
|
QImage ImageItem::image(){
|
|
|
|
return m_picture;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageItem::setResourcePath(const QString &value){
|
2019-08-07 21:06:47 +03:00
|
|
|
if (m_resourcePath != value){
|
|
|
|
QString oldValue = m_resourcePath;
|
|
|
|
m_resourcePath = value;
|
|
|
|
update();
|
|
|
|
notify("resourcePath", oldValue, value);
|
|
|
|
}
|
2019-08-07 12:40:42 +03:00
|
|
|
}
|
|
|
|
|
2017-09-29 18:46:32 +03:00
|
|
|
ImageItem::Format ImageItem::format() const
|
|
|
|
{
|
|
|
|
return m_format;
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2017-09-29 18:46:32 +03:00
|
|
|
void ImageItem::setFormat(Format format)
|
|
|
|
{
|
|
|
|
if (m_format!=format){
|
|
|
|
Format oldValue = m_format;
|
|
|
|
m_format=format;
|
|
|
|
update();
|
|
|
|
notify("format",oldValue,format);
|
|
|
|
}
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
}
|