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