exportxhtmldialog.cpp
author insilmaril
Mon, 08 Dec 2008 16:57:33 +0000
changeset 729 7ddbe3fa34a1
parent 624 4ffc47ae27e4
child 822 c2ce9944148c
permissions -rw-r--r--
More fixes for selections
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@624
    88
    const QString defxsl(vymBaseDir.path() + "/styles/vym2xhtml.xsl");
insilmaril@624
    89
    const QString defcss(vymBaseDir.path() + "/styles/vym.css");
insilmaril@382
    90
	if (options.isOn ("local"))
insilmaril@382
    91
	{
insilmaril@624
    92
		xsl=defxsl;
insilmaril@624
    93
		css=defcss;
insilmaril@382
    94
	} else
insilmaril@382
    95
	{
insilmaril@382
    96
		xsl=settings.readLocalEntry 
insilmaril@624
    97
			(filepath,"/export/xhtml/xsl",defxsl);
insilmaril@382
    98
		css=settings.readLocalEntry 
insilmaril@624
    99
			(filepath,"/export/xhtml/css",defcss);	
insilmaril@382
   100
	}
insilmaril@382
   101
	ui.lineEditXSL->setText(xsl);
insilmaril@382
   102
	ui.lineEditCSS->setText(css);
insilmaril@382
   103
	
insilmaril@382
   104
	prescript=settings.readLocalEntry
insilmaril@382
   105
		(filepath,"/export/xhtml/prescript","");
insilmaril@382
   106
	ui.lineEditPreScript->setText (prescript);	
insilmaril@382
   107
	
insilmaril@382
   108
	postscript=settings.readLocalEntry
insilmaril@382
   109
		(filepath,"/export/xhtml/postscript","");
insilmaril@382
   110
	ui.lineEditPostScript->setText (postscript);	
insilmaril@382
   111
insilmaril@382
   112
	if (!prescript.isEmpty() || !postscript.isEmpty())
insilmaril@382
   113
	{
insilmaril@382
   114
		QMessageBox::warning( 0, tr( "Warning" ),tr(
insilmaril@382
   115
		"The settings saved in the map "
insilmaril@382
   116
		"would like to run scripts:\n\n"
insilmaril@382
   117
		"%1\n\n"
insilmaril@382
   118
		"Please check, if you really\n"
insilmaril@382
   119
		"want to allow this in your system!").arg(prescript+"  "+postscript));
insilmaril@382
   120
		
insilmaril@382
   121
	}
insilmaril@382
   122
}
insilmaril@382
   123
insilmaril@515
   124
void ExportXHTMLDialog::setDir(const QString &d)
insilmaril@515
   125
{
insilmaril@515
   126
	dir=d;
insilmaril@515
   127
	if (dir.right(1)!="/") dir+="/";
insilmaril@515
   128
}
insilmaril@515
   129
insilmaril@382
   130
void ExportXHTMLDialog::dirChanged()
insilmaril@382
   131
{
insilmaril@515
   132
	setDir (ui.lineEditDir->text());
insilmaril@382
   133
	settingsChanged=true;
insilmaril@382
   134
}
insilmaril@382
   135
insilmaril@382
   136
void ExportXHTMLDialog::browseDirectoryPressed()
insilmaril@382
   137
{
insilmaril@382
   138
   	QFileDialog fd( this);
insilmaril@382
   139
	fd.setMode (QFileDialog::DirectoryOnly);
insilmaril@382
   140
	fd.setCaption(tr("VYM - Export HTML to directory"));
insilmaril@382
   141
	fd.setModal (true);
insilmaril@382
   142
	fd.setDirectory (QDir::current());
insilmaril@382
   143
	fd.show();
insilmaril@382
   144
insilmaril@382
   145
	if ( fd.exec() == QDialog::Accepted )
insilmaril@382
   146
	{
insilmaril@382
   147
		dir=fd.selectedFile();
insilmaril@382
   148
		ui.lineEditDir->setText (dir );
insilmaril@382
   149
		settingsChanged=true;
insilmaril@382
   150
	}
insilmaril@382
   151
}
insilmaril@382
   152
insilmaril@382
   153
void ExportXHTMLDialog::imageButtonPressed(bool b)
insilmaril@382
   154
{
insilmaril@382
   155
	useImage=b;
insilmaril@382
   156
	settingsChanged=true;
insilmaril@382
   157
}
insilmaril@382
   158
insilmaril@382
   159
void ExportXHTMLDialog::textcolorButtonPressed(bool b)
insilmaril@382
   160
{
insilmaril@382
   161
	useTextColor=b;	
insilmaril@382
   162
	settingsChanged=true;
insilmaril@382
   163
}
insilmaril@382
   164
insilmaril@382
   165
