exportxhtmldialog.cpp
author insilmaril
Wed, 04 Jul 2007 11:05:52 +0000
changeset 541 2501bce00de9
parent 515 f396157bbb06
child 624 4ffc47ae27e4
permissions -rw-r--r--
Version 1.9.0
insilmaril@382
     1
#include "exportxhtmldialog.h"
insilmaril@382
     2
insilmaril@382
     3
#include <QFileDialog>
insilmaril@382
     4
#include <QMessageBox>
insilmaril@382
     5
#include <QTextStream>
insilmaril@382
     6
insilmaril@382
     7
#include "options.h"
insilmaril@382
     8
#include "settings.h"
insilmaril@382
     9
insilmaril@382
    10
insilmaril@382
    11
extern Options options;
insilmaril@382
    12
extern QDir vymBaseDir;
insilmaril@382
    13
extern Settings settings;
insilmaril@515
    14
extern bool debug;
insilmaril@382
    15
insilmaril@382
    16
ExportXHTMLDialog::ExportXHTMLDialog(QWidget* parent) : QDialog(parent)
insilmaril@382
    17
{
insilmaril@382
    18
    ui.setupUi(this);
insilmaril@382
    19
insilmaril@382
    20
	filepath="";
insilmaril@382
    21
	settingsChanged=false;
insilmaril@382
    22
	scriptProc=new Process;
insilmaril@382
    23
insilmaril@382
    24
    // signals and slots connections
insilmaril@382
    25
    connect(ui.browseExportDirButton, SIGNAL(pressed()), this, SLOT(browseDirectoryPressed()));
insilmaril@382
    26
    connect(ui.outputButton, SIGNAL(toggled(bool)), this, SLOT(outputButtonPressed(bool)));
insilmaril@382
    27
    connect(ui.browseXSLButton, SIGNAL(pressed()), this, SLOT(browseXSLPressed()));
insilmaril@382
    28
    connect(ui.browseCSSButton, SIGNAL(pressed()), this, SLOT(browseCSSPressed()));
insilmaril@382
    29
    connect(ui.imageButton, SIGNAL(toggled(bool)), this, SLOT(imageButtonPressed(bool)));
insilmaril@382
    30
    connect(ui.textColorButton, SIGNAL(toggled(bool)), this, SLOT(textcolorButtonPressed(bool)));
insilmaril@382
    31
    connect(ui.lineEditDir, SIGNAL(textChanged(const QString&)), this, SLOT(dirChanged()));
insilmaril@382
    32
    connect(ui.lineEditCSS, SIGNAL(textChanged(const QString&)), this, SLOT(cssChanged()));
insilmaril@382
    33
    connect(ui.lineEditXSL, SIGNAL(textChanged(const QString&)), this, SLOT(xslChanged()));
insilmaril@382
    34
    connect(ui.warningsButton, SIGNAL(toggled(bool)), this, SLOT(warningsButtonPressed(bool)));
insilmaril@382
    35
    connect(ui.saveSettingsInMapButton, SIGNAL(toggled(bool)), this, SLOT(saveSettingsInMapButtonPressed(bool)));
insilmaril@382
    36
    connect(ui.browsePreExportButton, SIGNAL(pressed()), this, SLOT(browsePreExportButtonPressed()));
insilmaril@382
    37
    connect(ui.lineEditPreScript, SIGNAL(textChanged(const QString&)), this, SLOT(prescriptChanged()));
insilmaril@382
    38
    connect(ui.lineEditPostScript, SIGNAL(textChanged(const QString&)), this, SLOT(postscriptChanged()));
insilmaril@382
    39
    connect(ui.browsePostExportButton, SIGNAL(pressed()), this, SLOT(browsePostExportButtonPressed()));
insilmaril@382
    40
}	
insilmaril@382
    41
insilmaril@382
    42
insilmaril@382
    43
