Added dialog for HTML export. Grouping in Switchboard shortcuts
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=": ";
488 cssFileName="vym.css";
489 cssOriginalPath=""; // Is set in VymModel, based on default setting in ExportHTMLDialog
491 if (model &&model->getMapEditor())
492 offset=model->getMapEditor()->getTotalBBox().topLeft();
495 QString ExportHTML::getBranchText(BranchItem *current)
501 LinkableMapObj *lmo=current->getLMO();
504 hr=((BranchObj*)lmo)->getBBoxHeading();
505 if (lmo->isVisibleObj()) vis=true;
508 QString id=model->getSelectString(current);
510 col=QString("style='color:%1'").arg(current->getHeadingColor().name());
511 QString s=QString("<span class='vym-branch%1' %2 id='%3'>")
512 .arg(current->depth())
515 QString url=current->getURL();
518 s+=QString ("<a href=\"%1\">").arg(url);
519 s+=QString ("<img src=\"flags/flag-url-16x16.png\">%1</a>").arg(quotemeta(current->getHeading()));
522 QRectF fbox=current->getBBoxURLFlag ();
524 imageMap+=QString(" <area shape='rect' coords='%1,%2,%3,%4' href='%5'>\n")
525 .arg(fbox.left()-offset.x())
526 .arg(fbox.top()-offset.y())
527 .arg(fbox.right()-offset.x())
528 .arg(fbox.bottom()-offset.y())
531 s+=quotemeta(current->getHeading());
535 imageMap+=QString(" <area shape='rect' coords='%1,%2,%3,%4' href='#%5'>\n")
536 .arg(hr.left()-offset.x())
537 .arg(hr.top()-offset.y())
538 .arg(hr.right()-offset.x())
539 .arg(hr.bottom()-offset.y())
543 if (!current->getNoteObj().isEmpty())
544 s+="<table border=1><tr><td>"+current->getNote()+"</td></tr></table>";
551 QString ExportHTML::buildList (BranchItem *current)
556 BranchItem *bi=current->getFirstBranch();
558 // Only add itemized list, if we have more than one subitem.
559 // For only one subitem, just add a separator to keep page more compact
560 bool noSingularsHere=false;
561 if (current->branchCount()<2 && noSingulars) noSingularsHere=true;
565 if (!noSingularsHere)
568 r+=singularDelimiter;
572 if (!bi->hasHiddenExportParent() )
574 if (!noSingularsHere) r+="<li>";
575 r+=getBranchText (bi);
576 if (!bi->getURL().isEmpty() && frameURLs && noSingularsHere)
577 // Add frame, if we have subitems to an URL
578 r+="<table border=1><tr><td>"+buildList (bi)+"</td></tr></table>"; // recursivly add deeper branches
580 r+=buildList (bi); // recursivly add deeper branches
581 if (!noSingularsHere) r+="</li>";
585 bi=current->getBranchNum(i);
588 if (!noSingularsHere) r+="</ul>\n";
593 void ExportHTML::setCSSPath(const QString &p)
598 void ExportHTML::doExport()
600 //FIXME-1 check for errors// Copy CSS file
601 QFile css_src (cssOriginalPath);
602 QFile css_dst (outDir.path()+"/"+cssFileName);
603 if (!css_src.open ( QIODevice::ReadOnly))
604 QMessageBox::warning( 0, QObject::tr( "Warning","ExportHTML" ),QObject::tr("Could not open %1","ExportHTML").arg(cssOriginalPath));
607 if (!css_dst.open( QIODevice::WriteOnly))
608 QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr("Could not open %1").arg(css_dst.fileName()));
612 QTextStream tsout( &css_dst);
613 QTextStream tsin ( &css_src);
614 QString s= tsin.read();
623 // Open file for writing
624 QFile file (outputFile);
625 if ( !file.open( QIODevice::WriteOnly ) )
627 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
628 mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
631 QTextStream ts( &file ); // use LANG decoding here...
632 ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
635 ts<<"<html><title>"+model->getMapName()<<"</title><body>";
636 ts<<" <link rel='stylesheet' id='css.stylesheet' href='"<<cssFileName<<"' />\n";
639 ts<<"<center><img src=\""<<model->getMapName()<<".png\" usemap='#imagemap'></center>\n";
642 // Main loop over all mapcenters
644 TreeItem *rootItem=model->getRootItem();
646 for (int i=0; i<rootItem->branchCount(); i++)
648 bi=rootItem->getBranchNum(i);
649 if (!bi->hasHiddenExportParent())
651 ts<<getBranchText (bi);
657 ts<<"<map name='imagemap'>\n"+imageMap+"</map>\n";
661 ts<<"<table class=\"vym-footer\"> \n\
663 <td class=\"vym-footerL\">"+model->getFileName()+"</td> \n\
664 <td class=\"vym-footerC\">"+model->getDate()+"</td> \n\
665 <td class=\"vym-footerR\"> vym "+model->getVersion()+"</td> \n\
668 ts<<"</body></html>";
672 ////////////////////////////////////////////////////////////////////////
673 void ExportTaskjuggler::doExport()
675 model->exportXML(tmpDir.path(),false);
678 p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
679 p.setOutputFile (outputFile);
680 p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
684 ////////////////////////////////////////////////////////////////////////
685 void ExportLaTeX::doExport()
687 // Exports a map to a LaTex file.
688 // This file needs to be included
689 // or inported into a LaTex document
690 // it will not add a preamble, or anything
691 // that makes a full LaTex document.
692 QFile file (outputFile);
693 if ( !file.open( QIODevice::WriteOnly ) ) {
694 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
695 mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
698 QTextStream ts( &file ); // use LANG decoding here...
699 ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
701 // Main loop over all branches
703 // QString curIndent("");
706 BranchItem *cur=NULL;
707 BranchItem *prev=NULL;
708 model->nextBranch(cur,prev);
711 bo=(BranchObj*)(cur->getLMO());
713 if (!cur->hasHiddenExportParent() )
715 switch (cur->depth() )
719 ts << ("\\chapter{" + cur->getHeading()+ "}\n");
722 ts << ("\\section{" + cur->getHeading()+ "}\n");
725 ts << ("\\subsection{" + cur->getHeading()+ "}\n");
728 ts << ("\\subsubsection{" + cur->getHeading()+ "}\n");
731 ts << ("\\paragraph*{" + cur->getHeading()+ "}\n");
734 // If necessary, write note
735 if (!cur->getNoteObj().isEmpty()) {
736 ts << (cur->getNoteASCII());
740 cur=model->nextBranch(cur,prev);
745 ////////////////////////////////////////////////////////////////////////
751 ExportOO::~ExportOO()
755 QString ExportOO::buildList (TreeItem *current)
760 BranchItem *bi=current->getFirstBranch();
764 r+="<text:list text:style-name=\"vym-list\">\n";
767 if (!bi->hasHiddenExportParent() )
769 r+="<text:list-item><text:p >";
770 r+=quotemeta(bi->getHeading());
771 // If necessary, write note
772 if (!bi->getNoteObj().isEmpty())
773 r+=bi->getNoteOpenDoc();
775 r+=buildList (bi); // recursivly add deeper branches
776 r+="</text:list-item>\n";
779 bi=current->getBranchNum(i);
787 void ExportOO::exportPresentation()
791 BranchItem *firstMCO=(BranchItem*)(model->getRootItem()->getFirstBranch());
794 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("No objects in map!"));
798 // Insert new content
799 // FIXME add extra title in mapinfo for vym 1.13.x
800 content.replace ("<!-- INSERT TITLE -->",quotemeta(firstMCO->getHeading()));
801 content.replace ("<!-- INSERT AUTHOR -->",quotemeta(model->getAuthor()));
806 BranchItem *sectionBI;
811 int mapcenters=model->getRootItem()->branchCount();
813 // useSections already has been set in setConfigFile
817 sectionBI=firstMCO->getFirstBranch();
820 while (sectionBI && !sectionBI->hasHiddenExportParent() )
824 // Add page with section title
825 onePage=sectionTemplate;
826 onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBI->getHeading() ) );
828 pagesBI=sectionBI->getFirstBranch();
831 //i=-2; // only use inner loop to
832 // turn mainbranches into pages
833 //sectionBI=firstMCO;
838 while (pagesBI && !pagesBI->hasHiddenExportParent() )
840 // Add page with list of items
841 onePage=pageTemplate;
842 onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBI->getHeading() ) );
843 list=buildList (pagesBI);
844 onePage.replace ("<!-- INSERT LIST -->", list);
846 if (pagesBI!=sectionBI)
849 pagesBI=((BranchItem*)pagesBI->parent())->getBranchNum(j);
851 pagesBI=NULL; // We are already iterating over the sectionBIs
855 sectionBI=model->getRootItem()->getBranchNum (i);
857 sectionBI=firstMCO->getBranchNum (i);
860 content.replace ("<!-- INSERT PAGES -->",allPages);
862 // Write modified content
863 QFile f (contentFile);
864 if ( !f.open( QIODevice::WriteOnly ) )
866 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
867 mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
875 // zip tmpdir to destination
876 zipDir (tmpDir,outputFile);
879 bool ExportOO::setConfigFile (const QString &cf)
882 int i=cf.findRev ("/");
883 if (i>=0) configDir=cf.left(i);
885 set.readSettings(configFile);
888 templateDir=configDir+"/"+set.readEntry ("Template");
890 QDir d (templateDir);
893 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
898 contentTemplateFile=templateDir+"content-template.xml";
899 contentFile=tmpDir.path()+"/content.xml";
900 pageTemplateFile=templateDir+"page-template.xml";
901 sectionTemplateFile=templateDir+"section-template.xml";
903 if (set.readEntry("useSections").contains("yes"))
906 // Copy template to tmpdir
907 system ("cp -r "+templateDir+"* "+tmpDir.path());
909 // Read content-template
910 if (!loadStringFromDisk (contentTemplateFile,content))
912 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
916 // Read page-template
917 if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
919 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
923 // Read section-template
924 if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
926 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));