9 #include "branchitem.h"
12 #include "linkablemapobj.h"
13 #include "mainwindow.h"
16 static ImageItem *lastImageItem;
17 static MapItem *lastMI;
19 extern Main *mainWindow;
20 extern Settings settings;
21 extern QString vymVersion;
23 bool parseVYMHandler::startDocument()
27 laststate = StateInit;
29 stateStack.append(StateInit);
35 bool parseVYMHandler::startElement ( const QString&, const QString&,
36 const QString& eName, const QXmlAttributes& atts )
40 cout << "startElement <"<< qPrintable(eName)
42 <<" laststate="<<stateStack.last()
43 <<" loadMode="<<loadMode
44 // <<" line="<<QXmlDefaultHandler::lineNumber()
47 stateStack.append (state);
48 if ( state == StateInit && (eName == "vymmap") )
51 branchesTotal=branchesCurrent=0;
53 if (loadMode==NewMap )
59 if (!atts.value( "author").isEmpty() )
60 model->setAuthor(atts.value( "author" ) );
61 if (!atts.value( "comment").isEmpty() )
62 model->setComment (atts.value( "comment" ) );
63 if (!atts.value( "branchCount").isEmpty() )
65 branchesTotal=atts.value("branchCount").toInt();
68 mainWindow->setProgressMinimum (0);
69 mainWindow->setProgressMaximum (branchesTotal);
70 mainWindow->setProgressValue(0);
74 mainWindow->setProgressMinimum (0);
75 mainWindow->setProgressMaximum (0);
76 mainWindow->setProgressValue(0);
80 if (!atts.value( "backgroundColor").isEmpty() )
82 col.setNamedColor(atts.value("backgroundColor"));
83 model->getScene()->setBackgroundBrush(col);
85 if (!atts.value( "selectionColor").isEmpty() )
87 col.setNamedColor(atts.value("selectionColor"));
88 model->setSelectionColor(col);
90 if (!atts.value( "linkColorHint").isEmpty() )
92 if (atts.value("linkColorHint")=="HeadingColor")
93 model->setMapLinkColorHint(LinkableMapObj::HeadingColor);
95 model->setMapLinkColorHint(LinkableMapObj::DefaultColor);
97 if (!atts.value( "linkStyle").isEmpty() )
98 model->setMapLinkStyle(atts.value("linkStyle"));
99 if (!atts.value( "linkColor").isEmpty() )
101 col.setNamedColor(atts.value("linkColor"));
102 model->setMapDefLinkColor(col);
104 if (!atts.value( "defXLinkColor").isEmpty() )
106 col.setNamedColor(atts.value("defXLinkColor"));
107 model->setMapDefXLinkColor(col);
109 if (!atts.value( "defXLinkWidth").isEmpty() )
110 model->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
113 if (!atts.value( "version").isEmpty() )
115 if (!checkVersion(atts.value("version")))
116 QMessageBox::warning( 0, "Warning: Version Problem" ,
117 "<h3>Map is newer than VYM</h3>"
118 "<p>The map you are just trying to load was "
119 "saved using vym " +atts.value("version")+". "
120 "The version of this vym is " + vymVersion +
121 ". If you run into problems after pressing "
122 "the ok-button below, updating vym should help.");
124 model->setVersion(atts.value( "version" ));
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
143 lastBranch=model->createMapCenter();
146 // Treat the found mapcenter as a branch
147 // in an existing map
148 BranchItem *bi=model->getSelectedBranchItem();
152 if (loadMode==ImportAdd)
154 lastBranch=model->createBranch(lastBranch);
156 //FIXME-3 lastBranch->clear();
160 readBranchAttr (atts);
162 (eName == "standardflag" ||eName == "standardFlag") &&
163 (state == StateMapCenter || state==StateBranch))
165 state=StateStandardFlag;
166 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
170 if (!atts.value( "textColor").isEmpty() )
172 col.setNamedColor(atts.value("textColor"));
173 lastBranch->setHeadingColor(col );
175 } else if ( eName == "note" &&
176 (state == StateMapCenter ||state==StateBranch))
177 { // only for backward compatibility (<1.4.6). Use htmlnote now.
179 if (!readNoteAttr (atts) ) return false;
180 } else if ( eName == "htmlnote" && state == StateMapCenter)
184 } else if ( eName == "floatimage" &&
185 (state == StateMapCenter ||state==StateBranch))
188 lastImage=model->createImage(lastBranch);
189 if (!readImageAttr(atts)) return false;
190 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
192 // This is used in vymparts, which have no mapcenter!
194 TreeItem *ti=model->getSelectedItem();
197 // If a vym part is _loaded_ (not imported),
198 // selection==lmo==NULL
199 // Treat it like ImportAdd then...
201 // FIXME-3 lmo=model->first()->getLMO();
202 // Do we really have no MCO when loading?????
203 cout << "xml-vym aborted\n";
205 if (ti && ti->isBranchLikeType() )
207 lastBranch=(BranchItem*)ti;
211 if (loadMode==ImportAdd)
213 lastBranch=model->createBranch(lastBranch);
216 //FIXME-2 lastBranch->clear();
217 readBranchAttr (atts);
218 } else if (eName=="floatimage")
221 lastImage=model->createImage (lastBranch);
222 if (!readImageAttr(atts)) return false;
225 } else if ( eName == "branch" && state == StateMapCenter)
228 lastBranch=model->createBranch(lastBranch);
229 readBranchAttr (atts);
230 } else if ( eName == "htmlnote" && state == StateBranch)
235 if (!atts.value( "fonthint").isEmpty() )
236 no.setFontHint(atts.value ("fonthint") );
237 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
241 if (!readFrameAttr(atts)) return false;
242 } else if ( eName == "xlink" && state == StateBranch )
244 state=StateBranchXLink;
245 if (!readXLinkAttr (atts)) return false;
246 } else if ( eName == "branch" && state == StateBranch )
248 lastBranch=model->createBranch(lastBranch);
249 readBranchAttr (atts);
250 } else if ( eName == "html" && state == StateHtmlNote )
256 } else if ( state == StateHtml )
258 // accept all while in html mode,
263 return false; // Error
267 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
270 cout << "endElement </" <<qPrintable(eName)
272 <<" laststate=" <<laststate
273 <<" stateStack="<<stateStack.last()
279 mainWindow->removeProgressBar();
282 model->selectParent(); // FIXME-3 really needed to "select"? Maybe optimize...
283 model->emitDataHasChanged (lastBranch);
284 lastBranch=(BranchItem*)(lastBranch->parent());
285 lastBranch->setLastSelectedBranch (0); // Reset last selected to first child branch
288 model->selectParent();// FIXME-3 really needed to "select"? Maybe optimize...
289 model->emitDataHasChanged (lastBranch);
290 lastBranch=(BranchItem*)(lastBranch->parent());
291 lastBranch->setLastSelectedBranch (0); // Reset last selected to first child branch
294 htmldata+="</"+eName+">";
298 htmldata.replace ("<br></br>","<br />");
299 no.setNote (htmldata);
300 lastBranch->setNoteObj (no);
306 state=stateStack.takeLast();
310 bool parseVYMHandler::characters ( const QString& ch)
312 //cout << "characters \""<<ch.toStdString()<<"\" state="<<state <<" laststate="<<laststate<<endl;
314 QString ch_org=quotemeta (ch);
315 QString ch_simplified=ch.simplifyWhiteSpace();
316 if ( ch_simplified.isEmpty() ) return true;
320 case StateInit: break;
321 case StateMap: break;
323 model->select(ch_simplified);
325 case StateMapSetting:break;
326 case StateMapCenter: break;
328 lastBranch->setNote(ch_simplified);
330 case StateBranch: break;
331 case StateStandardFlag:
332 lastBranch->activateStandardFlag(ch_simplified);
334 case StateImage: break;
335 case StateHtmlNote: break;
340 lastBranch->setHeading(ch_simplified);
348 QString parseVYMHandler::errorString()
350 return "the document is not in the VYM file format";
353 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a) //FIXME-2
355 mainWindow->setProgressValue (branchesCurrent++);
359 if (!readOOAttr(a)) return false;
361 if (!a.value( "scrolled").isEmpty() )
362 lastBranch->toggleScroll();
364 if (!a.value( "frameType").isEmpty() )
365 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
367 if (!a.value( "incImgV").isEmpty() )
369 if (a.value("incImgV")=="true")
370 lastBranch->setIncludeImagesVer(true);
372 lastBranch->setIncludeImagesVer(false);
374 if (!a.value( "incImgH").isEmpty() )
376 if (a.value("incImgH")=="true")
377 lastBranch->setIncludeImagesHor(true);
379 lastBranch->setIncludeImagesHor(false);
385 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a) // FIXME-4 does not work if there is no lmo for treeitem
389 OrnamentedObj* oo=(OrnamentedObj*)(lastMI->getLMO());
395 if (!a.value( "frameType").isEmpty() )
396 oo->setFrameType (a.value("frameType"));
397 if (!a.value( "penColor").isEmpty() )
398 oo->setFramePenColor (a.value("penColor"));
399 if (!a.value( "brushColor").isEmpty() )
400 oo->setFrameBrushColor (a.value("brushColor"));
401 if (!a.value( "padding").isEmpty() )
403 x=a.value("padding").toInt(&ok);
404 if (ok) oo->setFramePadding(x);
406 if (!a.value( "borderWidth").isEmpty() )
408 x=a.value("borderWidth").toInt(&ok);
409 if (ok) oo->setFrameBorderWidth(x);
418 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
420 if (lastMI) //FIXME-1 below lastBranch is used, not lastMI. So it might not work for images
424 if (!a.value( "relPosX").isEmpty() )
426 if (!a.value( "relPosY").isEmpty() )
428 x=a.value("relPosX").toFloat (&okx);
429 y=a.value("relPosY").toFloat (&oky);
431 lastBranch->setRelPos (QPointF(x,y));
433 return false; // Couldn't read relPos
436 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap )
438 if (!a.value( "absPosY").isEmpty() )
440 x=a.value("absPosX").toFloat (&okx);
441 y=a.value("absPosY").toFloat (&oky);
443 lastBranch->setAbsPos (QPointF(x,y));
445 return false; // Couldn't read absPos
448 //if (!a.value( "id").isEmpty() )
449 // lastBranch->setID (a.value ("id"));
451 if (!a.value( "url").isEmpty() )
452 lastBranch->setURL (a.value ("url"));
453 if (!a.value( "vymLink").isEmpty() )
454 lastBranch->setVymLink (a.value ("vymLink"));
455 if (!a.value( "hideInExport").isEmpty() )
456 if (a.value("hideInExport")=="true")
457 lastBranch->setHideInExport(true);
460 if (!a.value( "hideLink").isEmpty())
462 if (a.value ("hideLink") =="true")
463 lastOO->setHideLinkUnselected(true);
465 lastOO->setHideLinkUnselected(false);
472 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
473 { // only for backward compatibility (<1.4.6). Use htmlnote now.
476 if (!a.value( "href").isEmpty() )
479 fn=parseHREF(a.value ("href") );
481 QString s; // Reading a note
483 if ( !file.open( QIODevice::ReadOnly) )
485 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
488 QTextStream stream( &file );
490 while ( !stream.atEnd() ) {
491 lines += stream.readLine()+"\n";
495 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
498 if (!a.value( "fonthint").isEmpty() )
499 no.setFontHint(a.value ("fonthint") );
500 lastBranch->setNoteObj(no);
504 bool parseVYMHandler::readImageAttr (const QXmlAttributes& a)
508 //if (!readOOAttr(a)) return false;
510 /* FIXME-1 if (!a.value( "useOrientation").isEmpty() )
512 if (a.value ("useOrientation") =="true")
513 lastImage->setUseOrientation (true);
515 lastImage->setUseOrientation (false);
518 if (!a.value( "href").isEmpty() )
521 if (!lastImage->load (parseHREF(a.value ("href") ) ))
523 QMessageBox::warning( 0, "Warning: " ,
524 "Couldn't load image\n"+parseHREF(a.value ("href") ));
530 if (!a.value( "zPlane").isEmpty() )
531 lastImage->setZValue (a.value("zPlane").toInt ());
534 if (!a.value( "relPosX").isEmpty() )
536 if (!a.value( "relPosY").isEmpty() )
539 x=a.value("relPosX").toFloat (&okx);
540 y=a.value("relPosY").toFloat (&oky);
542 lastImage->setRelPos (QPointF (x,y) );
544 // Couldn't read relPos
549 //FIXME-3 if (!readOOAttr(a)) return false;
551 if (!a.value ("originalName").isEmpty() )
553 lastImage->setOriginalFilename (a.value("originalName"));
558 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
563 XLinkObj *xlo=new XLinkObj (model->getScene());
564 if (!a.value( "color").isEmpty() )
566 col.setNamedColor(a.value("color"));
570 if (!a.value( "width").isEmpty() )
572 xlo->setWidth(a.value ("width").toInt (&okx, 10));
575 // Connecting by select string for compatibility with version < 1.8.76
576 if (!a.value( "beginBranch").isEmpty() )
578 if (!a.value( "endBranch").isEmpty() )
580 TreeItem *ti=model->findBySelectString (a.value( "beginBranch"));
581 if (ti && ti->isBranchLikeType())
584 xlo->setBegin ((BranchObj*)lmo);
585 lmo=model->findBySelectString (a.value( "endBranch"));
586 if (lmo && typid (*lmo)==typid (BranchObj))
588 xlo->setEnd ((BranchObj*)(lmo));
597 // object ID is used starting in version 1.8.76
599 if (!a.value( "beginID").isEmpty() )
601 if (!a.value( "endID").isEmpty() )
603 LinkableMapObj *lmo=model->findID (a.value( "beginID"));
604 if (lmo && typid (*lmo)==typid (BranchObj))
606 xlo->setBegin ((BranchObj*)lmo);
607 lmo=model->findID (a.value( "endID"));
608 if (lmo && typid (*lmo)==typid (BranchObj))
610 xlo->setEnd ((BranchObj*)(lmo));
618 if (!success) delete (xlo);
619 return true; // xLinks can only be established at the "end branch", return true
622 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
624 for (int i=1; i<=a.count(); i++)
625 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
629 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
631 if (!a.value( "key").isEmpty() )
633 if (!a.value( "value").isEmpty() )
634 settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));