0
0
mirror of https://github.com/fralx/LimeReport.git synced 2025-09-23 08:29:07 +03:00

SVGItem has been added

This commit is contained in:
Arin Alexander
2020-03-18 13:34:11 +03:00
parent 8348dd68d3
commit 3b3790d7bc
15 changed files with 427 additions and 14 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 600 B

View File

@@ -44,5 +44,6 @@
<file alias="ChartItem">images/pie_chart2.png</file>
<file alias="DataHeaderBand">images/DataHeaderBand.png</file>
<file alias="DataFooterBand">images/DataFooterBand.png</file>
<file alias="SVGItem">images/SVGItem.png</file>
</qresource>
</RCC>

View File

@@ -293,11 +293,11 @@ void ImageItem::setDatasource(const QString &datasource)
}
void ImageItem::paint(QPainter *ppainter, const QStyleOptionGraphicsItem *option, QWidget *widget)
void ImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
ppainter->save();
if (isSelected()) ppainter->setOpacity(Const::SELECTION_OPACITY);
else ppainter->setOpacity(qreal(opacity())/100);
painter->save();
if (isSelected()) painter->setOpacity(Const::SELECTION_OPACITY);
else painter->setOpacity(qreal(opacity())/100);
QPointF point = rect().topLeft();
QImage img;
@@ -339,22 +339,23 @@ void ImageItem::paint(QPainter *ppainter, const QStyleOptionGraphicsItem *option
}
}
if (img.isNull() && itemMode()==DesignMode){
if (img.isNull() && itemMode() == DesignMode){
QString text;
ppainter->setFont(transformToSceneFont(QFont("Arial",10)));
ppainter->setPen(Qt::black);
painter->setFont(transformToSceneFont(QFont("Arial",10)));
painter->setPen(Qt::black);
if (!datasource().isEmpty() && !field().isEmpty())
text = datasource()+"."+field();
else if (m_useExternalPainter) text = tr("Ext."); else text = tr("Image");
ppainter->drawText(rect().adjusted(4,4,-4,-4), Qt::AlignCenter, text );
painter->drawText(rect().adjusted(4,4,-4,-4), Qt::AlignCenter, text );
} else {
if (m_externalPainter && m_useExternalPainter)
m_externalPainter->paintByExternalPainter(this->patternName(), ppainter, option);
m_externalPainter->paintByExternalPainter(this->patternName(), painter, option);
else
ppainter->drawImage(point,img);
painter->drawImage(point,img);
}
ItemDesignIntf::paint(ppainter,option,widget);
ppainter->restore();
ItemDesignIntf::paint(painter,option,widget);
painter->restore();
}
void ImageItem::setImage(QImage value)

View File