void ExportXHTMLDialog::readSettings()
insilmaril@382
    44
{
insilmaril@382
    45
insilmaril@382
    46
	dir=settings.readLocalEntry (filepath,"/export/xhtml/exportDir",vymBaseDir.currentDirPath() );
insilmaril@382
    47
	ui.lineEditDir->setText(dir);
insilmaril@382
    48
	
insilmaril@382
    49
    if ( settings.readLocalEntry (filepath,"/export/xhtml/useImage","yes")=="yes")
insilmaril@382
    50
		useImage=true;
insilmaril@382
    51
	else	
insilmaril@382
    52
		useImage=false;
insilmaril@382
    53
	ui.imageButton->setChecked(useImage);
insilmaril@382
    54
		
insilmaril@382
    55
	if ( settings.readLocalEntry (filepath,"/export/xhtml/useTextColor","no")=="yes")
insilmaril@382
    56
		useTextColor=true;
insilmaril@382
    57
	else	
insilmaril@382
    58
		useTextColor=false;
insilmaril@382
    59
	ui.textColorButton->setChecked(useTextColor);
insilmaril@382
    60
	
insilmaril@412
    61
/* TODO this was used in old html export, is not yet in new stylesheet
insilmaril@382
    62
	if ( settings.readEntry ("/export/html/useHeading","no")=="yes")
insilmaril@382
    63
		useHeading=true;
insilmaril@382
    64
	else	
insilmaril@382
    65
		useHeading=false;
insilmaril@382
    66
	checkBox4_2->setChecked(useHeading);
insilmaril@382
    67
*/		
insilmaril@382
    68
insilmaril@382
    69
	if ( settings.readLocalEntry (filepath,"/export/xhtml/saveSettingsInMap","no")=="yes")
insilmaril@382
    70
		saveSettingsInMap=true;
insilmaril@382
    71
	else	
insilmaril@382
    72
		saveSettingsInMap=false;
insilmaril@382
    73
	ui.saveSettingsInMapButton->setChecked(saveSettingsInMap);
insilmaril@382
    74
insilmaril@382
    75
	if ( settings.readEntry ("/export/xhtml/showWarnings","yes")=="yes")
insilmaril@382
    76
		showWarnings=true;
insilmaril@382
    77
	else	
insilmaril@382
    78
		showWarnings=false;
insilmaril@382
    79
	ui.warningsButton->setChecked(showWarnings);
insilmaril@382
    80
	
insilmaril@382
    81
	if ( settings.readEntry ("/export/xhtml/showOutput","no")=="yes")
insilmaril@382
    82
		showOutput=true;
insilmaril@382
    83
	else	
insilmaril@382
    84
		showOutput=false;
insilmaril@382
    85
	ui.outputButton->setChecked(showOutput);
insilmaril@382
    86
insilmaril@382
    87
	// For testing better use local styles
insilmaril@382
    88
	if (options.isOn ("local"))
insilmaril@382
    89
	{
insilmaril@382
    90
		xsl=vymBaseDir.path()+"/styles/vym2xhtml.xsl";
insilmaril@382
    91
		css=vymBaseDir.path()+"/styles/vym.css";
insilmaril@382
    92
	} else
insilmaril@382
    93
	{
insilmaril@382
    94
		xsl=settings.readLocalEntry 
insilmaril@382
    95
			(filepath,"/export/xhtml/xsl","/usr/share/vym/styles/vym2xhtml.xsl");
insilmaril@382
    96
		css=settings.readLocalEntry 
insilmaril@382
    97
			(filepath,"/export/xhtml/css","/usr/share/vym/styles/vym.css");	
insilmaril@382
    98
	}
insilmaril@382
    99
	ui.lineEditXSL->setText(xsl);
insilmaril@382
   100
	ui.lineEditCSS->setText(css);
insilmaril@382
   101
	
insilmaril@382
   102
	prescript=settings.readLocalEntry
insilmaril@382
   103
		(filepath,"/export/xhtml/prescript","");
insilmaril@382
   104
	ui.lineEditPreScript->setText (prescript);	
insilmaril@382
   105
	
insilmaril@382
   106
	postscript=settings.readLocalEntry
insilmaril@382
   107
		(filepath,"/export/xhtml/postscript","");
insilmaril@382
   108
	ui.lineEditPostScript->setText (postscript);	
insilmaril@382
   109
insilmaril@382
   110
	if (!prescript.isEmpty() || !postscript.isEmpty())
insilmaril@382
   111
	{
insilmaril@382
   112
		QMessageBox::warning( 0, tr( "Warning" ),tr(
insilmaril@382
   113
		"The settings saved in the map "
insilmaril@382
   114
		"would like to run scripts:\n\n"
insilmaril@382
   115
		"%1\n\n"
insilmaril@382
   116
		"Please check, if you really\n"
insilmaril@382
   117
		"want to allow this in your system!").arg(prescript+"  "+postscript));
insilmaril@382
   118
		
insilmaril@382
   119
	}
insilmaril@382
   120
}
insilmaril@382
   121
insilmaril@515
   122
void ExportXHTMLDialog::setDir(const QString &d)
insilmaril@515
   123
{
insilmaril@515
   124
	dir=d;
insilmaril@515
   125
	if (dir.right(1)!="/") dir+="/";
insilmaril@515
   126
}
insilmaril@515
   127
insilmaril@382
   128
void ExportXHTMLDialog::dirChanged()
insilmaril@382
   129
{
insilmaril@515
   130
	setDir (ui.lineEditDir->text());
insilmaril@382
   131
	settingsChanged=true;
insilmaril@382
   132
}
insilmaril@382
   133
insilmaril@382
   134
void ExportXHTMLDialog::browseDirectoryPressed()
insilmaril@382
   135
{
insilmaril@382
   136
   	QFileDialog fd( this);
insilmaril@382
   137
	fd.setMode (QFileDialog::DirectoryOnly);
insilmaril@382
   138
	fd.setCaption(tr("VYM - Export HTML to directory"));
insilmaril@382
   139
	fd.setModal (true);
insilmaril@382
   140
	fd.setDirectory (QDir::current());
insilmaril@382
   141
	fd.show();
insilmaril@382
   142
insilmaril@382
   143
	if ( fd.exec() == QDialog::Accepted )
insilmaril@382
   144
	{
insilmaril@382
   145
		dir=fd.selectedFile();
insilmaril@382
   146
		ui.lineEditDir->setText (dir );
insilmaril@382
   147
		settingsChanged=true;
insilmaril@382
   148
	}
insilmaril@382
   149
}
insilmaril@382
   150
insilmaril@382
   151
void ExportXHTMLDialog::imageButtonPressed(bool b)
insilmaril@382
   152
{
insilmaril@382
   153
	useImage=b;
insilmaril@382
   154
	settingsChanged=true;
insilmaril@382
   155
}
insilmaril@382
   156
insilmaril@382
   157
void ExportXHTMLDialog::textcolorButtonPressed(bool b)
insilmaril@382
   158
{
insilmaril@382
   159
	useTextColor=b;	
insilmaril@382
   160
	settingsChanged=true;
insilmaril@382
   161
}
insilmaril@382
   162
insilmaril@382
   163
void ExportXHTMLDialog::saveSettingsInMapButtonPressed(bool b)
insilmaril@382
   164
{
insilmaril@382
   165
	saveSettingsInMap=b;	
insilmaril@382
   166
	settingsChanged=true;
insilmaril@382
   167
}
insilmaril@382
   168
insilmaril@382
   169
void ExportXHTMLDialog::warningsButtonPressed(bool b)
insilmaril@382
   170
{
insilmaril@382
   171
	showWarnings=b;
insilmaril@382
   172
	settingsChanged=true;
insilmaril@382
   173
}
insilmaril@382
   174
insilmaril@382
   175
insilmaril@382
   176
void ExportXHTMLDialog::outputButtonPressed(bool b)
insilmaril@382
   177
{
insilmaril@382
   178
	showOutput=b;
insilmaril@382
   179
	settingsChanged=true;
insilmaril@382
   180
}
insilmaril@382
   181
insilmaril@382
   182
void ExportXHTMLDialog::cssChanged()
insilmaril@382
   183
{
insilmaril@382
   184
	css=ui.lineEditCSS->text();
insilmaril@382
   185
	settingsChanged=true;
insilmaril@382
   186
}
insilmaril@382
   187
insilmaril@382
   188
void ExportXHTMLDialog::browseCSSPressed()
insilmaril@382
   189
{
insilmaril@382
   190
   	QFileDialog fd( this);
insilmaril@382
   191
	fd.setModal (true);
insilmaril@382
   192
	fd.setFilter ("Cascading Stylesheet (*.css)");
insilmaril@382
   193
	fd.setDirectory (QDir::current());
insilmaril@382
   194
	fd.show();
insilmaril@382
   195
insilmaril@382
   196
	if ( fd.exec() == QDialog::Accepted )
insilmaril@382
   197
	{
insilmaril@382
   198
		css=fd.selectedFile();
insilmaril@382
   199
		ui.lineEditCSS->setText (css );
insilmaril@382
   200
		settingsChanged=true;
insilmaril@382
   201
	}
insilmaril@382
   202
}
insilmaril@382
   203
insilmaril@382
   204
void ExportXHTMLDialog::xslChanged()
insilmaril@382
   205
{
insilmaril@382
   206
	xsl=ui.lineEditXSL->text();
insilmaril@382
   207
	settingsChanged=true;
insilmaril@382
   208
}
insilmaril@382
   209
insilmaril@382
   210
void ExportXHTMLDialog::prescriptChanged()
insilmaril@382
   211
{
insilmaril@382
   212
	prescript=ui.lineEditPreScript->text();
insilmaril@382
   213
	settingsChanged=true;
insilmaril@382
   214
}
insilmaril@382
   215
insilmaril@382
   216
void ExportXHTMLDialog::browseXSLPressed()
insilmaril@382
   217
{
insilmaril@382
   218
   	QFileDialog fd( this);
insilmaril@382
   219
	fd.setModal (true);
insilmaril@382
   220
	fd.setFilter ("Extensible Stylesheet Language (*.xsl)");
insilmaril@382
   221
	fd.setDirectory (QDir::current());
insilmaril@382
   222
	fd.show();
insilmaril@382
   223
insilmaril@382
   224
	if ( fd.exec() == QDialog::Accepted )
insilmaril@382
   225
	{
insilmaril@382
   226
		xsl=fd.selectedFile();
insilmaril@382
   227
		ui.lineEditXSL->setText (xsl );
insilmaril@382
   228
		settingsChanged=true;
insilmaril@382
   229
	}
insilmaril@382
   230
}
insilmaril@382
   231
insilmaril@382
   232
void ExportXHTMLDialog::postscriptChanged()
insilmaril@382
   233
{
insilmaril@382
   234
	postscript=ui.lineEditPostScript->text();
insilmaril@382
   235
	settingsChanged=true;
insilmaril@382
   236
}
insilmaril@382
   237
insilmaril@382
   238
void ExportXHTMLDialog::browsePreExportButtonPressed()
insilmaril@382
   239
{
insilmaril@382
   240
	QFileDialog fd( this);
insilmaril@382
   241
	fd.setModal (true);
insilmaril@382
   242
	fd.setFilter ("Scripts (*.sh *.pl *.py *.php)");
insilmaril@382
   243
	fd.setDirectory (QDir::current());
insilmaril@382
   244
	fd.show();
insilmaril@382
   245
insilmaril@382
   246
	if ( fd.exec() == QDialog::Accepted )
insilmaril@382
   247
	{
insilmaril@382
   248
		prescript=fd.selectedFile();
insilmaril@382
   249
		ui.lineEditPreScript->setText (prescript );
insilmaril@382
   250
		settingsChanged=true;
insilmaril@382
   251
	}
insilmaril@382
   252
insilmaril@382
   253
}
insilmaril@382
   254
insilmaril@382
   255
void ExportXHTMLDialog::browsePostExportButtonPressed()
insilmaril@382
   256
{
insilmaril@382
   257
	QFileDialog fd( this);
insilmaril@382
   258
	fd.setModal (true);
insilmaril@382
   259
	fd.setFilter ("Scripts (*.sh *.pl *.py *.php)");
insilmaril@382
   260
	fd.setDirectory (QDir::current());
insilmaril@382
   261
	fd.show();
insilmaril@382
   262
insilmaril@382
   263
	if ( fd.exec() == QDialog::Accepted )
insilmaril@382
   264
	{
insilmaril@382
   265
		postscript=fd.selectedFile();
insilmaril@382
   266
		ui.lineEditPostScript->setText (postscript );
insilmaril@382
   267
		settingsChanged=true;
insilmaril@382
   268
	}
insilmaril@382
   269
}
insilmaril@382
   270
insilmaril@382
   271
insilmaril@382
   272
