exports.cpp
author insilmaril
Tue Jan 24 15:09:49 2006 +0000 (2006-01-24)
changeset 193 544a64d7cbac
parent 171 a98a07994eed
child 194 de97201180ea
permissions -rw-r--r--
Introduced basic export to Open Document format
insilmaril@0
     1
#include "exports.h"
insilmaril@171
     2
#include "file.h"
insilmaril@171
     3
#include "linkablemapobj.h"
insilmaril@171
     4
#include "misc.h"
insilmaril@171
     5
#include "texteditor.h"
insilmaril@171
     6
#include "mainwindow.h"
insilmaril@0
     7
insilmaril@171
     8
extern Main *mainWindow;
insilmaril@131
     9
insilmaril@171
    10
ExportBase::ExportBase()
insilmaril@0
    11
{
insilmaril@0
    12
	indentPerDepth="  ";
insilmaril@0
    13
}
insilmaril@0
    14
insilmaril@171
    15
void ExportBase::setPath (const QString &p)
insilmaril@0
    16
{
insilmaril@0
    17
	filepath=p;
insilmaril@0
    18
}
insilmaril@0
    19
insilmaril@171
    20
void ExportBase::setMapCenter(MapCenterObj *mc)
insilmaril@0
    21
{
insilmaril@0
    22
	mapCenter=mc;
insilmaril@0
    23
}
insilmaril@0
    24
insilmaril@171
    25
QString ExportBase::getSectionString(BranchObj *bostart)
insilmaril@163
    26
{
insilmaril@163
    27
	QString r;
insilmaril@163
    28
	BranchObj *bo=bostart;
insilmaril@163
    29
	int depth=bo->getDepth();
insilmaril@163
    30
	while (depth>0)
insilmaril@163
    31
	{
insilmaril@163
    32
		r=QString("%1").arg(1+bo->getNum(),0,10)+"." + r;
insilmaril@163
    33
		bo=(BranchObj*)(bo->getParObj());
insilmaril@163
    34
		depth=bo->getDepth();
insilmaril@163
    35
	}	
insilmaril@163
    36
	if (r.isEmpty())
insilmaril@163
    37
		return r;
insilmaril@163
    38
	else	
insilmaril@163
    39
		return r + " ";
insilmaril@163
    40
}
insilmaril@163
    41
insilmaril@171
    42
void ExportBase::exportXML()
insilmaril@0
    43
{
insilmaril@0
    44
	QFile file (filepath);
insilmaril@0
    45
	if ( !file.open( IO_WriteOnly ) )
insilmaril@0
    46
	{
insilmaril@131
    47
		// FIXME experimental, testing
insilmaril@171
    48
		cout << "ExportBase::exportXML  couldn't open "<<filepath<<endl;
insilmaril@0
    49
		return;
insilmaril@0
    50
	}
insilmaril@0
    51
	QTextStream ts( &file );	// use LANG decoding here...
insilmaril@0
    52
insilmaril@0
    53
	// Main loop over all branches
insilmaril@0
    54
	QString s;
insilmaril@0
    55
	QString actIndent("");
insilmaril@160
    56
	int i;
insilmaril@160
    57
	uint j;
insilmaril@0
    58
	BranchObj *bo;
insilmaril@0
    59
	bo=mapCenter->first();
insilmaril@0
    60
	while (bo) 
insilmaril@0
    61
	{
insilmaril@0
    62
		// Make indentstring
insilmaril@0
    63
		for (i=0;i<bo->getDepth();i++) actIndent+= indentPerDepth;
insilmaril@0
    64
insilmaril@0
    65
		// Write heading
insilmaril@0
    66
		//	write (actIndent + getSectionString(bo) + bo->getHeading()+ "\n");
insilmaril@131
    67
		if (bo->getDepth()==0)
insilmaril@131
    68
		{
insilmaril@131
    69
			ts << (bo->getHeading()+ "\n");
insilmaril@160
    70
			for (j=0;j<bo->getHeading().length();j++) ts<<"=";
insilmaril@131
    71
			ts << "\n";
insilmaril@131
    72
		} else 	if (bo->getDepth()==1)
insilmaril@131
    73
			ts << ("\n"+getSectionString(bo) + bo->getHeading()+ "\n");
insilmaril@131
    74
		else	if (bo->getDepth()==2)
insilmaril@131
    75
			ts << (actIndent + " o " + bo->getHeading()+ "\n");
insilmaril@0
    76
		else	
insilmaril@0
    77
			ts << (actIndent + " - " + bo->getHeading()+ "\n");
insilmaril@0
    78
		
insilmaril@0
    79
		// If necessary, write note
insilmaril@0
    80
		if (!bo->getNote().isEmpty())
insilmaril@0
    81
		{
insilmaril@183
    82
			s =bo->getNoteASCII();
insilmaril@131
    83
			s=s.replace ("\n","\n"+actIndent);
insilmaril@131
    84
			ts << (s+"\n\n");
insilmaril@0
    85
		}
insilmaril@0
    86
		
insilmaril@0
    87
		bo=bo->next();
insilmaril@0
    88
		actIndent="";
insilmaril@0
    89
	}
insilmaril@0
    90
	file.close();
insilmaril@0
    91
}
insilmaril@0
    92
