QGIS API Documentation 4.1.0-Master (3b8ef1f72a3)
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;
42
43SIP_IF_MODULE( HAVE_QSCI_SIP )
44
45
50class GUI_EXPORT QgsCodeInterpreter
51{
52 public:
54
60 int exec( const QString &command );
61
68 virtual int currentState() const { return mState; }
69
74 virtual QString promptForState( int state ) const = 0;
75
76 protected:
83 virtual int execCommandImpl( const QString &command ) = 0;
84
85 private:
86 int mState = 0;
87};
88
89
90// TODO QGIS 5.0 -- Consider making QgsCodeEditor inherit QWidget only,
91// with a separate getter for the QsciScintilla child widget. This
92// would give us more flexibility to add functionality to the base
93// QgsCodeEditor class, eg adding a message bar or other child widgets
94// to the editor widget. For now this extra functionality lives in
95// the QgsCodeEditorWidget wrapper widget.
96
102class GUI_EXPORT QgsCodeEditor : public QsciScintilla
103{
104 Q_OBJECT
105
106 public:
107#ifndef SIP_RUN
108
109 static inline QgsSettingsTreeNode *sTreeCodeEditor = QgsSettingsTree::sTreeGui->createChildNode( u"code-editor"_s );
115#endif
116
122 enum class Mode
123 {
124 ScriptEditor,
125 OutputDisplay,
126 CommandInput,
127 };
128 Q_ENUM( Mode )
129
130
138 {
139 LineNumbers = 0,
140 ErrorIndicators = 1,
141 FoldingControls = 2,
142 };
143 Q_ENUM( MarginRole )
144
145
150 enum class Flag : int SIP_ENUM_BASETYPE( IntFlag )
151 {
152 CodeFolding = 1 << 0,
153 ImmediatelyUpdateHistory
154 = 1 << 1,
155 };
156 Q_ENUM( Flag )
157
158
163 Q_DECLARE_FLAGS( Flags, Flag )
164 Q_FLAG( Flags )
165
167 static constexpr int SEARCH_RESULT_INDICATOR = QsciScintilla::INDIC_MAX - 1;
168
180 QWidget *parent SIP_TRANSFERTHIS = nullptr,
181 const QString &title = QString(),
182 bool folding = false,
183 bool margin = false,
185 QgsCodeEditor::Mode mode = QgsCodeEditor::Mode::ScriptEditor
186 );
187
192 void setTitle( const QString &title );
193
199 virtual Qgis::ScriptLanguage language() const;
200
206 virtual Qgis::ScriptLanguageCapabilities languageCapabilities() const;
207
213 static QString languageToString( Qgis::ScriptLanguage language );
214
220 Q_DECL_DEPRECATED void setMarginVisible( bool margin ) SIP_DEPRECATED;
221
226 Q_DECL_DEPRECATED bool marginVisible() SIP_DEPRECATED { return mMargin; }
227
236 void setLineNumbersVisible( bool visible );
237
244 bool lineNumbersVisible() const;
245
250 void setFoldingVisible( bool folding );
251
256 bool foldingVisible();
257
263 void insertText( const QString &text );
264
276 static QColor defaultColor( QgsCodeEditorColorScheme::ColorRole role, const QString &theme = QString() );
277
287 static QColor color( QgsCodeEditorColorScheme::ColorRole role );
288
300 static void setColor( QgsCodeEditorColorScheme::ColorRole role, const QColor &color );
301
307 static QFont getMonospaceFont();
308
316 void setCustomAppearance(
317 const QString &scheme = QString(),
318 const QMap<QgsCodeEditorColorScheme::ColorRole, QColor> &customColors = QMap<QgsCodeEditorColorScheme::ColorRole, QColor>(),
319 const QString &fontFamily = QString(),
320 int fontSize = 0
321 ) SIP_SKIP;
322
329 void addWarning( int lineNumber, const QString &warning );
330
337 void clearWarnings();
338
344 QgsCodeEditor::Mode mode() const { return mMode; }
345
351 bool isCursorOnLastLine() const;
352
361 void setHistoryFilePath( const QString &path );
362
370 QStringList history() const;
371
378 QgsCodeInterpreter *interpreter() const;
379
387 void setInterpreter( QgsCodeInterpreter *newInterpreter );
388
394 int linearPosition() const;
395
401 void setLinearPosition( int position );
402
410 int selectionStart() const;
411
419 int selectionEnd() const;
420
426 void setLinearSelection( int start, int end );
427
428 // Override QsciScintilla::callTip to handle wrapping
429 void callTip() override;
430
438 int wrapPosition( int line = -1 );
439
440
449 int editingTimeoutInterval() const;
450
451
452 public slots:
453
464 void runCommand( const QString &command, bool skipHistory = false );
465
472 virtual void moveCursorToStart();
473
480 virtual void moveCursorToEnd();
481
489 void showPreviousCommand();
490
498 void showNextCommand();
499
507 void showHistory();
508
514 void removeHistoryCommand( int index );
515
523 void clearSessionHistory();
524
532 void clearPersistentHistory();
533
539 bool writeHistoryFile();
540
548 void reformatCode();
549
557 virtual bool checkSyntax();
558
566 virtual void toggleComment();
567
573 void adjustScrollWidth();
574
575 // Override QsciScintilla::setText to adjust the scroll width
576 void setText( const QString &text ) override;
577
587 void setEditingTimeoutInterval( int timeout );
588
589 signals:
590
597
604
605
611 void helpRequested( const QString &word );
612
613
626
627 protected:
631 static bool isFixedPitch( const QFont &font );
632
633 void focusOutEvent( QFocusEvent *event ) override;
634 void keyPressEvent( QKeyEvent *event ) override;
635 void contextMenuEvent( QContextMenuEvent *event ) override;
636 bool event( QEvent *event ) override;
637 bool eventFilter( QObject *watched, QEvent *event ) override;
638
646 virtual void initializeLexer();
647
654
660 QFont lexerFont() const;
661
668
674 void updateSoftHistory();
675
683 void updatePrompt();
684
694 virtual void populateContextMenu( QMenu *menu );
695
703 virtual QString reformatCodeString( const QString &string );
704
712 virtual void showMessage( const QString &title, const QString &message, Qgis::MessageLevel level );
713
719 void toggleLineComments( const QString &commentPrefix );
720
721 private slots:
722 void onLastEditTimeout();
723
724 private:
725 void setSciWidget();
726 void updateFolding();
727 bool readHistoryFile();
728 void syncSoftHistory();
729 void updateHistory( const QStringList &commands, bool skipSoftHistory = false );
730 char getCharacter( int &pos ) const;
731
732 QString mWidgetTitle;
733 bool mMargin = false;
736
737 bool mUseDefaultSettings = true;
738 // used if above is false, inplace of values taken from QSettings:
739 bool mOverrideColors = false;
740 QString mColorScheme;
741 QMap<QgsCodeEditorColorScheme::ColorRole, QColor> mCustomColors;
742 QString mFontFamily;
743 int mFontSize = 0;
744
745 QVector<int> mWarningLines;
746
747 // for use in command input mode
748 QStringList mHistory;
749 QStringList mSoftHistory;
750 int mSoftHistoryIndex = 0;
751 QString mHistoryFilePath;
752
753 QgsCodeInterpreter *mInterpreter = nullptr;
754
755 static QMap<QgsCodeEditorColorScheme::ColorRole, QString> sColorRoleToSettingsKey;
756
757 static constexpr int MARKER_NUMBER = 6;
758
759 QTimer *mLastEditTimer = nullptr;
760};
761
763
764// clazy:excludeall=qstring-allocations
765
766#endif
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:160
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 const QgsSettingsEntryString * settingFontFamily
Settings entry for code editor font family 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.
static const QgsSettingsEntryInteger * settingFontSize
Settings entry for code editor font size override.
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.
An integer settings entry.
A string 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:27
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:274
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:267
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)