QGIS API Documentation 3.35.0-Master (ffb31addd7f)
Loading...
Searching...
No Matches
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
93class GUI_EXPORT QgsCodeEditor : public QsciScintilla
94{
95 Q_OBJECT
96
97 public:
98
99
100#ifndef SIP_RUN
101
102 static inline QgsSettingsTreeNode *sTreeCodeEditor = QgsSettingsTree::sTreeGui->createChildNode( QStringLiteral( "code-editor" ) );
103
104#endif
105
111 enum class Mode
112 {
113 ScriptEditor,
114 OutputDisplay,
115 CommandInput,
116 };
117 Q_ENUM( Mode )
118
119
127 {
128 LineNumbers = 0,
129 ErrorIndicators = 1,
130 FoldingControls = 2,
131 };
132 Q_ENUM( MarginRole )
133
134
139 enum class Flag : int
140 {
141 CodeFolding = 1 << 0,
142 ImmediatelyUpdateHistory = 1 << 1,
143 };
144 Q_ENUM( Flag )
145
146
151 Q_DECLARE_FLAGS( Flags, Flag )
152 Q_FLAG( Flags )
153
154
165 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 );
166
171 void setTitle( const QString & title );
172
178 virtual Qgis::ScriptLanguage language() const;
179
185 virtual Qgis::ScriptLanguageCapabilities languageCapabilities() const;
186
192 static QString languageToString( Qgis::ScriptLanguage language );
193
199 Q_DECL_DEPRECATED void setMarginVisible( bool margin ) SIP_DEPRECATED;
200
205 Q_DECL_DEPRECATED bool marginVisible() SIP_DEPRECATED { return mMargin; }
206
215 void setLineNumbersVisible( bool visible );
216
223 bool lineNumbersVisible() const;
224
229 void setFoldingVisible( bool folding );
230
235 bool foldingVisible();
236
242 void insertText( const QString & text );
243
255 static QColor defaultColor( QgsCodeEditorColorScheme::ColorRole role, const QString & theme = QString() );
256
266 static QColor color( QgsCodeEditorColorScheme::ColorRole role );
267
279 static void setColor( QgsCodeEditorColorScheme::ColorRole role, const QColor & color );
280
286 static QFont getMonospaceFont();
287
295 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;
296
303 void addWarning( int lineNumber, const QString & warning );
304
311 void clearWarnings();
312
318 QgsCodeEditor::Mode mode() const { return mMode; }
319
325 bool isCursorOnLastLine() const;
326
335 void setHistoryFilePath( const QString &path );
336
344 QStringList history() const;
345
352 QgsCodeInterpreter *interpreter() const;
353
361 void setInterpreter( QgsCodeInterpreter *newInterpreter );
362
368 int linearPosition() const;
369
375 void setLinearPosition( int position );
376
384 int selectionStart() const;
385
393 int selectionEnd() const;
394
400 void setLinearSelection( int start, int end );
401
402 public slots:
403
414 void runCommand( const QString &command, bool skipHistory = false );
415
422 virtual void moveCursorToStart();
423
430 virtual void moveCursorToEnd();
431
439 void showPreviousCommand();
440
448 void showNextCommand();
449
457 void showHistory();
458
464 void removeHistoryCommand( int index );
465
473 void clearSessionHistory();
474
482 void clearPersistentHistory();
483
489 bool writeHistoryFile();
490
498 void reformatCode();
499
507 virtual bool checkSyntax();
508
516 virtual void toggleComment();
517
518 signals:
519
526
533
534 protected:
535
539 static bool isFixedPitch( const QFont &font );
540
541 void focusOutEvent( QFocusEvent *event ) override;
542 void keyPressEvent( QKeyEvent *event ) override;
543 void contextMenuEvent( QContextMenuEvent *event ) override;
544 bool eventFilter( QObject *watched, QEvent *event ) override;
545
553 virtual void initializeLexer();
554
560 QColor lexerColor( QgsCodeEditorColorScheme::ColorRole role ) const;
561
567 QFont lexerFont() const;
568
574 void runPostLexerConfigurationTasks();
575
581 void updateSoftHistory();
582
590 void updatePrompt();
591
601 virtual void populateContextMenu( QMenu *menu );
602
610 virtual QString reformatCodeString( const QString &string );
611
619 virtual void showMessage( const QString &title, const QString &message, Qgis::MessageLevel level );
620
621 private:
622
623 void setSciWidget();
624 void updateFolding();
625 bool readHistoryFile();
626 void syncSoftHistory();
627 void updateHistory( const QStringList &commands, bool skipSoftHistory = false );
628
629 QString mWidgetTitle;
630 bool mMargin = false;
631 QgsCodeEditor::Flags mFlags;
633
634 bool mUseDefaultSettings = true;
635 // used if above is false, inplace of values taken from QSettings:
636 bool mOverrideColors = false;
637 QString mColorScheme;
638 QMap< QgsCodeEditorColorScheme::ColorRole, QColor > mCustomColors;
639 QString mFontFamily;
640 int mFontSize = 0;
641
642 QVector< int > mWarningLines;
643
644 // for use in command input mode
645 QStringList mHistory;
646 QStringList mSoftHistory;
647 int mSoftHistoryIndex = 0;
648 QString mHistoryFilePath;
649
650 QgsCodeInterpreter *mInterpreter = nullptr;
651
652 static QMap< QgsCodeEditorColorScheme::ColorRole, QString > sColorRoleToSettingsKey;
653
654 static constexpr int MARKER_NUMBER = 6;
655};
656
657Q_DECLARE_OPERATORS_FOR_FLAGS( QgsCodeEditor::Flags )
658
659// clazy:excludeall=qstring-allocations
660
661#endif
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:99
ScriptLanguage
Scripting languages.
Definition qgis.h:3136
A text editor based on QScintilla2.
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.
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.
Q_DECL_DEPRECATED bool marginVisible()
Returns whether margins are in a visible state.
An interface for code interpreters.
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.
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_SKIP
Definition qgis_sip.h:126
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:266
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)