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