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