QGIS API Documentation 3.39.0-Master (47f7b3a4989)
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#endif
111
117 enum class Mode
118 {
119 ScriptEditor,
120 OutputDisplay,
121 CommandInput,
122 };
123 Q_ENUM( Mode )
124
125
133 {
134 LineNumbers = 0,
135 ErrorIndicators = 1,
136 FoldingControls = 2,
137 };
138 Q_ENUM( MarginRole )
139
140
145 enum class Flag : int SIP_ENUM_BASETYPE( IntFlag )
146 {
147 CodeFolding = 1 << 0,
148 ImmediatelyUpdateHistory = 1 << 1,
149 };
150 Q_ENUM( Flag )
151
152
157 Q_DECLARE_FLAGS( Flags, Flag )
158 Q_FLAG( Flags )
159
161 static constexpr int SEARCH_RESULT_INDICATOR = QsciScintilla::INDIC_MAX - 1;
162
173 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 );
174
179 void setTitle( const QString &title );
180
186 virtual Qgis::ScriptLanguage language() const;
187
193 virtual Qgis::ScriptLanguageCapabilities languageCapabilities() const;
194
200 static QString languageToString( Qgis::ScriptLanguage language );
201
207 Q_DECL_DEPRECATED void setMarginVisible( bool margin ) SIP_DEPRECATED;
208
213 Q_DECL_DEPRECATED bool marginVisible() SIP_DEPRECATED { return mMargin; }
214
223 void setLineNumbersVisible( bool visible );
224
231 bool lineNumbersVisible() const;
232
237 void setFoldingVisible( bool folding );
238
243 bool foldingVisible();
244
250 void insertText( const QString &text );
251
263 static QColor defaultColor( QgsCodeEditorColorScheme::ColorRole role, const QString &theme = QString() );
264
274 static QColor color( QgsCodeEditorColorScheme::ColorRole role );
275
287 static void setColor( QgsCodeEditorColorScheme::ColorRole role, const QColor &color );
288
294 static QFont getMonospaceFont();
295
303 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;
304
311 void addWarning( int lineNumber, const QString &warning );
312
319 void clearWarnings();
320
326 QgsCodeEditor::Mode mode() const { return mMode; }
327
333 bool isCursorOnLastLine() const;
334
343 void setHistoryFilePath( const QString &path );
344
352 QStringList history() const;
353
360 QgsCodeInterpreter *interpreter() const;
361
369 void setInterpreter( QgsCodeInterpreter *newInterpreter );
370
376 int linearPosition() const;
377
383 void setLinearPosition( int position );
384
392 int selectionStart() const;
393
401 int selectionEnd() const;
402
408 void setLinearSelection( int start, int end );
409
410 public slots:
411
422 void runCommand( const QString &command, bool skipHistory = false );
423
430 virtual void moveCursorToStart();
431
438 virtual void moveCursorToEnd();
439
447 void showPreviousCommand();
448
456 void showNextCommand();
457
465 void showHistory();
466
472 void removeHistoryCommand( int index );
473
481 void clearSessionHistory();
482
490 void clearPersistentHistory();
491
497 bool writeHistoryFile();
498
506 void reformatCode();
507
515 virtual bool checkSyntax();
516
524 virtual void toggleComment();
525
526 signals:
527
534
541
542 protected:
543
547 static bool isFixedPitch( const QFont &font );
548
549 void focusOutEvent( QFocusEvent *event ) override;
550 void keyPressEvent( QKeyEvent *event ) override;
551 void contextMenuEvent( QContextMenuEvent *event ) override;
552 bool eventFilter( QObject *watched, QEvent *event ) override;
553
561 virtual void initializeLexer();
562
568 QColor lexerColor( QgsCodeEditorColorScheme::ColorRole role ) const;
569
575 QFont lexerFont() const;
576
582 void runPostLexerConfigurationTasks();
583
589 void updateSoftHistory();
590
598 void updatePrompt();
599
609 virtual void populateContextMenu( QMenu *menu );
610
618 virtual QString reformatCodeString( const QString &string );
619
627 virtual void showMessage( const QString &title, const QString &message, Qgis::MessageLevel level );
628
629 private:
630
631 void setSciWidget();
632 void updateFolding();
633 bool readHistoryFile();
634 void syncSoftHistory();
635 void updateHistory( const QStringList &commands, bool skipSoftHistory = false );
636
637 QString mWidgetTitle;
638 bool mMargin = false;
641
642 bool mUseDefaultSettings = true;
643 // used if above is false, inplace of values taken from QSettings:
644 bool mOverrideColors = false;
645 QString mColorScheme;
646 QMap< QgsCodeEditorColorScheme::ColorRole, QColor > mCustomColors;
647 QString mFontFamily;
648 int mFontSize = 0;
649
650 QVector< int > mWarningLines;
651
652 // for use in command input mode
653 QStringList mHistory;
654 QStringList mSoftHistory;
655 int mSoftHistoryIndex = 0;
656 QString mHistoryFilePath;
657
658 QgsCodeInterpreter *mInterpreter = nullptr;
659
660 static QMap< QgsCodeEditorColorScheme::ColorRole, QString > sColorRoleToSettingsKey;
661
662 static constexpr int MARKER_NUMBER = 6;
663};
664
666
667// clazy:excludeall=qstring-allocations
668
669#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)