void ExportXHTMLDialog::doExport (const QString &mapname)
insilmaril@382
   273
{
insilmaril@382
   274
	// Save options to settings file 
insilmaril@382
   275
	// (but don't save at destructor, which
insilmaril@382
   276
	// is called for "cancel", too)
insilmaril@382
   277
	settings.setLocalEntry (filepath,"/export/xhtml/exportDir",dir);
insilmaril@382
   278
	settings.setLocalEntry (filepath,"/export/xhtml/prescript",prescript);
insilmaril@382
   279
	settings.setLocalEntry (filepath,"/export/xhtml/postscript",postscript);
insilmaril@382
   280
insilmaril@382
   281
    if (useImage)
insilmaril@382
   282
		settings.setLocalEntry (filepath,"/export/xhtml/useImage","yes");
insilmaril@382
   283
    else
insilmaril@382
   284
		settings.setLocalEntry (filepath,"/export/xhtml/useImage","no");	
insilmaril@382
   285
	
insilmaril@382
   286
  if (useTextColor)
insilmaril@382
   287
		settings.setLocalEntry (filepath,"/export/xhtml/useTextColor","yes");
insilmaril@382
   288
    else
insilmaril@382
   289
		settings.setLocalEntry (filepath,"/export/xhtml/useTextColor","no");	
insilmaril@382
   290
	
insilmaril@382
   291
   if (showWarnings)
insilmaril@382
   292
		settings.writeEntry ("/export/xhtml/showWarnings","yes");
insilmaril@382
   293
    else
insilmaril@382
   294
		settings.writeEntry ("/export/xhtml/showWarnings","no");	
insilmaril@382
   295
			
insilmaril@382
   296
	if (showOutput)
insilmaril@382
   297
		settings.writeEntry ("/export/xhtml/showOutput","yes");
insilmaril@382
   298
	else
insilmaril@382
   299
		settings.writeEntry ("/export/xhtml/showOutput","no");	
insilmaril@382
   300
insilmaril@382
   301
	QString ipath;	
insilmaril@382
   302
	ipath=vymBaseDir.path()+"/flags/flag-url-16x16.png";
insilmaril@382
   303
	if (!options.isOn ("local"))
insilmaril@382
   304
	{
insilmaril@382
   305
		settings.setLocalEntry 
insilmaril@382
   306
			(filepath,"/export/xhtml/xsl",xsl);
insilmaril@382
   307
		settings.setLocalEntry 
insilmaril@382
   308
			(filepath,"/export/xhtml/css",css);	
insilmaril@382
   309
	}
insilmaril@382
   310
insilmaril@382
   311
	// Provide a smaller URL-icon to improve Layout
insilmaril@382
   312
	QPixmap pm;
insilmaril@382
   313
	if (!pm.load(ipath,"PNG") )
insilmaril@382
   314
		QMessageBox::warning( 0, tr( "Warning" ),tr("Could not open %1").arg(ipath));
insilmaril@382
   315
		
insilmaril@382
   316
		
insilmaril@382
   317
	if(!pm.save (dir + "flags/flag-url-16x16.png","PNG"))
insilmaril@382
   318
		QMessageBox::warning( 0, tr( "Warning" ),tr("Could not write %1").arg(ipath));
insilmaril@382
   319
	if (!saveSettingsInMap)
insilmaril@382
   320
		settings.clearLocal("/export/xhtml");
insilmaril@382
   321
	else	
insilmaril@382
   322
		settings.setLocalEntry 
insilmaril@382
   323
			(filepath,"/export/xhtml/saveSettingsInMap","yes");
insilmaril@382
   324
insilmaril@382
   325
	// Copy CSS file
insilmaril@382
   326
	QFile css_src (css);
insilmaril@382
   327
	QFile css_dst (dir+"vym.css");
insilmaril@382
   328
	if (!css_src.open ( QIODevice::ReadOnly))
insilmaril@382
   329
		QMessageBox::warning( 0, tr( "Warning" ),tr("Could not open %1").arg(css));
insilmaril@382
   330
	else
insilmaril@382
   331
	{
insilmaril@382
   332
		if (!css_dst.open( QIODevice::WriteOnly))
insilmaril@382
   333
			QMessageBox::warning( 0, tr( "Warning" ), tr("Could not open %1").arg(dir+"vym.css"));
insilmaril@382
   334
		else
insilmaril@382
   335
		{	
insilmaril@382
   336
		
insilmaril@382
   337
			QTextStream tsout( &css_dst);
insilmaril@382
   338
			QTextStream tsin ( &css_src);
insilmaril@382
   339
			QString s= tsin.read();
insilmaril@382
   340
			tsout << s;
insilmaril@382
   341
			css_dst.close();
insilmaril@382
   342
		}	
insilmaril@382
   343
		css_src.close();
insilmaril@382
   344
	}
insilmaril@382
   345
insilmaril@382
   346
	if (!prescript.isEmpty()) runScript (prescript,dir+mapname+".xml");
insilmaril@382
   347
	
insilmaril@382
   348
	if (useImage)
insilmaril@382
   349
		p.addStringParam ("imagemap","images/"+mapname+".png");
insilmaril@382
   350
	if (useTextColor)
insilmaril@382
   351
		p.addStringParam ("use.textcolor","1");
insilmaril@382
   352
	p.addStringParam ("mapname",mapname+".vym");
insilmaril@382
   353
	
insilmaril@382
   354
	p.setOutputFile (dir+mapname+".html");
insilmaril@382
   355
	p.setInputFile (dir+mapname+".xml");
insilmaril@382
   356
	p.setXSLFile (xsl);
insilmaril@382
   357
	p.process();
insilmaril@382
   358
insilmaril@382
   359
	if (!postscript.isEmpty()) runScript (postscript,dir+mapname+".html");
insilmaril@382
   360
insilmaril@382
   361
}
insilmaril@382
   362
