hideLinkInExport for Branches (Floats still missing). Floats are now OrnamentedObj.
3 #include <qmessagebox.h>
5 #include <qstylesheet.h>
10 #include "linkablemapobj.h"
14 static BranchObj *lastBranch;
15 static FloatObj *lastFloat;
16 static LinkableMapObj *lastLMO;
18 extern Settings settings;
20 mapBuilderHandler::mapBuilderHandler() {}
22 mapBuilderHandler::~mapBuilderHandler() {}
24 QString mapBuilderHandler::errorProtocol() { return errorProt; }
26 bool mapBuilderHandler::startDocument()
30 laststate = StateInit;
38 QString mapBuilderHandler::parseHREF(QString href)
40 QString type=href.section(":",0,0);
41 QString path=href.section(":",1,1);
42 if (!tmpDir.endsWith("/"))
43 return tmpDir + "/" + path;
48 bool mapBuilderHandler::startElement ( const QString&, const QString&,
49 const QString& eName, const QXmlAttributes& atts )
52 //cout << "startElement <"<<eName<<"> state="<<state <<" laststate="<<laststate<<" loadMode="<<loadMode<<endl;
53 if ( state == StateInit && (eName == "vymmap") )
56 if (!atts.value( "version").isEmpty() )
58 mc->setVersion(atts.value( "version" ));
59 if (!mc->checkVersion())
60 QMessageBox::warning( 0, "Warning: Version Problem" ,
61 "<h3>Map is newer than VYM</h3>"
62 "<p>The map you are just trying to load was "
63 "saved using vym " +atts.value("version")+". "
64 "The version of this vym is " __VYM_VERSION
65 ". If you run into problems after pressing "
66 "the ok-button below, updating vym should help.");
71 if (!atts.value( "author").isEmpty() )
73 mc->setAuthor(atts.value( "author" ) );
75 if (!atts.value( "comment").isEmpty() )
77 mc->setComment (atts.value( "comment" ) );
79 if (!atts.value( "backgroundColor").isEmpty() )
81 col.setNamedColor(atts.value("backgroundColor"));
82 mc->getCanvas()->setBackgroundColor(col);
84 if (!atts.value( "linkColorHint").isEmpty() )
86 if (atts.value("linkColorHint")=="HeadingColor")
87 me->setLinkColorHint(HeadingColor);
89 me->setLinkColorHint(DefaultColor);
91 if (!atts.value( "linkStyle").isEmpty() )
93 QString s=atts.value("linkStyle");
95 me->setLinkStyle(StyleLine);
97 if (s=="StyleParabel")
98 me->setLinkStyle(StyleParabel);
100 if (s=="StylePolyLine")
101 me->setLinkStyle(StylePolyLine);
103 me->setLinkStyle(StylePolyParabel);
105 if (!atts.value( "linkColor").isEmpty() )
107 col.setNamedColor(atts.value("linkColor"));
108 me->setLinkColor(col);
110 if (!atts.value( "defXLinkColor").isEmpty() )
112 col.setNamedColor(atts.value("defXLinkColor"));
113 me->setDefXLinkColor(col);
115 if (!atts.value( "defXLinkWidth").isEmpty() )
117 me->setDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
120 } else if ( eName == "select" && state == StateMap )
122 state=StateMapSelect;
123 } else if ( eName == "setting" && state == StateMap )
125 state=StateMapSetting;
126 if (loadMode==NewMap)
127 readSettingAttr (atts);
128 } else if ( eName == "mapcenter" && state == StateMap )
130 state=StateMapCenter;
131 if (loadMode==NewMap)
133 // Really use the found mapcenter as MCO in a new map
134 lastBranch=mc; // avoid empty pointer
137 // Treat the found mapcenter as a branch
138 // in an existing map
139 LinkableMapObj* lmo=me->getSelection();
140 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
141 || (typeid(*lmo) == typeid(MapCenterObj) ) )
143 lastBranch=(BranchObj*)lmo;
144 if (loadMode==ImportAdd)
146 lastBranch->addBranch();
147 lastBranch=lastBranch->getLastBranch();
153 readBranchAttr (atts);
154 } else if ( (eName == "standardflag" ||eName == "standardFlag") && state == StateMapCenter)
156 state=StateMapCenterStandardFlag;
157 } else if ( eName == "heading" && state == StateMapCenter)
159 state=StateMapCenterHeading;
160 if (!atts.value( "textColor").isEmpty() )
162 col.setNamedColor(atts.value("textColor"));
163 lastBranch->setColor(col );
165 } else if ( eName == "note" && state == StateMapCenter)
166 { // only for backward compatibility (<1.4.6). Use htmlnote now.
167 state=StateMapCenterNote;
168 if (!readNoteAttr (atts) ) return false;
169 } else if ( eName == "htmlnote" && state == StateMapCenter)
173 } else if ( eName == "floatimage" && state == StateMapCenter )
175 state=StateMapCenterFloatImage;
176 lastBranch->addFloatImage();
177 lastFloat=lastBranch->getLastFloatImage();
178 if (!readFloatImageAttr(atts)) return false;
179 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
181 // This is used in vymparts, which have no mapcenter!
183 LinkableMapObj* lmo=me->getSelection();
186 // If a vym part is _loaded_ (not imported),
187 // selection==lmo==NULL
188 // Treat it like ImportAdd then...
192 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
193 || (typeid(*lmo) == typeid(MapCenterObj) ) )
195 lastBranch=(BranchObj*)(lmo);
199 if (loadMode==ImportAdd)
201 lastBranch->addBranch();
202 lastBranch=lastBranch->getLastBranch();
207 readBranchAttr (atts);
208 } else if (eName=="floatimage")
210 state=StateFloatImage;
211 lastBranch->addFloatImage();
212 lastFloat=lastBranch->getLastFloatImage();
213 if (!readFloatImageAttr(atts)) return false;
216 } else if ( eName == "branch" && state == StateMapCenter)
220 lastBranch->addBranch();
221 lastBranch=lastBranch->getLastBranch();
222 readBranchAttr (atts);
223 } else if ( (eName=="standardflag" ||eName == "standardFlag") && state == StateBranch)
225 state=StateBranchStandardFlag;
226 } else if ( eName == "heading" && state == StateBranch)
228 state=StateBranchHeading;
229 if (!atts.value( "textColor").isEmpty() )
231 col.setNamedColor(atts.value("textColor"));
232 lastBranch->setColor(col );
234 } else if ( eName == "note" && state == StateBranch)
236 state=StateBranchNote;
237 if (!readNoteAttr (atts) ) return false;
238 } else if ( eName == "htmlnote" && state == StateBranch)
243 if (!atts.value( "fonthint").isEmpty() )
244 no.setFontHint(atts.value ("fonthint") );
245 } else if ( eName == "floatimage" && state == StateBranch )
247 state=StateBranchFloatImage;
248 lastBranch->addFloatImage();
249 lastFloat=lastBranch->getLastFloatImage();
250 if (!readFloatImageAttr(atts)) return false;
251 } else if ( eName == "xlink" && state == StateBranch )
253 state=StateBranchXLink;
254 if (!readXLinkAttr (atts)) return false;
255 } else if ( eName == "branch" && state == StateBranch )
257 lastBranch->addBranch();
258 lastBranch=lastBranch->getLastBranch();
260 readBranchAttr (atts);
261 } else if ( eName == "html" && state == StateHtmlNote )
267 } else if ( state == StateHtml )
269 // accept all while in html mode,
274 return false; // Error
278 bool mapBuilderHandler::endElement ( const QString&, const QString&, const QString &eName)
280 // cout << "endElement </"<<eName<<"> state="<<state <<" laststate="<<laststate<<endl;
283 case StateMapSelect: state=StateMap; return true;
284 case StateMapSetting: state=StateMap; return true;
285 case StateMapCenter: state=StateMap; return true;
286 case StateMapCenterStandardFlag: state=StateMapCenter; return true;
287 case StateMapCenterHeading: state=StateMapCenter; return true;
288 case StateMapCenterNote: state=StateMapCenter; return true;
289 case StateMapCenterFloatImage: state=StateMapCenter; return true;
290 case StateFloatImage: state=StateMap; return true;
302 state=StateMapCenter;
304 lastBranch=(BranchObj*)(lastBranch->getParObj());
306 case StateBranchStandardFlag: state=StateBranch; return true;
307 case StateBranchHeading: state=StateBranch; return true;
308 case StateBranchNote: state=StateBranch; return true;
309 case StateBranchFloatImage: state=StateBranch; return true;
310 case StateBranchXLink: state=StateBranch; return true;
311 case StateHtmlNote: state=laststate; return true;
313 htmldata+="</"+eName+">";
317 htmldata.replace ("<br></br>","<br />");
318 no.setNote (htmldata);
319 lastBranch->setNote (no);
325 case StateMap: state=StateInit; return true;
327 // even for HTML includes, this should never be reached
332 bool mapBuilderHandler::characters ( const QString& ch)
334 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
336 QString ch_org=quotemeta (ch);
337 QString ch_simplified=ch.simplifyWhiteSpace();
338 if ( ch_simplified.isEmpty() ) return true;
342 case StateInit: break;
343 case StateMap: break;
345 me->select(ch_simplified);
347 case StateMapSetting:break;
348 case StateMapCenter: break;
349 case StateMapCenterStandardFlag:
350 lastBranch->activateStandardFlag(ch_simplified);
352 case StateMapCenterHeading:
353 lastBranch->setHeading(ch_simplified);
355 case StateMapCenterNote:
356 lastBranch->setNote(ch_simplified);
358 case StateBranch: break;
359 case StateBranchStandardFlag:
360 lastBranch->activateStandardFlag(ch_simplified);
362 case StateBranchHeading:
363 lastBranch->setHeading(ch_simplified);
365 case StateBranchNote:
366 lastBranch->setNote(ch_simplified);
368 case StateBranchFloatImage: break;
369 case StateHtmlNote: break;
379 QString mapBuilderHandler::errorString()
381 return "the document is not in the VYM file format";
384 bool mapBuilderHandler::fatalError( const QXmlParseException& exception )
386 errorProt += QString( "Fatal parsing error: %1 in line %2, column %3\n")
387 .arg( exception.message() )
388 .arg( exception.lineNumber() )
389 .arg( exception.columnNumber() );
390 // Try to read the bogus line
392 if (loadStringFromDisk (inputFile,s))
394 QStringList sl=QStringList::split ("\n",s);
396 QStringList::Iterator it = sl.begin();
397 while (i<exception.lineNumber())
403 s.insert (exception.columnNumber()-1,"<ERROR>");
406 return QXmlDefaultHandler::fatalError( exception );
409 void mapBuilderHandler::setMapEditor (MapEditor* e)
412 mc=me->getMapCenter();
415 void mapBuilderHandler::setTmpDir (QString tp)
420 void mapBuilderHandler::setInputFile (QString f)
425 void mapBuilderHandler::setLoadMode (const LoadMode &lm)
430 bool mapBuilderHandler::readBranchAttr (const QXmlAttributes& a)
433 if (!readLinkAttr(a)) return false;
437 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2)
439 if (!a.value( "absPosY").isEmpty() )
441 x=a.value("absPosX").toInt (&okx, 10);
442 y=a.value("absPosY").toInt (&oky, 10);
444 lastBranch->move(x,y);
446 return false; // Couldn't read absPos
449 if (!a.value( "relPosX").isEmpty() && loadMode==NewMap && branchDepth<9)
451 if (!a.value( "relPosY").isEmpty() )
454 fx=a.value("relPosX").toFloat (&okx);
455 fy=a.value("relPosY").toFloat (&oky);
457 lastBranch->move2RelPos ((int)fx,(int)fy);
459 return false; // Couldn't read relPos
462 if (!a.value( "url").isEmpty() )
463 lastBranch->setURL (a.value ("url"));
464 if (!a.value( "vymLink").isEmpty() )
465 lastBranch->setVymLink (a.value ("vymLink"));
466 if (!a.value( "hideInExport").isEmpty() )
467 if (a.value("hideInExport")=="true")
468 lastBranch->setHideInExport(true);
470 if (!a.value( "scrolled").isEmpty() )
471 lastBranch->toggleScroll();
472 if (!a.value( "frameType").isEmpty() )
473 lastBranch->setFrameType (a.value("frameType"));
475 if (!a.value( "incImgV").isEmpty() )
477 if (a.value("incImgV")=="true")
478 lastBranch->setIncludeImagesVer(true);
480 lastBranch->setIncludeImagesVer(false);
482 if (!a.value( "incImgH").isEmpty() )
484 if (a.value("incImgH")=="true")
485 lastBranch->setIncludeImagesHor(true);
487 lastBranch->setIncludeImagesHor(false);
492 bool mapBuilderHandler::readLinkAttr (const QXmlAttributes& a)
496 if (!a.value( "hideLink").isEmpty())
498 if (a.value ("hideLink") =="true")
499 lastLMO->setHideLinkUnselected(true);
501 lastLMO->setHideLinkUnselected(false);
507 bool mapBuilderHandler::readNoteAttr (const QXmlAttributes& a)
508 { // only for backward compatibility (<1.4.6). Use htmlnote now.
511 if (!a.value( "href").isEmpty() )
514 fn=parseHREF(a.value ("href") );
516 QString s; // Reading a note
518 if ( !file.open( IO_ReadOnly) )
520 qWarning ("mapBuilderHandler::readNoteAttr: Couldn't load "+fn);
523 QTextStream stream( &file );
525 while ( !stream.eof() ) {
526 lines += stream.readLine()+"\n";
529 // Convert to richtext
530 if ( !QStyleSheet::mightBeRichText( lines ) )
532 // Here we are workarounding the QT conversion method:
533 // convertFromPlainText does not generate valid xml, needed
534 // for the parser, but just <p> and <br> without closing tags.
535 // So we have to add those by ourselves
536 //lines=quotemeta (lines);
537 lines = QStyleSheet::convertFromPlainText( lines, QStyleSheetItem::WhiteSpaceNormal );
538 lines.replace ("<br>","<br />");
541 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
544 if (!a.value( "fonthint").isEmpty() )
545 no.setFontHint(a.value ("fonthint") );
546 if (state == StateMapCenterNote)
549 lastBranch->setNote(no);
553 bool mapBuilderHandler::readFloatImageAttr (const QXmlAttributes& a)
557 if (!a.value( "useOrientation").isEmpty() )
559 if (a.value ("useOrientation") =="true")
560 lastFloat->setUseOrientation (true);
562 lastFloat->setUseOrientation (false);
564 if (!a.value( "href").isEmpty() )
567 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
569 QMessageBox::warning( 0, "Warning: " ,
570 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
571 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
577 if (!a.value( "floatExport").isEmpty() )
579 if (a.value ("floatExport") =="true")
580 lastFloat->setFloatExport (true);
582 lastFloat->setFloatExport (false);
584 if (!a.value( "zPlane").isEmpty() )
585 lastFloat->setZ (a.value("zPlane").toInt ());
588 if (!a.value( "relPosX").isEmpty() )
590 if (!a.value( "relPosY").isEmpty() )
593 x=a.value("relPosX").toInt (&okx, 10);
594 y=a.value("relPosY").toInt (&oky, 10);
596 lastFloat->setRelPos (QPoint (x,y) );
598 // Couldn't read relPos
603 if (!readLinkAttr(a)) return false;
608 bool mapBuilderHandler::readXLinkAttr (const QXmlAttributes& a)
613 XLinkObj *xlo=new XLinkObj (mc->getCanvas());
614 if (!a.value( "color").isEmpty() )
616 col.setNamedColor(a.value("color"));
620 if (!a.value( "width").isEmpty() )
622 xlo->setWidth(a.value ("width").toInt (&okx, 10));
625 if (!a.value( "beginBranch").isEmpty() )
627 if (!a.value( "endBranch").isEmpty() )
629 LinkableMapObj *lmo=mc->findObjBySelect (a.value( "beginBranch"));
630 if (lmo && typeid (*lmo)==typeid (BranchObj))
632 xlo->setBegin ((BranchObj*)(lmo));
633 lmo=mc->findObjBySelect (a.value( "endBranch"));
634 if (lmo && typeid (*lmo)==typeid (BranchObj))
636 xlo->setEnd ((BranchObj*)(lmo));
640 success=true; // Not all branches there yet, no error
643 if (!success) delete (xlo);
647 bool mapBuilderHandler::readHtmlAttr (const QXmlAttributes& a)
649 for (int i=1; i<=a.count(); i++)
650 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
654 bool mapBuilderHandler::readSettingAttr (const QXmlAttributes& a)
656 if (!a.value( "key").isEmpty() )
658 if (!a.value( "value").isEmpty() )
659 settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));