insilmaril@171
    93
void ExportLaTeX::exportLaTeX() 
insilmaril@0
    94
{
insilmaril@183
    95
	// Exports a map to a LaTex file.  
insilmaril@183
    96
	// This file needs to be included 
insilmaril@183
    97
	// or inported into a LaTex document
insilmaril@183
    98
	// it will not add a preamble, or anything 
insilmaril@183
    99
	// that makes a full LaTex document.
insilmaril@163
   100
  QFile file (filepath);
insilmaril@163
   101
  if ( !file.open( IO_WriteOnly ) ) {
insilmaril@163
   102
    // FIXME
insilmaril@163
   103
    cout << "Export::exportMap  couldn't open "<<filepath<<endl;
insilmaril@163
   104
    return;
insilmaril@163
   105
  }
insilmaril@163
   106
  QTextStream ts( &file );	// use LANG decoding here...
insilmaril@163
   107
  ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
insilmaril@163
   108
  
insilmaril@163
   109
  // Main loop over all branches
insilmaril@163
   110
  QString s;
insilmaril@163
   111
  // QString actIndent("");
insilmaril@163
   112
  // int i;
insilmaril@163
   113
  BranchObj *bo;
insilmaril@163
   114
  bo=mapCenter->first();
insilmaril@163
   115
  while (bo) {
insilmaril@163
   116
    if (bo->getDepth()==0);
insilmaril@163
   117
    else if (bo->getDepth()==1) {
insilmaril@163
   118
      ts << ("\\chapter{" + bo->getHeading()+ "}\n");
insilmaril@163
   119
    }
insilmaril@163
   120
    else if (bo->getDepth()==2) {
insilmaril@163
   121
      ts << ("\\section{" + bo->getHeading()+ "}\n");
insilmaril@163
   122
    }
insilmaril@163
   123
    else if (bo->getDepth()==3) {
insilmaril@163
   124
      ts << ("\\subsection{" + bo->getHeading()+ "}\n");
insilmaril@163
   125
    }
insilmaril@163
   126
    else if (bo->getDepth()==4) {
insilmaril@163
   127
      ts << ("\\subsubsection{" + bo->getHeading()+ "}\n");
insilmaril@163
   128
    }
insilmaril@163
   129
    else {
insilmaril@163
   130
      ts << ("\\paragraph*{" + bo->getHeading()+ "}\n");
insilmaril@163
   131
    }
insilmaril@163
   132
    
insilmaril@163
   133
    // If necessary, write note
insilmaril@163
   134
    if (!bo->getNote().isEmpty()) {
insilmaril@183
   135
      ts << (bo->getNoteASCII());
insilmaril@163
   136
      ts << ("\n");
insilmaril@163
   137
    }
insilmaril@163
   138
    
insilmaril@163
   139
    bo=bo->next();
insilmaril@163
   140
   }
insilmaril@163
   141
  file.close();
insilmaril@0
   142
}
insilmaril@0
   143
insilmaril@171
   144
ExportOO::ExportOO()
insilmaril@171
   145
{
insilmaril@171
   146
	// Create tmpdir and set paths
insilmaril@171
   147
	tmpDir.setPath (makeUniqueDir("/tmp/vym-XXXXXX"));
insilmaril@183
   148
	templateDir="exports/novell/template/";
insilmaril@171
   149
	contentTemplateFile=templateDir+"content-template.xml";
insilmaril@171
   150
	contentFile=tmpDir.path()+"/content.xml";
insilmaril@171
   151
	pageTemplateFile=templateDir+"page-template.xml";
insilmaril@171
   152
	chapterTemplateFile=templateDir+"chapter-template.xml";
insilmaril@163
   153
insilmaril@183
   154
	outputFile=tmpDir.currentDirPath()+"/test.odp";
insilmaril@171
   155
}
insilmaril@163
   156
