mirror of
https://github.com/qgis/QGIS.git
synced 2025-12-15 00:07:25 -05:00
Add methods to set and get cursor position
This commit is contained in:
parent
b45e6da699
commit
f163937f3c
@ -326,6 +326,45 @@ is in the QgsCodeEditor.Mode.CommandInput mode.
|
||||
.. seealso:: :py:func:`interpreter`
|
||||
|
||||
.. versionadded:: 3.30
|
||||
%End
|
||||
|
||||
int linearPosition() const;
|
||||
%Docstring
|
||||
Convenience function to return the cursor position as a linear index
|
||||
|
||||
.. versionadded:: 3.36
|
||||
%End
|
||||
|
||||
void setLinearPosition( int position );
|
||||
%Docstring
|
||||
Convenience function to set the cursor position as a linear index
|
||||
|
||||
.. versionadded:: 3.36
|
||||
%End
|
||||
|
||||
int selectionStart() const;
|
||||
%Docstring
|
||||
Convenience function to return the start of the selection as a linear index
|
||||
Contrary to the getSelection method, this method returns the cursor position if
|
||||
no selection is made.
|
||||
|
||||
.. versionadded:: 3.36
|
||||
%End
|
||||
|
||||
int selectionEnd() const;
|
||||
%Docstring
|
||||
Convenience function to return the end of the selection as a linear index
|
||||
Contrary to the getSelection method, this method returns the cursor position if
|
||||
no selection is made.
|
||||
|
||||
.. versionadded:: 3.36
|
||||
%End
|
||||
|
||||
void setLinearSelection( int start, int end );
|
||||
%Docstring
|
||||
Convenience function to set the selection using linear indexes
|
||||
|
||||
.. versionadded:: 3.36
|
||||
%End
|
||||
|
||||
public slots:
|
||||
|
||||
@ -752,13 +752,8 @@ void QgsCodeEditor::reformatCode()
|
||||
if ( !( languageCapabilities() & Qgis::ScriptLanguageCapability::Reformat ) )
|
||||
return;
|
||||
|
||||
int line = 0;
|
||||
int index = 0;
|
||||
getCursorPosition( &line, &index );
|
||||
const QString textBeforeCursor = text( 0, positionFromLineIndex( line, index ) );
|
||||
|
||||
const QString textBeforeCursor = text( 0, linearPosition() );
|
||||
const QString originalText = text();
|
||||
|
||||
const QString newText = reformatCodeString( originalText );
|
||||
|
||||
if ( originalText == newText )
|
||||
@ -766,14 +761,13 @@ void QgsCodeEditor::reformatCode()
|
||||
|
||||
// try to preserve the cursor position and scroll position
|
||||
const int oldScrollValue = verticalScrollBar()->value();
|
||||
const int linearPosition = findMinimalDistanceIndex( newText, textBeforeCursor );
|
||||
const int linearIndex = findMinimalDistanceIndex( newText, textBeforeCursor );
|
||||
|
||||
beginUndoAction();
|
||||
selectAll();
|
||||
removeSelectedText();
|
||||
insert( newText );
|
||||
lineIndexFromPosition( linearPosition, &line, &index );
|
||||
setCursorPosition( line, index );
|
||||
setLinearPosition( linearIndex );
|
||||
verticalScrollBar()->setValue( oldScrollValue );
|
||||
endUndoAction();
|
||||
}
|
||||
@ -1121,6 +1115,50 @@ void QgsCodeEditor::moveCursorToEnd()
|
||||
updatePrompt();
|
||||
}
|
||||
|
||||
int QgsCodeEditor::linearPosition() const
|
||||
{
|
||||
int line, index;
|
||||
getCursorPosition( &line, &index );
|
||||
return positionFromLineIndex( line, index );
|
||||
}
|
||||
|
||||
void QgsCodeEditor::setLinearPosition( int linearIndex )
|
||||
{
|
||||
int line, index;
|
||||
lineIndexFromPosition( linearIndex, &line, &index );
|
||||
setCursorPosition( line, index );
|
||||
}
|
||||
|
||||
int QgsCodeEditor::selectionStart() const
|
||||
{
|
||||
int startLine, startIndex, _;
|
||||
getSelection( &startLine, &startIndex, &_, &_ );
|
||||
if ( startLine == -1 )
|
||||
{
|
||||
return linearPosition();
|
||||
}
|
||||
return positionFromLineIndex( startLine, startIndex );
|
||||
}
|
||||
|
||||
int QgsCodeEditor::selectionEnd() const
|
||||
{
|
||||
int endLine, endIndex, _;
|
||||
getSelection( &_, &_, &endLine, &endIndex );
|
||||
if ( endLine == -1 )
|
||||
{
|
||||
return linearPosition();
|
||||
}
|
||||
return positionFromLineIndex( endLine, endIndex );
|
||||
}
|
||||
|
||||
void QgsCodeEditor::setLinearSelection( int start, int end )
|
||||
{
|
||||
int startLine, startIndex, endLine, endIndex;
|
||||
lineIndexFromPosition( start, &startLine, &startIndex );
|
||||
lineIndexFromPosition( end, &endLine, &endIndex );
|
||||
setSelection( startLine, startIndex, endLine, endIndex );
|
||||
}
|
||||
|
||||
QgsCodeInterpreter::~QgsCodeInterpreter() = default;
|
||||
|
||||
int QgsCodeInterpreter::exec( const QString &command )
|
||||
|
||||
@ -360,6 +360,45 @@ class GUI_EXPORT QgsCodeEditor : public QsciScintilla
|
||||
*/
|
||||
void setInterpreter( QgsCodeInterpreter *newInterpreter );
|
||||
|
||||
/**
|
||||
* Convenience function to return the cursor position as a linear index
|
||||
*
|
||||
* \since QGIS 3.36
|
||||
*/
|
||||
int linearPosition() const;
|
||||
|
||||
/**
|
||||
* Convenience function to set the cursor position as a linear index
|
||||
*
|
||||
* \since QGIS 3.36
|
||||
*/
|
||||
void setLinearPosition( int position );
|
||||
|
||||
/**
|
||||
* Convenience function to return the start of the selection as a linear index
|
||||
* Contrary to the getSelection method, this method returns the cursor position if
|
||||
* no selection is made.
|
||||
*
|
||||
* \since QGIS 3.36
|
||||
*/
|
||||
int selectionStart() const;
|
||||
|
||||
/**
|
||||
* Convenience function to return the end of the selection as a linear index
|
||||
* Contrary to the getSelection method, this method returns the cursor position if
|
||||
* no selection is made.
|
||||
*
|
||||
* \since QGIS 3.36
|
||||
*/
|
||||
int selectionEnd() const;
|
||||
|
||||
/**
|
||||
* Convenience function to set the selection using linear indexes
|
||||
*
|
||||
* \since QGIS 3.36
|
||||
*/
|
||||
void setLinearSelection( int start, int end );
|
||||
|
||||
public slots:
|
||||
|
||||
/**
|
||||
|
||||
@ -584,9 +584,7 @@ bool QgsCodeEditorPython::loadScript( const QString &script )
|
||||
|
||||
bool QgsCodeEditorPython::isCursorInsideStringLiteralOrComment() const
|
||||
{
|
||||
int line, index;
|
||||
getCursorPosition( &line, &index );
|
||||
int position = positionFromLineIndex( line, index );
|
||||
int position = linearPosition();
|
||||
|
||||
// Special case: cursor at the end of the document. Style will always be Default,
|
||||
// so we have to check the style of the previous character.
|
||||
@ -621,9 +619,7 @@ bool QgsCodeEditorPython::isCursorInsideStringLiteralOrComment() const
|
||||
|
||||
QString QgsCodeEditorPython::characterBeforeCursor() const
|
||||
{
|
||||
int line, index;
|
||||
getCursorPosition( &line, &index );
|
||||
int position = positionFromLineIndex( line, index );
|
||||
int position = linearPosition();
|
||||
if ( position <= 0 )
|
||||
{
|
||||
return QString();
|
||||
@ -633,9 +629,7 @@ QString QgsCodeEditorPython::characterBeforeCursor() const
|
||||
|
||||
QString QgsCodeEditorPython::characterAfterCursor() const
|
||||
{
|
||||
int line, index;
|
||||
getCursorPosition( &line, &index );
|
||||
int position = positionFromLineIndex( line, index );
|
||||
int position = linearPosition();
|
||||
if ( position >= length() )
|
||||
{
|
||||
return QString();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user