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