changeset 176:770ac3c53bea

[add] Feature Request ID 1704290: Added a functionality to create a shell script which can be included, in an IDE for example, as external tool to call the indenter with the settings mad in UiGui. git-svn-id: svn://svn.code.sf.net/p/universalindent/code/trunk@380 59b1889a-e5ac-428c-b0c7-476e01d41282
author thomas_-_s <thomas_-_s@59b1889a-e5ac-428c-b0c7-476e01d41282>
date Mon, 04 Jun 2007 15:38:28 +0000
parents a3496aff3428
children aff3b5fbd669
files src/indentgui.ui src/indenthandler.cpp src/indenthandler.h src/mainwindow.cpp src/mainwindow.h
diffstat 5 files changed, 151 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/indentgui.ui	Mon Jun 04 15:15:20 2007 +0000
+++ b/src/indentgui.ui	Mon Jun 04 15:38:28 2007 +0000
@@ -63,13 +63,6 @@
     <addaction name="menuExport" />
     <addaction name="actionExit" />
    </widget>
-   <widget class="QMenu" name="menuIndenter" >
-    <property name="title" >
-     <string>Indenter</string>
-    </property>
-    <addaction name="actionLoad_Indenter_Config_File" />
-    <addaction name="actionSave_Indenter_Config_File" />
-   </widget>
    <widget class="QMenu" name="menuHelp" >
     <property name="title" >
      <string>Help</string>
@@ -87,6 +80,14 @@
     <addaction name="uiGuiLoadLastOpenedFileOnStartup" />
     <addaction name="actionShowSettings" />
    </widget>
+   <widget class="QMenu" name="menuIndenter" >
+    <property name="title" >
+     <string>Indenter</string>
+    </property>
+    <addaction name="actionLoad_Indenter_Config_File" />
+    <addaction name="actionSave_Indenter_Config_File" />
+    <addaction name="actionCreateShellScript" />
+   </widget>
    <addaction name="menuFile" />
    <addaction name="menuIndenter" />
    <addaction name="menuSettings" />
@@ -385,6 +386,14 @@
     <string>Opens the settings dialog, to set language etc.</string>
    </property>
   </action>
+  <action name="actionCreateShellScript" >
+   <property name="icon" >
+    <iconset resource="../resources/Icons.qrc" >:/mainWindow/shell.png</iconset>
+   </property>
+   <property name="text" >
+    <string>Create Indenter Call Shell Script</string>
+   </property>
+  </action>
  </widget>
  <resources>
   <include location="../resources/Icons.qrc" />
