QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
27// qscintilla includes
28#include <Qsci/qsciapis.h>
29#include "qgis_sip.h"
30#include "qgis_gui.h"
31
32#include <QMap>
33
35class QToolButton;
36class QCheckBox;
38
39SIP_IF_MODULE( HAVE_QSCI_SIP )
40
41
46class GUI_EXPORT QgsCodeInterpreter
47{
48 public:
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:
79 virtual int execCommandImpl( const QString &command ) = 0;
80
81 private:
82 int mState = 0;
83};
84
85
86// TODO QGIS 4.0 -- Consider making QgsCodeEditor inherit QWidget only,
87// with a separate getter for the QsciScintilla child widget. This
88// would give us more flexibility to add functionality to the base
89// QgsCodeEditor class, eg adding a message bar or other child widgets
90// to the editor widget. For now this extra functionality lives in
91// the QgsCodeEditorWidget wrapper widget.
92
98class GUI_EXPORT QgsCodeEditor : public QsciScintilla
99{
100 Q_OBJECT
101
102 public:
103#ifndef SIP_RUN
104
105 static inline QgsSettingsTreeNode *sTreeCodeEditor = QgsSettingsTree::sTreeGui->createChildNode( QStringLiteral( "code-editor" ) );
107#endif
108
114 enum class Mode
115 {
116 ScriptEditor,
117 OutputDisplay,
118 CommandInput,
119 };
120 Q_ENUM( Mode )
121
122
130 {
131 LineNumbers = 0,
132 ErrorIndicators = 1,
133 FoldingControls = 2,
134 };
135 Q_ENUM( MarginRole )
136
137
142 enum class Flag : int SIP_ENUM_BASETYPE( IntFlag )
143 {
144 CodeFolding = 1 << 0,
145 ImmediatelyUpdateHistory = 1 << 1,
146 };
147 Q_ENUM( Flag )
148
149
154 Q_DECLARE_FLAGS( Flags, Flag )
155 Q_FLAG( Flags )
156
158 static constexpr int SEARCH_RESULT_INDICATOR = QsciScintilla::INDIC_MAX - 1;
159
170 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 );
171
176 void setTitle( const QString &title );
177
183 virtual Qgis::ScriptLanguage language() const;
184
190 virtual Qgis::ScriptLanguageCapabilities languageCapabilities() const;
191
197 static QString languageToString( Qgis::ScriptLanguage language );
198
204 Q_DECL_DEPRECATED void setMarginVisible( bool margin ) SIP_DEPRECATED;
205
210 Q_DECL_DEPRECATED bool marginVisible() SIP_DEPRECATED { return mMargin; }
211
220 void setLineNumbersVisible( bool visible );
221
228 bool lineNumbersVisible() const;
229
234 void setFoldingVisible( bool folding );
235
240 bool foldingVisible();
241
247 void insertText( const QString &text );
248
260 static QColor defaultColor( QgsCodeEditorColorScheme::ColorRole role, const QString &theme = QString() );
261
271 static QColor color( QgsCodeEditorColorScheme::ColorRole role );
272
284 static void setColor( QgsCodeEditorColorScheme::ColorRole role, const QColor &color );
285
291 static QFont getMonospaceFont();
292
300 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;
301
308 void addWarning( int lineNumber, const QString &warning );
309
316 void clearWarnings();
317
323 QgsCodeEditor::Mode mode() const { return mMode; }
324
330 bool isCursorOnLastLine() const;
331
340 void setHistoryFilePath( const QString &path );
341
349 QStringList history() const;
350
357 QgsCodeInterpreter *interpreter() const;
358
366 void setInterpreter( QgsCodeInterpreter *newInterpreter );
367
373 int linearPosition() const;
374
380 void setLinearPosition( int position );
381
389 int selectionStart() const;
390
398 int selectionEnd() const;
399
405 void setLinearSelection( int start, int end );
406
407 // Override QsciScintilla::callTip to handle wrapping
408 void callTip() override;
409
417 int wrapPosition( int line = -1 );
418
419
428 int editingTimeoutInterval() const;
429
430
431 public slots:
432
443 void runCommand( const QString &command, bool skipHistory = false );
444
451 virtual void moveCursorToStart();
452
459 virtual void moveCursorToEnd();
460
468 void showPreviousCommand();
469
477 void showNextCommand();
478
486 void showHistory();
487
493 void removeHistoryCommand( int index );
494
502 void clearSessionHistory();
503
511 void clearPersistentHistory();
512
518 bool writeHistoryFile();
519
527 void reformatCode();
528
536 virtual bool checkSyntax();
537
545 virtual void toggleComment();
546
552 void adjustScrollWidth();
553
554 // Override QsciScintilla::setText to adjust the scroll width
555 void setText( const QString &text ) override;
556
566 void setEditingTimeoutInterval( int timeout );
567
568 signals:
569
576
583
584
590 void helpRequested( const QString &word );
591
592
605
606 protected:
610 static bool isFixedPitch( const QFont &font );
611
612 void focusOutEvent( QFocusEvent *event ) override;
613 void keyPressEvent( QKeyEvent *event ) override;
614 void contextMenuEvent( QContextMenuEvent *event ) override;
615 bool event( QEvent *event ) override;
616 bool eventFilter( QObject *watched, QEvent *event ) override;
617
625 virtual void initializeLexer();
626
633
639 QFont lexerFont() const;
640
647
653 void updateSoftHistory();
654
662 void updatePrompt();
663
673 virtual void populateContextMenu( QMenu *menu );
674
682 virtual QString reformatCodeString( const QString &string );
683
691 virtual void showMessage( const QString &title, const QString &message, Qgis::MessageLevel level );
692
698 void toggleLineComments( const QString &commentPrefix );
699
700 private slots:
701 void onLastEditTimeout();
702
703 private:
704 void setSciWidget();
705 void updateFolding();
706 bool readHistoryFile();
707 void syncSoftHistory();
708 void updateHistory( const QStringList &commands, bool skipSoftHistory = false );
709 char getCharacter( int &pos ) const;
710
711 QString mWidgetTitle;
712 bool mMargin = false;
715
716 bool mUseDefaultSettings = true;
717 // used if above is false, inplace of values taken from QSettings:
718 bool mOverrideColors = false;
719 QString mColorScheme;
720 QMap<QgsCodeEditorColorScheme::ColorRole, QColor> mCustomColors;
721 QString mFontFamily;
722 int mFontSize = 0;
723
724 QVector<int> mWarningLines;
725
726 // for use in command input mode
727 QStringList mHistory;
728 QStringList mSoftHistory;
729 int mSoftHistoryIndex = 0;
730 QString mHistoryFilePath;
731
732 QgsCodeInterpreter *mInterpreter = nullptr;
733
734 static QMap<QgsCodeEditorColorScheme::ColorRole, QString> sColorRoleToSettingsKey;
735
736 static constexpr int MARKER_NUMBER = 6;
737
738 QTimer *mLastEditTimer = nullptr;
739};
740
742
743// clazy:excludeall=qstring-allocations
744
745#endif
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:56
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:156
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)