0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-24 00:33:02 +03:00
LimeReport/limereport/items/lrsvgitem.cpp

185 lines
5.7 KiB
C++
Raw Normal View History

2020-03-18 13:34:11 +03:00
#include "lrsvgitem.h"
2020-03-18 13:34:11 +03:00
#include "lrdesignelementsfactory.h"
2020-03-19 20:09:35 +03:00
#include "lrimageitemeditor.h"
#include "lrpagedesignintf.h"
2020-03-18 13:34:11 +03:00
#include <QtSvg>
2020-03-18 13:34:11 +03:00
namespace {
const QString xmlTag = "SVGItem";
LimeReport::BaseDesignIntf* createSVGItem(QObject* owner, LimeReport::BaseDesignIntf* parent)
2020-03-18 13:34:11 +03:00
{
return new LimeReport::SVGItem(owner, parent);
2020-03-18 13:34:11 +03:00
}
bool VARIABLE_IS_NOT_USED registred = LimeReport::DesignElementsFactory::instance().registerCreator(
xmlTag, LimeReport::ItemAttribs(QObject::tr("SVG Item"), "Item"), createSVGItem);
} // namespace
namespace LimeReport {
SVGItem::SVGItem(QObject* owner, QGraphicsItem* parent): ItemDesignIntf(xmlTag, owner, parent) { }
2020-03-18 13:34:11 +03:00
void SVGItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
2020-03-18 13:34:11 +03:00
{
painter->save();
if (isSelected())
painter->setOpacity(Const::SELECTION_OPACITY);
else
painter->setOpacity(qreal(opacity()) / 100);
if (m_image.isNull() && itemMode() == DesignMode) {
2020-03-18 13:34:11 +03:00
QString text;
painter->setFont(transformToSceneFont(QFont("Arial", 10)));
2020-03-18 13:34:11 +03:00
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()) {
2020-03-18 13:34:11 +03:00
QSvgRenderer render;
render.load(m_image);
render.render(painter, option->rect);
}
ItemDesignIntf::paint(painter, option, widget);
2020-03-18 13:34:11 +03:00
painter->restore();
2020-03-19 20:09:35 +03:00
}
QByteArray SVGItem::imageAsByteArray() const { return m_image; }
2020-03-19 20:09:35 +03:00
void SVGItem::setImageAsByteArray(QByteArray image) { setImage(image); }
2020-03-19 20:09:35 +03:00
QString SVGItem::fileFilter() const { return tr("SVG (*.svg)"); }
2020-03-19 20:09:35 +03:00
void SVGItem::preparePopUpMenu(QMenu& menu)
2020-03-19 20:09:35 +03:00
{
QAction* editAction = menu.addAction(QIcon(":/report/images/edit_pecil2.png"), tr("Edit"));
menu.insertAction(menu.actions().at(0), editAction);
2020-03-19 20:09:35 +03:00
menu.insertSeparator(menu.actions().at(1));
menu.addSeparator();
QAction* action = menu.addAction(tr("Watermark"));
action->setCheckable(true);
action->setChecked(isWatermark());
}
void SVGItem::processPopUpAction(QAction* action)
2020-03-19 20:09:35 +03:00
{
if (action->text().compare(tr("Watermark")) == 0) {
page()->setPropertyToSelectedItems("watermark", action->isChecked());
2020-03-19 20:09:35 +03:00
}
if (action->text().compare(tr("Edit")) == 0) {
2020-03-19 20:09:35 +03:00
this->showEditorDialog();
}
ItemDesignIntf::processPopUpAction(action);
}
QWidget* SVGItem::defaultEditor()
2020-03-19 20:09:35 +03:00
{
ImageItemEditor* editor = new ImageItemEditor(this);
editor->setAttribute(Qt::WA_DeleteOnClose);
return editor;
2020-03-18 13:34:11 +03:00
};
BaseDesignIntf* SVGItem::createSameTypeItem(QObject* owner, QGraphicsItem* parent)
{
2020-03-18 13:34:11 +03:00
return new SVGItem(owner, parent);
}
void SVGItem::updateItemSize(DataSourceManager* dataManager, RenderPass pass, int maxHeight)
2020-03-18 13:34:11 +03:00
{
Q_UNUSED(maxHeight)
if (m_image.isEmpty()) {
if (!m_datasource.isEmpty() && !m_field.isEmpty()) {
2020-03-18 13:34:11 +03:00
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);
2020-03-18 13:34:11 +03:00
m_resourcePath = expandDataFields(m_resourcePath, NoEscapeSymbols, dataManager);
m_image = imageFromResource(m_resourcePath);
} else if (!m_variable.isEmpty()) {
// TODO: Migrate to QMetaType
2020-03-18 13:34:11 +03:00
QVariant data = dataManager->variable(m_variable);
2022-07-19 00:43:54 +03:00
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
if (data.typeId() == QMetaType::QString) {
2022-07-19 00:43:54 +03:00
#else
if (data.type() == QVariant::String) {
2022-07-19 00:43:54 +03:00
#endif
2020-03-18 13:34:11 +03:00
m_image = imageFromResource(data.toString());
2022-07-19 00:43:54 +03:00
} else {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
if (data.typeId() == QMetaType::QByteArray) {
#else
if (data.type() == QVariant::ByteArray) {
#endif
m_image = data.value<QByteArray>();
2022-07-19 00:43:54 +03:00
}
2020-03-18 13:34:11 +03:00
}
}
}
}
QByteArray SVGItem::imageFromResource(QString resourcePath)
{
QFile file(resourcePath);
if (file.open(QIODevice::ReadOnly)) {
2020-03-18 13:34:11 +03:00
return file.readAll();
}
return QByteArray();
2020-03-18 13:34:11 +03:00
}
QString SVGItem::variable() const { return m_variable; }
2020-03-18 13:34:11 +03:00
void SVGItem::setVariable(const QString& variable)
2020-03-18 13:34:11 +03:00
{
if (m_variable != variable) {
2020-03-18 13:34:11 +03:00
QString oldValue = m_variable;
m_variable = variable;
update();
notify("variable", oldValue, m_variable);
}
m_variable = variable;
}
bool SVGItem::isNeedUpdateSize(RenderPass) const { return m_image.isNull(); }
2021-02-20 15:40:13 +03:00
QString SVGItem::resourcePath() const { return m_resourcePath; }
2020-03-18 13:34:11 +03:00
void SVGItem::setResourcePath(const QString& resourcePath)
2020-03-18 13:34:11 +03:00
{
if (m_resourcePath != resourcePath) {
2020-03-18 13:34:11 +03:00
QString oldValue = m_resourcePath;
m_resourcePath = resourcePath;
QFile file(resourcePath);
if (file.open(QIODevice::ReadOnly)) {
2020-03-18 13:34:11 +03:00
m_image = file.readAll();
}
update();
notify("resourcePath", oldValue, resourcePath);
}
}
QByteArray SVGItem::image() const { return m_image; }
2020-03-18 13:34:11 +03:00
void SVGItem::setImage(const QByteArray& image)
2020-03-18 13:34:11 +03:00
{
if (m_image != image) {
2020-03-18 13:34:11 +03:00
QByteArray oldValue = m_image;
m_image = image;
update();
notify("image", oldValue, image);
}
}
QString SVGItem::datasource() const { return m_datasource; }
2020-03-18 13:34:11 +03:00
void SVGItem::setDatasource(const QString& datasource) { m_datasource = datasource; }
2020-03-18 13:34:11 +03:00
QString SVGItem::field() const { return m_field; }
2020-03-18 13:34:11 +03:00
void SVGItem::setField(const QString& field) { m_field = field; };
2020-03-18 13:34:11 +03:00
} // namespace LimeReport