Salome HOME
Automatic indentation of Python code in Python editor pane is implemented.
authorasv <asv@opencascade.com>
Tue, 21 Dec 2004 12:59:07 +0000 (12:59 +0000)
committerasv <asv@opencascade.com>
Tue, 21 Dec 2004 12:59:07 +0000 (12:59 +0000)
src/SUPERVGUI/SUPERVGUI_Service.cxx
src/SUPERVGUI/SUPERVGUI_Service.h

index 78bed9cbe1d23866647c22c55d57a4e393063bc0..fe85677f48894f33257617f04986f71c97a17321 100644 (file)
@@ -37,7 +37,7 @@ using namespace std;
 #include <qlayout.h>
 #include <qhbox.h>
 #include <qtextstream.h>
-
+#include <qregexp.h>
 
 static const char * ComponentIcon[] = {
 "20 20 2 1",
@@ -746,6 +746,7 @@ SUPERVGUI_PythonEditPane::SUPERVGUI_PythonEditPane(QWidget* theParent)
   myText = new QTextEdit(this);
   myText->setWordWrap(QTextEdit::FixedColumnWidth);
   myText->setWrapColumnOrWidth(80);
+  connect( myText, SIGNAL( returnPressed() ), this, SLOT( autoIndentLine() ) );
 
   aEditLayout->addMultiCellWidget(myText, 1, 1, 0, 3);
 }
@@ -892,6 +893,40 @@ void SUPERVGUI_PythonEditPane::setFunction(SUPERV_Strings theStr) {
   myText->ensureVisible( 0,0 );
 }
 
+/**
+ * Automatic indentation rule: if a previous line ended with 
+ * ':', then add N additional spaces in the current line.  If no ':' found, then 
+ * the same amount of spaces as in the previous line is added. Connected to 
+ * "returnPressed" signal of myText text edit.
+*/
+void SUPERVGUI_PythonEditPane::autoIndentLine() {
+  const int N = 4; // width of indentation "tab"
+  if ( myText && myText->paragraphs() ) {
+
+    // get current cursor position and previous line (the one to be analized) 
+    int pos, para, i;
+    myText->getCursorPosition( &para, &pos ); // pos==0, beginning of line
+    QString line = myText->text( para-1 ); // previous paragraph line
+
+    // construct a string containing all leading space characters of previous line (tabs, etc.)
+    QString spacesStr;
+    i = -1;
+    while ( line[++i].isSpace() ) // append all isSpace() characters at beginning of line to spacesStr
+      spacesStr += line[i];
+
+    // if ':' was found -- add more spaces to spacesStr
+    line = line.stripWhiteSpace();
+    if ( line[ line.length()-1 ] == ':' ) {
+      i = 0;
+      while ( i++ < N ) 
+       spacesStr += ' ';
+    }
+
+    // ok, append spacesStr at the beginning of the current line = make indentation
+    myText->insertAt( spacesStr, para, pos );
+    myText->setCursorPosition( para, pos+spacesStr.length() );
+  }
+}
 
 
 /*!
index 9a2f1613d18b39d45ae73d37d51bdb7e6e42ba1e..bc3fabe781ca3a2467f5b8ec85fe4d09ac00f9fd 100644 (file)
@@ -56,6 +56,7 @@ class SUPERVGUI_PythonEditPane: public QFrame {
  public slots:
   void loadFile();
   void readFunction();
+  void autoIndentLine();
 
  private:
   QTextEdit*   myText;