QGIS API Documentation 3.41.0-Master (3440c17df1d)
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#include "qgspanelwidget.h"
25
26// qscintilla includes
27#include <Qsci/qsciapis.h>
28#include "qgis_sip.h"
29#include "qgis_gui.h"
30
31#include <QMap>
32
34class QToolButton;
35class QCheckBox;
37
38SIP_IF_MODULE( HAVE_QSCI_SIP )
39
40
45class GUI_EXPORT QgsCodeInterpreter
46{
47 public:
48
50
56 int exec( const QString &command );
57
64 virtual int currentState() const { return mState; }
65
70 virtual QString promptForState( int state ) const = 0;
71
72 protected:
73
80 virtual int execCommandImpl( const QString &command ) = 0;
81
82 private:
83
84 int mState = 0;
85
86};
87
88
89// TODO QGIS 4.0 -- Consider making QgsCodeEditor inherit QWidget only,
90// with a separate getter for the QsciScintilla child widget. This
91// would give us more flexibility to add functionality to the base
92// QgsCodeEditor class, eg adding a message bar or other child widgets
93// to the editor widget. For now this extra functionality lives in
94// the QgsCodeEditorWidget wrapper widget.
95
101class GUI_EXPORT QgsCodeEditor : public QsciScintilla
102{
103 Q_OBJECT
104
105 public:
106
107
108#ifndef SIP_RUN
109
110 static inline QgsSettingsTreeNode *sTreeCodeEditor = QgsSettingsTree::sTreeGui->createChildNode( QStringLiteral( "code-editor" ) );
112#endif
113
119 enum class Mode
120 {
121 ScriptEditor,
122 OutputDisplay,
123 CommandInput,
124 };
125 Q_ENUM( Mode )
126
127
135 {
136 LineNumbers = 0,
137 ErrorIndicators = 1,
138 FoldingControls = 2,
139 };
140 Q_ENUM( MarginRole )
141
142
147 enum class Flag : int SIP_ENUM_BASETYPE( IntFlag )
148 {
149 CodeFolding = 1 << 0,
150 ImmediatelyUpdateHistory = 1 << 1,
151 };
152 Q_ENUM( Flag )
153
154
159 Q_DECLARE_FLAGS( Flags, Flag )
160 Q_FLAG( Flags )
161
163 static constexpr int SEARCH_RESULT_INDICATOR = QsciScintilla::INDIC_MAX - 1;
164
175 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 );
176
181 void setTitle( const QString &title );
182
188 virtual Qgis::ScriptLanguage language() const;
189
195 virtual Qgis::ScriptLanguageCapabilities languageCapabilities() const;
196
202 static QString languageToString( Qgis::ScriptLanguage language );
203
209 Q_DECL_DEPRECATED void setMarginVisible( bool margin ) SIP_DEPRECATED;
210
215 Q_DECL_DEPRECATED bool marginVisible() SIP_DEPRECATED { return mMargin; }
216
225 void setLineNumbersVisible( bool visible );
226
233 bool lineNumbersVisible() const;
234
239 void setFoldingVisible( bool folding );
240
245 bool foldingVisible();
246
252 void insertText( const QString &text );
253
265 static QColor defaultColor( QgsCodeEditorColorScheme::ColorRole role, const QString &theme = QString() );
266
276 static QColor color( QgsCodeEditorColorScheme::ColorRole role );
277
289 static void setColor( QgsCodeEditorColorScheme::ColorRole role, const QColor &color );
290
296 static QFont getMonospaceFont();
297
305 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;
306
313 void addWarning( int lineNumber, const QString &warning );
314
321 void clearWarnings();
322
328 QgsCodeEditor::Mode mode() const { return mMode; }
329
335 bool isCursorOnLastLine() const;
336
345 void setHistoryFilePath( const QString &path );
346
354 QStringList history() const;
355
362 QgsCodeInterpreter *interpreter() const;
363
371 void setInterpreter( QgsCodeInterpreter *newInterpreter );
372
378 int linearPosition() const;
379
385 void setLinearPosition( int position );
386
394 int selectionStart() const;
395
403 int selectionEnd() const;
404
410 void setLinearSelection( int start, int end );
411
412 // Override QsciScintilla::callTip to handle wrapping
413 virtual void callTip() override;
414
422 int wrapPosition( int line = -1 );
423
424 public slots:
425
436 void runCommand( const QString &command, bool skipHistory = false );
437
444 virtual void moveCursorToStart();
445
452 virtual void moveCursorToEnd();
453
461 void showPreviousCommand();
462
470 void showNextCommand();
471
479 void showHistory();
480
486 void removeHistoryCommand( int index );
487
495 void clearSessionHistory();
496
504 void clearPersistentHistory();
505
511 bool writeHistoryFile();
512
520 void reformatCode();
521
529 virtual bool checkSyntax();
530
538 virtual void toggleComment();
539
540 signals:
541
548
555
556
562 void helpRequested( const QString &word );
563
564 protected:
565
569 static bool isFixedPitch( const QFont &font );
570
571 void focusOutEvent( QFocusEvent *event ) override;
572 void keyPressEvent( QKeyEvent *event ) override;
573 void contextMenuEvent( QContextMenuEvent *event ) override;
574 bool eventFilter( QObject *watched, QEvent *event ) override;
575
583 virtual void initializeLexer();
584
590 QColor lexerColor( QgsCodeEditorColorScheme::ColorRole role ) const;
591
597 QFont lexerFont() const;
598
604 void runPostLexerConfigurationTasks();
605
611 void updateSoftHistory();
612
620 void updatePrompt();
621
631 virtual void populateContextMenu( QMenu *menu );
632
640 virtual QString reformatCodeString( const QString &string );
641
649 virtual void showMessage( const QString &title, const QString &message, Qgis::MessageLevel level );
650
651 private:
652
653 void setSciWidget();
654 void updateFolding();
655 bool readHistoryFile();
656 void syncSoftHistory();
657 void updateHistory( const QStringList &commands, bool skipSoftHistory = false );
658 char getCharacter( int &pos ) const;
659
660 QString mWidgetTitle;
661 bool mMargin = false;
664
665 bool mUseDefaultSettings = true;
666 // used if above is false, inplace of values taken from QSettings:
667 bool mOverrideColors = false;
668 QString mColorScheme;
669 QMap< QgsCodeEditorColorScheme::ColorRole, QColor > mCustomColors;
670 QString mFontFamily;
671 int mFontSize = 0;
672
673 QVector< int > mWarningLines;
674
675 // for use in command input mode
676 QStringList mHistory;
677 QStringList mSoftHistory;
678 int mSoftHistoryIndex = 0;
679 QString mHistoryFilePath;
680
681 QgsCodeInterpreter *mInterpreter = nullptr;
682
683 static QMap< QgsCodeEditorColorScheme::ColorRole, QString > sColorRoleToSettingsKey;
684
685 static constexpr int MARKER_NUMBER = 6;
686};
687
689
690// clazy:excludeall=qstring-allocations
691
692#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:154
A text editor based on QScintilla2.
void sessionHistoryCleared()
Emitted when the history of commands run in the current session is cleared.
static const QgsSettingsEntryBool * settingContextHelpHover
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.
void helpRequested(const QString &word)
Emitted when documentation was requested for the specified word.
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()
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
A boolean settings entry.
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)