insilmaril@171
   157
ExportOO::~ExportOO()
insilmaril@171
   158
{
insilmaril@171
   159
	// Remove tmpdir
insilmaril@183
   160
	removeDir (tmpDir);
insilmaril@171
   161
}	
insilmaril@163
   162
insilmaril@171
   163
QString ExportOO::buildList (BranchObj *current)
insilmaril@171
   164
{
insilmaril@171
   165
    QString r;
insilmaril@171
   166
    BranchObj *bo;
insilmaril@183
   167
insilmaril@183
   168
    uint i=0;
insilmaril@171
   169
    bo=current->getFirstBranch();
insilmaril@171
   170
    if (bo)
insilmaril@171
   171
    {
insilmaril@171
   172
        // Start list
insilmaril@183
   173
        r+="<text:list text:style-name=\"L4\">\n";
insilmaril@171
   174
        while (bo)
insilmaril@171
   175
        {
insilmaril@183
   176
            r+="<text:list-item><text:p >";
insilmaril@183
   177
			r+=quotemeta(bo->getHeading());
insilmaril@183
   178
			// If necessary, write note
insilmaril@183
   179
			if (!bo->getNote().isEmpty())
insilmaril@183
   180
				r+=bo->getNoteOpenDoc();
insilmaril@183
   181
			r+="</text:p>";
insilmaril@183
   182
			r+=buildList (bo);	// recursivly add deeper branches
insilmaril@183
   183
            r+="</text:list-item>\n";
insilmaril@183
   184
			i++;
insilmaril@183
   185
			bo=current->getBranchNum(i);
insilmaril@171
   186
        }
insilmaril@171
   187
        r+="</text:list>\n";
insilmaril@171
   188
    }
insilmaril@171
   189
    return r;
insilmaril@171
   190
}
insilmaril@163
   191
insilmaril@171
   192
insilmaril@171
   193
void ExportOO::exportPresentation()
insilmaril@171
   194
{
insilmaril@183
   195
	QString allPages;
insilmaril@171
   196
insilmaril@183
   197
	bool useChapters=true;
insilmaril@183
   198
insilmaril@163
   199
	// Insert new content
insilmaril@171
   200
	content.replace ("<!-- INSERT TITLE -->",quotemeta(mapCenter->getHeading()));
insilmaril@171
   201
	content.replace ("<!-- INSERT AUTHOR -->",quotemeta(mapCenter->getAuthor()));
insilmaril@183
   202
insilmaril@183
   203
	QString	onePage;
insilmaril@183
   204
	QString list;
insilmaril@183
   205
	
insilmaril@183
   206
	BranchObj *chapterBO=mapCenter->getFirstBranch();
insilmaril@183
   207
    int i=0;
insilmaril@183
   208
	BranchObj *pagesBO;
insilmaril@183
   209
    int j=0;
insilmaril@183
   210
insilmaril@183
   211
	// Walk chapters
insilmaril@183
   212
	while (chapterBO)
insilmaril@183
   213
	{
insilmaril@183
   214
		if (useChapters)
insilmaril@183
   215
		{
insilmaril@183
   216
			// Add page with chapter title
insilmaril@183
   217
			onePage=chapterTemplate;
insilmaril@183
   218
			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(chapterBO->getHeading() ) );
insilmaril@183
   219
			allPages+=onePage;
insilmaril@183
   220
		} else
insilmaril@183
   221
		{
insilmaril@183
   222
			i=-2;	// only use inner loop to 
insilmaril@183
   223
			        // turn mainbranches into pages
insilmaril@183
   224
			chapterBO=mapCenter;
insilmaril@183
   225
		}
insilmaril@183
   226
insilmaril@183
   227
		// Walk mainpages
insilmaril@183
   228
		pagesBO=chapterBO->getFirstBranch();
insilmaril@183
   229
		j=0;
insilmaril@183
   230
		while (pagesBO)
insilmaril@183
   231
		{
insilmaril@183
   232
			// Add page with list of items
insilmaril@183
   233
			onePage=pageTemplate;
insilmaril@183
   234
			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
insilmaril@183
   235
			list=buildList (pagesBO);
insilmaril@183
   236
			onePage.replace ("<!-- INSERT LIST -->", list);
insilmaril@183
   237
			allPages+=onePage;
insilmaril@183
   238
			j++;
insilmaril@183
   239
			pagesBO=chapterBO->getBranchNum(j);
insilmaril@183
   240
		}
insilmaril@183
   241
		i++;
insilmaril@183
   242
		chapterBO=mapCenter->getBranchNum(i);
insilmaril@183
   243
	}
