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