@@ -283,6 +283,19 @@ void SrcEditor::setShowLineNumbers(bool show) {
283283 updateMargins (0 );
284284}
285285
286+ void SrcEditor::setEnableHighlight (bool enable) {
287+ if (!enable) {
288+ // clear old styles
289+ QList<QTextEdit::ExtraSelection> extra_selections;
290+ setExtraSelections (extra_selections);
291+ }
292+ enable_highlight = enable;
293+ }
294+
295+ void SrcEditor::setEnableFocusChange (bool enable) {
296+ enable_focus_change = enable;
297+ }
298+
286299void SrcEditor::insertFromMimeData (const QMimeData *source) {
287300 if (source->hasText ()) { insertPlainText (source->text ()); }
288301}
@@ -292,27 +305,34 @@ bool SrcEditor::canInsertFromMimeData(const QMimeData *source) const {
292305}
293306
294307void SrcEditor::highlightBlock (int block_num) {
295- QList<QTextEdit::ExtraSelection> extra_selections ;
308+ QTextBlock block = document ()-> findBlockByNumber (block_num - 1 ) ;
296309
297- // set hightly style
298- QTextEdit::ExtraSelection selection;
299- QColor lineColor = QColor (Qt::yellow).lighter (160 );
300- selection.format .setBackground (lineColor);
301- selection.format .setProperty (QTextFormat::FullWidthSelection, true );
310+ if (enable_highlight) {
311+ // set hightly style
312+ QList<QTextEdit::ExtraSelection> extra_selections;
313+ QTextEdit::ExtraSelection selection;
314+ QColor lineColor = QColor (Qt::yellow).lighter (160 );
315+ selection.format .setBackground (lineColor);
316+ selection.format .setProperty (QTextFormat::FullWidthSelection, true );
317+
318+ // move cursor
319+ selection.cursor = QTextCursor (block);
320+ // select until the end of block
321+ selection.cursor .movePosition (
322+ QTextCursor::EndOfBlock, QTextCursor::KeepAnchor, block.length ());
323+ // select an extra \n
324+ selection.cursor .movePosition (QTextCursor::Right, QTextCursor::KeepAnchor, 1 );
325+ extra_selections.append (selection);
326+ setExtraSelections (extra_selections);
327+ }
302328
303- // select block
304- QTextBlock block = document ()->findBlockByNumber (block_num - 1 );
305- selection.cursor = QTextCursor (block);
306- selection.cursor .movePosition (QTextCursor::EndOfBlock, QTextCursor::KeepAnchor, block.length ());
307- extra_selections.append (selection);
308-
309- // calculate viewport line count
310- int viewport_line_count
311- = viewport ()->height () / QFontMetrics (document ()->defaultFont ()).height ();
312- // scroll to block and show it in editor middle
313- QScrollBar *vScrollBar = verticalScrollBar ();
314- vScrollBar->setValue (
315- vScrollBar->singleStep () * (block.firstLineNumber () - viewport_line_count / 2 ));
316-
317- setExtraSelections (extra_selections);
329+ if (enable_focus_change) {
330+ // calculate viewport line count
331+ int viewport_line_count
332+ = viewport ()->height () / QFontMetrics (document ()->defaultFont ()).height ();
333+ // scroll to block and show it in editor middle
334+ QScrollBar *vScrollBar = verticalScrollBar ();
335+ vScrollBar->setValue (
336+ vScrollBar->singleStep () * (block.firstLineNumber () - viewport_line_count / 2 ));
337+ }
318338}
0 commit comments