exports.cpp
author insilmaril
Tue Mar 07 11:32:02 2006 +0000 (2006-03-07)
changeset 233 c3943fbd0e74
parent 205 30c4a6c7ff10
child 234 5115a9d93e86
permissions -rw-r--r--
Added xsl for Taskjuggler Export by Matt
     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 void ExportASCII::doExport()
   113 {
   114 	QFile file (outputFile);
   115 	if ( !file.open( IO_WriteOnly ) )
   116 	{
   117 		// FIXME experimental, testing
   118 		qWarning ("ExportBase::exportXML  couldn't open "+outputFile);
   119 		return;
   120 	}
   121 	QTextStream ts( &file );	// use LANG decoding here...
   122 
   123 	// Main loop over all branches
   124 	QString s;
   125 	QString actIndent("");
   126 	int i;
   127 	uint j;
   128 	BranchObj *bo;
   129 	bo=mapCenter->first();
   130 	while (bo) 
   131 	{
   132 		// Make indentstring
   133 		for (i=0;i<bo->getDepth();i++) actIndent+= indentPerDepth;
   134 
   135 		if (bo->getDepth()==0)
   136 		{
   137 			ts << (bo->getHeading()+ "\n");
   138 			for (j=0;j<bo->getHeading().length();j++) ts<<"=";
   139 			ts << "\n";
   140 		} else 	if (bo->getDepth()==1)
   141 			ts << ("\n"+getSectionString(bo) + bo->getHeading()+ "\n");
   142 		else	if (bo->getDepth()==2)
   143 			ts << (actIndent + " o " + bo->getHeading()+ "\n");
   144 		else	
   145 			ts << (actIndent + " - " + bo->getHeading()+ "\n");
   146 		
   147 		// If necessary, write note
   148 		if (!bo->getNote().isEmpty())
   149 		{
   150 			s =bo->getNoteASCII();
   151 			s=s.replace ("\n","\n"+actIndent);
   152 			ts << (s+"\n\n");
   153 		}
   154 		
   155 		bo=bo->next();
   156 		actIndent="";
   157 	}
   158 	file.close();
   159 }
   160 
   161 void ExportTaskjuggler::doExport() 
   162 {
   163 	MapEditor *me=NULL;
   164 	if (mapCenter) me=mapCenter->getMapEditor();
   165 	if (me)
   166 	{
   167 		me->exportXML(tmpDir.path());
   168 		//FIXME testing
   169 		cout << "tmpDir="<<tmpDir.path()<<endl;
   170 
   171 		XSLTProc p;
   172 		p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   173 		p.setOutputFile (outputFile);
   174 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
   175 		p.process();
   176 	}
   177 
   178 }
   179 
   180 void ExportLaTeX::doExport() 
   181 {
   182 	// Exports a map to a LaTex file.  
   183 	// This file needs to be included 
   184 	// or inported into a LaTex document
   185 	// it will not add a preamble, or anything 
   186 	// that makes a full LaTex document.
   187   QFile file (outputFile);
   188   if ( !file.open( IO_WriteOnly ) ) {
   189 	QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
   190 	mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   191     return;
   192   }
   193   QTextStream ts( &file );	// use LANG decoding here...
   194   ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
   195   
   196   // Main loop over all branches
   197   QString s;
   198   // QString actIndent("");
   199   // int i;
   200   BranchObj *bo;
   201   bo=mapCenter->first();
   202   while (bo) {
   203     if (bo->getDepth()==0);
   204     else if (bo->getDepth()==1) {
   205       ts << ("\\chapter{" + bo->getHeading()+ "}\n");
   206     }
   207     else if (bo->getDepth()==2) {
   208       ts << ("\\section{" + bo->getHeading()+ "}\n");
   209     }
   210     else if (bo->getDepth()==3) {
   211       ts << ("\\subsection{" + bo->getHeading()+ "}\n");
   212     }
   213     else if (bo->getDepth()==4) {
   214       ts << ("\\subsubsection{" + bo->getHeading()+ "}\n");
   215     }
   216     else {
   217       ts << ("\\paragraph*{" + bo->getHeading()+ "}\n");
   218     }
   219     
   220     // If necessary, write note
   221     if (!bo->getNote().isEmpty()) {
   222       ts << (bo->getNoteASCII());
   223       ts << ("\n");
   224     }
   225     
   226     bo=bo->next();
   227    }
   228   file.close();
   229 }
   230 
   231 ExportOO::ExportOO()
   232 {
   233 	useSections=false;
   234 }
   235 
   236 ExportOO::~ExportOO()
   237 {
   238 }	
   239 
   240 QString ExportOO::buildList (BranchObj *current)
   241 {
   242     QString r;
   243     BranchObj *bo;
   244 
   245     uint i=0;
   246     bo=current->getFirstBranch();
   247     if (bo)
   248     {
   249         // Start list
   250         r+="<text:list text:style-name=\"L4\">\n";
   251         while (bo)
   252         {
   253             r+="<text:list-item><text:p >";
   254 			r+=quotemeta(bo->getHeading());
   255 			// If necessary, write note
   256 			if (!bo->getNote().isEmpty())
   257 				r+=bo->getNoteOpenDoc();
   258 			r+="</text:p>";
   259 			r+=buildList (bo);	// recursivly add deeper branches
   260             r+="</text:list-item>\n";
   261 			i++;
   262 			bo=current->getBranchNum(i);
   263         }
   264         r+="</text:list>\n";
   265     }
   266     return r;
   267 }
   268 
   269 
   270 void ExportOO::exportPresentation()
   271 {
   272 	QString allPages;
   273 
   274 	// Insert new content
   275 	content.replace ("<!-- INSERT TITLE -->",quotemeta(mapCenter->getHeading()));
   276 	content.replace ("<!-- INSERT AUTHOR -->",quotemeta(mapCenter->getAuthor()));
   277 
   278 	QString	onePage;
   279 	QString list;
   280 	
   281 	BranchObj *sectionBO=mapCenter->getFirstBranch();
   282     int i=0;
   283 	BranchObj *pagesBO;
   284     int j=0;
   285 
   286 	// Walk sections
   287 	while (sectionBO)
   288 	{
   289 		if (useSections)
   290 		{
   291 			// Add page with section title
   292 			onePage=sectionTemplate;
   293 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBO->getHeading() ) );
   294 			allPages+=onePage;
   295 		} else
   296 		{
   297 			i=-2;	// only use inner loop to 
   298 			        // turn mainbranches into pages
   299 			sectionBO=mapCenter;
   300 		}
   301 
   302 		// Walk mainpages
   303 		pagesBO=sectionBO->getFirstBranch();
   304 		j=0;
   305 		while (pagesBO)
   306 		{
   307 			// Add page with list of items
   308 			onePage=pageTemplate;
   309 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
   310 			list=buildList (pagesBO);
   311 			onePage.replace ("<!-- INSERT LIST -->", list);
   312 			allPages+=onePage;
   313 			j++;
   314 			pagesBO=sectionBO->getBranchNum(j);
   315 		}
   316 		i++;
   317 		sectionBO=mapCenter->getBranchNum(i);
   318 	}
   319 	
   320 	content.replace ("<!-- INSERT PAGES -->",allPages);
   321 
   322 	// Write modified content
   323 	QFile f (contentFile);
   324     if ( !f.open( IO_WriteOnly ) ) 
   325 	{
   326 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
   327 		mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   328 		return;
   329     }
   330 
   331     QTextStream t( &f );
   332     t << content;
   333     f.close();
   334 
   335 	// zip tmpdir to destination
   336 	zipDir (tmpDir,outputFile);	
   337 }
   338 
   339 bool ExportOO::setConfigFile (const QString &cf)
   340 {
   341 	configFile=cf;
   342 	int i=cf.findRev ("/");
   343 	if (i>=0) configDir=cf.left(i);
   344 	SimpleSettings set;
   345 	set.readSettings(configFile);
   346 
   347 	// set paths
   348 	templateDir=configDir+"/"+set.readEntry ("Template");
   349 
   350 	QDir d (templateDir);
   351 	if (!d.exists())
   352 	{
   353 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
   354 		return false;
   355 
   356 	}
   357 
   358 	contentTemplateFile=templateDir+"content-template.xml";
   359 	contentFile=tmpDir.path()+"/content.xml";
   360 	pageTemplateFile=templateDir+"page-template.xml";
   361 	sectionTemplateFile=templateDir+"section-template.xml";
   362 
   363 	if (set.readEntry("useSections").contains("yes"))
   364 		useSections=true;
   365 
   366 	// Copy template to tmpdir
   367 	system ("cp -r "+templateDir+"* "+tmpDir.path());
   368 
   369 	// Read content-template
   370 	if (!loadStringFromDisk (contentTemplateFile,content))
   371 	{
   372 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
   373 		return false;
   374 	}
   375 
   376 	// Read page-template
   377 	if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
   378 	{
   379 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
   380 		return false;
   381 	}
   382 	
   383 	// Read section-template
   384 	if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
   385 	{
   386 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));
   387 		return false;
   388 	}
   389 	return true;
   390 }
   391