From 9a001c2247cbd12efaf1f5c281e4bb636095b6ca Mon Sep 17 00:00:00 2001 From: akl Date: Fri, 24 Nov 2017 12:20:56 +0300 Subject: [PATCH] Add setCurrentLine to PyEditor API. --- tools/PyEditor/src/PyEditor.cxx | 15 ++++++++++++++- tools/PyEditor/src/PyEditor_Editor.cxx | 13 +++++++++++++ tools/PyEditor/src/PyEditor_Editor.h | 2 ++ tools/PyEditor/src/PyEditor_Settings.cxx | 2 +- tools/PyEditor/src/PyEditor_Widget.cxx | 10 ++++++++++ tools/PyEditor/src/PyEditor_Widget.h | 2 ++ tools/PyEditor/src/PyEditor_Window.cxx | 10 ++++++++++ tools/PyEditor/src/PyEditor_Window.h | 2 ++ tools/PyEditor/src/python/PyEditorPy.sip | 3 +++ 9 files changed, 57 insertions(+), 2 deletions(-) diff --git a/tools/PyEditor/src/PyEditor.cxx b/tools/PyEditor/src/PyEditor.cxx index 5a213338d..0c8d2b295 100644 --- a/tools/PyEditor/src/PyEditor.cxx +++ b/tools/PyEditor/src/PyEditor.cxx @@ -20,6 +20,7 @@ // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com) // +#include "PyEditor_Widget.h" #include "PyEditor_Window.h" #include "PyEditor_StdSettings.h" @@ -110,6 +111,10 @@ int main( int argc, char *argv[] ) QCommandLineParser parser; parser.setApplicationDescription( QApplication::translate( "PyEditor", "PROGRAM_DESCRIPTION" ) ); parser.addHelpOption(); + QCommandLineOption gotoOption( QStringList() << "l" << "line", + QApplication::translate( "PyEditor", "Set initial line number." ), + "line" ); + parser.addOption( gotoOption ); parser.addPositionalArgument( QApplication::translate( "PyEditor", "FILE_PARAM_NAME" ), QApplication::translate( "PyEditor", "FILE_PARAM_DESCRIPTION" ) ); @@ -121,8 +126,16 @@ int main( int argc, char *argv[] ) window.resize( 650, 700 ); window.show(); - if ( args.count() > 0 ) + if ( args.count() > 0 ) { window.loadFile( args[0], false ); + + if ( parser.isSet( gotoOption ) ) { + bool ok; + int line = parser.value( gotoOption ).toInt( &ok ); + if ( ok ) + window.editor()->setCurrentLine( line ); + } + } return app.exec(); } diff --git a/tools/PyEditor/src/PyEditor_Editor.cxx b/tools/PyEditor/src/PyEditor_Editor.cxx index 7e62c6f7b..4f62b8221 100644 --- a/tools/PyEditor/src/PyEditor_Editor.cxx +++ b/tools/PyEditor/src/PyEditor_Editor.cxx @@ -30,6 +30,7 @@ #include #include #include +#include #include @@ -920,3 +921,15 @@ PyEditor_Keywords* PyEditor_Editor::standardKeywords() const { return myStdKeywords; } + +/*! + \brief Move cursor to the given line. + \note Line count starts from 1. + \param line Line number. +*/ +void PyEditor_Editor::setCurrentLine( int line ) +{ + QTextCursor cursor( document()->findBlockByLineNumber( line - 1 ) ); + setTextCursor( cursor ); + ensureCursorVisible(); +} diff --git a/tools/PyEditor/src/PyEditor_Editor.h b/tools/PyEditor/src/PyEditor_Editor.h index 8ec688366..1cdcabe96 100644 --- a/tools/PyEditor/src/PyEditor_Editor.h +++ b/tools/PyEditor/src/PyEditor_Editor.h @@ -46,6 +46,7 @@ public: void setSettings( const PyEditor_Settings& ); const PyEditor_Settings& settings() const; + QString text() const; QStringList keywords() const; @@ -59,6 +60,7 @@ public Q_SLOTS: void deleteSelected(); void append( const QString& ); void setText( const QString& text ); + void setCurrentLine( int ); protected: virtual void keyPressEvent( QKeyEvent* ); diff --git a/tools/PyEditor/src/PyEditor_Settings.cxx b/tools/PyEditor/src/PyEditor_Settings.cxx index 64a43d700..fff5cda6f 100644 --- a/tools/PyEditor/src/PyEditor_Settings.cxx +++ b/tools/PyEditor/src/PyEditor_Settings.cxx @@ -64,7 +64,7 @@ PyEditor_Settings::PyEditor_Settings() myTabSpaceVisible( true ), myTabSize( 4 ), myFont( "Courier", 10 ), - myCompletionPolicy( PyEditor_Editor::Always ) + myCompletionPolicy( PyEditor_Editor::None ) { } diff --git a/tools/PyEditor/src/PyEditor_Widget.cxx b/tools/PyEditor/src/PyEditor_Widget.cxx index 58d4c285c..bc2701959 100644 --- a/tools/PyEditor/src/PyEditor_Widget.cxx +++ b/tools/PyEditor/src/PyEditor_Widget.cxx @@ -267,3 +267,13 @@ const PyEditor_Settings& PyEditor_Widget::settings() const { return myEditor->settings(); } + +/*! + \brief Move editor's cursor to the given line. + \note Line count starts from 1. + \param line Line number. +*/ +void PyEditor_Widget::setCurrentLine( int line ) +{ + myEditor->setCurrentLine( line ); +} diff --git a/tools/PyEditor/src/PyEditor_Widget.h b/tools/PyEditor/src/PyEditor_Widget.h index f7ba95977..4e5bffee5 100644 --- a/tools/PyEditor/src/PyEditor_Widget.h +++ b/tools/PyEditor/src/PyEditor_Widget.h @@ -72,6 +72,8 @@ public slots: void setText( const QString& ); + void setCurrentLine( int ); + signals: void modificationChanged( bool ); void undoAvailable( bool ); diff --git a/tools/PyEditor/src/PyEditor_Window.cxx b/tools/PyEditor/src/PyEditor_Window.cxx index cfeebe2bd..33d621947 100644 --- a/tools/PyEditor/src/PyEditor_Window.cxx +++ b/tools/PyEditor/src/PyEditor_Window.cxx @@ -471,6 +471,16 @@ bool PyEditor_Window::saveFile( const QString& filePath, bool verbose ) return true; } + +/*! + Get editor. + \return Editor widget. +*/ +PyEditor_Widget* PyEditor_Window::editor() +{ + return myEditor; +} + /*! Slot, called when user clicks "Help" button in "Preferences" dialog box. */ diff --git a/tools/PyEditor/src/PyEditor_Window.h b/tools/PyEditor/src/PyEditor_Window.h index 1369db1cd..cd16eddb6 100644 --- a/tools/PyEditor/src/PyEditor_Window.h +++ b/tools/PyEditor/src/PyEditor_Window.h @@ -46,6 +46,8 @@ public: void loadFile( const QString&, bool = true ); bool saveFile( const QString&, bool = true ); + + PyEditor_Widget* editor(); protected: virtual void closeEvent( QCloseEvent* ); diff --git a/tools/PyEditor/src/python/PyEditorPy.sip b/tools/PyEditor/src/python/PyEditorPy.sip index 854e7a86b..ec0d1e7cc 100644 --- a/tools/PyEditor/src/python/PyEditorPy.sip +++ b/tools/PyEditor/src/python/PyEditorPy.sip @@ -93,6 +93,7 @@ public slots: void deleteSelected(); void append( const QString& ); void setText( const QString& text ); + void setCurrentLine( int ); protected: virtual void keyPressEvent( QKeyEvent* ); @@ -165,6 +166,8 @@ public slots: void setText( const QString& ); + void setCurrentLine( int ); + signals: void modificationChanged( bool ); void undoAvailable( bool ); -- 2.30.2