insilmaril@183
   244
	
insilmaril@171
   245
	content.replace ("<!-- INSERT PAGES -->",allPages);
insilmaril@171
   246
insilmaril@171
   247
	// Write modified content
insilmaril@171
   248
	QFile f (contentFile);
insilmaril@171
   249
    if ( !f.open( IO_WriteOnly ) ) 
insilmaril@171
   250
	{
insilmaril@171
   251
		mainWindow->statusMessage(QString(QObject::tr("Could not write to %1")).arg(outputFile));
insilmaril@171
   252
		return;
insilmaril@171
   253
    }
insilmaril@171
   254
insilmaril@171
   255
    QTextStream t( &f );
insilmaril@171
   256
    t << content;
insilmaril@171
   257
    f.close();
insilmaril@171
   258
insilmaril@163
   259
	// zip tmpdir to destination
insilmaril@171
   260
	zipDir (tmpDir,outputFile);	
insilmaril@163
   261
}
insilmaril@163
   262
insilmaril@183
   263
void ExportOO::setConfigFile (const QString &cf)
insilmaril@183
   264
{
insilmaril@183
   265
	configFile=cf;
insilmaril@183
   266
	int i=cf.findRev ("/");
insilmaril@183
   267
	if (i>=0) configDir=cf.left(i);
insilmaril@183
   268
	SimpleSettings set;
insilmaril@183
   269
	set.readSettings(configFile);
insilmaril@183
   270
insilmaril@183
   271
	// set paths
insilmaril@183
   272
	templateDir=configDir+"/"+set.readEntry ("Template");
insilmaril@183
   273
insilmaril@183
   274
	//FIXME check if templateDir is really set, otherwise abort
insilmaril@183
   275
insilmaril@183
   276
	contentTemplateFile=templateDir+"content-template.xml";
insilmaril@183
   277
	contentFile=tmpDir.path()+"/content.xml";
insilmaril@183
   278
	pageTemplateFile=templateDir+"page-template.xml";
insilmaril@183
   279
	chapterTemplateFile=templateDir+"chapter-template.xml";
insilmaril@183
   280
insilmaril@183
   281
	outputFile=tmpDir.currentDirPath()+"/test.odp";
insilmaril@183
   282
	
insilmaril@183
   283
	// Copy template to tmpdir
insilmaril@183
   284
	system ("cp -r "+templateDir+"* "+tmpDir.path());
insilmaril@183
   285
	
insilmaril@183
   286
insilmaril@183
   287
	// Read content-template
insilmaril@183
   288
	if (!loadStringFromDisk (contentTemplateFile,content))
insilmaril@183
   289
	{
insilmaril@183
   290
		qWarning ("Export::exportOO() Couldn't load from "+contentTemplateFile);
insilmaril@183
   291
		return;
insilmaril@183
   292
	}
insilmaril@183
   293
insilmaril@183
   294
	// Read page-template
insilmaril@183
   295
	if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
insilmaril@183
   296
	{
insilmaril@183
   297
		qWarning ("Export::exportOO() Couldn't load from "+pageTemplateFile);
insilmaril@183
   298
		return;
insilmaril@183
   299
	}
insilmaril@183
   300
	
insilmaril@183
   301
	// Read chapter-template
insilmaril@183
   302
	if (!loadStringFromDisk (chapterTemplateFile,chapterTemplate))
insilmaril@183
   303
	{
insilmaril@183
   304
		qWarning ("Export::exportOO() Couldn't load from "+chapterTemplateFile);
insilmaril@183
   305
		return;
insilmaril@183
   306
	}
insilmaril@183
   307
insilmaril@183
   308
}
insilmaril@183
   309