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