QGIS API Documentation 3.99.0-Master (d270888f95f)
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 "qgis.h"
22#include "qgspanelwidget.h"
23#include "qgssettingstree.h"
24
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
29// qscintilla includes
30#include <Qsci/qsciapis.h>
31#include "qgis_sip.h"
32#include "qgis_gui.h"
33
34#include <QMap>
35
37class QToolButton;
38class QCheckBox;
40
41SIP_IF_MODULE( HAVE_QSCI_SIP )
42
43
48class GUI_EXPORT QgsCodeInterpreter
49{
50 public:
52
58 int exec( const QString &command );
59
66 virtual int currentState() const { return mState; }
67
72 virtual QString promptForState( int state ) const = 0;
73
74 protected:
81 virtual int execCommandImpl( const QString &command ) = 0;
82
83 private:
84 int mState = 0;
85};
86
87
88// TODO QGIS 5.0 -- Consider making QgsCodeEditor inherit QWidget only,
89// with a separate getter for the QsciScintilla child widget. This
90// would give us more flexibility to add functionality to the base
91// QgsCodeEditor class, eg adding a message bar or other child widgets
92// to the editor widget. For now this extra functionality lives in
93// the QgsCodeEditorWidget wrapper widget.
94
100class GUI_EXPORT QgsCodeEditor : public QsciScintilla
101{
102 Q_OBJECT
103
104 public:
105#ifndef SIP_RUN
106
107 static inline QgsSettingsTreeNode *sTreeCodeEditor = QgsSettingsTree::sTreeGui->createChildNode( u"code-editor"_s );
109#endif
110
116 enum class Mode
117 {
118 ScriptEditor,
119 OutputDisplay,
120 CommandInput,
121 };
122 Q_ENUM( Mode )
123
124
132 {
133 LineNumbers = 0,
134 ErrorIndicators = 1,
135 FoldingControls = 2,
136 };
137 Q_ENUM( MarginRole )
138
139
144 enum class Flag : int SIP_ENUM_BASETYPE( IntFlag )
145 {
146 CodeFolding = 1 << 0,
147 ImmediatelyUpdateHistory = 1 << 1,
148 };
149 Q_ENUM( Flag )
150
151
156 Q_DECLARE_FLAGS( Flags, Flag )
157 Q_FLAG( Flags )
158
160 static constexpr int SEARCH_RESULT_INDICATOR = QsciScintilla::INDIC_MAX - 1;
161
172 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 );
173
178 void setTitle( const QString &title );
179
185 virtual Qgis::ScriptLanguage language() const;
186
192 virtual Qgis::ScriptLanguageCapabilities languageCapabilities() const;
193
199 static QString languageToString( Qgis::ScriptLanguage language );
200
206 Q_DECL_DEPRECATED void setMarginVisible( bool margin ) SIP_DEPRECATED;
207
212 Q_DECL_DEPRECATED bool marginVisible() SIP_DEPRECATED { return mMargin; }
213
222 void setLineNumbersVisible( bool visible );
223
230 bool lineNumbersVisible() const;
231
236 void setFoldingVisible( bool folding );
237
242 bool foldingVisible();
243
249 void insertText( const QString &text );
250
262 static QColor defaultColor( QgsCodeEditorColorScheme::ColorRole role, const QString &theme = QString() );
263
273 static QColor color( QgsCodeEditorColorScheme::ColorRole role );
274
286 static void setColor( QgsCodeEditorColorScheme::ColorRole role, const QColor &color );
287
293 static QFont getMonospaceFont();
294
302 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;
303
310 void addWarning( int lineNumber, const QString &warning );
311
318 void clearWarnings();
319
325 QgsCodeEditor::Mode mode() const { return mMode; }
326
332 bool isCursorOnLastLine() const;
333
342 void setHistoryFilePath( const QString &path );
343
351 QStringList history() const;
352
359 QgsCodeInterpreter *interpreter() const;
360
368 void setInterpreter( QgsCodeInterpreter *newInterpreter );
369
375 int linearPosition() const;
376
382 void setLinearPosition( int position );
383
391 int selectionStart() const;
392
400 int selectionEnd() const;
401
407 void setLinearSelection( int start, int end );
408
409 // Override QsciScintilla::callTip to handle wrapping
410 void callTip() override;
411
419 int wrapPosition( int line = -1 );
420
421
430 int editingTimeoutInterval() const;
431
432
433 public slots:
434
445 void runCommand( const QString &command, bool skipHistory = false );
446
453 virtual void moveCursorToStart();
454
461 virtual void moveCursorToEnd();
462
470 void showPreviousCommand();
471
479 void showNextCommand();
480
488 void showHistory();
489
495 void removeHistoryCommand( int index );
496
504 void clearSessionHistory();
505
513 void clearPersistentHistory();
514
520 bool writeHistoryFile();
521
529 void reformatCode();
530
538 virtual bool checkSyntax();
539
547 virtual void toggleComment();
548
554 void adjustScrollWidth();
555
556 // Override QsciScintilla::setText to adjust the scroll width
557 void setText( const QString &text ) override;
558
568 void setEditingTimeoutInterval( int timeout );
569
570 signals:
571
578
585
586
592 void helpRequested( const QString &word );
593
594
607
608 protected:
612 static bool isFixedPitch( const QFont &font );
613
614 void focusOutEvent( QFocusEvent *event ) override;
615 void keyPressEvent( QKeyEvent *event ) override;
616 void contextMenuEvent( QContextMenuEvent *event ) override;
617 bool event( QEvent *event ) override;
618 bool eventFilter( QObject *watched, QEvent *event ) override;
619
627 virtual void initializeLexer();
628
635
641 QFont lexerFont() const;
642
649
655 void updateSoftHistory();
656
664 void updatePrompt();
665
675 virtual void populateContextMenu( QMenu *menu );
676
684 virtual QString reformatCodeString( const QString &string );
685
693 virtual void showMessage( const QString &title, const QString &message, Qgis::MessageLevel level );
694
700 void toggleLineComments( const QString &commentPrefix );
701
702 private slots:
703 void onLastEditTimeout();
704
705 private:
706 void setSciWidget();
707 void updateFolding();
708 bool readHistoryFile();
709 void syncSoftHistory();
710 void updateHistory( const QStringList &commands, bool skipSoftHistory = false );
711 char getCharacter( int &pos ) const;
712
713 QString mWidgetTitle;
714 bool mMargin = false;
717
718 bool mUseDefaultSettings = true;
719 // used if above is false, inplace of values taken from QSettings:
720 bool mOverrideColors = false;
721 QString mColorScheme;
722 QMap<QgsCodeEditorColorScheme::ColorRole, QColor> mCustomColors;
723 QString mFontFamily;
724 int mFontSize = 0;
725
726 QVector<int> mWarningLines;
727
728 // for use in command input mode
729 QStringList mHistory;
730 QStringList mSoftHistory;
731 int mSoftHistoryIndex = 0;
732 QString mHistoryFilePath;
733
734 QgsCodeInterpreter *mInterpreter = nullptr;
735
736 static QMap<QgsCodeEditorColorScheme::ColorRole, QString> sColorRoleToSettingsKey;
737
738 static constexpr int MARKER_NUMBER = 6;
739
740 QTimer *mLastEditTimer = nullptr;
741};
742
744
745// clazy:excludeall=qstring-allocations
746
747#endif
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:59
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:159
bool eventFilter(QObject *watched, QEvent *event) override
void sessionHistoryCleared()
Emitted when the history of commands run in the current session is cleared.
static const QgsSettingsEntryBool * settingContextHelpHover
QgsCodeEditor::Mode mode() const
Returns the code editor mode.
Mode
Code editor modes.
@ ScriptEditor
Standard mode, allows for display and edit of entire scripts.
void contextMenuEvent(QContextMenuEvent *event) override
static constexpr int SEARCH_RESULT_INDICATOR
Indicator index for search results.
void keyPressEvent(QKeyEvent *event) override
virtual void populateContextMenu(QMenu *menu)
Called when the context menu for the widget is about to be shown, after it has been fully populated w...
QFlags< Flag > Flags
Flags controlling behavior of code editor.
void persistentHistoryCleared()
Emitted when the persistent history of commands run in the editor is cleared.
static QgsSettingsTreeNode * sTreeCodeEditor
virtual Qgis::ScriptLanguageCapabilities languageCapabilities() const
Returns the associated scripting language capabilities.
MarginRole
Margin roles.
void runPostLexerConfigurationTasks()
Performs tasks which must be run after a lexer has been set for the widget.
bool event(QEvent *event) override
virtual void showMessage(const QString &title, const QString &message, Qgis::MessageLevel level)
Shows a user facing message (eg a warning message).
virtual void initializeLexer()
Called when the dialect specific code lexer needs to be initialized (or reinitialized).
void setTitle(const QString &title)
Set the widget title.
QgsCodeEditor(QWidget *parent=nullptr, const QString &title=QString(), bool folding=false, bool margin=false, QgsCodeEditor::Flags flags=QgsCodeEditor::Flags(), QgsCodeEditor::Mode mode=QgsCodeEditor::Mode::ScriptEditor)
Construct a new code editor.
void focusOutEvent(QFocusEvent *event) override
Flag
Flags controlling behavior of code editor.
void helpRequested(const QString &word)
Emitted when documentation was requested for the specified word.
static bool isFixedPitch(const QFont &font)
Returns true if a font is a fixed pitch font.
void updateSoftHistory()
Updates the soft history by storing the current editor text in the history.
Q_DECL_DEPRECATED bool marginVisible()
Returns whether margins are in a visible state.
static QString languageToString(Qgis::ScriptLanguage language)
Returns a user-friendly, translated name of the specified script language.
virtual Qgis::ScriptLanguage language() const
Returns the associated scripting language.
QFont lexerFont() const
Returns the font to use in the lexer.
void toggleLineComments(const QString &commentPrefix)
Toggles comment for selected lines with the given comment prefix.
virtual QString reformatCodeString(const QString &string)
Applies code reformatting to a string and returns the result.
QColor lexerColor(QgsCodeEditorColorScheme::ColorRole role) const
Returns the color to use in the lexer for the specified role.
Q_DECL_DEPRECATED void setMarginVisible(bool margin)
Set margin visible state.
void updatePrompt()
Triggers an update of the interactive prompt part of the editor.
void editingTimeout()
Emitted when either:
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.
int exec(const QString &command)
Executes a command in the interpreter.
virtual ~QgsCodeInterpreter()
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
A boolean settings entry.
A tree node for the settings tree to help organizing and introspecting the tree.
static QgsSettingsTreeNode * sTreeGui
#define SIP_IF_MODULE(condition)
Definition qgis_sip.h:28
#define SIP_DEPRECATED
Definition qgis_sip.h:114
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:275
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:268
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)