insilmaril@194: #include insilmaril@194: insilmaril@0: #include "exports.h" insilmaril@171: #include "file.h" insilmaril@171: #include "linkablemapobj.h" insilmaril@171: #include "misc.h" insilmaril@171: #include "texteditor.h" insilmaril@171: #include "mainwindow.h" insilmaril@0: insilmaril@171: extern Main *mainWindow; insilmaril@131: insilmaril@171: ExportBase::ExportBase() insilmaril@0: { insilmaril@0: indentPerDepth=" "; insilmaril@0: } insilmaril@0: insilmaril@171: void ExportBase::setPath (const QString &p) insilmaril@0: { insilmaril@0: filepath=p; insilmaril@0: } insilmaril@0: insilmaril@171: void ExportBase::setMapCenter(MapCenterObj *mc) insilmaril@0: { insilmaril@0: mapCenter=mc; insilmaril@0: } insilmaril@0: insilmaril@171: QString ExportBase::getSectionString(BranchObj *bostart) insilmaril@163: { insilmaril@163: QString r; insilmaril@163: BranchObj *bo=bostart; insilmaril@163: int depth=bo->getDepth(); insilmaril@163: while (depth>0) insilmaril@163: { insilmaril@163: r=QString("%1").arg(1+bo->getNum(),0,10)+"." + r; insilmaril@163: bo=(BranchObj*)(bo->getParObj()); insilmaril@163: depth=bo->getDepth(); insilmaril@163: } insilmaril@163: if (r.isEmpty()) insilmaril@163: return r; insilmaril@163: else insilmaril@163: return r + " "; insilmaril@163: } insilmaril@163: insilmaril@171: void ExportBase::exportXML() insilmaril@0: { insilmaril@0: QFile file (filepath); insilmaril@0: if ( !file.open( IO_WriteOnly ) ) insilmaril@0: { insilmaril@131: // FIXME experimental, testing insilmaril@171: cout << "ExportBase::exportXML couldn't open "<first(); insilmaril@0: while (bo) insilmaril@0: { insilmaril@0: // Make indentstring insilmaril@0: for (i=0;igetDepth();i++) actIndent+= indentPerDepth; insilmaril@0: insilmaril@0: // Write heading insilmaril@0: // write (actIndent + getSectionString(bo) + bo->getHeading()+ "\n"); insilmaril@131: if (bo->getDepth()==0) insilmaril@131: { insilmaril@131: ts << (bo->getHeading()+ "\n"); insilmaril@160: for (j=0;jgetHeading().length();j++) ts<<"="; insilmaril@131: ts << "\n"; insilmaril@131: } else if (bo->getDepth()==1) insilmaril@131: ts << ("\n"+getSectionString(bo) + bo->getHeading()+ "\n"); insilmaril@131: else if (bo->getDepth()==2) insilmaril@131: ts << (actIndent + " o " + bo->getHeading()+ "\n"); insilmaril@0: else insilmaril@0: ts << (actIndent + " - " + bo->getHeading()+ "\n"); insilmaril@0: insilmaril@0: // If necessary, write note insilmaril@0: if (!bo->getNote().isEmpty()) insilmaril@0: { insilmaril@183: s =bo->getNoteASCII(); insilmaril@131: s=s.replace ("\n","\n"+actIndent); insilmaril@131: ts << (s+"\n\n"); insilmaril@0: } insilmaril@0: insilmaril@0: bo=bo->next(); insilmaril@0: actIndent=""; insilmaril@0: } insilmaril@0: file.close(); insilmaril@0: } insilmaril@0: insilmaril@171: void ExportLaTeX::exportLaTeX() insilmaril@0: { insilmaril@183: // Exports a map to a LaTex file. insilmaril@183: // This file needs to be included insilmaril@183: // or inported into a LaTex document insilmaril@183: // it will not add a preamble, or anything insilmaril@183: // that makes a full LaTex document. insilmaril@163: QFile file (filepath); insilmaril@163: if ( !file.open( IO_WriteOnly ) ) { insilmaril@163: // FIXME insilmaril@163: cout << "Export::exportMap couldn't open "<first(); insilmaril@163: while (bo) { insilmaril@163: if (bo->getDepth()==0); insilmaril@163: else if (bo->getDepth()==1) { insilmaril@163: ts << ("\\chapter{" + bo->getHeading()+ "}\n"); insilmaril@163: } insilmaril@163: else if (bo->getDepth()==2) { insilmaril@163: ts << ("\\section{" + bo->getHeading()+ "}\n"); insilmaril@163: } insilmaril@163: else if (bo->getDepth()==3) { insilmaril@163: ts << ("\\subsection{" + bo->getHeading()+ "}\n"); insilmaril@163: } insilmaril@163: else if (bo->getDepth()==4) { insilmaril@163: ts << ("\\subsubsection{" + bo->getHeading()+ "}\n"); insilmaril@163: } insilmaril@163: else { insilmaril@163: ts << ("\\paragraph*{" + bo->getHeading()+ "}\n"); insilmaril@163: } insilmaril@163: insilmaril@163: // If necessary, write note insilmaril@163: if (!bo->getNote().isEmpty()) { insilmaril@183: ts << (bo->getNoteASCII()); insilmaril@163: ts << ("\n"); insilmaril@163: } insilmaril@163: insilmaril@163: bo=bo->next(); insilmaril@163: } insilmaril@163: file.close(); insilmaril@0: } insilmaril@0: insilmaril@171: ExportOO::ExportOO() insilmaril@171: { insilmaril@194: // Create tmpdir insilmaril@171: tmpDir.setPath (makeUniqueDir("/tmp/vym-XXXXXX")); insilmaril@171: } insilmaril@163: insilmaril@171: ExportOO::~ExportOO() insilmaril@171: { insilmaril@171: // Remove tmpdir insilmaril@183: removeDir (tmpDir); insilmaril@171: } insilmaril@163: insilmaril@171: QString ExportOO::buildList (BranchObj *current) insilmaril@171: { insilmaril@171: QString r; insilmaril@171: BranchObj *bo; insilmaril@183: insilmaril@183: uint i=0; insilmaril@171: bo=current->getFirstBranch(); insilmaril@171: if (bo) insilmaril@171: { insilmaril@171: // Start list insilmaril@183: r+="\n"; insilmaril@171: while (bo) insilmaril@171: { insilmaril@183: r+=""; insilmaril@183: r+=quotemeta(bo->getHeading()); insilmaril@183: // If necessary, write note insilmaril@183: if (!bo->getNote().isEmpty()) insilmaril@183: r+=bo->getNoteOpenDoc(); insilmaril@183: r+=""; insilmaril@183: r+=buildList (bo); // recursivly add deeper branches insilmaril@183: r+="\n"; insilmaril@183: i++; insilmaril@183: bo=current->getBranchNum(i); insilmaril@171: } insilmaril@171: r+="\n"; insilmaril@171: } insilmaril@171: return r; insilmaril@171: } insilmaril@163: insilmaril@171: insilmaril@171: void ExportOO::exportPresentation() insilmaril@171: { insilmaril@183: QString allPages; insilmaril@171: insilmaril@194: bool useSections=true; insilmaril@183: insilmaril@163: // Insert new content insilmaril@171: content.replace ("",quotemeta(mapCenter->getHeading())); insilmaril@171: content.replace ("",quotemeta(mapCenter->getAuthor())); insilmaril@183: insilmaril@183: QString onePage; insilmaril@183: QString list; insilmaril@183: insilmaril@194: BranchObj *sectionBO=mapCenter->getFirstBranch(); insilmaril@183: int i=0; insilmaril@183: BranchObj *pagesBO; insilmaril@183: int j=0; insilmaril@183: insilmaril@194: // Walk sections insilmaril@194: while (sectionBO) insilmaril@183: { insilmaril@194: if (useSections) insilmaril@183: { insilmaril@194: // Add page with section title insilmaril@194: onePage=sectionTemplate; insilmaril@194: onePage.replace ("", quotemeta(sectionBO->getHeading() ) ); insilmaril@183: allPages+=onePage; insilmaril@183: } else insilmaril@183: { insilmaril@183: i=-2; // only use inner loop to insilmaril@183: // turn mainbranches into pages insilmaril@194: sectionBO=mapCenter; insilmaril@183: } insilmaril@183: insilmaril@183: // Walk mainpages insilmaril@194: pagesBO=sectionBO->getFirstBranch(); insilmaril@183: j=0; insilmaril@183: while (pagesBO) insilmaril@183: { insilmaril@183: // Add page with list of items insilmaril@183: onePage=pageTemplate; insilmaril@183: onePage.replace ("", quotemeta (pagesBO->getHeading() ) ); insilmaril@183: list=buildList (pagesBO); insilmaril@183: onePage.replace ("", list); insilmaril@183: allPages+=onePage; insilmaril@183: j++; insilmaril@194: pagesBO=sectionBO->getBranchNum(j); insilmaril@183: } insilmaril@183: i++; insilmaril@194: sectionBO=mapCenter->getBranchNum(i); insilmaril@183: } insilmaril@183: insilmaril@171: content.replace ("",allPages); insilmaril@171: insilmaril@171: // Write modified content insilmaril@171: QFile f (contentFile); insilmaril@171: if ( !f.open( IO_WriteOnly ) ) insilmaril@171: { insilmaril@171: mainWindow->statusMessage(QString(QObject::tr("Could not write to %1")).arg(outputFile)); insilmaril@171: return; insilmaril@171: } insilmaril@171: insilmaril@171: QTextStream t( &f ); insilmaril@171: t << content; insilmaril@171: f.close(); insilmaril@171: insilmaril@163: // zip tmpdir to destination insilmaril@171: zipDir (tmpDir,outputFile); insilmaril@163: } insilmaril@163: insilmaril@194: bool ExportOO::setConfigFile (const QString &cf) insilmaril@183: { insilmaril@183: configFile=cf; insilmaril@183: int i=cf.findRev ("/"); insilmaril@183: if (i>=0) configDir=cf.left(i); insilmaril@183: SimpleSettings set; insilmaril@183: set.readSettings(configFile); insilmaril@183: insilmaril@183: // set paths insilmaril@183: templateDir=configDir+"/"+set.readEntry ("Template"); insilmaril@183: insilmaril@194: QDir d (templateDir); insilmaril@194: if (!d.exists()) insilmaril@194: { insilmaril@194: QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile)); insilmaril@194: return false; insilmaril@194: insilmaril@194: } insilmaril@183: insilmaril@183: contentTemplateFile=templateDir+"content-template.xml"; insilmaril@183: contentFile=tmpDir.path()+"/content.xml"; insilmaril@183: pageTemplateFile=templateDir+"page-template.xml"; insilmaril@194: sectionTemplateFile=templateDir+"section-template.xml"; insilmaril@183: insilmaril@194: outputFile=tmpDir.currentDirPath()+"/vym-out.odp"; insilmaril@183: insilmaril@183: // Copy template to tmpdir insilmaril@183: system ("cp -r "+templateDir+"* "+tmpDir.path()); insilmaril@183: insilmaril@183: // Read content-template insilmaril@183: if (!loadStringFromDisk (contentTemplateFile,content)) insilmaril@183: { insilmaril@194: QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile)); insilmaril@194: return false; insilmaril@183: } insilmaril@183: insilmaril@183: // Read page-template insilmaril@183: if (!loadStringFromDisk (pageTemplateFile,pageTemplate)) insilmaril@183: { insilmaril@194: QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile)); insilmaril@194: return false; insilmaril@183: } insilmaril@183: insilmaril@194: // Read section-template insilmaril@194: if (!loadStringFromDisk (sectionTemplateFile,sectionTemplate)) insilmaril@183: { insilmaril@194: QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile)); insilmaril@194: return false; insilmaril@183: } insilmaril@194: return true; insilmaril@183: } insilmaril@183: