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