Spanish doc is found, if LANG is set. Fixed wrong position of floatimages
3 #include <qmessagebox.h>
5 #include <q3stylesheet.h>
12 #include "linkablemapobj.h"
16 static BranchObj *lastBranch;
17 static FloatObj *lastFloat;
18 static OrnamentedObj *lastOO;
20 extern Settings settings;
22 mapBuilderHandler::mapBuilderHandler() {}
24 mapBuilderHandler::~mapBuilderHandler() {}
26 QString mapBuilderHandler::errorProtocol() { return errorProt; }
28 bool mapBuilderHandler::startDocument()
32 laststate = StateInit;
40 QString mapBuilderHandler::parseHREF(QString href)
42 QString type=href.section(":",0,0);
43 QString path=href.section(":",1,1);
44 if (!tmpDir.endsWith("/"))
45 return tmpDir + "/" + path;
50 bool mapBuilderHandler::startElement ( const QString&, const QString&,
51 const QString& eName, const QXmlAttributes& atts )
55 cout << "startElement <"<< eName.ascii()<<
57 " laststate="<<laststate<<
58 " loadMode="<<loadMode<<endl;
60 if ( state == StateInit && (eName == "vymmap") )
63 if (!atts.value( "version").isEmpty() )
65 if (!checkVersion(atts.value("version")))
66 QMessageBox::warning( 0, "Warning: Version Problem" ,
67 "<h3>Map is newer than VYM</h3>"
68 "<p>The map you are just trying to load was "
69 "saved using vym " +atts.value("version")+". "
70 "The version of this vym is " __VYM_VERSION
71 ". If you run into problems after pressing "
72 "the ok-button below, updating vym should help.");
74 mc->setVersion(atts.value( "version" ));
79 if (!atts.value( "author").isEmpty() )
81 mc->setAuthor(atts.value( "author" ) );
83 if (!atts.value( "comment").isEmpty() )
85 mc->setComment (atts.value( "comment" ) );
87 if (!atts.value( "backgroundColor").isEmpty() )
89 col.setNamedColor(atts.value("backgroundColor"));
90 mc->getCanvas()->setBackgroundColor(col);
92 if (!atts.value( "linkColorHint").isEmpty() )
94 if (atts.value("linkColorHint")=="HeadingColor")
95 me->setLinkColorHint(HeadingColor);
97 me->setLinkColorHint(DefaultColor);
99 if (!atts.value( "linkStyle").isEmpty() )
101 QString s=atts.value("linkStyle");
103 me->setLinkStyle(StyleLine);
105 if (s=="StyleParabel")
106 me->setLinkStyle(StyleParabel);
108 if (s=="StylePolyLine")
109 me->setLinkStyle(StylePolyLine);
111 me->setLinkStyle(StylePolyParabel);
113 if (!atts.value( "linkColor").isEmpty() )
115 col.setNamedColor(atts.value("linkColor"));
116 me->setLinkColor(col);
118 if (!atts.value( "defXLinkColor").isEmpty() )
120 col.setNamedColor(atts.value("defXLinkColor"));
121 me->setDefXLinkColor(col);
123 if (!atts.value( "defXLinkWidth").isEmpty() )
125 me->setDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
128 } else if ( eName == "select" && state == StateMap )
130 state=StateMapSelect;
131 } else if ( eName == "setting" && state == StateMap )
133 state=StateMapSetting;
134 if (loadMode==NewMap)
135 readSettingAttr (atts);
136 } else if ( eName == "mapcenter" && state == StateMap )
138 state=StateMapCenter;
139 if (loadMode==NewMap)
141 // Really use the found mapcenter as MCO in a new map
142 lastBranch=mc; // avoid empty pointer
145 // Treat the found mapcenter as a branch
146 // in an existing map
147 LinkableMapObj* lmo=me->getSelection();
148 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
149 || (typeid(*lmo) == typeid(MapCenterObj) ) )
151 lastBranch=(BranchObj*)lmo;
152 if (loadMode==ImportAdd)
154 lastBranch->addBranch();
155 lastBranch=lastBranch->getLastBranch();
161 readBranchAttr (atts);
162 } else if ( (eName == "standardflag" ||eName == "standardFlag") && state == StateMapCenter)
164 state=StateMapCenterStandardFlag;
165 } else if ( eName == "heading" && state == StateMapCenter)
167 state=StateMapCenterHeading;
168 if (!atts.value( "textColor").isEmpty() )
170 col.setNamedColor(atts.value("textColor"));
171 lastBranch->setColor(col );
173 } else if ( eName == "note" && state == StateMapCenter)
174 { // only for backward compatibility (<1.4.6). Use htmlnote now.
175 state=StateMapCenterNote;
176 if (!readNoteAttr (atts) ) return false;
177 } else if ( eName == "htmlnote" && state == StateMapCenter)
181 } else if ( eName == "floatimage" && state == StateMapCenter )
183 state=StateMapCenterFloatImage;
184 lastBranch->addFloatImage();
185 lastFloat=lastBranch->getLastFloatImage();
186 if (!readFloatImageAttr(atts)) return false;
187 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
189 // This is used in vymparts, which have no mapcenter!
191 LinkableMapObj* lmo=me->getSelection();
194 // If a vym part is _loaded_ (not imported),
195 // selection==lmo==NULL
196 // Treat it like ImportAdd then...
200 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
201 || (typeid(*lmo) == typeid(MapCenterObj) ) )
203 lastBranch=(BranchObj*)(lmo);
207 if (loadMode==ImportAdd)
209 lastBranch->addBranch();
210 lastBranch=lastBranch->getLastBranch();
215 readBranchAttr (atts);
216 } else if (eName=="floatimage")
218 state=StateFloatImage;
219 lastBranch->addFloatImage();
220 lastFloat=lastBranch->getLastFloatImage();
221 if (!readFloatImageAttr(atts)) return false;
224 } else if ( eName == "branch" && state == StateMapCenter)
228 lastBranch->addBranch();
229 lastBranch=lastBranch->getLastBranch();
230 readBranchAttr (atts);
231 } else if ( (eName=="standardflag" ||eName == "standardFlag") && state == StateBranch)
233 state=StateBranchStandardFlag;
234 } else if ( eName == "heading" && state == StateBranch)
236 state=StateBranchHeading;
237 if (!atts.value( "textColor").isEmpty() )
239 col.setNamedColor(atts.value("textColor"));
240 lastBranch->setColor(col );
242 } else if ( eName == "note" && state == StateBranch)
244 state=StateBranchNote;
245 if (!readNoteAttr (atts) ) return false;
246 } else if ( eName == "htmlnote" && state == StateBranch)
251 if (!atts.value( "fonthint").isEmpty() )
252 no.setFontHint(atts.value ("fonthint") );
253 } else if ( eName == "floatimage" && state == StateBranch )
255 state=StateBranchFloatImage;
256 lastBranch->addFloatImage();
257 lastFloat=lastBranch->getLastFloatImage();
258 if (!readFloatImageAttr(atts)) return false;
259 } else if ( eName == "xlink" && state == StateBranch )
261 state=StateBranchXLink;
262 if (!readXLinkAttr (atts)) return false;
263 } else if ( eName == "branch" && state == StateBranch )
265 lastBranch->addBranch();
266 lastBranch=lastBranch->getLastBranch();
268 readBranchAttr (atts);
269 } else if ( eName == "html" && state == StateHtmlNote )
275 } else if ( state == StateHtml )
277 // accept all while in html mode,
282 return false; // Error
286 bool mapBuilderHandler::endElement ( const QString&, const QString&, const QString &eName)
288 // cout << "endElement </"<<eName<<"> state="<<state <<" laststate="<<laststate<<endl;
291 case StateMapSelect: state=StateMap; return true;
292 case StateMapSetting: state=StateMap; return true;
293 case StateMapCenter: state=StateMap; return true;
294 case StateMapCenterStandardFlag: state=StateMapCenter; return true;
295 case StateMapCenterHeading: state=StateMapCenter; return true;
296 case StateMapCenterNote: state=StateMapCenter; return true;
297 case StateMapCenterFloatImage: state=StateMapCenter; return true;
298 case StateFloatImage: state=StateMap; return true;
310 state=StateMapCenter;
312 lastBranch=(BranchObj*)(lastBranch->getParObj());
314 case StateBranchStandardFlag: state=StateBranch; return true;
315 case StateBranchHeading: state=StateBranch; return true;
316 case StateBranchNote: state=StateBranch; return true;
317 case StateBranchFloatImage: state=StateBranch; return true;
318 case StateBranchXLink: state=StateBranch; return true;
319 case StateHtmlNote: state=laststate; return true;
321 htmldata+="</"+eName+">";
325 htmldata.replace ("<br></br>","<br />");
326 no.setNote (htmldata);
327 lastBranch->setNote (no);
333 case StateMap: state=StateInit; return true;
335 // even for HTML includes, this should never be reached
340 bool mapBuilderHandler::characters ( const QString& ch)
342 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
344 QString ch_org=quotemeta (ch);
345 QString ch_simplified=ch.simplifyWhiteSpace();
346 if ( ch_simplified.isEmpty() ) return true;
350 case StateInit: break;
351 case StateMap: break;
353 me->select(ch_simplified);
355 case StateMapSetting:break;
356 case StateMapCenter: break;
357 case StateMapCenterStandardFlag:
358 lastBranch->activateStandardFlag(ch_simplified);
360 case StateMapCenterHeading:
361 lastBranch->setHeading(ch_simplified);
363 case StateMapCenterNote:
364 lastBranch->setNote(ch_simplified);
366 case StateBranch: break;
367 case StateBranchStandardFlag:
368 lastBranch->activateStandardFlag(ch_simplified);
370 case StateBranchHeading:
371 lastBranch->setHeading(ch_simplified);
373 case StateBranchNote:
374 lastBranch->setNote(ch_simplified);
376 case StateBranchFloatImage: break;
377 case StateHtmlNote: break;
387 QString mapBuilderHandler::errorString()
389 return "the document is not in the VYM file format";
392 bool mapBuilderHandler::fatalError( const QXmlParseException& exception )
394 errorProt += QString( "Fatal parsing error: %1 in line %2, column %3\n")
395 .arg( exception.message() )
396 .arg( exception.lineNumber() )
397 .arg( exception.columnNumber() );
398 // Try to read the bogus line
399 errorProt+=QString("File is: %1\n").arg(inputFile);
401 if (loadStringFromDisk (inputFile,s))
403 QStringList sl=QStringList::split ("\n",s);
405 QStringList::Iterator it = sl.begin();
406 while (i<exception.lineNumber()-1)
412 s.insert (exception.columnNumber()-1,"<ERROR>");
415 return QXmlDefaultHandler::fatalError( exception );
418 void mapBuilderHandler::setMapEditor (MapEditor* e)
421 mc=me->getMapCenter();
424 void mapBuilderHandler::setTmpDir (QString tp)
429 void mapBuilderHandler::setInputFile (QString f)
434 void mapBuilderHandler::setLoadMode (const LoadMode &lm)
439 bool mapBuilderHandler::readBranchAttr (const QXmlAttributes& a)
442 if (!readOOAttr(a)) return false;
444 if (!a.value( "scrolled").isEmpty() )
445 lastBranch->toggleScroll();
446 if (!a.value( "frameType").isEmpty() )
447 lastBranch->setFrameType (a.value("frameType"));
449 if (!a.value( "incImgV").isEmpty() )
451 if (a.value("incImgV")=="true")
452 lastBranch->setIncludeImagesVer(true);
454 lastBranch->setIncludeImagesVer(false);
456 if (!a.value( "incImgH").isEmpty() )
458 if (a.value("incImgH")=="true")
459 lastBranch->setIncludeImagesHor(true);
461 lastBranch->setIncludeImagesHor(false);
466 bool mapBuilderHandler::readOOAttr (const QXmlAttributes& a)
472 if (!a.value( "relPosX").isEmpty() )
474 if (!a.value( "relPosY").isEmpty() )
476 x=a.value("relPosX").toInt (&okx, 10);
477 y=a.value("relPosY").toInt (&oky, 10);
480 lastOO->setUseRelPos (true);
481 lastOO->move2RelPos (x,y);
484 return false; // Couldn't read relPos
487 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2)
489 if (!a.value( "absPosY").isEmpty() )
491 x=a.value("absPosX").toInt (&okx, 10);
492 y=a.value("absPosY").toInt (&oky, 10);
496 return false; // Couldn't read absPos
499 if (!a.value( "url").isEmpty() )
500 lastOO->setURL (a.value ("url"));
501 if (!a.value( "vymLink").isEmpty() )
502 lastOO->setVymLink (a.value ("vymLink"));
503 if (!a.value( "hideInExport").isEmpty() )
504 if (a.value("hideInExport")=="true")
505 lastOO->setHideInExport(true);
507 if (!a.value( "hideLink").isEmpty())
509 if (a.value ("hideLink") =="true")
510 lastOO->setHideLinkUnselected(true);
512 lastOO->setHideLinkUnselected(false);
518 bool mapBuilderHandler::readNoteAttr (const QXmlAttributes& a)
519 { // only for backward compatibility (<1.4.6). Use htmlnote now.
522 if (!a.value( "href").isEmpty() )
525 fn=parseHREF(a.value ("href") );
527 QString s; // Reading a note
529 if ( !file.open( QIODevice::ReadOnly) )
531 qWarning ("mapBuilderHandler::readNoteAttr: Couldn't load "+fn);
534 QTextStream stream( &file );
536 while ( !stream.atEnd() ) {
537 lines += stream.readLine()+"\n";
540 // Convert to richtext
541 if ( !Q3StyleSheet::mightBeRichText( lines ) )
543 // Here we are workarounding the QT conversion method:
544 // convertFromPlainText does not generate valid xml, needed
545 // for the parser, but just <p> and <br> without closing tags.
546 // So we have to add those by ourselves
547 //lines=quotemeta (lines);
548 lines = Q3StyleSheet::convertFromPlainText( lines, Q3StyleSheetItem::WhiteSpaceNormal );
549 lines.replace ("<br>","<br />");
552 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
555 if (!a.value( "fonthint").isEmpty() )
556 no.setFontHint(a.value ("fonthint") );
557 if (state == StateMapCenterNote)
560 lastBranch->setNote(no);
564 bool mapBuilderHandler::readFloatImageAttr (const QXmlAttributes& a)
568 //if (!readOOAttr(a)) return false;
570 if (!a.value( "useOrientation").isEmpty() )
572 if (a.value ("useOrientation") =="true")
573 lastFloat->setUseOrientation (true);
575 lastFloat->setUseOrientation (false);
577 if (!a.value( "href").isEmpty() )
580 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
582 QMessageBox::warning( 0, "Warning: " ,
583 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
584 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
590 if (!a.value( "floatExport").isEmpty() )
592 // Only for compatibility. THis is not used since 1.7.11
593 if (a.value ("floatExport") =="true")
594 lastFloat->setFloatExport(true);
596 lastFloat->setFloatExport (false);
598 if (!a.value( "zPlane").isEmpty() )
599 lastFloat->setZ (a.value("zPlane").toInt ());
602 if (!a.value( "relPosX").isEmpty() )
604 if (!a.value( "relPosY").isEmpty() )
607 x=a.value("relPosX").toInt (&okx, 10);
608 y=a.value("relPosY").toInt (&oky, 10);
612 lastFloat->setRelPos (QPoint (x,y) );
613 // make sure floats in mapcenter are repositioned to relative pos
614 if (mc==lastBranch) mc->positionContents();
617 // Couldn't read relPos
622 if (!readOOAttr(a)) return false;
624 if (!a.value ("orgName").isEmpty() )
626 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
631 bool mapBuilderHandler::readXLinkAttr (const QXmlAttributes& a)
636 XLinkObj *xlo=new XLinkObj (mc->getCanvas());
637 if (!a.value( "color").isEmpty() )
639 col.setNamedColor(a.value("color"));
643 if (!a.value( "width").isEmpty() )
645 xlo->setWidth(a.value ("width").toInt (&okx, 10));
648 if (!a.value( "beginBranch").isEmpty() )
650 if (!a.value( "endBranch").isEmpty() )
652 LinkableMapObj *lmo=mc->findObjBySelect (a.value( "beginBranch"));
653 if (lmo && typeid (*lmo)==typeid (BranchObj))
655 xlo->setBegin ((BranchObj*)(lmo));
656 lmo=mc->findObjBySelect (a.value( "endBranch"));
657 if (lmo && typeid (*lmo)==typeid (BranchObj))
659 xlo->setEnd ((BranchObj*)(lmo));
663 success=true; // Not all branches there yet, no error
666 if (!success) delete (xlo);
670 bool mapBuilderHandler::readHtmlAttr (const QXmlAttributes& a)
672 for (int i=1; i<=a.count(); i++)
673 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
677 bool mapBuilderHandler::readSettingAttr (const QXmlAttributes& a)
679 if (!a.value( "key").isEmpty() )
681 if (!a.value( "value").isEmpty() )
682 settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));