-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
QGISVGTemplate.cpp
226 lines (179 loc) · 7.56 KB
/
QGISVGTemplate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/***************************************************************************
* Copyright (c) 2012-2014 Luke Parry <l.parry@warwick.ac.uk> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
#include <QFile>
#include <QFont>
#include <QPen>
#include <QSvgRenderer>
#include <QGraphicsSvgItem>
#include <strstream>
#include <boost/regex.hpp>
#endif // #ifndef _PreComp_
#include <App/Application.h>
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/Parameter.h>
#include <Gui/Command.h>
#include <Mod/TechDraw/App/Geometry.h>
#include <Mod/TechDraw/App/DrawSVGTemplate.h>
#include "ZVALUE.h"
#include "TemplateTextField.h"
#include "QGISVGTemplate.h"
using namespace TechDrawGui;
QGISVGTemplate::QGISVGTemplate(QGraphicsScene *scene, QWidget* srWidget)
: QGITemplate(scene),
qgview(srWidget),
firstTime(true)
{
m_svgItem = new QGraphicsSvgItem(this);
m_svgRender = new QSvgRenderer();
m_svgItem->setSharedRenderer(m_svgRender);
m_svgItem->setFlags(QGraphicsItem::ItemClipsToShape);
m_svgItem->setCacheMode(QGraphicsItem::NoCache);
addToGroup(m_svgItem);
}
QGISVGTemplate::~QGISVGTemplate()
{
delete m_svgRender;
}
QVariant QGISVGTemplate::itemChange(GraphicsItemChange change,
const QVariant &value)
{
return QGraphicsItemGroup::itemChange(change, value);
}
void QGISVGTemplate::openFile(const QFile &file)
{
}
void QGISVGTemplate::load(const QString &fileName)
{
if (fileName.isEmpty()){
return;
}
QFile file(fileName);
if (!file.exists()) {
return;
}
m_svgRender->load(file.fileName());
QSize size = m_svgRender->defaultSize();
m_svgItem->setSharedRenderer(m_svgRender);
if (firstTime) {
createClickHandles();
firstTime = false;
}
//This is probably first time only logic too.
TechDraw::DrawSVGTemplate *tmplte = getSVGTemplate();
double xaspect, yaspect;
xaspect = tmplte->getWidth() / (double) size.width();
yaspect = tmplte->getHeight() / (double) size.height();
QTransform qtrans;
qtrans.translate(0.f, -tmplte->getHeight());
qtrans.scale(xaspect , yaspect);
m_svgItem->setTransform(qtrans);
}
TechDraw::DrawSVGTemplate * QGISVGTemplate::getSVGTemplate()
{
if(pageTemplate && pageTemplate->isDerivedFrom(TechDraw::DrawSVGTemplate::getClassTypeId()))
return static_cast<TechDraw::DrawSVGTemplate *>(pageTemplate);
else
return 0;
}
void QGISVGTemplate::draw()
{
TechDraw::DrawSVGTemplate *tmplte = getSVGTemplate();
if(!tmplte)
throw Base::Exception("Template Feature not set for QGISVGTemplate");
load(QString::fromUtf8(tmplte->PageResult.getValue()));
}
void QGISVGTemplate::updateView(bool update)
{
draw();
}
void QGISVGTemplate::createClickHandles(void)
{
TechDraw::DrawSVGTemplate *tmplte = getSVGTemplate();
std::string temp = tmplte->PageResult.getValue();
if (temp.empty())
return;
Base::FileInfo fi(temp);
std::ostringstream oStream;
std::string tempendl = "--endOfLine--";
std::string line;
//read all of PageResult into oStream (except the DrawingContent marker comment - why??)
std::ifstream ifile (fi.filePath().c_str());
while (!ifile.eof())
{
std::getline(ifile,line);
// check if the marker in the template is found
if(line.find("<!-- DrawingContent -->") == std::string::npos) {
// if not - write line to oStream
oStream << line << tempendl;
}
}
std::string outfragment(oStream.str());
// Find text tags with freecad:editable attribute and their matching tspans
// keep tagRegex in sync with App/DrawSVGTemplate.cpp
boost::regex tagRegex("<text([^>]*freecad:editable=[^>]*)>[^<]*<tspan[^>]*>([^<]*)</tspan>");
// Smaller regexes for parsing matches to tagRegex
boost::regex editableNameRegex("freecad:editable=\"(.*?)\"");
boost::regex xRegex("x=\"([\\d.-]+)\"");
boost::regex yRegex("y=\"([\\d.-]+)\"");
//Note: some templates have fancy Transform clauses and don't use absolute x,y to position editableFields.
// editableFields will be in the wrong place in this case.
std::string::const_iterator begin, end;
begin = outfragment.begin();
end = outfragment.end();
boost::match_results<std::string::const_iterator> tagMatch, nameMatch, xMatch, yMatch;
//TODO: Find location of special fields (first/third angle) and make graphics items for them
while (boost::regex_search(begin, end, tagMatch, tagRegex)) {
if ( boost::regex_search(tagMatch[1].first, tagMatch[1].second, nameMatch, editableNameRegex) &&
boost::regex_search(tagMatch[1].first, tagMatch[1].second, xMatch, xRegex) &&
boost::regex_search(tagMatch[1].first, tagMatch[1].second, yMatch, yRegex) ) {
QString xStr = QString::fromStdString(xMatch[1].str());
QString yStr = QString::fromStdString(yMatch[1].str());
QString editableName = QString::fromStdString(nameMatch[1].str());
double x = xStr.toDouble();
double y = yStr.toDouble();
//TODO: this should probably be configurable without a code change
double editClickBoxSize = 1.5;
QColor editClickBoxColor = Qt::green;
double width = editClickBoxSize;
double height = editClickBoxSize;
TemplateTextField *item = new TemplateTextField(this, tmplte, nameMatch[1].str(), qgview);
float pad = 1;
item->setRect(x - pad, -tmplte->getHeight() + y - height - pad,
width + 2 * pad, height + 2 * pad);
QPen myPen;
QBrush myBrush(editClickBoxColor,Qt::SolidPattern);
myPen.setStyle(Qt::SolidLine);
myPen.setColor(editClickBoxColor);
myPen.setWidth(0); // 0 means "cosmetic pen" - always 1px
item->setPen(myPen);
item->setBrush(myBrush);
item->setZValue(ZVALUE::SVGTEMPLATE);
addToGroup(item);
textFields.push_back(item);
}
begin = tagMatch[0].second;
}
}
#include <Mod/TechDraw/Gui/moc_QGISVGTemplate.cpp>