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
162 BranchItem *cur=NULL;
163 BranchItem *prev=NULL;
168 cur=model->nextBranch (cur,prev);
171 if (cur->getType()==TreeItem::Branch || cur->getType()==TreeItem::MapCenter)
175 for (i=0;i<cur->depth()-1;i++) curIndent+= indentPerDepth;
177 if (!cur->hasHiddenExportParent() )
179 col=cur->getHeadingColor();
180 if (col==QColor (255,0,0))
182 else if (col==QColor (217,81,0))
184 else if (col==QColor (0,85,0))
190 switch (cur->depth())
193 //ts << underline (cur->getHeading(),QString("="));
198 //ts << (underline ( cur->getHeading(), QString("-") ) );
201 case 2: // Main heading
203 ts << underline ( cur->getHeading(), QString("=") );
206 case 3: // Achievement, Bonus, Objective ...
208 ts << underline ( cur->getHeading(), "-");
211 case 4: // That's the item we need to know
212 //ts << (curIndent + "* " + colString+" "+ cur->getHeading());
213 ts << colString+" "+ cur->getHeading();
214 if (cur->isActiveStandardFlag ("hook-green"))
216 else if (cur->isActiveStandardFlag ("wip"))
218 else if (cur->isActiveStandardFlag ("cross-red"))
219 ts << " [NOT STARTED] ";
223 ts << (curIndent + "- " + cur->getHeading());
229 // If necessary, write URL
230 if (!cur->getURL().isEmpty())
231 ts << (curIndent + dashIndent + cur->getURL()) +"\n";
233 // If necessary, write note
234 if (!cur->getNoteObj().isEmpty())
237 s=cur->getNoteASCII( curIndent, 80);
242 cur=model->nextBranch(cur,prev);
247 QString ExportAO::underline (const QString &text, const QString &line)
249 QString r=text + "\n";
250 for (int j=0;j<text.length();j++) r+=line;
255 ////////////////////////////////////////////////////////////////////////
256 ExportASCII::ExportASCII()
258 filter="TXT (*.txt)";
259 caption=vymName+ " -" +QObject::tr("Export as ASCII")+" "+QObject::tr("(still experimental)");
262 void ExportASCII::doExport()
264 QFile file (outputFile);
265 if ( !file.open( QIODevice::WriteOnly ) )
267 qWarning ("ExportASCII::doExport couldn't open "+outputFile);
270 QTextStream ts( &file ); // use LANG decoding here...
272 // Main loop over all branches
277 BranchItem *cur=NULL;
278 BranchItem *prev=NULL;
280 cur=model->nextBranch (cur,prev);
283 if (cur->getType()==TreeItem::Branch || cur->getType()==TreeItem::MapCenter)
287 for (i=1;i<cur->depth()-1;i++) curIndent+= indentPerDepth;
289 if (!cur->hasHiddenExportParent() )
291 //std::cout << "ExportASCII:: "<<curIndent.toStdString()<<cur->getHeading().toStdString()<<std::endl;
294 switch (cur->depth())
297 ts << underline (cur->getHeading(),QString("="));
302 ts << (underline (getSectionString(cur) + cur->getHeading(), QString("-") ) );
307 ts << (curIndent + "* " + cur->getHeading());
312 ts << (curIndent + "- " + cur->getHeading());
317 ts << (curIndent + "- " + cur->getHeading());
323 // If necessary, write URL
324 if (!cur->getURL().isEmpty())
325 ts << (curIndent + dashIndent + cur->getURL()) +"\n";
327 // If necessary, write note
328 if (!cur->getNoteObj().isEmpty())
331 s=cur->getNoteASCII( curIndent, 80);
336 cur=model->nextBranch(cur,prev);
341 QString ExportASCII::underline (const QString &text, const QString &line)
343 QString r=text + "\n";
344 for (int j=0;j<text.length();j++) r+=line;
349 ////////////////////////////////////////////////////////////////////////
350 void ExportCSV::doExport()
352 QFile file (outputFile);
353 if ( !file.open( QIODevice::WriteOnly ) )
355 qWarning ("ExportBase::exportXML couldn't open "+outputFile);
358 QTextStream ts( &file ); // use LANG decoding here...
361 ts << "\"Note\"" <<endl;
363 // Main loop over all branches
365 QString curIndent("");
367 BranchItem *cur=NULL;
368 BranchItem *prev=NULL;
369 cur=model->nextBranch (cur,prev);
372 if (!cur->hasHiddenExportParent() )
374 // If necessary, write note
375 if (!cur->getNoteObj().isEmpty())
377 s =cur->getNoteASCII();
378 s=s.replace ("\n","\n"+curIndent);
379 ts << ("\""+s+"\",");
384 for (i=0;i<cur->depth();i++) curIndent+= "\"\",";
387 ts << curIndent << "\"" << cur->getHeading()<<"\""<<endl;
390 cur=model->nextBranch(cur,prev);
396 ////////////////////////////////////////////////////////////////////////
397 void ExportKDE3Bookmarks::doExport()
400 dia.showCancelButton (true);
401 dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE"));
402 dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE 3"));
403 dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
404 if (dia.exec()==QDialog::Accepted)
406 model->exportXML(tmpDir.path(),false);
409 p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
410 p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
411 p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
414 QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
415 QProcess *proc= new QProcess ;
417 if (!proc->waitForStarted())
419 QMessageBox::warning(0,
420 QObject::tr("Warning"),
421 QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
426 ////////////////////////////////////////////////////////////////////////
427 void ExportKDE4Bookmarks::doExport()
430 dia.showCancelButton (true);
431 dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE 4"));
432 dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE"));
433 dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
434 if (dia.exec()==QDialog::Accepted)
436 model->exportXML(tmpDir.path(),false);
439 p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
440 p.setOutputFile (tmpDir.home().path()+"/.kde4/share/apps/konqueror/bookmarks.xml");
441 p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
444 QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
445 QProcess *proc= new QProcess ;
447 if (!proc->waitForStarted())
449 QMessageBox::warning(0,
450 QObject::tr("Warning"),
451 QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
456 ////////////////////////////////////////////////////////////////////////
457 void ExportFirefoxBookmarks::doExport()
460 dia.showCancelButton (true);
461 dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox"));
462 dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox"));
463 dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks");
464 if (dia.exec()==QDialog::Accepted)
466 model->exportXML(tmpDir.path(),false);
470 p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
471 p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
472 p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
475 QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
476 QProcess *proc = new QProcess( );
477 proc->addArgument(ub);
479 if ( !proc->start() )
481 QMessageBox::warning(0,
482 QObject::tr("Warning"),
483 QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
490 ////////////////////////////////////////////////////////////////////////
491 ExportHTML::ExportHTML():ExportBase()
496 ExportHTML::ExportHTML(VymModel *m):ExportBase(m)
501 void ExportHTML::init()
503 singularDelimiter=": ";
506 cssFileName="vym.css";
507 cssOriginalPath=""; // Is set in VymModel, based on default setting in ExportHTMLDialog
509 if (model &&model->getMapEditor())
510 offset=model->getMapEditor()->getTotalBBox().topLeft();
513 QString ExportHTML::getBranchText(BranchItem *current)
519 LinkableMapObj *lmo=current->getLMO();
522 hr=((BranchObj*)lmo)->getBBoxHeading();
523 if (lmo->isVisibleObj()) vis=true;
526 QString id=model->getSelectString(current);
527 if (dia.useTextColor)
528 col=QString("style='color:%1'").arg(current->getHeadingColor().name());
529 QString s=QString("<span class='vym-branch%1' %2 id='%3'>")
530 .arg(current->depth())
533 QString url=current->getURL();
534 QString heading=quotemeta(current->getHeading());
537 s+=QString ("<a href=\"%1\">").arg(url);
538 s+=QString ("<img src=\"flags/flag-url-16x16.png\">%1</a>").arg(heading);
541 QRectF fbox=current->getBBoxURLFlag ();
543 imageMap+=QString(" <area shape='rect' coords='%1,%2,%3,%4' href='%5'>\n")
544 .arg(fbox.left()-offset.x())
545 .arg(fbox.top()-offset.y())
546 .arg(fbox.right()-offset.x())
547 .arg(fbox.bottom()-offset.y())
550 s+=quotemeta(current->getHeading());
553 if (vis && dia.useImage)
554 imageMap+=QString(" <area shape='rect' coords='%1,%2,%3,%4' href='#%5'>\n")
555 .arg(hr.left()-offset.x())
556 .arg(hr.top()-offset.y())
557 .arg(hr.right()-offset.x())
558 .arg(hr.bottom()-offset.y())
562 if (!current->getNoteObj().isEmpty())
563 s+="<table border=1><tr><td>"+current->getNote()+"</td></tr></table>";
570 QString ExportHTML::buildList (BranchItem *current)
575 BranchItem *bi=current->getFirstBranch();
577 // Only add itemized list, if we have more than one subitem.
578 // For only one subitem, just add a separator to keep page more compact
579 bool noSingularsHere=false;
580 if (current->branchCount()<2 && noSingulars) noSingularsHere=true;
584 if (!noSingularsHere)
587 r+=singularDelimiter;
591 if (!bi->hasHiddenExportParent() )
593 if (!noSingularsHere) r+="<li>";
594 r+=getBranchText (bi);
595 if (!bi->getURL().isEmpty() && frameURLs && noSingularsHere)
596 // Add frame, if we have subitems to an URL
597 r+="<table border=1><tr><td>"+buildList (bi)+"</td></tr></table>"; // recursivly add deeper branches
599 r+=buildList (bi); // recursivly add deeper branches
600 if (!noSingularsHere) r+="</li>";
604 bi=current->getBranchNum(i);
607 if (!noSingularsHere) r+="</ul>\n";
612 void ExportHTML::setCSSPath(const QString &p)
617 void ExportHTML::doExport(bool useDialog)
620 dia.setFilePath (model->getFilePath());
621 dia.setMapName (model->getMapName());
623 if (useDialog && dia.exec()!=QDialog::Accepted) return;
625 // Check if destination is not empty
627 // Check, if warnings should be used before overwriting
628 // the output directory
629 if (d.exists() && d.count()>0)
632 warn.showCancelButton (true);
633 warn.setText(QString(
634 "The directory %1 is not empty.\n"
635 "Do you risk to overwrite some of its contents?").arg(d.path() ));
636 warn.setCaption("Warning: Directory not empty");
637 warn.setShowAgainName("mainwindow/export-XML-overwrite-dir");
639 if (warn.exec()!=QDialog::Accepted)
641 mainWindow->statusMessage(QString(QObject::tr("Export aborted.")));
646 setFile (d.path()+"/"+model->getMapName()+".html");
647 setCSSPath( dia.getCSSPath() ); // FIXME-2 css not copied?
650 QFile css_src (cssOriginalPath);
651 QFile css_dst (outDir.path()+"/"+cssFileName);
652 if (!css_src.open ( QIODevice::ReadOnly))
653 QMessageBox::warning( 0, QObject::tr( "Warning","ExportHTML" ),QObject::tr("Could not open %1","ExportHTML").arg(cssOriginalPath));
656 if (!css_dst.open( QIODevice::WriteOnly))
657 QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr("Could not open %1").arg(css_dst.fileName()));
661 QTextStream tsout( &css_dst);
662 QTextStream tsin ( &css_src);
663 QString s= tsin.read();
672 // Open file for writing
673 QFile file (outputFile);
674 if ( !file.open( QIODevice::WriteOnly ) )
676 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
677 mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
680 QTextStream ts( &file ); // use LANG decoding here...
681 ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
683 // Hide stuff during export
684 model->setExportMode (true);
688 ts<<"<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"> ";
689 ts<<"<title>"+model->getMapName()<<"</title><body>";
690 ts<<" <link rel='stylesheet' id='css.stylesheet' href='"<<cssFileName<<"' />\n";
695 ts<<"<center><img src=\""<<model->getMapName()<<".png\" usemap='#imagemap'></center>\n";
696 model->exportImage (d.path()+"/"+model->getMapName()+".png",false,"PNG");
699 // Main loop over all mapcenters
701 TreeItem *rootItem=model->getRootItem();
703 for (int i=0; i<rootItem->branchCount(); i++)
705 bi=rootItem->getBranchNum(i);
706 if (!bi->hasHiddenExportParent())
708 ts<<getBranchText (bi);
714 ts<<"<map name='imagemap'>\n"+imageMap+"</map>\n";
718 ts<<"<table class=\"vym-footer\"> \n\
720 <td class=\"vym-footerL\">"+model->getFileName()+"</td> \n\
721 <td class=\"vym-footerC\">"+model->getDate()+"</td> \n\
722 <td class=\"vym-footerR\"> vym "+model->getVersion()+"</td> \n\
725 ts<<"</body></html>";
728 if (!dia.postscript.isEmpty())
731 p.runScript (dia.postscript,d.path()+"/"+model->getMapName()+".html");
736 model->setExportMode (false);
739 ////////////////////////////////////////////////////////////////////////
740 void ExportTaskjuggler::doExport()
742 model->exportXML(tmpDir.path(),false);
745 p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
746 p.setOutputFile (outputFile);
747 p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
751 ////////////////////////////////////////////////////////////////////////
752 void ExportLaTeX::doExport()
754 // Exports a map to a LaTex file.
755 // This file needs to be included
756 // or inported into a LaTex document
757 // it will not add a preamble, or anything
758 // that makes a full LaTex document.
759 QFile file (outputFile);
760 if ( !file.open( QIODevice::WriteOnly ) ) {
761 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
762 mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
765 QTextStream ts( &file ); // use LANG decoding here...
766 ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
768 // Main loop over all branches
770 // QString curIndent("");
773 BranchItem *cur=NULL;
774 BranchItem *prev=NULL;
775 model->nextBranch(cur,prev);
778 bo=(BranchObj*)(cur->getLMO());
780 if (!cur->hasHiddenExportParent() )
782 switch (cur->depth() )
786 ts << ("\\chapter{" + cur->getHeading()+ "}\n");
789 ts << ("\\section{" + cur->getHeading()+ "}\n");
792 ts << ("\\subsection{" + cur->getHeading()+ "}\n");
795 ts << ("\\subsubsection{" + cur->getHeading()+ "}\n");
798 ts << ("\\paragraph*{" + cur->getHeading()+ "}\n");
801 // If necessary, write note
802 if (!cur->getNoteObj().isEmpty()) {
803 ts << (cur->getNoteASCII());
807 cur=model->nextBranch(cur,prev);
812 ////////////////////////////////////////////////////////////////////////
818 ExportOO::~ExportOO()
822 QString ExportOO::buildList (TreeItem *current)
827 BranchItem *bi=current->getFirstBranch();
831 r+="<text:list text:style-name=\"vym-list\">\n";
834 if (!bi->hasHiddenExportParent() )
836 r+="<text:list-item><text:p >";
837 r+=quotemeta(bi->getHeading());
838 // If necessary, write note
839 if (!bi->getNoteObj().isEmpty())
840 r+=bi->getNoteOpenDoc();
842 r+=buildList (bi); // recursivly add deeper branches
843 r+="</text:list-item>\n";
846 bi=current->getBranchNum(i);
854 void ExportOO::exportPresentation()
858 BranchItem *firstMCO=(BranchItem*)(model->getRootItem()->getFirstBranch());
861 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("No objects in map!"));
865 // Insert new content
866 // FIXME add extra title in mapinfo for vym 1.13.x
867 content.replace ("<!-- INSERT TITLE -->",quotemeta(firstMCO->getHeading()));
868 content.replace ("<!-- INSERT AUTHOR -->",quotemeta(model->getAuthor()));
873 BranchItem *sectionBI;
878 int mapcenters=model->getRootItem()->branchCount();
880 // useSections already has been set in setConfigFile
884 sectionBI=firstMCO->getFirstBranch();
887 while (sectionBI && !sectionBI->hasHiddenExportParent() )
891 // Add page with section title
892 onePage=sectionTemplate;
893 onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBI->getHeading() ) );
895 pagesBI=sectionBI->getFirstBranch();
898 //i=-2; // only use inner loop to
899 // turn mainbranches into pages
900 //sectionBI=firstMCO;
905 while (pagesBI && !pagesBI->hasHiddenExportParent() )
907 // Add page with list of items
908 onePage=pageTemplate;
909 onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBI->getHeading() ) );
910 list=buildList (pagesBI);
911 onePage.replace ("<!-- INSERT LIST -->", list);
913 if (pagesBI!=sectionBI)
916 pagesBI=((BranchItem*)pagesBI->parent())->getBranchNum(j);
918 pagesBI=NULL; // We are already iterating over the sectionBIs
922 sectionBI=model->getRootItem()->getBranchNum (i);
924 sectionBI=firstMCO->getBranchNum (i);
927 content.replace ("<!-- INSERT PAGES -->",allPages);
929 // Write modified content
930 QFile f (contentFile);
931 if ( !f.open( QIODevice::WriteOnly ) )
933 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
934 mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
942 // zip tmpdir to destination
943 zipDir (tmpDir,outputFile);
946 bool ExportOO::setConfigFile (const QString &cf)
949 int i=cf.findRev ("/");
950 if (i>=0) configDir=cf.left(i);
952 set.readSettings(configFile);
955 templateDir=configDir+"/"+set.readEntry ("Template");
957 QDir d (templateDir);
960 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
965 contentTemplateFile=templateDir+"content-template.xml";
966 contentFile=tmpDir.path()+"/content.xml";
967 pageTemplateFile=templateDir+"page-template.xml";
968 sectionTemplateFile=templateDir+"section-template.xml";
970 if (set.readEntry("useSections").contains("yes"))
973 // Copy template to tmpdir
974 system ("cp -r "+templateDir+"* "+tmpDir.path());
976 // Read content-template
977 if (!loadStringFromDisk (contentTemplateFile,content))
979 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
983 // Read page-template
984 if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
986 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
990 // Read section-template
991 if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
993 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));