Changeset View
Changeset View
Standalone View
Standalone View
filters/karbon/svg/SvgImport.cpp
Show All 29 Lines | |||||
30 | #include <KarbonDocument.h> | 30 | #include <KarbonDocument.h> | ||
31 | 31 | | |||
32 | #include <KoShape.h> | 32 | #include <KoShape.h> | ||
33 | #include <KoShapeLayer.h> | 33 | #include <KoShapeLayer.h> | ||
34 | #include <KoShapeGroup.h> | 34 | #include <KoShapeGroup.h> | ||
35 | #include <KoFilterChain.h> | 35 | #include <KoFilterChain.h> | ||
36 | #include <commands/KoShapeUngroupCommand.h> | 36 | #include <commands/KoShapeUngroupCommand.h> | ||
37 | #include <KoXmlReader.h> | 37 | #include <KoXmlReader.h> | ||
38 | #include <KoPAPage.h> | ||||
39 | #include <KoPAMasterPage.h> | ||||
40 | #include <KoPageLayout.h> | ||||
38 | 41 | | |||
39 | #include <kpluginfactory.h> | 42 | #include <kpluginfactory.h> | ||
40 | #include <KCompressionDevice> | 43 | #include <KCompressionDevice> | ||
41 | 44 | | |||
42 | #include <QFileInfo> | 45 | #include <QFileInfo> | ||
43 | #include <QDebug> | 46 | #include <QDebug> | ||
44 | #include <QLoggingCategory> | 47 | #include <QLoggingCategory> | ||
45 | 48 | | |||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Line(s) | 114 | if (! parsed) { | |||
112 | errorSvg << "Error while parsing file: " | 115 | errorSvg << "Error while parsing file: " | ||
113 | << "at line " << line << " column: " << col | 116 | << "at line " << line << " column: " << col | ||
114 | << " message: " << errormessage << endl; | 117 | << " message: " << errormessage << endl; | ||
115 | // ### TODO: feedback to the user | 118 | // ### TODO: feedback to the user | ||
116 | return KoFilter::ParsingError; | 119 | return KoFilter::ParsingError; | ||
117 | } | 120 | } | ||
118 | 121 | | |||
119 | m_document = dynamic_cast<KarbonDocument*>(m_chain->outputDocument()); | 122 | m_document = dynamic_cast<KarbonDocument*>(m_chain->outputDocument()); | ||
120 | if (! m_document) | 123 | if (!m_document) { | ||
121 | return KoFilter::CreationError; | 124 | return KoFilter::CreationError; | ||
122 | 125 | } | |||
126 | if (m_document->pages().isEmpty()) { | ||||
127 | KoPAMasterPage *mp = dynamic_cast<KoPAMasterPage*>(m_document->pages(true).value(0)); | ||||
128 | if (!mp) { | ||||
129 | mp = new KoPAMasterPage(); | ||||
130 | m_document->insertPage(mp, 0); | ||||
131 | } | ||||
132 | m_document->insertPage(new KoPAPage(mp), 0); | ||||
133 | } | ||||
123 | // Do the conversion! | 134 | // Do the conversion! | ||
124 | convert(inputDoc.documentElement()); | 135 | convert(inputDoc.documentElement()); | ||
125 | 136 | | |||
126 | return KoFilter::OK; | 137 | return KoFilter::OK; | ||
127 | } | 138 | } | ||
128 | 139 | | |||
129 | void SvgImport::convert(const KoXmlElement &rootElement) | 140 | void SvgImport::convert(const KoXmlElement &rootElement) | ||
130 | { | 141 | { | ||
131 | if (! m_document) | 142 | if (! m_document) | ||
132 | return; | 143 | return; | ||
133 | 144 | | |||
134 | // set default page size to A4 | 145 | // set default page size to A4 | ||
135 | QSizeF pageSize(550.0, 841.0); | 146 | QSizeF pageSize(550.0, 841.0); | ||
136 | 147 | | |||
137 | SvgParser parser(m_document->resourceManager()); | 148 | SvgParser parser(m_document->resourceManager()); | ||
138 | 149 | | |||
139 | parser.setXmlBaseDir(QFileInfo(m_chain->inputFile()).filePath()); | 150 | parser.setXmlBaseDir(QFileInfo(m_chain->inputFile()).filePath()); | ||
140 | 151 | | |||
141 | QList<KoShape*> toplevelShapes = parser.parseSvg(rootElement, &pageSize); | 152 | QList<KoShape*> toplevelShapes = parser.parseSvg(rootElement, &pageSize); | ||
142 | // parse the root svg element | 153 | // parse the root svg element | ||
143 | buildDocument(toplevelShapes, parser.shapes()); | 154 | buildDocument(toplevelShapes, parser.shapes()); | ||
144 | 155 | | |||
145 | // set the page size | 156 | // set the page size | ||
146 | m_document->setPageSize(pageSize); | 157 | KoPageLayout & layout = m_document->pages().at(0)->pageLayout(); | ||
158 | layout.width = pageSize.width(); | ||||
159 | layout.height = pageSize.height(); | ||||
147 | } | 160 | } | ||
148 | 161 | | |||
149 | void SvgImport::buildDocument(const QList<KoShape*> &toplevelShapes, const QList<KoShape*> &shapes) | 162 | void SvgImport::buildDocument(const QList<KoShape*> &toplevelShapes, const QList<KoShape*> &shapes) | ||
150 | { | 163 | { | ||
164 | Q_UNUSED(shapes); | ||||
165 | KoPAPageBase *page = m_document->pages().first(); | ||||
151 | // if we have only top level groups, make them layers | 166 | // if we have only top level groups, make them layers | ||
152 | bool onlyTopLevelGroups = true; | 167 | bool onlyTopLevelGroups = true; | ||
153 | foreach(KoShape * shape, toplevelShapes) { | 168 | foreach(KoShape * shape, toplevelShapes) { | ||
154 | if (! dynamic_cast<KoShapeGroup*>(shape) || shape->filterEffectStack()) { | 169 | if (! dynamic_cast<KoShapeGroup*>(shape) || shape->filterEffectStack()) { | ||
155 | onlyTopLevelGroups = false; | 170 | onlyTopLevelGroups = false; | ||
156 | break; | 171 | break; | ||
157 | } | 172 | } | ||
158 | } | 173 | } | ||
159 | | ||||
160 | // add all shapes to the document | | |||
161 | foreach(KoShape * shape, shapes) { | | |||
162 | m_document->add(shape); | | |||
163 | } | | |||
164 | | ||||
165 | KoShapeLayer * oldLayer = 0; | 174 | KoShapeLayer *oldLayer = 0; | ||
166 | if (m_document->layers().count()) | 175 | if (page->shapeCount()) { | ||
167 | oldLayer = m_document->layers().first(); | 176 | oldLayer = dynamic_cast<KoShapeLayer*>(page->shapes().first()); | ||
168 | 177 | } | |||
169 | if (onlyTopLevelGroups) { | 178 | if (onlyTopLevelGroups) { | ||
170 | foreach(KoShape * shape, toplevelShapes) { | 179 | foreach(KoShape * shape, toplevelShapes) { | ||
171 | // ungroup toplevel groups | 180 | // ungroup toplevel groups | ||
172 | KoShapeGroup * group = dynamic_cast<KoShapeGroup*>(shape); | 181 | KoShapeGroup *group = dynamic_cast<KoShapeGroup*>(shape); | ||
173 | QList<KoShape*> children = group->shapes(); | 182 | QList<KoShape*> children = group->shapes(); | ||
174 | KoShapeUngroupCommand cmd(group, children, QList<KoShape*>() << group); | 183 | KoShapeUngroupCommand cmd(group, children, QList<KoShape*>() << group); | ||
175 | cmd.redo(); | 184 | cmd.redo(); | ||
176 | 185 | | |||
177 | KoShapeLayer * layer = new KoShapeLayer(); | 186 | KoShapeLayer *layer = new KoShapeLayer(); | ||
178 | foreach(KoShape * child, children) { | 187 | foreach(KoShape * child, children) { | ||
179 | m_document->add(child); | | |||
180 | layer->addShape(child); | 188 | layer->addShape(child); | ||
181 | } | 189 | } | ||
182 | if (! group->name().isEmpty()) | 190 | if (!group->name().isEmpty()) { | ||
183 | layer->setName(group->name()); | 191 | layer->setName(group->name()); | ||
192 | } | ||||
184 | layer->setVisible(group->isVisible()); | 193 | layer->setVisible(group->isVisible()); | ||
185 | layer->setZIndex(group->zIndex()); | 194 | layer->setZIndex(group->zIndex()); | ||
186 | m_document->insertLayer(layer); | 195 | page->addShape(layer); | ||
187 | delete group; | 196 | delete group; | ||
188 | } | 197 | } | ||
189 | } else { | 198 | } else { | ||
190 | KoShapeLayer * layer = new KoShapeLayer(); | 199 | KoShapeLayer *layer = new KoShapeLayer(); | ||
191 | foreach(KoShape * shape, toplevelShapes) { | 200 | foreach(KoShape * shape, toplevelShapes) { | ||
192 | m_document->add(shape); | | |||
193 | layer->addShape(shape); | 201 | layer->addShape(shape); | ||
194 | } | 202 | } | ||
195 | m_document->insertLayer(layer); | | |||
196 | } | 203 | } | ||
197 | | ||||
198 | if (oldLayer) { | 204 | if (oldLayer) { | ||
199 | m_document->removeLayer(oldLayer); | 205 | page->removeShape(oldLayer); | ||
200 | delete oldLayer; | 206 | delete oldLayer; | ||
201 | } | 207 | } | ||
202 | } | 208 | } | ||
203 | 209 | | |||
204 | #include "SvgImport.moc" | 210 | #include "SvgImport.moc" |