#include "lrsvgitem.h" #include "lrdesignelementsfactory.h" #include 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(); } } 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 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