--- a/src/indenthandler.cpp	Mon Jun 04 15:15:20 2007 +0000
+++ b/src/indenthandler.cpp	Mon Jun 04 15:38:28 2007 +0000
@@ -79,6 +79,100 @@
 
 
 /*!
+    Creates the content for a shell script that can be used as a external too call
+    to indent a as parameter defined file.
+ */
+QString IndentHandler::generateCommandlineCall(QString inputFileExtension) {
+
+	QString indentCallString;
+	QString parameterInputFile;
+	QString parameterOuputFile;
+	QString parameterParameterFile;
+    QString replaceInputFileCommand;
+
+    // Define the placeholder for variable either in batch or bash programming.
+#if defined(Q_OS_WIN32)
+    QString shellPlaceholder = "%1";
+#else
+    QString shellPlaceholder = "$1";
+#endif
+
+    // Generate the parameter string that will be save to the indenters config file.
+    QString parameterString = getParameterString();
+
+	if ( !configFilename.isEmpty() ) {
+		writeConfigFile( parameterString );
+	}
+
+    // Only add point to file extension if the string is not empty.
+    if ( !inputFileExtension.isEmpty() ) {
+        inputFileExtension = "." + inputFileExtension;
+    }
+
+    parameterInputFile = " " + inputFileParameter + "\"" + shellPlaceholder + "\"";
+
+    if ( outputFileParameter != "none" && outputFileParameter != "stdout" ) {
+        if ( outputFileName == inputFileName ) {
+            parameterOuputFile = " " + outputFileParameter + "\"" + shellPlaceholder + "\"";
+        }
+        else {
+            parameterOuputFile = " " + outputFileParameter + outputFileName + inputFileExtension;
+        }
+    }
+
+	// If the config file name is empty it is assumed that all parameters are sent via command line call
+	if ( configFilename.isEmpty() ) {
+		parameterParameterFile = " " + parameterString;
+	}
+	// Else if needed add the parameter to the indenter call string where the config file can be found.
+	else if (useCfgFileParameter != "none") {
+		parameterParameterFile = " " + useCfgFileParameter + "\"" + QFileInfo(dataDirctoryStr).absoluteFilePath() + "/" + configFilename + "\"";
+	}
+
+	// Assemble indenter call string for parameters according to the set order.
+	if ( parameterOrder == "ipo" ) {
+		indentCallString = parameterInputFile + parameterParameterFile + parameterOuputFile;
+	} 
+	else if ( parameterOrder == "pio" ) {
+		indentCallString = parameterParameterFile + parameterInputFile + parameterOuputFile;
+	}
+	else {
+		indentCallString = parameterInputFile + parameterOuputFile + parameterParameterFile;
+	}
+
+    // Generate the indenter call string either for win32 or other systems.
+#if defined(Q_OS_WIN32)
+    indentCallString = "\"" + QFileInfo(dataDirctoryStr).absoluteFilePath() + "/" + indenterFileName +".exe\""+ indentCallString;
+#else
+    indentCallString = "\"" + QFileInfo(dataDirctoryStr).absoluteFilePath() + "/" + indenterFileName + "\"" + indentCallString;
+#endif
+
+#if defined(Q_OS_LINUX)
+    // If no Linux binary exists to run the indenter, use wine to run the Windows exe and test if wine is installed.
+    if ( !QFile::exists(dataDirctoryStr + indenterFileName) ) {
+        indentCallString = "wine " + indentCallString;
+    }
+#endif
+
+    // If the indenter writes to stdout pipe the output into a file
+    if ( outputFileParameter == "stdout" ) {
+        indentCallString = indentCallString + " >" + outputFileName + inputFileExtension;
+    }
+
+    // If the output filename is not the same as the input filename copy the output over the input.
+    if ( outputFileName != inputFileName ) {
+#if defined(Q_OS_WIN32)
+        replaceInputFileCommand = "move /Y " + outputFileName + inputFileExtension + " \"" + shellPlaceholder + "\"\n";
+#else
+        replaceInputFileCommand = "mv " + outputFileName + inputFileExtension + " \"" + shellPlaceholder + "\"\n";
+#endif
+    }
+
+    return indentCallString + "\n" + replaceInputFileCommand;
+}
+
+
+/*!
    Format \a sourceCode by calling the indenter. The \a inputFileExtension has to be given as parameter
    so the called indenter can identify the programming language if needed.
  */
@@ -122,7 +216,7 @@
 	}
 	// if needed add the parameter to the indenter call string where the config file can be found
 	else if (useCfgFileParameter != "none") {
-		parameterParamterFile = " " + useCfgFileParameter + configFilename;
+		parameterParamterFile = " " + useCfgFileParameter + "\"" + QFileInfo(dataDirctoryStr).absoluteFilePath() + "/" + configFilename + "\"";
 	}
 
 	// Assemble indenter call string for parameters according to the set order.
@@ -154,9 +248,9 @@
 
     // generate the indenter call string either for win32 or other systems
 #if defined(Q_OS_WIN32)
-    indentCallString = dataDirctoryStr + indenterFileName +".exe"+ indentCallString;
+    indentCallString = "\"" + QFileInfo(dataDirctoryStr).absoluteFilePath() + "/" + indenterFileName +".exe\""+ indentCallString;
 #else
-    indentCallString = "./" + indenterFileName + indentCallString;
+    indentCallString = "\"" + QFileInfo(dataDirctoryStr).absoluteFilePath() + "/" + indenterFileName + "\"" + indentCallString;
 #endif
 
     // write the source code to the input file for the indenter
@@ -240,11 +334,12 @@
     }
 
 
+    // If the indenter results are written to stdout, read them from there.
 	if ( indentProcess.exitCode() == 0 && outputFileParameter == "stdout"  ) {
 		formattedSourceCode = processReturnString;
 	}
