Skip to content

Commit

Permalink
QgsCodeEditor: workaround 2 QScintilla bugs
Browse files Browse the repository at this point in the history
- Fix one bug with Scintilla 2.8.X when focus out event does not
  disable blinking cursor
- Make QScintilla widget not capture Escape key so that it can
  propagate to parent.
  • Loading branch information
rouault committed May 12, 2016
1 parent 583eaef commit 4a761df
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/gui/qgscodeeditor.cpp
Expand Up @@ -20,6 +20,7 @@
#include <QWidget>
#include <QFont>
#include <QDebug>
#include <QFocusEvent>

QgsCodeEditor::QgsCodeEditor( QWidget *parent, const QString& title, bool folding, bool margin )
: QsciScintilla( parent )
Expand All @@ -44,6 +45,50 @@ QgsCodeEditor::~QgsCodeEditor()
{
}

// Workaround a bug in QScintilla 2.8.X
void QgsCodeEditor::focusOutEvent( QFocusEvent *event )
{
#if QSCINTILLA_VERSION >= 0x020800 && QSCINTILLA_VERSION < 0x020900
if ( event->reason() != Qt::ActiveWindowFocusReason )
{
/* There's a bug in all QScintilla 2.8.X, where
a focus out event that is not due to ActiveWindowFocusReason doesn't
lead to the bliking caret being disabled. The hack consists in making
QsciScintilla::focusOutEvent believe that the event is a ActiveWindowFocusReason
The bug was fixed in 2.9 per:
2015-04-14 Phil Thompson <phil@riverbankcomputing.com>
* qt/qsciscintillabase.cpp:
Fixed a problem notifying when focus is lost to another application
widget.
[41734678234e]
*/
QFocusEvent newFocusEvent( QEvent::FocusOut, Qt::ActiveWindowFocusReason );
QsciScintilla::focusOutEvent( &newFocusEvent );
}
else
#endif
{
QsciScintilla::focusOutEvent( event );
}
}

// This workaround a likely bug in QScintilla. The ESC key should not be consumned
// by the main entry, so that the default behaviour (Dialog closing) can trigger,
// but only is the auto-completion suggestion list isn't displayed
void QgsCodeEditor::keyPressEvent( QKeyEvent * event )
{
if ( event->key() == Qt::Key_Escape && !isListActive() )
{
// Shortcut QScintilla and redirect the event to the QWidget handler
QWidget::keyPressEvent( event );
}
else
{
QsciScintilla::keyPressEvent( event );
}
}

void QgsCodeEditor::setSciWidget()
{
setUtf8( true );
Expand Down
3 changes: 3 additions & 0 deletions src/gui/qgscodeeditor.h
Expand Up @@ -73,6 +73,9 @@ class GUI_EXPORT QgsCodeEditor : public QsciScintilla

bool isFixedPitch( const QFont& font );

void focusOutEvent( QFocusEvent *event ) override;
void keyPressEvent( QKeyEvent * event ) override;

QFont getMonospaceFont();

private:
Expand Down

0 comments on commit 4a761df

Please sign in to comment.