|
| 1 | +/*************************************************************************** |
| 2 | + qgscodeeditor.cpp - description |
| 3 | + -------------------------------------- |
| 4 | + Date : 06-Oct-2013 |
| 5 | + Copyright : (C) 2013 by Salvatore Larosa |
| 6 | + Email : lrssvtml (at) gmail (dot) com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | +#include "qgscodeeditor.h" |
| 17 | + |
| 18 | +#include <QSettings> |
| 19 | +#include <QWidget> |
| 20 | +#include <QFont> |
| 21 | +#include <QDebug> |
| 22 | + |
| 23 | +QgsCodeEditor::QgsCodeEditor( QWidget *parent, QString title, bool folding, bool margin ) |
| 24 | + : QsciScintilla( parent ), |
| 25 | + mWidgetTitle( title ), |
| 26 | + mFolding( folding ), |
| 27 | + mMargin( margin ) |
| 28 | +{ |
| 29 | + if ( !parent && mWidgetTitle.isEmpty() ) |
| 30 | + { |
| 31 | + setWindowTitle( "QScintilla2 Text Editor" ); |
| 32 | + setMinimumSize( 800, 300 ); |
| 33 | + } |
| 34 | + else |
| 35 | + { |
| 36 | + setWindowTitle( mWidgetTitle ); |
| 37 | + } |
| 38 | + setSciWidget(); |
| 39 | +} |
| 40 | + |
| 41 | +QgsCodeEditor::~QgsCodeEditor() |
| 42 | +{ |
| 43 | +} |
| 44 | + |
| 45 | +void QgsCodeEditor::setSciWidget() |
| 46 | +{ |
| 47 | + setUtf8( true ); |
| 48 | + setCaretLineVisible( true ); |
| 49 | + setCaretLineBackgroundColor( QColor( "#fcf3ed" ) ); |
| 50 | + |
| 51 | + setBraceMatching( QsciScintilla::SloppyBraceMatch ); |
| 52 | + setMatchedBraceBackgroundColor( QColor( "#b7f907" ) ); |
| 53 | + // whether margin will be shown |
| 54 | + enableMargin( mMargin ); |
| 55 | + // whether margin will be shown |
| 56 | + enableFolding( mFolding ); |
| 57 | + // indentation |
| 58 | + setAutoIndent( true ); |
| 59 | + setIndentationWidth( 4 ); |
| 60 | + setTabIndents( true ); |
| 61 | + setBackspaceUnindents( true ); |
| 62 | + setTabWidth( 4 ); |
| 63 | + // autocomplete |
| 64 | + setAutoCompletionThreshold( 2 ); |
| 65 | + setAutoCompletionSource( QsciScintilla::AcsAPIs ); |
| 66 | +} |
| 67 | + |
| 68 | +bool QgsCodeEditor::enableMargin( bool margin ) |
| 69 | +{ |
| 70 | + if ( margin ) |
| 71 | + { |
| 72 | + QFont marginFont( "Courier", 10 ); |
| 73 | + setMarginLineNumbers( 1, true ); |
| 74 | + setMarginsFont( marginFont ); |
| 75 | + setMarginWidth( 1, "00000" ); |
| 76 | + setMarginsForegroundColor( QColor( "#3E3EE3" ) ); |
| 77 | + setMarginsBackgroundColor( QColor( "#f9f9f9" ) ); |
| 78 | + return true; |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + setMarginWidth( 0, 0 ); |
| 83 | + setMarginWidth( 1, 0 ); |
| 84 | + setMarginWidth( 2, 0 ); |
| 85 | + return false; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +void QgsCodeEditor::enableFolding( bool folding ) |
| 90 | +{ |
| 91 | + if ( folding ) |
| 92 | + { |
| 93 | + setFolding( QsciScintilla::PlainFoldStyle ); |
| 94 | + setFoldMarginColors( QColor( "#f4f4f4" ), QColor( "#f4f4f4" ) ); |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + setFolding( QsciScintilla::NoFoldStyle ); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// Settings for font and fontsize |
| 103 | +bool QgsCodeEditor::isFixedPitch( const QFont& font ) |
| 104 | +{ |
| 105 | + const QFontInfo fi( font ); |
| 106 | + qDebug() << fi.family() << fi.fixedPitch(); |
| 107 | + return fi.fixedPitch(); |
| 108 | +} |
| 109 | + |
| 110 | +QFont QgsCodeEditor::getMonospaceFont() |
| 111 | +{ |
| 112 | + QFont font( "monospace" ); |
| 113 | + if ( isFixedPitch( font ) ) |
| 114 | + { |
| 115 | + return font; |
| 116 | + } |
| 117 | + font.setStyleHint( QFont::Monospace ); |
| 118 | + if ( isFixedPitch( font ) ) |
| 119 | + { |
| 120 | + return font; |
| 121 | + } |
| 122 | + font.setStyleHint( QFont::TypeWriter ); |
| 123 | + if ( isFixedPitch( font ) ) |
| 124 | + { |
| 125 | + return font; |
| 126 | + } |
| 127 | + font.setFamily( "courier" ); |
| 128 | + if ( isFixedPitch( font ) ) |
| 129 | + { |
| 130 | + return font; |
| 131 | + } |
| 132 | + return font; |
| 133 | +} |
0 commit comments