insilmaril@382
   363
void ExportXHTMLDialog::setFilePath(const QString &s)
insilmaril@382
   364
{
insilmaril@382
   365
	filepath=s;
insilmaril@382
   366
}
insilmaril@382
   367
insilmaril@382
   368
void ExportXHTMLDialog::setMapName(const QString &s)
insilmaril@382
   369
{
insilmaril@382
   370
	mapname=s;
insilmaril@382
   371
}
insilmaril@382
   372
insilmaril@382
   373
QString ExportXHTMLDialog::getDir()
insilmaril@382
   374
{
insilmaril@382
   375
	return dir;
insilmaril@382
   376
}
insilmaril@382
   377
insilmaril@382
   378
bool ExportXHTMLDialog::warnings()
insilmaril@382
   379
{
insilmaril@382
   380
	return showWarnings;
insilmaril@382
   381
}
insilmaril@382
   382
insilmaril@382
   383
bool ExportXHTMLDialog::hasChanged()
insilmaril@382
   384
{
insilmaril@382
   385
	return settingsChanged;
insilmaril@382
   386
}
insilmaril@382
   387
insilmaril@382
   388
insilmaril@382
   389
void ExportXHTMLDialog::runScript(QString spath, QString fpath)
insilmaril@382
   390
{
insilmaril@382
   391
	spath.replace ("%f",fpath);
insilmaril@382
   392
	QStringList args=QStringList::split (' ',spath,false);
insilmaril@382
   393
		
insilmaril@408
   394
	p.addOutput ("vym is executing: \n" + spath+" "+args.join(" ") );	
insilmaril@408
   395
	scriptProc->start (spath,args);
insilmaril@408
   396
	if (!scriptProc->waitForStarted() )
insilmaril@382
   397
	{
insilmaril@382
   398
		QMessageBox::critical( 0, tr( "Critical Error" ),
insilmaril@382
   399
					   tr("Could not start %1").arg(spath) );
insilmaril@382
   400
	} else
insilmaril@382
   401
	{
insilmaril@428
   402
		if (!scriptProc->waitForFinished())
insilmaril@382
   403
			QMessageBox::critical( 0, tr( "Critical Error" ),
insilmaril@382
   404
			   tr("%1 didn't exit normally").arg(spath) +
insilmaril@382
   405
			   scriptProc->getErrout() );
insilmaril@382
   406
		else
insilmaril@382
   407
			if (scriptProc->exitStatus()>0) showOutput=true;
insilmaril@382
   408
			
insilmaril@382
   409
	}	
insilmaril@382
   410
	p.addOutput ("\n");
insilmaril@382
   411
	p.addOutput (scriptProc->getErrout());
insilmaril@382
   412
	p.addOutput (scriptProc->getStdout());
insilmaril@382
   413
}
insilmaril@382
   414