QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgscodeeditorhtml.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscodeeditorhtml.cpp - A HTML editor based on QScintilla
3  --------------------------------------
4  Date : 20-Jul-2014
5  Copyright : (C) 2014 by Nathan Woodrow
6  Email : woodrow.nathan (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 "qgsapplication.h"
17 #include "qgscodeeditorhtml.h"
18 #include "qgssymbollayerutils.h"
19 
20 #include <QWidget>
21 #include <QString>
22 #include <QFont>
23 #include <QLabel>
24 #include <Qsci/qscilexerhtml.h>
25 
26 
28  : QgsCodeEditor( parent )
29 {
30  if ( !parent )
31  {
32  setTitle( tr( "HTML Editor" ) );
33  }
34  setMarginVisible( false );
35  setFoldingVisible( true );
36  setSciLexerHTML();
37 }
38 
39 void QgsCodeEditorHTML::setSciLexerHTML()
40 {
41  QHash< QString, QColor > colors;
42  if ( QgsApplication::instance()->themeName() != QStringLiteral( "default" ) )
43  {
44  QSettings ini( QgsApplication::instance()->uiThemes().value( QgsApplication::instance()->themeName() ) + "/qscintilla.ini", QSettings::IniFormat );
45  for ( const auto &key : ini.allKeys() )
46  {
47  colors.insert( key, QgsSymbolLayerUtils::decodeColor( ini.value( key ).toString() ) );
48  }
49  }
50 
51  QFont font = getMonospaceFont();
52 #ifdef Q_OS_MAC
53  // The font size gotten from getMonospaceFont() is too small on Mac
54  font.setPointSize( QLabel().font().pointSize() );
55 #endif
56  QColor defaultColor = colors.value( QStringLiteral( "html/defaultFontColor" ), Qt::black );
57 
58  QsciLexerHTML *lexer = new QsciLexerHTML( this );
59  lexer->setDefaultFont( font );
60  lexer->setDefaultColor( defaultColor );
61  lexer->setDefaultPaper( colors.value( QStringLiteral( "html/paperBackgroundColor" ), Qt::white ) );
62  lexer->setFont( font, -1 );
63 
64  lexer->setColor( defaultColor, QsciLexerHTML::Default );
65  lexer->setColor( colors.value( QStringLiteral( "html/tagFontColor" ), QColor( 66, 113, 174 ) ), QsciLexerHTML::Tag );
66  lexer->setColor( colors.value( QStringLiteral( "html/unknownTagFontColor" ), QColor( 255, 0, 0 ) ), QsciLexerHTML::UnknownTag );
67  lexer->setColor( colors.value( QStringLiteral( "html/numberFontColor" ), QColor( 200, 40, 41 ) ), QsciLexerHTML::HTMLNumber );
68  lexer->setColor( colors.value( QStringLiteral( "html/commentFontColor" ), QColor( 142, 144, 140 ) ), QsciLexerHTML::HTMLComment );
69  lexer->setColor( colors.value( QStringLiteral( "html/singleQuoteFontColor" ), QColor( 113, 140, 0 ) ), QsciLexerHTML::HTMLSingleQuotedString );
70  lexer->setColor( colors.value( QStringLiteral( "html/doubleQuoteFontColor" ), QColor( 113, 140, 0 ) ), QsciLexerHTML::HTMLDoubleQuotedString );
71 
72  setLexer( lexer );
73 }
A text editor based on QScintilla2.
Definition: qgscodeeditor.h:38
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
void setFoldingVisible(bool folding)
Set folding visible state.
QFont getMonospaceFont()
QgsCodeEditorHTML(QWidget *parent=nullptr)
Constructor for QgsCodeEditorHTML.
void setMarginVisible(bool margin)
Set margin visible state.
void setTitle(const QString &title)
Set the widget title.
static QColor decodeColor(const QString &str)