Skip to content

Commit

Permalink
Prevent creation of superflous QGItems for Text Fields
Browse files Browse the repository at this point in the history
  • Loading branch information
WandererFan authored and wwmayer committed Aug 12, 2016
1 parent ef69519 commit d8ccf14
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 38 deletions.
81 changes: 46 additions & 35 deletions src/Mod/TechDraw/Gui/QGISVGTemplate.cpp
Expand Up @@ -49,7 +49,8 @@ using namespace TechDrawGui;

QGISVGTemplate::QGISVGTemplate(QGraphicsScene *scene, QWidget* srWidget)
: QGITemplate(scene),
qgview(srWidget)
qgview(srWidget),
firstTime(true)
{

m_svgItem = new QGraphicsSvgItem(this);
Expand All @@ -69,7 +70,7 @@ QGISVGTemplate::~QGISVGTemplate()
}

QVariant QGISVGTemplate::itemChange(GraphicsItemChange change,
const QVariant &value)
const QVariant &value)
{
return QGraphicsItemGroup::itemChange(change, value);
}
Expand All @@ -95,10 +96,50 @@ void QGISVGTemplate::load(const QString &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();

std::string temp = tmplte->PageResult.getValue(); //fixes non-drawing of restored template
if (temp.empty())
return;

Expand All @@ -108,13 +149,14 @@ void QGISVGTemplate::load(const QString &fileName)
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 through
// if not - write line to oStream
oStream << line << tempendl;
}
}
Expand All @@ -139,7 +181,6 @@ void QGISVGTemplate::load(const QString &fileName)

//TODO: Find location of special fields (first/third angle) and make graphics items for them

// and update the sketch
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) &&
Expand Down Expand Up @@ -180,36 +221,6 @@ void QGISVGTemplate::load(const QString &fileName)
begin = tagMatch[0].second;
}

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();
}

#include <Mod/TechDraw/Gui/moc_QGISVGTemplate.cpp>
2 changes: 2 additions & 0 deletions src/Mod/TechDraw/Gui/QGISVGTemplate.h
Expand Up @@ -55,9 +55,11 @@ class TechDrawGuiExport QGISVGTemplate : public QGITemplate
protected:
void openFile(const QFile &file);
void load (const QString & fileName);
void createClickHandles(void);
QWidget* qgview; //for parenting dlgTemplateField

protected:
bool firstTime;
TechDraw::DrawSVGTemplate * getSVGTemplate();
QGraphicsSvgItem *m_svgItem;
QSvgRenderer *m_svgRender;
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/TechDraw/Gui/QGITemplate.cpp
Expand Up @@ -37,7 +37,7 @@
using namespace TechDrawGui;

QGITemplate::QGITemplate(QGraphicsScene *scene) : QGraphicsItemGroup(),
pageTemplate(0)
pageTemplate(0)
{
setHandlesChildEvents(false);
setCacheMode(QGraphicsItem::NoCache);
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/TechDraw/Gui/QGITemplate.h
Expand Up @@ -57,7 +57,7 @@ class TechDrawGuiExport QGITemplate : public QObject, public QGraphicsItemGroup
inline qreal getY() { return y() * -1; }

virtual void updateView(bool update = false);
std::vector<TemplateTextField *> getTestFields(void) { return textFields; };
std::vector<TemplateTextField *> getTextFields(void) { return textFields; };

virtual void draw() = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/Mod/TechDraw/Gui/QGVPage.cpp
Expand Up @@ -442,7 +442,7 @@ void QGVPage::toggleMarkers(bool enable)
}
QGISVGTemplate* itemTemplate = dynamic_cast<QGISVGTemplate*> (*it);
if (itemTemplate) {
std::vector<TemplateTextField *> textFields = itemTemplate->getTestFields();
std::vector<TemplateTextField *> textFields = itemTemplate->getTextFields();
for (auto& t:textFields) {
if (enable) {
t->show();
Expand Down

0 comments on commit d8ccf14

Please sign in to comment.