3 #include "branchitem.h"
5 #include "linkablemapobj.h"
7 #include "mainwindow.h"
8 #include "warningdialog.h"
12 extern Main *mainWindow;
13 extern QDir vymBaseDir;
14 extern QString vymName;
16 ExportBase::ExportBase()
21 ExportBase::ExportBase(VymModel *m)
27 ExportBase::~ExportBase()
33 void ExportBase::init()
37 tmpDir.setPath (makeTmpDir(ok,"vym-export"));
38 if (!tmpDir.exists() || !ok)
39 QMessageBox::critical( 0, QObject::tr( "Error" ),
40 QObject::tr("Couldn't access temporary directory\n"));
44 void ExportBase::setDir(const QDir &d)
49 void ExportBase::setFile (const QString &p)
54 QString ExportBase::getFile ()
59 void ExportBase::setModel(VymModel *m)
64 void ExportBase::setCaption (const QString &s)
69 void ExportBase::addFilter(const QString &s)
74 bool ExportBase::execDialog()
77 QFileDialog *fd=new QFileDialog( 0, caption);
78 fd->setFilter (filter);
79 fd->setCaption(caption);
80 fd->setMode( QFileDialog::AnyFile );
84 if ( fd->exec() == QDialog::Accepted )
86 if (QFile (fd->selectedFile()).exists() )
88 QMessageBox mb( vymName,
89 QObject::tr("The file %1 exists already.\nDo you want to overwrite it?").arg(fd->selectedFile()),
91 QMessageBox::Yes | QMessageBox::Default,
92 QMessageBox::Cancel | QMessageBox::Escape,
94 mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
95 mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
99 case QMessageBox::Yes:
102 case QMessageBox::Cancel:
108 outputFile=fd->selectedFile();
116 bool ExportBase::canceled()
121 QString ExportBase::getSectionString(TreeItem *start)
123 // Make prefix like "2.5.3" for "bo:2,bo:5,bo:3"
126 int depth=ti->depth();
129 r=QString("%1").arg(1+ti->num(),0,10)+"." + r;
139 ////////////////////////////////////////////////////////////////////////
142 filter="TXT (*.txt)";
143 caption=vymName+ " -" +QObject::tr("Export as ASCII")+" "+QObject::tr("(still experimental)");
146 void ExportAO::doExport()
148 QFile file (outputFile);
149 if ( !file.open( QIODevice::WriteOnly ) )
151 qWarning ("ExportAO::doExport couldn't open "+outputFile);
154 QTextStream ts( &file ); // use LANG decoding here...
156 // Main loop over all branches
160 BranchItem *cur=NULL;
161 BranchItem *prev=NULL;
166 cur=model->nextBranch (cur,prev);
169 if (cur->getType()==TreeItem::Branch || cur->getType()==TreeItem::MapCenter)
173 for (i=0;i<cur->depth()-1;i++) curIndent+= indentPerDepth;
175 if (!cur->hasHiddenExportParent() )
177 col=cur->getHeadingColor();
178 if (col==QColor (255,0,0))
180 else if (col==QColor (217,81,0))
182 else if (col==QColor (0,85,0))
186 switch (cur->depth())
189 //ts << underline (cur->getHeading(),QString("="));
194 //ts << (underline ( cur->getHeading(), QString("-") ) );
197 case 2: // Main heading
199 ts << underline ( cur->getHeading(), QString("=") );
202 case 3: // Achievement, Bonus, Objective ...
204 ts << underline ( cur->getHeading(), "-");
207 case 4: // That's the item we need to know
208 //ts << (curIndent + "* " + colString+" "+ cur->getHeading());
209 ts << colString+" "+ cur->getHeading();
210 if (cur->isActiveStandardFlag ("hook-green"))
212 else if (cur->isActiveStandardFlag ("wip"))
214 else if (cur->isActiveStandardFlag ("cross-red"))
215 ts << " [NOT STARTED] ";
219 ts << (curIndent + "- " + cur->getHeading());
224 // If necessary, write note
225 if (!cur->getNoteObj().isEmpty())
228 s=cur->getNoteASCII( curIndent, 80);
233 cur=model->nextBranch(cur,prev);
238 QString ExportAO::underline (const QString &text, const QString &line)
240 QString r=text + "\n";
241 for (int j=0;j<text.length();j++) r+=line;
246 ////////////////////////////////////////////////////////////////////////
247 ExportASCII::ExportASCII()
249 filter="TXT (*.txt)";
250 caption=vymName+ " -" +QObject::tr("Export as ASCII")+" "+QObject::tr("(still experimental)");
253 void ExportASCII::doExport()
255 QFile file (outputFile);
256 if ( !file.open( QIODevice::WriteOnly ) )
258 qWarning ("ExportASCII::doExport couldn't open "+outputFile);
261 QTextStream ts( &file ); // use LANG decoding here...
263 // Main loop over all branches
267 BranchItem *cur=NULL;
268 BranchItem *prev=NULL;
270 cur=model->nextBranch (cur,prev);
273 if (cur->getType()==TreeItem::Branch || cur->getType()==TreeItem::MapCenter)
277 for (i=1;i<cur->depth()-1;i++) curIndent+= indentPerDepth;
279 if (!cur->hasHiddenExportParent() )
281 //std::cout << "ExportASCII:: "<<curIndent.toStdString()<<cur->getHeading().toStdString()<<std::endl;
282 switch (cur->depth())
285 ts << underline (cur->getHeading(),QString("="));
290 ts << (underline (getSectionString(cur) + cur->getHeading(), QString("-") ) );
295 ts << (curIndent + "* " + cur->getHeading());
299 ts << (curIndent + "- " + cur->getHeading());
303 ts << (curIndent + "- " + cur->getHeading());
308 // If necessary, write note
309 if (!cur->getNoteObj().isEmpty())
312 s=cur->getNoteASCII( curIndent, 80);
317 cur=model->nextBranch(cur,prev);
322 QString ExportASCII::underline (const QString &text, const QString &line)
324 QString r=text + "\n";
325 for (int j=0;j<text.length();j++) r+=line;
330 ////////////////////////////////////////////////////////////////////////
331 void ExportCSV::doExport()
333 QFile file (outputFile);
334 if ( !file.open( QIODevice::WriteOnly ) )
336 qWarning ("ExportBase::exportXML couldn't open "+outputFile);
339 QTextStream ts( &file ); // use LANG decoding here...
342 ts << "\"Note\"" <<endl;
344 // Main loop over all branches
346 QString curIndent("");
348 BranchItem *cur=NULL;
349 BranchItem *prev=NULL;
350 cur=model->nextBranch (cur,prev);
353 if (!cur->hasHiddenExportParent() )
355 // If necessary, write note
356 if (!cur->getNoteObj().isEmpty())
358 s =cur->getNoteASCII();
359 s=s.replace ("\n","\n"+curIndent);
360 ts << ("\""+s+"\",");
365 for (i=0;i<cur->depth();i++) curIndent+= "\"\",";
368 ts << curIndent << "\"" << cur->getHeading()<<"\""<<endl;
371 cur=model->nextBranch(cur,prev);
377 ////////////////////////////////////////////////////////////////////////
378 void ExportKDE3Bookmarks::doExport()
381 dia.showCancelButton (true);
382 dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE"));
383 dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE 3"));
384 dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
385 if (dia.exec()==QDialog::Accepted)
387 model->exportXML(tmpDir.path(),false);
390 p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
391 p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
392 p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
395 QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
396 QProcess *proc= new QProcess ;
398 if (!proc->waitForStarted())
400 QMessageBox::warning(0,
401 QObject::tr("Warning"),
402 QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
407 ////////////////////////////////////////////////////////////////////////
408 void ExportKDE4Bookmarks::doExport()
411 dia.showCancelButton (true);
412 dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE 4"));
413 dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE"));
414 dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
415 if (dia.exec()==QDialog::Accepted)
417 model->exportXML(tmpDir.path(),false);
420 p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
421 p.setOutputFile (tmpDir.home().path()+"/.kde4/share/apps/konqueror/bookmarks.xml");
422 p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
425 QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
426 QProcess *proc= new QProcess ;
428 if (!proc->waitForStarted())
430 QMessageBox::warning(0,
431 QObject::tr("Warning"),
432 QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
437 ////////////////////////////////////////////////////////////////////////
438 void ExportFirefoxBookmarks::doExport()
441 dia.showCancelButton (true);
442 dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox"));
443 dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox"));
444 dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks");
445 if (dia.exec()==QDialog::Accepted)
447 model->exportXML(tmpDir.path(),false);
451 p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
452 p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
453 p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
456 QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
457 QProcess *proc = new QProcess( );
458 proc->addArgument(ub);
460 if ( !proc->start() )
462 QMessageBox::warning(0,
463 QObject::tr("Warning"),
464 QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
471 ////////////////////////////////////////////////////////////////////////
472 ExportHTML::ExportHTML():ExportBase()
477 ExportHTML::ExportHTML(VymModel *m):ExportBase(m)
482 void ExportHTML::init()
484 singularDelimiter=": ";
487 cssFileName="vym.css";
488 cssOriginalPath=""; // Is set in VymModel, based on default setting in ExportHTMLDialog
490 if (model &&model->getMapEditor())
491 offset=model->getMapEditor()->getTotalBBox().topLeft();
494 QString ExportHTML::getBranchText(BranchItem *current)
500 LinkableMapObj *lmo=current->getLMO();
503 hr=((BranchObj*)lmo)->getBBoxHeading();
504 if (lmo->isVisibleObj()) vis=true;
507 QString id=model->getSelectString(current);
508 if (dia.useTextColor)
509 col=QString("style='color:%1'").arg(current->getHeadingColor().name());
510 QString s=QString("<span class='vym-branch%1' %2 id='%3'>")
511 .arg(current->depth())
514 QString url=current->getURL();
517 s+=QString ("<a href=\"%1\">").arg(url);
518 s+=QString ("<img src=\"flags/flag-url-16x16.png\">%1</a>").arg(quotemeta(current->getHeading()));
521 QRectF fbox=current->getBBoxURLFlag ();
523 imageMap+=QString(" <area shape='rect' coords='%1,%2,%3,%4' href='%5'>\n")
524 .arg(fbox.left()-offset.x())
525 .arg(fbox.top()-offset.y())
526 .arg(fbox.right()-offset.x())
527 .arg(fbox.bottom()-offset.y())
530 s+=quotemeta(current->getHeading());
533 if (vis && dia.useImage)
534 imageMap+=QString(" <area shape='rect' coords='%1,%2,%3,%4' href='#%5'>\n")
535 .arg(hr.left()-offset.x())
536 .arg(hr.top()-offset.y())
537 .arg(hr.right()-offset.x())
538 .arg(hr.bottom()-offset.y())
542 if (!current->getNoteObj().isEmpty())
543 s+="<table border=1><tr><td>"+current->getNote()+"</td></tr></table>";
550 QString ExportHTML::buildList (BranchItem *current)
555 BranchItem *bi=current->getFirstBranch();
557 // Only add itemized list, if we have more than one subitem.
558 // For only one subitem, just add a separator to keep page more compact
559 bool noSingularsHere=false;
560 if (current->branchCount()<2 && noSingulars) noSingularsHere=true;
564 if (!noSingularsHere)
567 r+=singularDelimiter;
571 if (!bi->hasHiddenExportParent() )
573 if (!noSingularsHere) r+="<li>";
574 r+=getBranchText (bi);
575 if (!bi->getURL().isEmpty() && frameURLs && noSingularsHere)
576 // Add frame, if we have subitems to an URL
577 r+="<table border=1><tr><td>"+buildList (bi)+"</td></tr></table>"; // recursivly add deeper branches
579 r+=buildList (bi); // recursivly add deeper branches
580 if (!noSingularsHere) r+="</li>";
584 bi=current->getBranchNum(i);
587 if (!noSingularsHere) r+="</ul>\n";
592 void ExportHTML::setCSSPath(const QString &p)
597 void ExportHTML::doExport(bool useDialog)
600 dia.setFilePath (model->getFilePath());
601 dia.setMapName (model->getMapName());
603 if (useDialog && dia.exec()!=QDialog::Accepted) return;
605 // Check if destination is not empty
607 // Check, if warnings should be used before overwriting
608 // the output directory
609 if (d.exists() && d.count()>0)
612 warn.showCancelButton (true);
613 warn.setText(QString(
614 "The directory %1 is not empty.\n"
615 "Do you risk to overwrite some of its contents?").arg(d.path() ));
616 warn.setCaption("Warning: Directory not empty");
617 warn.setShowAgainName("mainwindow/export-XML-overwrite-dir");
619 if (warn.exec()!=QDialog::Accepted)
621 mainWindow->statusMessage(QString(QObject::tr("Export aborted.")));
626 setFile (d.path()+"/"+model->getMapName()+".html");
627 setCSSPath( dia.getCSSPath() );
630 QFile css_src (cssOriginalPath);
631 QFile css_dst (outDir.path()+"/"+cssFileName);
632 if (!css_src.open ( QIODevice::ReadOnly))
633 QMessageBox::warning( 0, QObject::tr( "Warning","ExportHTML" ),QObject::tr("Could not open %1","ExportHTML").arg(cssOriginalPath));
636 if (!css_dst.open( QIODevice::WriteOnly))
637 QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr("Could not open %1").arg(css_dst.fileName()));
641 QTextStream tsout( &css_dst);
642 QTextStream tsin ( &css_src);
643 QString s= tsin.read();
652 // Open file for writing
653 QFile file (outputFile);
654 if ( !file.open( QIODevice::WriteOnly ) )
656 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
657 mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
660 QTextStream ts( &file ); // use LANG decoding here...
661 ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
663 // Hide stuff during export
664 model->setExportMode (true);
667 ts<<"<html><title>"+model->getMapName()<<"</title><body>";
668 ts<<" <link rel='stylesheet' id='css.stylesheet' href='"<<cssFileName<<"' />\n";
673 ts<<"<center><img src=\""<<model->getMapName()<<".png\" usemap='#imagemap'></center>\n";
674 model->exportImage (d.path()+"/"+model->getMapName()+".png",false,"PNG");
677 // Main loop over all mapcenters
679 TreeItem *rootItem=model->getRootItem();
681 for (int i=0; i<rootItem->branchCount(); i++)
683 bi=rootItem->getBranchNum(i);
684 if (!bi->hasHiddenExportParent())
686 ts<<getBranchText (bi);
692 ts<<"<map name='imagemap'>\n"+imageMap+"</map>\n";
696 ts<<"<table class=\"vym-footer\"> \n\
698 <td class=\"vym-footerL\">"+model->getFileName()+"</td> \n\
699 <td class=\"vym-footerC\">"+model->getDate()+"</td> \n\
700 <td class=\"vym-footerR\"> vym "+model->getVersion()+"</td> \n\
703 ts<<"</body></html>";
706 if (!dia.postscript.isEmpty())
709 p.runScript (dia.postscript,d.path()+"/"+model->getMapName()+".html");
714 model->setExportMode (true);
717 ////////////////////////////////////////////////////////////////////////
718 void ExportTaskjuggler::doExport()
720 model->exportXML(tmpDir.path(),false);
723 p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
724 p.setOutputFile (outputFile);
725 p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
729 ////////////////////////////////////////////////////////////////////////
730 void ExportLaTeX::doExport()
732 // Exports a map to a LaTex file.
733 // This file needs to be included
734 // or inported into a LaTex document
735 // it will not add a preamble, or anything
736 // that makes a full LaTex document.
737 QFile file (outputFile);
738 if ( !file.open( QIODevice::WriteOnly ) ) {
739 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
740 mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
743 QTextStream ts( &file ); // use LANG decoding here...
744 ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
746 // Main loop over all branches
748 // QString curIndent("");
751 BranchItem *cur=NULL;
752 BranchItem *prev=NULL;
753 model->nextBranch(cur,prev);
756 bo=(BranchObj*)(cur->getLMO());
758 if (!cur->hasHiddenExportParent() )
760 switch (cur->depth() )
764 ts << ("\\chapter{" + cur->getHeading()+ "}\n");
767 ts << ("\\section{" + cur->getHeading()+ "}\n");
770 ts << ("\\subsection{" + cur->getHeading()+ "}\n");
773 ts << ("\\subsubsection{" + cur->getHeading()+ "}\n");
776 ts << ("\\paragraph*{" + cur->getHeading()+ "}\n");
779 // If necessary, write note
780 if (!cur->getNoteObj().isEmpty()) {
781 ts << (cur->getNoteASCII());
785 cur=model->nextBranch(cur,prev);
790 ////////////////////////////////////////////////////////////////////////
796 ExportOO::~ExportOO()
800 QString ExportOO::buildList (TreeItem *current)
805 BranchItem *bi=current->getFirstBranch();
809 r+="<text:list text:style-name=\"vym-list\">\n";
812 if (!bi->hasHiddenExportParent() )
814 r+="<text:list-item><text:p >";
815 r+=quotemeta(bi->getHeading());
816 // If necessary, write note
817 if (!bi->getNoteObj().isEmpty())
818 r+=bi->getNoteOpenDoc();
820 r+=buildList (bi); // recursivly add deeper branches
821 r+="</text:list-item>\n";
824 bi=current->getBranchNum(i);
832 void ExportOO::exportPresentation()
836 BranchItem *firstMCO=(BranchItem*)(model->getRootItem()->getFirstBranch());
839 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("No objects in map!"));
843 // Insert new content
844 // FIXME add extra title in mapinfo for vym 1.13.x
845 content.replace ("<!-- INSERT TITLE -->",quotemeta(firstMCO->getHeading()));
846 content.replace ("<!-- INSERT AUTHOR -->",quotemeta(model->getAuthor()));
851 BranchItem *sectionBI;
856 int mapcenters=model->getRootItem()->branchCount();
858 // useSections already has been set in setConfigFile
862 sectionBI=firstMCO->getFirstBranch();
865 while (sectionBI && !sectionBI->hasHiddenExportParent() )
869 // Add page with section title
870 onePage=sectionTemplate;
871 onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBI->getHeading() ) );
873 pagesBI=sectionBI->getFirstBranch();
876 //i=-2; // only use inner loop to
877 // turn mainbranches into pages
878 //sectionBI=firstMCO;
883 while (pagesBI && !pagesBI->hasHiddenExportParent() )
885 // Add page with list of items
886 onePage=pageTemplate;
887 onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBI->getHeading() ) );
888 list=buildList (pagesBI);
889 onePage.replace ("<!-- INSERT LIST -->", list);
891 if (pagesBI!=sectionBI)
894 pagesBI=((BranchItem*)pagesBI->parent())->getBranchNum(j);
896 pagesBI=NULL; // We are already iterating over the sectionBIs
900 sectionBI=model->getRootItem()->getBranchNum (i);
902 sectionBI=firstMCO->getBranchNum (i);
905 content.replace ("<!-- INSERT PAGES -->",allPages);
907 // Write modified content
908 QFile f (contentFile);
909 if ( !f.open( QIODevice::WriteOnly ) )
911 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
912 mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
920 // zip tmpdir to destination
921 zipDir (tmpDir,outputFile);
924 bool ExportOO::setConfigFile (const QString &cf)
927 int i=cf.findRev ("/");
928 if (i>=0) configDir=cf.left(i);
930 set.readSettings(configFile);
933 templateDir=configDir+"/"+set.readEntry ("Template");
935 QDir d (templateDir);
938 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
943 contentTemplateFile=templateDir+"content-template.xml";
944 contentFile=tmpDir.path()+"/content.xml";
945 pageTemplateFile=templateDir+"page-template.xml";
946 sectionTemplateFile=templateDir+"section-template.xml";
948 if (set.readEntry("useSections").contains("yes"))
951 // Copy template to tmpdir
952 system ("cp -r "+templateDir+"* "+tmpDir.path());
954 // Read content-template
955 if (!loadStringFromDisk (contentTemplateFile,content))
957 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
961 // Read page-template
962 if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
964 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
968 // Read section-template
969 if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
971 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));