exports.cpp
author insilmaril
Wed, 25 Jan 2006 12:34:55 +0000
changeset 197 d529766ed9db
parent 194 de97201180ea
child 199 202572375c98
permissions -rw-r--r--
Fixed outputFile in OO export
     1 #include <qmessagebox.h>
     2 
     3 #include "exports.h"
     4 #include "file.h"
     5 #include "linkablemapobj.h"
     6 #include "misc.h"
     7 #include "texteditor.h"
     8 #include "mainwindow.h"
     9 
    10 extern Main *mainWindow;
    11 
    12 ExportBase::ExportBase()
    13 {
    14 	indentPerDepth="  ";
    15 }
    16 
    17 void ExportBase::setPath (const QString &p)
    18 {
    19 	filepath=p;
    20 }
    21 
    22 void ExportBase::setMapCenter(MapCenterObj *mc)
    23 {
    24 	mapCenter=mc;
    25 }
    26 
    27 QString ExportBase::getSectionString(BranchObj *bostart)
    28 {
    29 	QString r;
    30 	BranchObj *bo=bostart;
    31 	int depth=bo->getDepth();
    32 	while (depth>0)
    33 	{
    34 		r=QString("%1").arg(1+bo->getNum(),0,10)+"." + r;
    35 		bo=(BranchObj*)(bo->getParObj());
    36 		depth=bo->getDepth();
    37 	}	
    38 	if (r.isEmpty())
    39 		return r;
    40 	else	
    41 		return r + " ";
    42 }
    43 
    44 void ExportBase::exportXML()
    45 {
    46 	QFile file (filepath);
    47 	if ( !file.open( IO_WriteOnly ) )
    48 	{
    49 		// FIXME experimental, testing
    50 		cout << "ExportBase::exportXML  couldn't open "<<filepath<<endl;
    51 		return;
    52 	}
    53 	QTextStream ts( &file );	// use LANG decoding here...
    54 
    55 	// Main loop over all branches
    56 	QString s;
    57 	QString actIndent("");
    58 	int i;
    59 	uint j;
    60 	BranchObj *bo;
    61 	bo=mapCenter->first();
    62 	while (bo) 
    63 	{
    64 		// Make indentstring
    65 		for (i=0;i<bo->getDepth();i++) actIndent+= indentPerDepth;
    66 
    67 		// Write heading
    68 		//	write (actIndent + getSectionString(bo) + bo->getHeading()+ "\n");
    69 		if (bo->getDepth()==0)
    70 		{
    71 			ts << (bo->getHeading()+ "\n");
    72 			for (j=0;j<bo->getHeading().length();j++) ts<<"=";
    73 			ts << "\n";
    74 		} else 	if (bo->getDepth()==1)
    75 			ts << ("\n"+getSectionString(bo) + bo->getHeading()+ "\n");
    76 		else	if (bo->getDepth()==2)
    77 			ts << (actIndent + " o " + bo->getHeading()+ "\n");
    78 		else	
    79 			ts << (actIndent + " - " + bo->getHeading()+ "\n");
    80 		
    81 		// If necessary, write note
    82 		if (!bo->getNote().isEmpty())
    83 		{
    84 			s =bo->getNoteASCII();
    85 			s=s.replace ("\n","\n"+actIndent);
    86 			ts << (s+"\n\n");
    87 		}
    88 		
    89 		bo=bo->next();
    90 		actIndent="";
    91 	}
    92 	file.close();
    93 }
    94 
    95 void ExportLaTeX::exportLaTeX() 
    96 {
    97 	// Exports a map to a LaTex file.  
    98 	// This file needs to be included 
    99 	// or inported into a LaTex document
   100 	// it will not add a preamble, or anything 
   101 	// that makes a full LaTex document.
   102   QFile file (filepath);
   103   if ( !file.open( IO_WriteOnly ) ) {
   104     // FIXME
   105     cout << "Export::exportMap  couldn't open "<<filepath<<endl;
   106     return;
   107   }
   108   QTextStream ts( &file );	// use LANG decoding here...
   109   ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
   110   
   111   // Main loop over all branches
   112   QString s;
   113   // QString actIndent("");
   114   // int i;
   115   BranchObj *bo;
   116   bo=mapCenter->first();
   117   while (bo) {
   118     if (bo->getDepth()==0);
   119     else if (bo->getDepth()==1) {
   120       ts << ("\\chapter{" + bo->getHeading()+ "}\n");
   121     }
   122     else if (bo->getDepth()==2) {
   123       ts << ("\\section{" + bo->getHeading()+ "}\n");
   124     }
   125     else if (bo->getDepth()==3) {
   126       ts << ("\\subsection{" + bo->getHeading()+ "}\n");
   127     }
   128     else if (bo->getDepth()==4) {
   129       ts << ("\\subsubsection{" + bo->getHeading()+ "}\n");
   130     }
   131     else {
   132       ts << ("\\paragraph*{" + bo->getHeading()+ "}\n");
   133     }
   134     
   135     // If necessary, write note
   136     if (!bo->getNote().isEmpty()) {
   137       ts << (bo->getNoteASCII());
   138       ts << ("\n");
   139     }
   140     
   141     bo=bo->next();
   142    }
   143   file.close();
   144 }
   145 
   146 ExportOO::ExportOO()
   147 {
   148 	// Create tmpdir
   149 	tmpDir.setPath (makeUniqueDir("/tmp/vym-XXXXXX"));
   150 }
   151 
   152 ExportOO::~ExportOO()
   153 {
   154 	// Remove tmpdir
   155 	removeDir (tmpDir);
   156 }	
   157 
   158 QString ExportOO::buildList (BranchObj *current)
   159 {
   160     QString r;
   161     BranchObj *bo;
   162 
   163     uint i=0;
   164     bo=current->getFirstBranch();
   165     if (bo)
   166     {
   167         // Start list
   168         r+="<text:list text:style-name=\"L4\">\n";
   169         while (bo)
   170         {
   171             r+="<text:list-item><text:p >";
   172 			r+=quotemeta(bo->getHeading());
   173 			// If necessary, write note
   174 			if (!bo->getNote().isEmpty())
   175 				r+=bo->getNoteOpenDoc();
   176 			r+="</text:p>";
   177 			r+=buildList (bo);	// recursivly add deeper branches
   178             r+="</text:list-item>\n";
   179 			i++;
   180 			bo=current->getBranchNum(i);
   181         }
   182         r+="</text:list>\n";
   183     }
   184     return r;
   185 }
   186 
   187 
   188 void ExportOO::exportPresentation()
   189 {
   190 	QString allPages;
   191 
   192 	bool useSections=true;
   193 
   194 	// Insert new content
   195 	content.replace ("<!-- INSERT TITLE -->",quotemeta(mapCenter->getHeading()));
   196 	content.replace ("<!-- INSERT AUTHOR -->",quotemeta(mapCenter->getAuthor()));
   197 
   198 	QString	onePage;
   199 	QString list;
   200 	
   201 	BranchObj *sectionBO=mapCenter->getFirstBranch();
   202     int i=0;
   203 	BranchObj *pagesBO;
   204     int j=0;
   205 
   206 	// Walk sections
   207 	while (sectionBO)
   208 	{
   209 		if (useSections)
   210 		{
   211 			// Add page with section title
   212 			onePage=sectionTemplate;
   213 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBO->getHeading() ) );
   214 			allPages+=onePage;
   215 		} else
   216 		{
   217 			i=-2;	// only use inner loop to 
   218 			        // turn mainbranches into pages
   219 			sectionBO=mapCenter;
   220 		}
   221 
   222 		// Walk mainpages
   223 		pagesBO=sectionBO->getFirstBranch();
   224 		j=0;
   225 		while (pagesBO)
   226 		{
   227 			// Add page with list of items
   228 			onePage=pageTemplate;
   229 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
   230 			list=buildList (pagesBO);
   231 			onePage.replace ("<!-- INSERT LIST -->", list);
   232 			allPages+=onePage;
   233 			j++;
   234 			pagesBO=sectionBO->getBranchNum(j);
   235 		}
   236 		i++;
   237 		sectionBO=mapCenter->getBranchNum(i);
   238 	}
   239 	
   240 	content.replace ("<!-- INSERT PAGES -->",allPages);
   241 
   242 	// Write modified content
   243 	QFile f (contentFile);
   244     if ( !f.open( IO_WriteOnly ) ) 
   245 	{
   246 		mainWindow->statusMessage(QString(QObject::tr("Could not write to %1")).arg(outputFile));
   247 		return;
   248     }
   249 
   250     QTextStream t( &f );
   251     t << content;
   252     f.close();
   253 
   254 	// zip tmpdir to destination
   255 	zipDir (tmpDir,outputFile);	
   256 }
   257 
   258 bool ExportOO::setConfigFile (const QString &cf)
   259 {
   260 	configFile=cf;
   261 	int i=cf.findRev ("/");
   262 	if (i>=0) configDir=cf.left(i);
   263 	SimpleSettings set;
   264 	set.readSettings(configFile);
   265 
   266 	// set paths
   267 	templateDir=configDir+"/"+set.readEntry ("Template");
   268 
   269 	QDir d (templateDir);
   270 	if (!d.exists())
   271 	{
   272 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
   273 		return false;
   274 
   275 	}
   276 
   277 	contentTemplateFile=templateDir+"content-template.xml";
   278 	contentFile=tmpDir.path()+"/content.xml";
   279 	pageTemplateFile=templateDir+"page-template.xml";
   280 	sectionTemplateFile=templateDir+"section-template.xml";
   281 
   282 	outputFile=tmpDir.currentDirPath()+"/vym-out.odp";
   283 	
   284 	// Copy template to tmpdir
   285 	system ("cp -r "+templateDir+"* "+tmpDir.path());
   286 
   287 	// Read content-template
   288 	if (!loadStringFromDisk (contentTemplateFile,content))
   289 	{
   290 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
   291 		return false;
   292 	}
   293 
   294 	// Read page-template
   295 	if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
   296 	{
   297 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
   298 		return false;
   299 	}
   300 	
   301 	// Read section-template
   302 	if (!loadStringFromDisk (sectionTemplateFile,sectionTemplate))
   303 	{
   304 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));
   305 		return false;
   306 	}
   307 	return true;
   308 }
   309