exportxhtmldialog.cpp
author insilmaril
Wed, 10 Feb 2010 13:48:42 +0000
changeset 822 c2ce9944148c
parent 624 4ffc47ae27e4
permissions -rw-r--r--
More fixes and sorting lexically backwards
     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 
   277 	// Save options to settings file 
   278 	// (but don't save at destructor, which
   279 	// is called for "cancel", too)
   280 	settings.setLocalEntry (filepath,"/export/xhtml/exportDir",dir);
   281 	settings.setLocalEntry (filepath,"/export/xhtml/prescript",prescript);
   282 	settings.setLocalEntry (filepath,"/export/xhtml/postscript",postscript);
   283 
   284     if (useImage)
   285 		settings.setLocalEntry (filepath,"/export/xhtml/useImage","yes");
   286     else
   287 		settings.setLocalEntry (filepath,"/export/xhtml/useImage","no");	
   288 	
   289   if (useTextColor)
   290 		settings.setLocalEntry (filepath,"/export/xhtml/useTextColor","yes");
   291     else
   292 		settings.setLocalEntry (filepath,"/export/xhtml/useTextColor","no");	
   293 	
   294    if (showWarnings)
   295 		settings.writeEntry ("/export/xhtml/showWarnings","yes");
   296     else
   297 		settings.writeEntry ("/export/xhtml/showWarnings","no");	
   298 			
   299 	if (showOutput)
   300 		settings.writeEntry ("/export/xhtml/showOutput","yes");
   301 	else
   302 		settings.writeEntry ("/export/xhtml/showOutput","no");	
   303 
   304 	QString ipath;	
   305 	ipath=vymBaseDir.path()+"/flags/flag-url-16x16.png";
   306 	if (!options.isOn ("local"))
   307 	{
   308 		settings.setLocalEntry 
   309 			(filepath,"/export/xhtml/xsl",xsl);
   310 		settings.setLocalEntry 
   311 			(filepath,"/export/xhtml/css",css);	
   312 	}
   313 
   314 	if (!saveSettingsInMap)
   315 		settings.clearLocal("/export/xhtml");
   316 	else	
   317 		settings.setLocalEntry 
   318 			(filepath,"/export/xhtml/saveSettingsInMap","yes");
   319 
   320 	// Provide a smaller URL-icon to improve Layout
   321 	QPixmap pm;
   322 	if (!pm.load(ipath,"PNG") )
   323 		QMessageBox::warning( 0, tr( "Warning" ),tr("Could not open %1").arg(ipath));
   324 		
   325 		
   326 	if(!pm.save (dir + "flags/flag-url-16x16.png","PNG"))
   327 		QMessageBox::warning( 0, tr( "Warning" ),tr("Could not write %1").arg(ipath));
   328 
   329 	// Copy CSS file
   330 	QFile css_src (css);
   331 	QFile css_dst (dir+"vym.css");
   332 	if (!css_src.open ( QIODevice::ReadOnly))
   333 		QMessageBox::warning( 0, tr( "Warning" ),tr("Could not open %1").arg(css));
   334 	else
   335 	{
   336 		if (!css_dst.open( QIODevice::WriteOnly))
   337 			QMessageBox::warning( 0, tr( "Warning" ), tr("Could not open %1").arg(dir+"vym.css"));
   338 		else
   339 		{	
   340 		
   341 			QTextStream tsout( &css_dst);
   342 			QTextStream tsin ( &css_src);
   343 			QString s= tsin.read();
   344 			tsout << s;
   345 			css_dst.close();
   346 		}	
   347 		css_src.close();
   348 	}
   349 
   350 	if (!prescript.isEmpty()) runScript (prescript,dir+mapname+".xml");
   351 	
   352 	if (useImage)
   353 		p.addStringParam ("imagemap","images/"+mapname+".png");
   354 	if (useTextColor)
   355 		p.addStringParam ("use.textcolor","1");
   356 	p.addStringParam ("mapname",mapname+".vym");
   357 	
   358 	p.setOutputFile (dir+mapname+".html");
   359 	p.setInputFile (dir+mapname+".xml");
   360 	p.setXSLFile (xsl);
   361 	p.process();
   362 
   363 	if (!postscript.isEmpty()) runScript (postscript,dir+mapname+".html");
   364 
   365 }
   366 
   367 void ExportXHTMLDialog::setFilePath(const QString &s)
   368 {
   369 	filepath=s;
   370 }
   371 
   372 void ExportXHTMLDialog::setMapName(const QString &s)
   373 {
   374 	mapname=s;
   375 }
   376 
   377 QString ExportXHTMLDialog::getDir()
   378 {
   379 	return dir;
   380 }
   381 
   382 bool ExportXHTMLDialog::warnings()
   383 {
   384 	return showWarnings;
   385 }
   386 
   387 bool ExportXHTMLDialog::hasChanged()
   388 {
   389 	return settingsChanged;
   390 }
   391 
   392 
   393 void ExportXHTMLDialog::runScript(QString spath, QString fpath)
   394 {
   395 	spath.replace ("%f",fpath);
   396 	QStringList args=QStringList::split (' ',spath,false);
   397 		
   398 	p.addOutput ("vym is executing: \n" + spath+" "+args.join(" ") );	
   399 	scriptProc->start (spath,args);
   400 	if (!scriptProc->waitForStarted() )
   401 	{
   402 		QMessageBox::critical( 0, tr( "Critical Error" ),
   403 					   tr("Could not start %1").arg(spath) );
   404 	} else
   405 	{
   406 		if (!scriptProc->waitForFinished())
   407 			QMessageBox::critical( 0, tr( "Critical Error" ),
   408 			   tr("%1 didn't exit normally").arg(spath) +
   409 			   scriptProc->getErrout() );
   410 		else
   411 			if (scriptProc->exitStatus()>0) showOutput=true;
   412 			
   413 	}	
   414 	p.addOutput ("\n");
   415 	p.addOutput (scriptProc->getErrout());
   416 	p.addOutput (scriptProc->getStdout());
   417 }
   418