QGIS API Documentation 3.38.0-Grenoble (exported)
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;
36
37SIP_IF_MODULE( HAVE_QSCI_SIP )
38
39
44class GUI_EXPORT QgsCodeInterpreter
45{
46 public:
47
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:
72
79 virtual int execCommandImpl( const QString &command ) = 0;
80
81 private:
82
83 int mState = 0;
84
85};
86
87
88// TODO QGIS 4.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
106
107#ifndef SIP_RUN
108
109 static inline QgsSettingsTreeNode *sTreeCodeEditor = QgsSettingsTree::sTreeGui->createChildNode( QStringLiteral( "code-editor" ) );
110
111#endif
112
118 enum class Mode
119 {
120 ScriptEditor,
121 OutputDisplay,
122 CommandInput,
123 };
124 Q_ENUM( Mode )
125
126
134 {
135 LineNumbers = 0,
136 ErrorIndicators = 1,
137 FoldingControls = 2,
138 };
139 Q_ENUM( MarginRole )
140
141
146 enum class Flag : int SIP_ENUM_BASETYPE( IntFlag )
147 {
148 CodeFolding = 1 << 0,
149 ImmediatelyUpdateHistory = 1 << 1,
150 };
151 Q_ENUM( Flag )
152
153
158 Q_DECLARE_FLAGS( Flags, Flag )
159 Q_FLAG( Flags )
160
162 static constexpr int SEARCH_RESULT_INDICATOR = QsciScintilla::INDIC_MAX - 1;
163
174 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 );
175
180 void setTitle( const QString &title );
181
187 virtual Qgis::ScriptLanguage language() const;
188
194 virtual Qgis::ScriptLanguageCapabilities languageCapabilities() const;
195
201 static QString languageToString( Qgis::ScriptLanguage language );
202
208 Q_DECL_DEPRECATED void setMarginVisible( bool margin ) SIP_DEPRECATED;
209
214 Q_DECL_DEPRECATED bool marginVisible() SIP_DEPRECATED { return mMargin; }
215
224 void setLineNumbersVisible( bool visible );
225
232 bool lineNumbersVisible() const;
233
238 void setFoldingVisible( bool folding );
239
244 bool foldingVisible();
245
251 void insertText( const QString &text );
252
264 static QColor defaultColor( QgsCodeEditorColorScheme::ColorRole role, const QString &theme = QString() );
265
275 static QColor color( QgsCodeEditorColorScheme::ColorRole role );
276
288 static void setColor( QgsCodeEditorColorScheme::ColorRole role, const QColor &color );
289
295 static QFont getMonospaceFont();
296
304 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;
305
312 void addWarning( int lineNumber, const QString &warning );
313
320 void clearWarnings();
321
327 QgsCodeEditor::Mode mode() const { return mMode; }
328
334 bool isCursorOnLastLine() const;
335
344 void setHistoryFilePath( const QString &path );
345
353 QStringList history() const;
354
361 QgsCodeInterpreter *interpreter() const;
362
370 void setInterpreter( QgsCodeInterpreter *newInterpreter );
371
377 int linearPosition() const;
378
384 void setLinearPosition( int position );
385
393 int selectionStart() const;
394
402 int selectionEnd() const;
403
409 void setLinearSelection( int start, int end );
410
411 public slots:
412
423 void runCommand( const QString &command, bool skipHistory = false );
424
431 virtual void moveCursorToStart();
432
439 virtual void moveCursorToEnd();
440
448 void showPreviousCommand();
449
457 void showNextCommand();
458
466 void showHistory();
467
473 void removeHistoryCommand( int index );
474
482 void clearSessionHistory();
483
491 void clearPersistentHistory();
492
498 bool writeHistoryFile();
499
507 void reformatCode();
508
516 virtual bool checkSyntax();
517
525 virtual void toggleComment();
526
527 signals:
528
535
542
543 protected:
544
548 static bool isFixedPitch( const QFont &font );
549
550 void focusOutEvent( QFocusEvent *event ) override;
551 void keyPressEvent( QKeyEvent *event ) override;
552 void contextMenuEvent( QContextMenuEvent *event ) override;
553 bool eventFilter( QObject *watched, QEvent *event ) override;
554
562 virtual void initializeLexer();
563
569 QColor lexerColor( QgsCodeEditorColorScheme::ColorRole role ) const;
570
576 QFont lexerFont() const;
577
583 void runPostLexerConfigurationTasks();
584
590 void updateSoftHistory();
591
599 void updatePrompt();
600
610 virtual void populateContextMenu( QMenu *menu );
611
619 virtual QString reformatCodeString( const QString &string );
620
628 virtual void showMessage( const QString &title, const QString &message, Qgis::MessageLevel level );
629
630 private:
631
632 void setSciWidget();
633 void updateFolding();
634 bool readHistoryFile();
635 void syncSoftHistory();
636 void updateHistory( const QStringList &commands, bool skipSoftHistory = false );
637
638 QString mWidgetTitle;
639 bool mMargin = false;
642
643 bool mUseDefaultSettings = true;
644 // used if above is false, inplace of values taken from QSettings:
645 bool mOverrideColors = false;
646 QString mColorScheme;
647 QMap< QgsCodeEditorColorScheme::ColorRole, QColor > mCustomColors;
648 QString mFontFamily;
649 int mFontSize = 0;
650
651 QVector< int > mWarningLines;
652
653 // for use in command input mode
654 QStringList mHistory;
655 QStringList mSoftHistory;
656 int mSoftHistoryIndex = 0;
657 QString mHistoryFilePath;
658
659 QgsCodeInterpreter *mInterpreter = nullptr;
660
661 static QMap< QgsCodeEditorColorScheme::ColorRole, QString > sColorRoleToSettingsKey;
662
663 static constexpr int MARKER_NUMBER = 6;
664};
665
667
668// clazy:excludeall=qstring-allocations
669
670#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:99
A text editor based on QScintilla2.
void sessionHistoryCleared()
Emitted when the history of commands run in the current session is cleared.
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.
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...
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)