QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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
25// qscintilla includes
26#include <Qsci/qsciapis.h>
27#include "qgis_sip.h"
28#include "qgis_gui.h"
29
30#include <QMap>
31
32
33SIP_IF_MODULE( HAVE_QSCI_SIP )
34
35
40class GUI_EXPORT QgsCodeInterpreter
41{
42 public:
43
45
51 int exec( const QString &command );
52
59 virtual int currentState() const { return mState; }
60
65 virtual QString promptForState( int state ) const = 0;
66
67 protected:
68
75 virtual int execCommandImpl( const QString &command ) = 0;
76
77 private:
78
79 int mState = 0;
80
81};
82
83
84
85class QWidget;
86
93class GUI_EXPORT QgsCodeEditor : public QsciScintilla
94{
95 Q_OBJECT
96
97 public:
98
99
100#ifndef SIP_RUN
101
102 static inline QgsSettingsTreeNode *sTreeCodeEditor = QgsSettingsTree::sTreeGui->createChildNode( QStringLiteral( "code-editor" ) );
103
104#endif
105
111 enum class Mode
112 {
113 ScriptEditor,
114 OutputDisplay,
115 CommandInput,
116 };
117 Q_ENUM( Mode )
118
119
127 {
128 LineNumbers = 0,
129 ErrorIndicators = 1,
130 FoldingControls = 2,
131 };
132 Q_ENUM( MarginRole )
133
134
139 enum class Flag : int
140 {
141 CodeFolding = 1 << 0,
142 ImmediatelyUpdateHistory = 1 << 1,
143 };
144 Q_ENUM( Flag )
145
146
151 Q_DECLARE_FLAGS( Flags, Flag )
152 Q_FLAG( Flags )
153
154
165 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 );
166
171 void setTitle( const QString & title );
172
178 virtual Qgis::ScriptLanguage language() const;
179
185 virtual Qgis::ScriptLanguageCapabilities languageCapabilities() const;
186
192 static QString languageToString( Qgis::ScriptLanguage language );
193
199 Q_DECL_DEPRECATED void setMarginVisible( bool margin ) SIP_DEPRECATED;
200
205 Q_DECL_DEPRECATED bool marginVisible() SIP_DEPRECATED { return mMargin; }
206
215 void setLineNumbersVisible( bool visible );
216
223 bool lineNumbersVisible() const;
224
229 void setFoldingVisible( bool folding );
230
235 bool foldingVisible();
236
242 void insertText( const QString & text );
243
255 static QColor defaultColor( QgsCodeEditorColorScheme::ColorRole role, const QString & theme = QString() );
256
266 static QColor color( QgsCodeEditorColorScheme::ColorRole role );
267
279 static void setColor( QgsCodeEditorColorScheme::ColorRole role, const QColor & color );
280
286 static QFont getMonospaceFont();
287
295 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;
296
303 void addWarning( int lineNumber, const QString & warning );
304
311 void clearWarnings();
312
318 QgsCodeEditor::Mode mode() const { return mMode; }
319
325 bool isCursorOnLastLine() const;
326
335 void setHistoryFilePath( const QString &path );
336
344 QStringList history() const;
345
352 QgsCodeInterpreter *interpreter() const;
353
361 void setInterpreter( QgsCodeInterpreter *newInterpreter );
362
363 public slots:
364
375 void runCommand( const QString &command, bool skipHistory = false );
376
383 virtual void moveCursorToStart();
384
391 virtual void moveCursorToEnd();
392
400 void showPreviousCommand();
401
409 void showNextCommand();
410
418 void showHistory();
419
425 void removeHistoryCommand( int index );
426
434 void clearSessionHistory();
435
443 void clearPersistentHistory();
444
450 bool writeHistoryFile();
451
459 void reformatCode();
460
468 virtual bool checkSyntax();
469
477 virtual void toggleComment();
478
479 signals:
480
487
494
495 protected:
496
500 static bool isFixedPitch( const QFont &font );
501
502 void focusOutEvent( QFocusEvent *event ) override;
503 void keyPressEvent( QKeyEvent *event ) override;
504 void contextMenuEvent( QContextMenuEvent *event ) override;
505 bool eventFilter( QObject *watched, QEvent *event ) override;
506
514 virtual void initializeLexer();
515
521 QColor lexerColor( QgsCodeEditorColorScheme::ColorRole role ) const;
522
528 QFont lexerFont() const;
529
535 void runPostLexerConfigurationTasks();
536
542 void updateSoftHistory();
543
551 void updatePrompt();
552
562 virtual void populateContextMenu( QMenu *menu );
563
571 virtual QString reformatCodeString( const QString &string );
572
580 virtual void showMessage( const QString &title, const QString &message, Qgis::MessageLevel level );
581
582 private:
583
584 void setSciWidget();
585 void updateFolding();
586 bool readHistoryFile();
587 void syncSoftHistory();
588 void updateHistory( const QStringList &commands, bool skipSoftHistory = false );
589
590 QString mWidgetTitle;
591 bool mMargin = false;
592 QgsCodeEditor::Flags mFlags;
594
595 bool mUseDefaultSettings = true;
596 // used if above is false, inplace of values taken from QSettings:
597 bool mOverrideColors = false;
598 QString mColorScheme;
599 QMap< QgsCodeEditorColorScheme::ColorRole, QColor > mCustomColors;
600 QString mFontFamily;
601 int mFontSize = 0;
602
603 QVector< int > mWarningLines;
604
605 // for use in command input mode
606 QStringList mHistory;
607 QStringList mSoftHistory;
608 int mSoftHistoryIndex = 0;
609 QString mHistoryFilePath;
610
611 QgsCodeInterpreter *mInterpreter = nullptr;
612
613 static QMap< QgsCodeEditorColorScheme::ColorRole, QString > sColorRoleToSettingsKey;
614
615 static constexpr int MARKER_NUMBER = 6;
616};
617
618Q_DECLARE_OPERATORS_FOR_FLAGS( QgsCodeEditor::Flags )
619
620// clazy:excludeall=qstring-allocations
621
622#endif
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:99
ScriptLanguage
Scripting languages.
Definition qgis.h:3106
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.
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.
Q_DECL_DEPRECATED bool marginVisible()
Returns whether margins are in a visible state.
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()
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_SKIP
Definition qgis_sip.h:126
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:266
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)