exports.cpp
author insilmaril
Wed, 08 Mar 2006 12:59:07 +0000
changeset 235 402f4cde3ea6
parent 234 5115a9d93e86
child 239 bdeb503d2b7f
permissions -rw-r--r--
Fixed Image Preview in FileDialog
     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 	skipPageFlag="cross-red";
   235 }
   236 
   237 ExportOO::~ExportOO()
   238 {
   239 }	
   240 
   241 QString ExportOO::buildList (BranchObj *current)
   242 {
   243     QString r;
   244     BranchObj *bo;
   245 
   246     uint i=0;
   247     bo=current->getFirstBranch();
   248     if (bo)
   249     {
   250         // Start list
   251         r+="<text:list text:style-name=\"vym-list\">\n";
   252         while (bo)
   253         {
   254 			if (!bo->isActiveFlag (skipPageFlag))
   255 			{
   256 				r+="<text:list-item><text:p >";
   257 				r+=quotemeta(bo->getHeading());
   258 				// If necessary, write note
   259 				if (!bo->getNote().isEmpty())
   260 					r+=bo->getNoteOpenDoc();
   261 				r+="</text:p>";
   262 				r+=buildList (bo);	// recursivly add deeper branches
   263 				r+="</text:list-item>\n";
   264 			}
   265 			i++;
   266 			bo=current->getBranchNum(i);
   267         }
   268         r+="</text:list>\n";
   269     }
   270     return r;
   271 }
   272 
   273 
   274 void ExportOO::exportPresentation()
   275 {
   276 	QString allPages;
   277 
   278 	// Insert new content
   279 	content.replace ("<!-- INSERT TITLE -->",quotemeta(mapCenter->getHeading()));
   280 	content.replace ("<!-- INSERT AUTHOR -->",quotemeta(mapCenter->getAuthor()));
   281 
   282 	QString	onePage;
   283 	QString list;
   284 	
   285 	BranchObj *sectionBO=mapCenter->getFirstBranch();
   286     int i=0;
   287 	BranchObj *pagesBO;
   288     int j=0;
   289 
   290 	// Walk sections
   291 	while (sectionBO)
   292 	{
   293 		if (useSections)
   294 		{
   295 			// Add page with section title
   296 			onePage=sectionTemplate;
   297 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBO->getHeading() ) );
   298 			allPages+=onePage;
   299 		} else
   300 		{
   301 			i=-2;	// only use inner loop to 
   302 			        // turn mainbranches into pages
   303 			sectionBO=mapCenter;
   304 		}
   305 
   306 		// Walk mainpages
   307 		pagesBO=sectionBO->getFirstBranch();
   308 		j=0;
   309 		while (pagesBO)
   310 		{
   311 			// Add page with list of items
   312 			if (!pagesBO->isActiveFlag (skipPageFlag))
   313 			{
   314 				onePage=pageTemplate;
   315 				onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
   316 				list=buildList (pagesBO);
   317 				onePage.replace ("<!-- INSERT LIST -->", list);
   318 				allPages+=onePage;
   319 			}
   320 			j++;
   321 			pagesBO=sectionBO->getBranchNum(j);
   322 		}
   323 		i++;
   324 		sectionBO=mapCenter->getBranchNum(i);
   325 	}
   326 	
   327 	content.replace ("<!-- INSERT PAGES -->",allPages);
   328 
   329 	// Write modified content
   330 	QFile f (contentFile);
   331     if ( !f.open( IO_WriteOnly ) ) 
   332 	{
   333 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
   334 		mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   335 		return;
   336     }
   337 
   338     QTextStream t( &f );
   339     t << content;
   340     f.close();
   341 
   342 	// zip tmpdir to destination
   343 	zipDir (tmpDir,outputFile);	
   344 }
   345 
   346 bool ExportOO::setConfigFile (const QString &cf)
   347 {
   348 	configFile=cf;
   349 	int i=cf.findRev ("/");
   350 	if (i>=0) configDir=cf.left(i);
   351 	SimpleSettings set;
   352 	set.readSettings(configFile);
   353 
   354 	// set paths
   355 	templateDir=configDir+"/"+set.readEntry ("Template");
   356 
   357 	QDir d (templateDir);
   358 	if (!d.exists())
   359 	{
   360 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
   361 		return false;
   362 
   363 	}
   364 
   365 	contentTemplateFile=templateDir+"content-template.xml";
   366 	contentFile=tmpDir.path()+"/content.xml";
   367 	pageTemplateFile=templateDir+"page-template.xml";
   368 	sectionTemplateFile=templateDir+"section-template.xml";
   369 
   370 	if (set.readEntry("useSections").contains("yes"))
   371 		useSections=true;
   372 
   373 	// Copy template to tmpdir
   374 	system ("cp -r "+templateDir+"* "+tmpDir.path());
   375 
   376 	// Read content-template
   377 	if (!loadStringFromDisk (contentTemplateFile,content))
   378 	{
   379 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
   380 		return false;
   381 	}
   382 
   383 	// Read page-template
   384 	if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
   385 	{
   386 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
   387 		return false;
   388 	}
   389 	
   390 	// Read section-template
   391 	if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
   392 	{
   393 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));
   394 		return false;
   395 	}
   396 	return true;
   397 }
   398