QGIS API Documentation 3.41.0-Master (af5edcb665c)
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:
49
55 int exec( const QString &command );
56
63 virtual int currentState() const { return mState; }
64
69 virtual QString promptForState( int state ) const = 0;
70
71 protected:
78 virtual int execCommandImpl( const QString &command ) = 0;
79
80 private:
81 int mState = 0;
82};
83
84
85// TODO QGIS 4.0 -- Consider making QgsCodeEditor inherit QWidget only,
86// with a separate getter for the QsciScintilla child widget. This
87// would give us more flexibility to add functionality to the base
88// QgsCodeEditor class, eg adding a message bar or other child widgets
89// to the editor widget. For now this extra functionality lives in
90// the QgsCodeEditorWidget wrapper widget.
91
97class GUI_EXPORT QgsCodeEditor : public QsciScintilla
98{
99 Q_OBJECT
100
101 public:
102#ifndef SIP_RUN
103
104 static inline QgsSettingsTreeNode *sTreeCodeEditor = QgsSettingsTree::sTreeGui->createChildNode( QStringLiteral( "code-editor" ) );
106#endif
107
113 enum class Mode
114 {
115 ScriptEditor,
116 OutputDisplay,
117 CommandInput,
118 };
119 Q_ENUM( Mode )
120
121
129 {
130 LineNumbers = 0,
131 ErrorIndicators = 1,
132 FoldingControls = 2,
133 };
134 Q_ENUM( MarginRole )
135
136
141 enum class Flag : int SIP_ENUM_BASETYPE( IntFlag )
142 {
143 CodeFolding = 1 << 0,
144 ImmediatelyUpdateHistory = 1 << 1,
145 };
146 Q_ENUM( Flag )
147
148
153 Q_DECLARE_FLAGS( Flags, Flag )
154 Q_FLAG( Flags )
155
157 static constexpr int SEARCH_RESULT_INDICATOR = QsciScintilla::INDIC_MAX - 1;
158
169 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 );
170
175 void setTitle( const QString &title );
176
182 virtual Qgis::ScriptLanguage language() const;
183
189 virtual Qgis::ScriptLanguageCapabilities languageCapabilities() const;
190
196 static QString languageToString( Qgis::ScriptLanguage language );
197
203 Q_DECL_DEPRECATED void setMarginVisible( bool margin ) SIP_DEPRECATED;
204
209 Q_DECL_DEPRECATED bool marginVisible() SIP_DEPRECATED { return mMargin; }
210
219 void setLineNumbersVisible( bool visible );
220
227 bool lineNumbersVisible() const;
228
233 void setFoldingVisible( bool folding );
234
239 bool foldingVisible();
240
246 void insertText( const QString &text );
247
259 static QColor defaultColor( QgsCodeEditorColorScheme::ColorRole role, const QString &theme = QString() );
260
270 static QColor color( QgsCodeEditorColorScheme::ColorRole role );
271
283 static void setColor( QgsCodeEditorColorScheme::ColorRole role, const QColor &color );
284
290 static QFont getMonospaceFont();
291
299 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;
300
307 void addWarning( int lineNumber, const QString &warning );
308
315 void clearWarnings();
316
322 QgsCodeEditor::Mode mode() const { return mMode; }
323
329 bool isCursorOnLastLine() const;
330
339 void setHistoryFilePath( const QString &path );
340
348 QStringList history() const;
349
356 QgsCodeInterpreter *interpreter() const;
357
365 void setInterpreter( QgsCodeInterpreter *newInterpreter );
366
372 int linearPosition() const;
373
379 void setLinearPosition( int position );
380
388 int selectionStart() const;
389
397 int selectionEnd() const;
398
404 void setLinearSelection( int start, int end );
405
406 // Override QsciScintilla::callTip to handle wrapping
407 virtual void callTip() override;
408
416 int wrapPosition( int line = -1 );
417
418 public slots:
419
430 void runCommand( const QString &command, bool skipHistory = false );
431
438 virtual void moveCursorToStart();
439
446 virtual void moveCursorToEnd();
447
455 void showPreviousCommand();
456
464 void showNextCommand();
465
473 void showHistory();
474
480 void removeHistoryCommand( int index );
481
489 void clearSessionHistory();
490
498 void clearPersistentHistory();
499
505 bool writeHistoryFile();
506
514 void reformatCode();
515
523 virtual bool checkSyntax();
524
532 virtual void toggleComment();
533
534 signals:
535
542
549
550
556 void helpRequested( const QString &word );
557
558 protected:
562 static bool isFixedPitch( const QFont &font );
563
564 void focusOutEvent( QFocusEvent *event ) override;
565 void keyPressEvent( QKeyEvent *event ) override;
566 void contextMenuEvent( QContextMenuEvent *event ) override;
567 bool eventFilter( QObject *watched, QEvent *event ) override;
568
576 virtual void initializeLexer();
577
583 QColor lexerColor( QgsCodeEditorColorScheme::ColorRole role ) const;
584
590 QFont lexerFont() const;
591
597 void runPostLexerConfigurationTasks();
598
604 void updateSoftHistory();
605
613 void updatePrompt();
614
624 virtual void populateContextMenu( QMenu *menu );
625
633 virtual QString reformatCodeString( const QString &string );
634
642 virtual void showMessage( const QString &title, const QString &message, Qgis::MessageLevel level );
643
644 private:
645 void setSciWidget();
646 void updateFolding();
647 bool readHistoryFile();
648 void syncSoftHistory();
649 void updateHistory( const QStringList &commands, bool skipSoftHistory = false );
650 char getCharacter( int &pos ) const;
651
652 QString mWidgetTitle;
653 bool mMargin = false;
656
657 bool mUseDefaultSettings = true;
658 // used if above is false, inplace of values taken from QSettings:
659 bool mOverrideColors = false;
660 QString mColorScheme;
661 QMap<QgsCodeEditorColorScheme::ColorRole, QColor> mCustomColors;
662 QString mFontFamily;
663 int mFontSize = 0;
664
665 QVector<int> mWarningLines;
666
667 // for use in command input mode
668 QStringList mHistory;
669 QStringList mSoftHistory;
670 int mSoftHistoryIndex = 0;
671 QString mHistoryFilePath;
672
673 QgsCodeInterpreter *mInterpreter = nullptr;
674
675 static QMap<QgsCodeEditorColorScheme::ColorRole, QString> sColorRoleToSettingsKey;
676
677 static constexpr int MARKER_NUMBER = 6;
678};
679
681
682// clazy:excludeall=qstring-allocations
683
684#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)