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