void ExportXHTMLDialog::saveSettingsInMapButtonPressed(bool b)
insilmaril@382
   166
{
insilmaril@382
   167
	saveSettingsInMap=b;	
insilmaril@382
   168
	settingsChanged=true;
insilmaril@382
   169
}
insilmaril@382
   170
insilmaril@382
   171
void ExportXHTMLDialog::warningsButtonPressed(bool b)
insilmaril@382
   172
{
insilmaril@382
   173
	showWarnings=b;
insilmaril@382
   174
	settingsChanged=true;
insilmaril@382
   175
}
insilmaril@382
   176
insilmaril@382
   177
insilmaril@382
   178
void ExportXHTMLDialog::outputButtonPressed(bool b)
insilmaril@382
   179
{
insilmaril@382
   180
	showOutput=b;
insilmaril@382
   181
	settingsChanged=true;
insilmaril@382
   182
}
insilmaril@382
   183
insilmaril@382
   184
void ExportXHTMLDialog::cssChanged()
insilmaril@382
   185
{
insilmaril@382
   186
	css=ui.lineEditCSS->text();
insilmaril@382
   187
	settingsChanged=true;
insilmaril@382
   188
}
insilmaril@382
   189
insilmaril@382
   190
void ExportXHTMLDialog::browseCSSPressed()
insilmaril@382
   191
{
insilmaril@382
   192
   	QFileDialog fd( this);
insilmaril@382
   193
	fd.setModal (true);
insilmaril@382
   194
	fd.setFilter ("Cascading Stylesheet (*.css)");
insilmaril@382
   195
	fd.setDirectory (QDir::current());
insilmaril@382
   196
	fd.show();
insilmaril@382
   197
insilmaril@382
   198
	if ( fd.exec() == QDialog::Accepted )
insilmaril@382
   199
	{
insilmaril@382
   200
		css=fd.selectedFile();
insilmaril@382
   201
		ui.lineEditCSS->setText (css );
insilmaril@382
   202
		settingsChanged=true;
insilmaril@382
   203
	}
insilmaril@382
   204
}
insilmaril@382
   205
insilmaril@382
   206
void ExportXHTMLDialog::xslChanged()
insilmaril@382
   207
{
insilmaril@382
   208
	xsl=ui.lineEditXSL->text();
insilmaril@382
   209
	settingsChanged=true;
insilmaril@382
   210
}
insilmaril@382
   211
insilmaril@382
   212
void ExportXHTMLDialog::prescriptChanged()
insilmaril@382
   213
{
insilmaril@382
   214
	prescript=ui.lineEditPreScript->text();
insilmaril@382
   215
	settingsChanged=true;
insilmaril@382
   216
}
insilmaril@382
   217
insilmaril@382
   218
void ExportXHTMLDialog::browseXSLPressed()
insilmaril@382
   219
{
insilmaril@382
   220
   	QFileDialog fd( this);
insilmaril@382
   221
	fd.setModal (true);
insilmaril@382
   222
	fd.setFilter ("Extensible Stylesheet Language (*.xsl)");
insilmaril@382
   223
	fd.setDirectory (QDir::current());
insilmaril@382
   224
	fd.show();
insilmaril@382
   225
insilmaril@382
   226
	if ( fd.exec() == QDialog::Accepted )
insilmaril@382
   227
	{
insilmaril@382
   228
		xsl=fd.selectedFile();
insilmaril@382
   229
		ui.lineEditXSL->setText (xsl );
insilmaril@382
   230
		settingsChanged=true;
insilmaril@382
   231
	}
insilmaril@382
   232
}
insilmaril@382
   233
insilmaril@382
   234
void ExportXHTMLDialog::postscriptChanged()
insilmaril@382
   235
{
insilmaril@382
   236
	postscript=ui.lineEditPostScript->text();
insilmaril@382
   237
	settingsChanged=true;
insilmaril@382
   238
}
insilmaril@382
   239
insilmaril@382
   240
void ExportXHTMLDialog::browsePreExportButtonPressed()
insilmaril@382
   241
{
insilmaril@382
   242
	QFileDialog fd( this);
insilmaril@382
   243
	fd.setModal (true);
insilmaril@382
   244
	fd.setFilter ("Scripts (*.sh *.pl *.py *.php)");
insilmaril@382
   245
	fd.setDirectory (QDir::current());
insilmaril@382
   246
	fd.show();
insilmaril@382
   247
insilmaril@382
   248
	if ( fd.exec() == QDialog::Accepted )
insilmaril@382
   249
	{
insilmaril@382
   250
		prescript=fd.selectedFile();
insilmaril@382
   251
		ui.lineEditPreScript->setText (prescript );
insilmaril@382
   252
		settingsChanged=true;
insilmaril@382
   253
	}
insilmaril@382
   254
insilmaril@382
   255
}
insilmaril@382
   256
insilmaril@382
   257
