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