From: asv Date: Tue, 21 Dec 2004 12:59:07 +0000 (+0000) Subject: Automatic indentation of Python code in Python editor pane is implemented. X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=dfc35b9f4051a5e9f582a7352a4ba5133bafc801;p=modules%2Fsuperv.git Automatic indentation of Python code in Python editor pane is implemented. --- diff --git a/src/SUPERVGUI/SUPERVGUI_Service.cxx b/src/SUPERVGUI/SUPERVGUI_Service.cxx index 78bed9c..fe85677 100644 --- a/src/SUPERVGUI/SUPERVGUI_Service.cxx +++ b/src/SUPERVGUI/SUPERVGUI_Service.cxx @@ -37,7 +37,7 @@ using namespace std; #include #include #include - +#include 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( ¶, &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() ); + } +} /*! diff --git a/src/SUPERVGUI/SUPERVGUI_Service.h b/src/SUPERVGUI/SUPERVGUI_Service.h index 9a2f161..bc3fabe 100644 --- a/src/SUPERVGUI/SUPERVGUI_Service.h +++ b/src/SUPERVGUI/SUPERVGUI_Service.h @@ -56,6 +56,7 @@ class SUPERVGUI_PythonEditPane: public QFrame { public slots: void loadFile(); void readFunction(); + void autoIndentLine(); private: QTextEdit* myText;