File indexing completed on 2025-06-29 04:09:32
0001 /*************************************************************************** 0002 * * 0003 * copyright : (C) 2007 The University of Toronto * 0004 * netterfield@astro.utoronto.ca * 0005 * * 0006 * This program is free software; you can redistribute it and/or modify * 0007 * it under the terms of the GNU General Public License as published by * 0008 * the Free Software Foundation; either version 2 of the License, or * 0009 * (at your option) any later version. * 0010 * * 0011 ***************************************************************************/ 0012 0013 #include "svgitem.h" 0014 #include "debug.h" 0015 #include "dialogdefaults.h" 0016 0017 #include <QDebug> 0018 #include <QFileDialog> 0019 #include <QGraphicsScene> 0020 #include <QSvgRenderer> 0021 0022 0023 namespace Kst { 0024 0025 SvgItem::SvgItem(View *parent, const QString &file) 0026 : ViewItem(parent) { 0027 0028 if (!file.isNull()) { 0029 _svg = new QSvgRenderer(file); 0030 QFile svgfile(file); 0031 if (svgfile.open(QIODevice::ReadOnly | QIODevice::Text)) { 0032 while (!svgfile.atEnd()) { 0033 _svgData.append(svgfile.readLine()); 0034 } 0035 } 0036 } else { 0037 _svg = new QSvgRenderer(); 0038 } 0039 setTypeName(tr("Svg", "an svg picture")); 0040 setLockAspectRatio(true); 0041 //setLockAspectRatioFixed(true); 0042 } 0043 0044 0045 SvgItem::~SvgItem() { 0046 } 0047 0048 0049 void SvgItem::paint(QPainter *painter) { 0050 // We can do better here. Cache the svg also. 0051 if (_svg->isValid() && rect().isValid()) { 0052 _svg->render(painter, rect()); 0053 } 0054 } 0055 0056 0057 void SvgItem::save(QXmlStreamWriter &xml) { 0058 if (isVisible()) { 0059 xml.writeStartElement("svg"); 0060 ViewItem::save(xml); 0061 xml.writeStartElement("data"); 0062 xml.writeCharacters(qCompress(_svgData).toBase64()); 0063 xml.writeEndElement(); 0064 xml.writeEndElement(); 0065 } 0066 } 0067 0068 0069 void SvgItem::creationPolygonChanged(View::CreationEvent event) { 0070 0071 double aspect = 1.0; 0072 if ((_svg->defaultSize().width()>0) && (_svg->defaultSize().height()>0)) { 0073 aspect = double(_svg->defaultSize().width())/double(_svg->defaultSize().height()); 0074 } 0075 0076 creationPolygonChangedFixedAspect(event, aspect); 0077 0078 } 0079 0080 0081 0082 void SvgItem::setSvgData(const QByteArray &svgData) { 0083 _svg->load(svgData); 0084 _svgData = svgData; 0085 } 0086 0087 void CreateSvgCommand::createItem() { 0088 QString start_dir = dialogDefaults().value("svg/startdir", ".").toString(); 0089 QString filter = "SVG Images (*.svg *.SVG)"; 0090 QString file = QFileDialog::getOpenFileName(_view, tr("Kst: Open SVG Image"), start_dir, filter); 0091 0092 if (file.isEmpty()) 0093 return; 0094 0095 dialogDefaults().setValue("svg/startdir", QFileInfo(file).path()); 0096 _item = new SvgItem(_view, file); 0097 _view->setCursor(Qt::CrossCursor); 0098 0099 CreateCommand::createItem(); 0100 } 0101 0102 0103 SvgItemFactory::SvgItemFactory() 0104 : GraphicsFactory() { 0105 registerFactory("svg", this); 0106 } 0107 0108 0109 SvgItemFactory::~SvgItemFactory() { 0110 } 0111 0112 0113 ViewItem* SvgItemFactory::generateGraphics(QXmlStreamReader& xml, ObjectStore *store, View *view, ViewItem *parent) { 0114 SvgItem *rc = 0; 0115 while (!xml.atEnd()) { 0116 bool validTag = true; 0117 if (xml.isStartElement()) { 0118 if (!rc && xml.name().toString() == "svg") { 0119 Q_ASSERT(!rc); 0120 rc = new SvgItem(view); 0121 if (parent) { 0122 rc->setParentViewItem(parent); 0123 } 0124 // Add any new specialized SvgItem Properties here. 0125 } else if (xml.name().toString() == "data") { 0126 Q_ASSERT(rc); 0127 xml.readNext(); 0128 QByteArray qbca = QByteArray::fromBase64(xml.text().toString().toLatin1()); 0129 rc->setSvgData(qUncompress(qbca)); 0130 xml.readNext(); 0131 if (!xml.isEndElement() || (xml.name().toString() != "data")) { 0132 validTag = false; 0133 } 0134 xml.readNext(); 0135 } else { 0136 Q_ASSERT(rc); 0137 if (!rc->parse(xml, validTag) && validTag) { 0138 ViewItem *i = GraphicsFactory::parse(xml, store, view, rc); 0139 if (!i) { 0140 } 0141 } 0142 } 0143 } else if (xml.isEndElement()) { 0144 if (xml.name().toString() == "svg") { 0145 break; 0146 } else { 0147 validTag = false; 0148 } 0149 } 0150 if (!validTag) { 0151 qDebug("invalid Tag\n"); 0152 Debug::self()->log(QObject::tr("Error creating svg object from Kst file."), Debug::Warning); 0153 delete rc; 0154 return 0; 0155 } 0156 xml.readNext(); 0157 } 0158 return rc; 0159 } 0160 0161 0162 } 0163 0164 // vim: ts=2 sw=2 et