exports.cpp
author insilmaril
Tue, 14 Mar 2006 14:27:16 +0000
changeset 245 27b71695d690
parent 239 bdeb503d2b7f
child 254 d3080e02b13a
permissions -rw-r--r--
hideLinkInExport for Branches (Floats still missing). Floats are now OrnamentedObj.
     1 #include <qfiledialog.h>
     2 #include <qmessagebox.h>
     3 
     4 #include "exports.h"
     5 #include "file.h"
     6 #include "linkablemapobj.h"
     7 #include "misc.h"
     8 #include "mainwindow.h"
     9 #include "xsltproc.h"
    10 
    11 extern Main *mainWindow;
    12 extern QDir vymBaseDir;
    13 
    14 
    15 ExportBase::ExportBase()
    16 {
    17 	indentPerDepth="  ";
    18 	// Create tmpdir
    19 	tmpDir.setPath (makeUniqueDir("/tmp/vym-XXXXXX"));
    20 }
    21 
    22 ExportBase::~ExportBase()
    23 {
    24 	// Remove tmpdir
    25 	removeDir (tmpDir);
    26 }
    27 
    28 void ExportBase::setDir(const QString &p)
    29 {
    30 	outputDir=p;
    31 }
    32 
    33 void ExportBase::setFile (const QString &p)
    34 {
    35 	outputFile=p;
    36 }
    37 
    38 void ExportBase::setMapCenter(MapCenterObj *mc)
    39 {
    40 	mapCenter=mc;
    41 }
    42 
    43 void ExportBase::setCaption (const QString &s)
    44 {
    45 	caption=s;
    46 }
    47 
    48 void ExportBase::addFilter(const QString &s)
    49 {
    50 	filter=s;
    51 }
    52 
    53 bool ExportBase::execDialog()
    54 {
    55 	if (mapCenter && mapCenter->getMapEditor())
    56 	{
    57 		QFileDialog *fd=new QFileDialog( mapCenter->getMapEditor(), caption);
    58 		fd->addFilter (filter);
    59 		fd->setCaption(caption);
    60 		fd->setMode( QFileDialog::AnyFile );
    61 		fd->show();
    62 
    63 		if ( fd->exec() == QDialog::Accepted )
    64 		{
    65 			if (QFile (fd->selectedFile()).exists() )
    66 			{
    67 				QMessageBox mb( __VYM,
    68 					QObject::tr("The file %1 exists already.\nDo you want to overwrite it?").arg(fd->selectedFile()), 
    69 				QMessageBox::Warning,
    70 				QMessageBox::Yes | QMessageBox::Default,
    71 				QMessageBox::Cancel | QMessageBox::Escape,
    72 				QMessageBox::NoButton );
    73 				mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
    74 				mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
    75 				ExportBase ex;
    76 				switch( mb.exec() ) 
    77 				{
    78 					case QMessageBox::Yes:
    79 						// save 
    80 						break;;
    81 					case QMessageBox::Cancel:
    82 						// return, do nothing
    83 						return false;
    84 						break;
    85 				}
    86 			}
    87 			outputFile=fd->selectedFile();
    88 			return true;
    89 		}
    90 	}
    91 	return false;
    92 }
    93 
    94 QString ExportBase::getSectionString(BranchObj *bostart)
    95 {
    96 	// Make prefix like "2.5.3" for "bo:2,bo:5,bo:3"
    97 	QString r;
    98 	BranchObj *bo=bostart;
    99 	int depth=bo->getDepth();
   100 	while (depth>0)
   101 	{
   102 		r=QString("%1").arg(1+bo->getNum(),0,10)+"." + r;
   103 		bo=(BranchObj*)(bo->getParObj());
   104 		depth=bo->getDepth();
   105 	}	
   106 	if (r.isEmpty())
   107 		return r;
   108 	else	
   109 		return r + " ";
   110 }
   111 
   112 
   113 ////////////////////////////////////////////////////////////////////////
   114 void ExportASCII::doExport()
   115 {
   116 	QFile file (outputFile);
   117 	if ( !file.open( IO_WriteOnly ) )
   118 	{
   119 		// FIXME experimental, testing
   120 		qWarning ("ExportBase::exportXML  couldn't open "+outputFile);
   121 		return;
   122 	}
   123 	QTextStream ts( &file );	// use LANG decoding here...
   124 
   125 	// Main loop over all branches
   126 	QString s;
   127 	QString actIndent("");
   128 	int i;
   129 	uint j;
   130 	BranchObj *bo;
   131 	bo=mapCenter->first();
   132 	while (bo) 
   133 	{
   134 		if (!bo->hideInExport())
   135 		{
   136 			// Make indentstring
   137 			for (i=0;i<bo->getDepth();i++) actIndent+= indentPerDepth;
   138 
   139 			if (bo->getDepth()==0)
   140 			{
   141 				ts << (bo->getHeading()+ "\n");
   142 				for (j=0;j<bo->getHeading().length();j++) ts<<"=";
   143 				ts << "\n";
   144 			} else 	if (bo->getDepth()==1)
   145 				ts << ("\n"+getSectionString(bo) + bo->getHeading()+ "\n");
   146 			else	if (bo->getDepth()==2)
   147 				ts << (actIndent + " o " + bo->getHeading()+ "\n");
   148 			else	
   149 				ts << (actIndent + " - " + bo->getHeading()+ "\n");
   150 			
   151 			// If necessary, write note
   152 			if (!bo->getNote().isEmpty())
   153 			{
   154 				s =bo->getNoteASCII();
   155 				s=s.replace ("\n","\n"+actIndent);
   156 				ts << (s+"\n\n");
   157 			}
   158 		}
   159 		bo=bo->next();
   160 		actIndent="";
   161 	}
   162 	file.close();
   163 }
   164 
   165 ////////////////////////////////////////////////////////////////////////
   166 void ExportKDEBookmarks::doExport() 
   167 {
   168 	MapEditor *me=NULL;
   169 	if (mapCenter) me=mapCenter->getMapEditor();
   170 	if (me)
   171 	{
   172 		me->exportXML(tmpDir.path());
   173 		//FIXME testing
   174 		cout << "tmpDir="<<tmpDir.path()<<endl;
   175 
   176 		XSLTProc p;
   177 		p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   178 		p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   179 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   180 		p.process();
   181 	}
   182 
   183 }
   184 
   185 ////////////////////////////////////////////////////////////////////////
   186 void ExportTaskjuggler::doExport() 
   187 {
   188 	MapEditor *me=NULL;
   189 	if (mapCenter) me=mapCenter->getMapEditor();
   190 	if (me)
   191 	{
   192 		me->exportXML(tmpDir.path());
   193 		//FIXME testing
   194 		cout << "tmpDir="<<tmpDir.path()<<endl;
   195 
   196 		XSLTProc p;
   197 		p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   198 		p.setOutputFile (outputFile);
   199 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
   200 		p.process();
   201 	}
   202 
   203 }
   204 
   205 ////////////////////////////////////////////////////////////////////////
   206 void ExportLaTeX::doExport() 
   207 {
   208 	// Exports a map to a LaTex file.  
   209 	// This file needs to be included 
   210 	// or inported into a LaTex document
   211 	// it will not add a preamble, or anything 
   212 	// that makes a full LaTex document.
   213   QFile file (outputFile);
   214   if ( !file.open( IO_WriteOnly ) ) {
   215 	QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
   216 	mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   217     return;
   218   }
   219   QTextStream ts( &file );	// use LANG decoding here...
   220   ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
   221   
   222   // Main loop over all branches
   223   QString s;
   224   // QString actIndent("");
   225   // int i;
   226   BranchObj *bo;
   227   bo=mapCenter->first();
   228   while (bo) {
   229 	if (!bo->hideInExport())
   230 	{
   231 		if (bo->getDepth()==0);
   232 		else if (bo->getDepth()==1) {
   233 		  ts << ("\\chapter{" + bo->getHeading()+ "}\n");
   234 		}
   235 		else if (bo->getDepth()==2) {
   236 		  ts << ("\\section{" + bo->getHeading()+ "}\n");
   237 		}
   238 		else if (bo->getDepth()==3) {
   239 		  ts << ("\\subsection{" + bo->getHeading()+ "}\n");
   240 		}
   241 		else if (bo->getDepth()==4) {
   242 		  ts << ("\\subsubsection{" + bo->getHeading()+ "}\n");
   243 		}
   244 		else {
   245 		  ts << ("\\paragraph*{" + bo->getHeading()+ "}\n");
   246 		}
   247 		
   248 		// If necessary, write note
   249 		if (!bo->getNote().isEmpty()) {
   250 		  ts << (bo->getNoteASCII());
   251 		  ts << ("\n");
   252 		}
   253     }
   254     bo=bo->next();
   255    }
   256   file.close();
   257 }
   258 
   259 ////////////////////////////////////////////////////////////////////////
   260 ExportOO::ExportOO()
   261 {
   262 	useSections=false;
   263 }
   264 
   265 ExportOO::~ExportOO()
   266 {
   267 }	
   268 
   269 QString ExportOO::buildList (BranchObj *current)
   270 {
   271     QString r;
   272     BranchObj *bo;
   273 
   274     uint i=0;
   275     bo=current->getFirstBranch();
   276     if (bo)
   277     {
   278         // Start list
   279         r+="<text:list text:style-name=\"vym-list\">\n";
   280         while (bo)
   281         {
   282 			if (!bo->hideInExport())
   283 			{
   284 				r+="<text:list-item><text:p >";
   285 				r+=quotemeta(bo->getHeading());
   286 				// If necessary, write note
   287 				if (!bo->getNote().isEmpty())
   288 					r+=bo->getNoteOpenDoc();
   289 				r+="</text:p>";
   290 				r+=buildList (bo);	// recursivly add deeper branches
   291 				r+="</text:list-item>\n";
   292 			}
   293 			i++;
   294 			bo=current->getBranchNum(i);
   295         }
   296         r+="</text:list>\n";
   297     }
   298     return r;
   299 }
   300 
   301 
   302 void ExportOO::exportPresentation()
   303 {
   304 	QString allPages;
   305 
   306 	// Insert new content
   307 	content.replace ("<!-- INSERT TITLE -->",quotemeta(mapCenter->getHeading()));
   308 	content.replace ("<!-- INSERT AUTHOR -->",quotemeta(mapCenter->getAuthor()));
   309 
   310 	QString	onePage;
   311 	QString list;
   312 	
   313 	BranchObj *sectionBO=mapCenter->getFirstBranch();
   314     int i=0;
   315 	BranchObj *pagesBO;
   316     int j=0;
   317 
   318 	// Walk sections
   319 	while (sectionBO)
   320 	{
   321 		if (useSections)
   322 		{
   323 			// Add page with section title
   324 			onePage=sectionTemplate;
   325 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBO->getHeading() ) );
   326 			allPages+=onePage;
   327 		} else
   328 		{
   329 			i=-2;	// only use inner loop to 
   330 			        // turn mainbranches into pages
   331 			sectionBO=mapCenter;
   332 		}
   333 
   334 		// Walk mainpages
   335 		pagesBO=sectionBO->getFirstBranch();
   336 		j=0;
   337 		while (pagesBO)
   338 		{
   339 			// Add page with list of items
   340 			if (!pagesBO->hideInExport())
   341 			{
   342 				onePage=pageTemplate;
   343 				onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
   344 				list=buildList (pagesBO);
   345 				onePage.replace ("<!-- INSERT LIST -->", list);
   346 				allPages+=onePage;
   347 			}
   348 			j++;
   349 			pagesBO=sectionBO->getBranchNum(j);
   350 		}
   351 		i++;
   352 		sectionBO=mapCenter->getBranchNum(i);
   353 	}
   354 	
   355 	content.replace ("<!-- INSERT PAGES -->",allPages);
   356 
   357 	// Write modified content
   358 	QFile f (contentFile);
   359     if ( !f.open( IO_WriteOnly ) ) 
   360 	{
   361 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
   362 		mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   363 		return;
   364     }
   365 
   366     QTextStream t( &f );
   367     t << content;
   368     f.close();
   369 
   370 	// zip tmpdir to destination
   371 	zipDir (tmpDir,outputFile);	
   372 }
   373 
   374 bool ExportOO::setConfigFile (const QString &cf)
   375 {
   376 	configFile=cf;
   377 	int i=cf.findRev ("/");
   378 	if (i>=0) configDir=cf.left(i);
   379 	SimpleSettings set;
   380 	set.readSettings(configFile);
   381 
   382 	// set paths
   383 	templateDir=configDir+"/"+set.readEntry ("Template");
   384 
   385 	QDir d (templateDir);
   386 	if (!d.exists())
   387 	{
   388 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
   389 		return false;
   390 
   391 	}
   392 
   393 	contentTemplateFile=templateDir+"content-template.xml";
   394 	contentFile=tmpDir.path()+"/content.xml";
   395 	pageTemplateFile=templateDir+"page-template.xml";
   396 	sectionTemplateFile=templateDir+"section-template.xml";
   397 
   398 	if (set.readEntry("useSections").contains("yes"))
   399 		useSections=true;
   400 
   401 	// Copy template to tmpdir
   402 	system ("cp -r "+templateDir+"* "+tmpDir.path());
   403 
   404 	// Read content-template
   405 	if (!loadStringFromDisk (contentTemplateFile,content))
   406 	{
   407 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
   408 		return false;
   409 	}
   410 
   411 	// Read page-template
   412 	if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
   413 	{
   414 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
   415 		return false;
   416 	}
   417 	
   418 	// Read section-template
   419 	if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
   420 	{
   421 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));
   422 		return false;
   423 	}
   424 	return true;
   425 }
   426