@@ -64,7 +64,7 @@ public:
#endif
ImageItem(QObject *owner, QGraphicsItem *parent);
virtual void paint(QPainter *ppainter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void setImage(QImage value);
QImage image();
void setResourcePath(const QString &value);

View File

@@ -0,0 +1,151 @@
#include "lrsvgitem.h"
#include "lrdesignelementsfactory.h"
#include <QtSvg>
namespace{
const QString xmlTag = "SVGItem";
LimeReport::BaseDesignIntf * createSVGItem(QObject* owner, LimeReport::BaseDesignIntf* parent){
return new LimeReport::SVGItem(owner,parent);
}
bool VARIABLE_IS_NOT_USED registred = LimeReport::DesignElementsFactory::instance().registerCreator(
xmlTag, LimeReport::ItemAttribs(QObject::tr("SVG Item"),"Item"), createSVGItem
);
}
namespace LimeReport{
SVGItem::SVGItem(QObject *owner, QGraphicsItem *parent)
:ItemDesignIntf(xmlTag,owner,parent)
{
}
void SVGItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->save();
if (isSelected()) painter->setOpacity(Const::SELECTION_OPACITY);
else painter->setOpacity(qreal(opacity())/100);
if (m_image.isNull() && itemMode() == DesignMode){
QString text;
painter->setFont(transformToSceneFont(QFont("Arial",10)));
painter->setPen(Qt::black);
if (!datasource().isEmpty() && !field().isEmpty())
text = datasource()+"."+field();
else text = tr("SVG Image");
painter->drawText(rect().adjusted(4,4,-4,-4), Qt::AlignCenter, text );
}
else if (!m_image.isEmpty()){
QSvgRenderer render;
render.load(m_image);
render.render(painter, option->rect);
}
ItemDesignIntf::paint(painter,option,widget);
painter->restore();
};
BaseDesignIntf* SVGItem::createSameTypeItem(QObject *owner, QGraphicsItem *parent){
return new SVGItem(owner, parent);
}
void SVGItem::updateItemSize(DataSourceManager *dataManager, RenderPass pass, int maxHeight)
{
Q_UNUSED(maxHeight)
if (m_image.isEmpty()){
if (!m_datasource.isEmpty() && !m_field.isEmpty()){
IDataSource* ds = dataManager->dataSource(m_datasource);
if (ds) {
QVariant data = ds->data(m_field);
m_image = data.value<QByteArray>();
}
} else if (!m_resourcePath.isEmpty()){
m_resourcePath = expandUserVariables(m_resourcePath, pass, NoEscapeSymbols, dataManager);
m_resourcePath = expandDataFields(m_resourcePath, NoEscapeSymbols, dataManager);
m_image = imageFromResource(m_resourcePath);
} else if (!m_variable.isEmpty()){
QVariant data = dataManager->variable(m_variable);
if (data.type() == QVariant::String){
m_image = imageFromResource(data.toString());
} else if (data.type() == QVariant::Image){
m_image = data.value<QByteArray>();
}
}
}
}
QByteArray SVGItem::imageFromResource(QString resourcePath)
{
QFile file(resourcePath);
if (file.open(QIODevice::ReadOnly)){
return file.readAll();
}
return QByteArray();
}
QString SVGItem::variable() const
{
return m_variable;
}
void SVGItem::setVariable(const QString &variable)
{
if (m_variable != variable){
QString oldValue = m_variable;
m_variable = variable;
update();
notify("variable", oldValue, m_variable);
}
m_variable = variable;
}
QString SVGItem::resourcePath() const
{
return m_resourcePath;
}
void SVGItem::setResourcePath(const QString &resourcePath)
{
if (m_resourcePath != resourcePath){
QString oldValue = m_resourcePath;
m_resourcePath = resourcePath;
QFile file(resourcePath);
if (file.open(QIODevice::ReadOnly)){
m_image = file.readAll();
}
update();
notify("resourcePath", oldValue, resourcePath);
}
}
QByteArray SVGItem::image() const
{
return m_image;
}
void SVGItem::setImage(const QByteArray &image)
{
if (m_image != image){
QByteArray oldValue = m_image;
m_image = image;
update();
notify("image", oldValue, image);
}
}
QString SVGItem::datasource() const
{
return m_datasource;
}
void SVGItem::setDatasource(const QString &datasource)
{
m_datasource = datasource;
}
QString SVGItem::field() const
{
return m_field;
}
void SVGItem::setField(const QString &field)
{
m_field = field;
};
} // namespace LimeReport

View File

@@ -0,0 +1,44 @@
#ifndef SVGITEM_H
#define SVGITEM_H
#include "lritemdesignintf.h"
namespace LimeReport{
class SVGItem: public ItemDesignIntf
{
Q_OBJECT
Q_PROPERTY(QString resourcePath READ resourcePath WRITE setResourcePath)
Q_PROPERTY(QByteArray image READ image WRITE setImage)
Q_PROPERTY(QString datasource READ datasource WRITE setDatasource)
Q_PROPERTY(QString field READ field WRITE setField)
Q_PROPERTY(int opacity READ opacity WRITE setOpacity)
Q_PROPERTY(QString variable READ variable WRITE setVariable)
Q_PROPERTY(bool watermark READ isWatermark WRITE setWatermark)
public:
SVGItem(QObject *owner, QGraphicsItem *parent);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QString resourcePath() const;
void setResourcePath(const QString &resourcePath);
QByteArray image() const;
void setImage(const QByteArray &image);
QString datasource() const;
void setDatasource(const QString &datasource);
QString field() const;
void setField(const QString &field);
QString variable() const;
void setVariable(const QString &variable);
protected:
BaseDesignIntf *createSameTypeItem(QObject *owner, QGraphicsItem *parent);
void updateItemSize(DataSourceManager *dataManager, RenderPass pass, int maxHeight);
QByteArray imageFromResource(QString resourcePath);
private:
QString m_resourcePath;
QByteArray m_image;
QString m_datasource;
QString m_field;
QString m_variable;
};
} // namespace LimeReport
#endif // SVGITEM_H