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