void ExportXHTMLDialog::browsePostExportButtonPressed()
insilmaril@382
   258
{
insilmaril@382
   259
	QFileDialog fd( this);
insilmaril@382
   260
	fd.setModal (true);
insilmaril@382
   261
	fd.setFilter ("Scripts (*.sh *.pl *.py *.php)");
insilmaril@382
   262
	fd.setDirectory (QDir::current());
insilmaril@382
   263
	fd.show();
insilmaril@382
   264
insilmaril@382
   265
	if ( fd.exec() == QDialog::Accepted )
insilmaril@382
   266
	{
insilmaril@382
   267
		postscript=fd.selectedFile();
insilmaril@382
   268
		ui.lineEditPostScript->setText (postscript );
insilmaril@382
   269
		settingsChanged=true;
insilmaril@382
   270
	}
insilmaril@382
   271
}
insilmaril@382
   272
insilmaril@382
   273
insilmaril@382
   274
void ExportXHTMLDialog::doExport (const QString &mapname)
insilmaril@382
   275
{
insilmaril@382
   276
	// Save options to settings file 
insilmaril@382
   277
	// (but don't save at destructor, which
insilmaril@382
   278
	// is called for "cancel", too)
insilmaril@382
   279
	settings.setLocalEntry (filepath,"/export/xhtml/exportDir",dir);
insilmaril@382
   280
	settings.setLocalEntry (filepath,"/export/xhtml/prescript",prescript);
insilmaril@382
   281
	settings.setLocalEntry (filepath,"/export/xhtml/postscript",postscript);
insilmaril@382
   282
insilmaril@382
   283
    if (useImage)
insilmaril@382
   284
		settings.setLocalEntry (filepath,"/export/xhtml/useImage","yes");
insilmaril@382
   285
    else
insilmaril@382
   286
		settings.setLocalEntry (filepath,"/export/xhtml/useImage","no");	
insilmaril@382
   287
	
insilmaril@382
   288
  if (useTextColor)
insilmaril@382
   289
		settings.setLocalEntry (filepath,"/export/xhtml/useTextColor","yes");
insilmaril@382
   290
    else
insilmaril@382
   291
		settings.setLocalEntry (filepath,"/export/xhtml/useTextColor","no");	
insilmaril@382
   292
	
insilmaril@382
   293
   if (showWarnings)
insilmaril@382
   294
		settings.writeEntry ("/export/xhtml/showWarnings","yes");
insilmaril@382
   295
    else
insilmaril@382
   296
		settings.writeEntry ("/export/xhtml/showWarnings","no");	
insilmaril@382
   297
			
insilmaril@382
   298
	if (showOutput)
insilmaril@382
   299
		settings.writeEntry ("/export/xhtml/showOutput","yes");
insilmaril@382
   300
	else
insilmaril@382
   301
		settings.writeEntry ("/export/xhtml/showOutput","no");	
insilmaril@382
   302
insilmaril@382
   303
	QString ipath;	
insilmaril@382
   304
	ipath=vymBaseDir.path()+"/flags/flag-url-16x16.png";
insilmaril@382
   305
	if (!options.isOn ("local"))
insilmaril@382
   306
	{
insilmaril@382
   307
		settings.setLocalEntry 
insilmaril@382
   308
			(filepath,"/export/xhtml/xsl",xsl);
insilmaril@382
   309
		settings.setLocalEntry 
insilmaril@382
   310
			(filepath,"/export/xhtml/css",css);	
insilmaril@382
   311
	}
insilmaril@382
   312
insilmaril@382
   313
	// Provide a smaller URL-icon to improve Layout
insilmaril@382
   314
	QPixmap pm;
insilmaril@382
   315
	if (!pm.load(ipath,"PNG") )
insilmaril@382
   316
		QMessageBox::warning( 0, tr( "Warning" ),tr("Could not open %1").arg(ipath));
insilmaril@382
   317
		
insilmaril@382
   318
		
insilmaril@382
   319
	if(!pm.save (dir + "flags/flag-url-16x16.png","PNG"))
insilmaril@382
   320
		QMessageBox::warning( 0, tr( "Warning" ),tr("Could not write %1").arg(ipath));
insilmaril@382
   321
	if (!saveSettingsInMap)
insilmaril@382
   322
		settings.clearLocal("/export/xhtml");
insilmaril@382
   323
	else	
insilmaril@382
   324
		settings.setLocalEntry 
insilmaril@382
   325
			(filepath,"/export/xhtml/saveSettingsInMap","yes");
insilmaril@382
   326
insilmaril@382
   327
	// Copy CSS file
insilmaril@382
   328
	QFile css_src (css);
insilmaril@382
   329
	QFile css_dst (dir+"vym.css");
insilmaril@382
   330
	if (!css_src.open ( QIODevice::ReadOnly))
insilmaril@382
   331
		QMessageBox::warning( 0, tr( "Warning" ),tr("Could not open %1").arg(css));
insilmaril@382
   332
	else
insilmaril@382
   333
	{
insilmaril@382
   334
		if (!css_dst.open( QIODevice::WriteOnly))
insilmaril@382
   335
			QMessageBox::warning( 0, tr( "Warning" ), tr("Could not open %1").arg(dir+"vym.css"));
insilmaril@382
   336
		else
insilmaril@382
   337
		{	
insilmaril@382
   338
		
insilmaril@382
   339
			QTextStream tsout( &css_dst);
insilmaril@382
   340
			QTextStream tsin ( &css_src);
insilmaril@382
   341
			QString s= tsin.read();
insilmaril@382
   342
			tsout << s;
insilmaril@382
   343
			css_dst.close();
insilmaril@382
   344
		}	
insilmaril@382
   345
		css_src.close();
insilmaril@382
   346
	}
insilmaril@382
   347
insilmaril@382
   348
	if (!prescript.isEmpty()) runScript (prescript,dir+mapname+".xml");
insilmaril@382
   349
	
insilmaril@382
   350
	if (useImage)
insilmaril@382
   351
		p.addStringParam ("imagemap","images/"+mapname+".png");
insilmaril@382
   352
	if (useTextColor)
insilmaril@382
   353
		p.addStringParam ("use.textcolor","1");
insilmaril@382
   354
	p.addStringParam ("mapname",mapname+".vym");
insilmaril@382
   355
	
insilmaril@382
   356
	p.setOutputFile (dir+mapname+".html");
insilmaril@382
   357
	p.setInputFile (dir+mapname+".xml");
insilmaril@382
   358
	p.setXSLFile (xsl);
insilmaril@382
   359
	p.process();
insilmaril@382
   360
insilmaril@382
   361
	if (!postscript.isEmpty()) runScript (postscript,dir+mapname+".html");
insilmaril@382
   362
insilmaril@382
   363
}
insilmaril@382
   364
