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