+    // Else read the output file generated by the indenter call.
 	else {
-		// read the output file generated by the indenter call
 		outSrcFile.setFileName(dataDirctoryStr + outputFileName + inputFileExtension);
 		outSrcFile.open(QFile::ReadOnly | QFile::Text);
 		QTextStream outSrcStrm(&outSrcFile);
@@ -253,7 +348,7 @@
 		outSrcFile.close();
 	}
 
-    // delete the temporary input and output files
+    // Delete the temporary input and output files.
     QFile::remove(dataDirctoryStr + outputFileName + inputFileExtension);
     QFile::remove(dataDirctoryStr + inputFileName + inputFileExtension);
 
--- a/src/indenthandler.h	Mon Jun 04 15:15:20 2007 +0000
+++ b/src/indenthandler.h	Mon Jun 04 15:38:28 2007 +0000
@@ -48,17 +48,12 @@
 public:
     IndentHandler(QString dataDirPathStr, int indenterID, QMainWindow *mainWindow = 0, QWidget *parent = 0);
 
-    //! Format source code with the currently selected indenter
+    QString generateCommandlineCall(QString inputFileExtension);
     QString callIndenter(QString sourceCode, QString inputFileExtension);
-
     void loadConfigFile(QString filePathName);
-
     QStringList getAvailableIndenters();
-
     QString getPossibleIndenterFileExtensions();
-
     QString getParameterString();
-
     QString getIndenterCfgFile();
 
 private:
--- a/src/mainwindow.cpp	Mon Jun 04 15:15:20 2007 +0000
+++ b/src/mainwindow.cpp	Mon Jun 04 15:38:28 2007 +0000
@@ -143,6 +143,7 @@
     connect( actionExportHTML, SIGNAL(activated()), this, SLOT(exportToHTML()) );
     connect( actionLoad_Indenter_Config_File, SIGNAL(activated()), this, SLOT(openConfigFileDialog()) );
     connect( actionSave_Indenter_Config_File, SIGNAL(activated()), this, SLOT(saveasIndentCfgFileDialog()) );
+    connect( actionCreateShellScript, SIGNAL(activated()), this, SLOT(createIndenterCallShellScript()) );
 
     // Init of some variables.
     dataDirctoryStr = "./data/";
@@ -1267,3 +1268,35 @@
         QWidget::changeEvent(event);
     }
 }
+
+
+/*!
+    Lets the indenter create a shell script for calling the indenter out of any
+    other application and open a save dialog for saving the shell script.
+ */
+void MainWindow::createIndenterCallShellScript() {
+    QString indenterCallShellScript = indentHandler->generateCommandlineCall(currentSourceFileExtension);
+
+    QString shellScriptExtension;
+#if defined(Q_OS_WIN32)
+        shellScriptExtension = "bat";
+#else
+        shellScriptExtension = "sh";
+#endif
+
+    QString fileExtensions = tr("Shell Script")+" (*."+shellScriptExtension+");;"+tr("All files")+" (*.*)";
+
+    //QString openedSourceFileContent = openFileDialog( tr("Choose source code file"), "./", fileExtensions );
+    QString fileName = QFileDialog::getSaveFileName( this, tr("Save chell script"), "callIndenter."+shellScriptExtension, fileExtensions);
+
+    // Saving has been canceled if the filename is empty
+    if ( fileName.isEmpty() ) {
+        return;
+    }
+
+    QFile::remove(fileName);
+    QFile outSrcFile(fileName);
+    outSrcFile.open( QFile::ReadWrite | QFile::Text );
+    outSrcFile.write( indenterCallShellScript.toAscii() );
+    outSrcFile.close();
+}
--- a/src/mainwindow.h	Mon Jun 04 15:15:20 2007 +0000
+++ b/src/mainwindow.h	Mon Jun 04 15:38:28 2007 +0000
@@ -139,6 +139,7 @@
     void encodingChanged(QAction *encodingAction);
     void highlighterChanged(QAction* highlighterAction);
 	void numberOfLinesChanged();
+    void createIndenterCallShellScript();
 };
 
 #endif // MAINWINDOW_H