insilmaril@382
   365
void ExportXHTMLDialog::setFilePath(const QString &s)
insilmaril@382
   366
{
insilmaril@382
   367
	filepath=s;
insilmaril@382
   368
}
insilmaril@382
   369
insilmaril@382
   370
void ExportXHTMLDialog::setMapName(const QString &s)
insilmaril@382
   371
{
insilmaril@382
   372
	mapname=s;
insilmaril@382
   373
}
insilmaril@382
   374
insilmaril@382
   375
QString ExportXHTMLDialog::getDir()
insilmaril@382
   376
{
insilmaril@382
   377
	return dir;
insilmaril@382
   378
}
insilmaril@382
   379
insilmaril@382
   380
bool ExportXHTMLDialog::warnings()
insilmaril@382
   381
{
insilmaril@382
   382
	return showWarnings;
insilmaril@382
   383
}
insilmaril@382
   384
insilmaril@382
   385
bool ExportXHTMLDialog::hasChanged()
insilmaril@382
   386
{
insilmaril@382
   387
	return settingsChanged;
insilmaril@382
   388
}
insilmaril@382
   389
insilmaril@382
   390
insilmaril@382
   391
void ExportXHTMLDialog::runScript(QString spath, QString fpath)
insilmaril@382
   392
{
insilmaril@382
   393
	spath.replace ("%f",fpath);
insilmaril@382
   394
	QStringList args=QStringList::split (' ',spath,false);
insilmaril@382
   395
		
insilmaril@408
   396
	p.addOutput ("vym is executing: \n" + spath+" "+args.join(" ") );	
insilmaril@408
   397
	scriptProc->start (spath,args);
insilmaril@408
   398
	if (!scriptProc->waitForStarted() )
insilmaril@382
   399
	{
insilmaril@382
   400
		QMessageBox::critical( 0, tr( "Critical Error" ),
insilmaril@382
   401
					   tr("Could not start %1").arg(spath) );
insilmaril@382
   402
	} else
insilmaril@382
   403
	{
insilmaril@428
   404
		if (!scriptProc->waitForFinished())
insilmaril@382
   405
			QMessageBox::critical( 0, tr( "Critical Error" ),
insilmaril@382
   406
			   tr("%1 didn't exit normally").arg(spath) +
insilmaril@382
   407
			   scriptProc->getErrout() );
insilmaril@382
   408
		else
insilmaril@382
   409
			if (scriptProc->exitStatus()>0) showOutput=true;
insilmaril@382
   410
			
insilmaril@382
   411
	}	
insilmaril@382
   412
	p.addOutput ("\n");
insilmaril@382
   413
	p.addOutput (scriptProc->getErrout());
insilmaril@382
   414
	p.addOutput (scriptProc->getStdout());
insilmaril@382
   415
}
insilmaril@382
   416