Add adjustScrollWidth method

This commit is contained in:
Yoann Quenach de Quivillic 2024-12-26 01:56:47 +01:00 committed by Loïc Bartoletti
parent 9e1be4bb53
commit 86bd2c5353
4 changed files with 58 additions and 0 deletions

View File

@ -499,6 +499,16 @@ This is only supported for editors which return the :py:class:`Qgis`.ScriptLangu
.. versionadded:: 3.32
%End
void adjustScrollWidth();
%Docstring
Adjust the width of the scroll bar to fit the content.
.. versionadded:: 3.42
%End
virtual void setText( const QString &text );
signals:
void sessionHistoryCleared();

View File

@ -499,6 +499,16 @@ This is only supported for editors which return the :py:class:`Qgis`.ScriptLangu
.. versionadded:: 3.32
%End
void adjustScrollWidth();
%Docstring
Adjust the width of the scroll bar to fit the content.
.. versionadded:: 3.42
%End
virtual void setText( const QString &text );
signals:
void sessionHistoryCleared();

View File

@ -817,6 +817,34 @@ void QgsCodeEditor::toggleComment()
{
}
void QgsCodeEditor::adjustScrollWidth()
{
// A zero width would make setScrollWidth crash
long maxWidth = 10;
// Get the number of lines
int lineCount = lines();
// Loop through all the lines to get the longest one
for ( int line = 0; line < lineCount; line++ )
{
// Get the linear position at the end of the current line
const long endLine = SendScintilla( SCI_GETLINEENDPOSITION, line );
// Get the x coordinates of the end of the line
const long x = SendScintilla( SCI_POINTXFROMPOSITION, 0, endLine );
maxWidth = std::max( maxWidth, x );
}
// Use the longest line width as the new scroll width
setScrollWidth( maxWidth );
}
void QgsCodeEditor::setText( const QString &text )
{
QsciScintilla::setText( text );
adjustScrollWidth();
}
QStringList QgsCodeEditor::history() const
{
return mHistory;

View File

@ -531,6 +531,16 @@ class GUI_EXPORT QgsCodeEditor : public QsciScintilla
*/
virtual void toggleComment();
/**
* Adjust the width of the scroll bar to fit the content.
*
* \since QGIS 3.42
*/
void adjustScrollWidth();
// Override QsciScintilla::setText to adjust the scroll width
void setText( const QString &text ) override;
signals:
/**