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