QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgscodeeditor.h
Go to the documentation of this file.
1/***************************************************************************
2 qgscodeeditor.h - A base code editor for QGIS and plugins. Provides
3 a base editor using QScintilla for editors
4 --------------------------------------
5 Date : 06-Oct-2013
6 Copyright : (C) 2013 by Salvatore Larosa
7 Email : lrssvtml (at) gmail (dot) com
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#ifndef QGSCODEEDITOR_H
18#define QGSCODEEDITOR_H
19
20#include <QString>
22#include "qgis.h"
23#include "qgssettingstree.h"
24
25// qscintilla includes
26#include <Qsci/qsciapis.h>
27#include "qgis_sip.h"
28#include "qgis_gui.h"
29
30#include <QMap>
31
32
33SIP_IF_MODULE( HAVE_QSCI_SIP )
34
35
40class GUI_EXPORT QgsCodeInterpreter
41{
42 public:
43
45
51 int exec( const QString &command );
52
59 virtual int currentState() const { return mState; }
60
65 virtual QString promptForState( int state ) const = 0;
66
67 protected:
68
75 virtual int execCommandImpl( const QString &command ) = 0;
76
77 private:
78
79 int mState = 0;
80
81};
82
83
84
85class QWidget;
86
92class GUI_EXPORT QgsCodeEditor : public QsciScintilla
93{
94 Q_OBJECT
95
96 public:
97
98
99#ifndef SIP_RUN
100
101 static inline QgsSettingsTreeNode *sTreeCodeEditor = QgsSettingsTree::sTreeGui->createChildNode( QStringLiteral( "code-editor" ) );
102
103#endif
104
110 enum class Mode
111 {
112 ScriptEditor,
113 OutputDisplay,
114 CommandInput,
115 };
116 Q_ENUM( Mode )
117
118
126 {
127 LineNumbers = 0,
128 ErrorIndicators = 1,
129 FoldingControls = 2,
130 };
131 Q_ENUM( MarginRole )
132
133
138 enum class Flag : int SIP_ENUM_BASETYPE( IntFlag )
139 {
140 CodeFolding = 1 << 0,
141 ImmediatelyUpdateHistory = 1 << 1,
142 };
143 Q_ENUM( Flag )
144
145
150 Q_DECLARE_FLAGS( Flags, Flag )
151 Q_FLAG( Flags )
152
163 QgsCodeEditor( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &title = QString(), bool folding = false, bool margin = false, QgsCodeEditor::Flags flags = QgsCodeEditor::Flags(), QgsCodeEditor::Mode mode = QgsCodeEditor::Mode::ScriptEditor );
164
169 void setTitle( const QString &title );
170
176 virtual Qgis::ScriptLanguage language() const;
177
183 virtual Qgis::ScriptLanguageCapabilities languageCapabilities() const;
184
190 static QString languageToString( Qgis::ScriptLanguage language );
191
197 Q_DECL_DEPRECATED void setMarginVisible( bool margin ) SIP_DEPRECATED;
198
203 Q_DECL_DEPRECATED bool marginVisible() SIP_DEPRECATED { return mMargin; }
204
213 void setLineNumbersVisible( bool visible );
214
221 bool lineNumbersVisible() const;
222
227 void setFoldingVisible( bool folding );
228
233 bool foldingVisible();
234
240 void insertText( const QString &text );
241
253 static QColor defaultColor( QgsCodeEditorColorScheme::ColorRole role, const QString &theme = QString() );
254
264 static QColor color( QgsCodeEditorColorScheme::ColorRole role );
265
277 static void setColor( QgsCodeEditorColorScheme::ColorRole role, const QColor &color );
278
284 static QFont getMonospaceFont();
285
293 void setCustomAppearance( const QString &scheme = QString(), const QMap< QgsCodeEditorColorScheme::ColorRole, QColor > &customColors = QMap< QgsCodeEditorColorScheme::ColorRole, QColor >(), const QString &fontFamily = QString(), int fontSize = 0 ) SIP_SKIP;
294
301 void addWarning( int lineNumber, const QString &warning );
302
309 void clearWarnings();
310
316 QgsCodeEditor::Mode mode() const { return mMode; }
317
323 bool isCursorOnLastLine() const;
324
333 void setHistoryFilePath( const QString &path );
334
342 QStringList history() const;
343
350 QgsCodeInterpreter *interpreter() const;
351
359 void setInterpreter( QgsCodeInterpreter *newInterpreter );
360
366 int linearPosition() const;
367
373 void setLinearPosition( int position );
374
382 int selectionStart() const;
383
391 int selectionEnd() const;
392
398 void setLinearSelection( int start, int end );
399
400 public slots:
401
412 void runCommand( const QString &command, bool skipHistory = false );
413
420 virtual void moveCursorToStart();
421
428 virtual void moveCursorToEnd();
429
437 void showPreviousCommand();
438
446 void showNextCommand();
447
455 void showHistory();
456
462 void removeHistoryCommand( int index );
463
471 void clearSessionHistory();
472
480 void clearPersistentHistory();
481
487 bool writeHistoryFile();
488
496 void reformatCode();
497
505 virtual bool checkSyntax();
506
514 virtual void toggleComment();
515
516 signals:
517
524
531
532 protected:
533
537 static bool isFixedPitch( const QFont &font );
538
539 void focusOutEvent( QFocusEvent *event ) override;
540 void keyPressEvent( QKeyEvent *event ) override;
541 void contextMenuEvent( QContextMenuEvent *event ) override;
542 bool eventFilter( QObject *watched, QEvent *event ) override;
543
551 virtual void initializeLexer();
552
558 QColor lexerColor( QgsCodeEditorColorScheme::ColorRole role ) const;
559
565 QFont lexerFont() const;
566
572 void runPostLexerConfigurationTasks();
573
579 void updateSoftHistory();
580
588 void updatePrompt();
589
599 virtual void populateContextMenu( QMenu *menu );
600
608 virtual QString reformatCodeString( const QString &string );
609
617 virtual void showMessage( const QString &title, const QString &message, Qgis::MessageLevel level );
618
619 private:
620
621 void setSciWidget();
622 void updateFolding();
623 bool readHistoryFile();
624 void syncSoftHistory();
625 void updateHistory( const QStringList &commands, bool skipSoftHistory = false );
626
627 QString mWidgetTitle;
628 bool mMargin = false;
631
632 bool mUseDefaultSettings = true;
633 // used if above is false, inplace of values taken from QSettings:
634 bool mOverrideColors = false;
635 QString mColorScheme;
636 QMap< QgsCodeEditorColorScheme::ColorRole, QColor > mCustomColors;
637 QString mFontFamily;
638 int mFontSize = 0;
639
640 QVector< int > mWarningLines;
641
642 // for use in command input mode
643 QStringList mHistory;
644 QStringList mSoftHistory;
645 int mSoftHistoryIndex = 0;
646 QString mHistoryFilePath;
647
648 QgsCodeInterpreter *mInterpreter = nullptr;
649
650 static QMap< QgsCodeEditorColorScheme::ColorRole, QString > sColorRoleToSettingsKey;
651
652 static constexpr int MARKER_NUMBER = 6;
653};
654
656
657// clazy:excludeall=qstring-allocations
658
659#endif
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:54
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:99
A text editor based on QScintilla2.
Definition: qgscodeeditor.h:93
void sessionHistoryCleared()
Emitted when the history of commands run in the current session is cleared.
Mode
Code editor modes.
@ ScriptEditor
Standard mode, allows for display and edit of entire scripts.
QFlags< Flag > Flags
Flags controlling behavior of code editor.
void persistentHistoryCleared()
Emitted when the persistent history of commands run in the editor is cleared.
MarginRole
Margin roles.
Flag
Flags controlling behavior of code editor.
An interface for code interpreters.
Definition: qgscodeeditor.h:41
virtual int execCommandImpl(const QString &command)=0
Pure virtual method for executing commands in the interpreter.
virtual int currentState() const
Returns the current interpreter state.
Definition: qgscodeeditor.h:59
virtual QString promptForState(int state) const =0
Returns the interactive prompt string to use for the interpreter, given a state.
virtual ~QgsCodeInterpreter()
QgsSettingsTreeNode is a tree node for the settings tree to help organizing and introspecting the tre...
QgsSettingsTreeNode * createChildNode(const QString &key)
Creates a normal tree node It will return the existing child node if it exists at the given key.
static QgsSettingsTreeNode * sTreeGui
#define SIP_IF_MODULE(condition)
Definition: qgis_sip.h:28
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_ENUM_BASETYPE(type)
Definition: qgis_sip.h:278
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition: qgis_sip.h:271
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)