QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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
24// qscintilla includes
25#include <Qsci/qsciapis.h>
26#include "qgis_sip.h"
27#include "qgis_gui.h"
28
29#include <QMap>
30
31
32SIP_IF_MODULE( HAVE_QSCI_SIP )
33
34
39class GUI_EXPORT QgsCodeInterpreter
40{
41 public:
42
44
50 int exec( const QString &command );
51
58 virtual int currentState() const { return mState; }
59
64 virtual QString promptForState( int state ) const = 0;
65
66 protected:
67
74 virtual int execCommandImpl( const QString &command ) = 0;
75
76 private:
77
78 int mState = 0;
79
80};
81
82
83
84class QWidget;
85
92class GUI_EXPORT QgsCodeEditor : public QsciScintilla
93{
94 Q_OBJECT
95
96 public:
97
103 enum class Mode
104 {
105 ScriptEditor,
106 OutputDisplay,
107 CommandInput,
108 };
109 Q_ENUM( Mode )
110
111
119 {
120 LineNumbers = 0,
121 ErrorIndicators = 1,
122 FoldingControls = 2,
123 };
124 Q_ENUM( MarginRole )
125
126
131 enum class Flag : int
132 {
133 CodeFolding = 1 << 0,
134 };
135 Q_ENUM( Flag )
136
137
142 Q_DECLARE_FLAGS( Flags, Flag )
143 Q_FLAG( Flags )
144
145
156 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 );
157
162 void setTitle( const QString & title );
163
169 virtual Qgis::ScriptLanguage language() const;
170
176 static QString languageToString( Qgis::ScriptLanguage language );
177
183 Q_DECL_DEPRECATED void setMarginVisible( bool margin ) SIP_DEPRECATED;
184
189 Q_DECL_DEPRECATED bool marginVisible() SIP_DEPRECATED { return mMargin; }
190
199 void setLineNumbersVisible( bool visible );
200
207 bool lineNumbersVisible() const;
208
213 void setFoldingVisible( bool folding );
214
219 bool foldingVisible();
220
226 void insertText( const QString & text );
227
239 static QColor defaultColor( QgsCodeEditorColorScheme::ColorRole role, const QString & theme = QString() );
240
250 static QColor color( QgsCodeEditorColorScheme::ColorRole role );
251
263 static void setColor( QgsCodeEditorColorScheme::ColorRole role, const QColor & color );
264
270 static QFont getMonospaceFont();
271
279 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;
280
287 void addWarning( int lineNumber, const QString & warning );
288
295 void clearWarnings();
296
302 QgsCodeEditor::Mode mode() const { return mMode; }
303
309 bool isCursorOnLastLine() const;
310
319 void setHistoryFilePath( const QString &path );
320
328 QStringList history() const;
329
336 QgsCodeInterpreter *interpreter() const;
337
345 void setInterpreter( QgsCodeInterpreter *newInterpreter );
346
347 public slots:
348
356 void runCommand( const QString &command );
357
364 virtual void moveCursorToStart();
365
372 virtual void moveCursorToEnd();
373
381 void showPreviousCommand();
382
390 void showNextCommand();
391
399 void showHistory();
400
406 void removeHistoryCommand( int index );
407
415 void clearSessionHistory();
416
424 void clearPersistentHistory();
425
431 bool writeHistoryFile();
432
433 signals:
434
441
448
449 protected:
450
454 static bool isFixedPitch( const QFont &font );
455
456 void focusOutEvent( QFocusEvent *event ) override;
457 void keyPressEvent( QKeyEvent *event ) override;
458 void contextMenuEvent( QContextMenuEvent *event ) override;
459
467 virtual void initializeLexer();
468
474 QColor lexerColor( QgsCodeEditorColorScheme::ColorRole role ) const;
475
481 QFont lexerFont() const;
482
488 void runPostLexerConfigurationTasks();
489
495 void updateSoftHistory();
496
504 void updatePrompt();
505
515 virtual void populateContextMenu( QMenu *menu );
516
517 private:
518
519 void setSciWidget();
520 void updateFolding();
521 bool readHistoryFile();
522 void syncSoftHistory();
523 void updateHistory( const QStringList &commands, bool skipSoftHistory = false );
524
525 QString mWidgetTitle;
526 bool mMargin = false;
527 QgsCodeEditor::Flags mFlags;
529
530 bool mUseDefaultSettings = true;
531 // used if above is false, inplace of values taken from QSettings:
532 bool mOverrideColors = false;
533 QString mColorScheme;
534 QMap< QgsCodeEditorColorScheme::ColorRole, QColor > mCustomColors;
535 QString mFontFamily;
536 int mFontSize = 0;
537
538 QVector< int > mWarningLines;
539
540 // for use in command input mode
541 QStringList mHistory;
542 QStringList mSoftHistory;
543 int mSoftHistoryIndex = 0;
544 QString mHistoryFilePath;
545
546 QgsCodeInterpreter *mInterpreter = nullptr;
547
548 static QMap< QgsCodeEditorColorScheme::ColorRole, QString > sColorRoleToSettingsKey;
549
550 static constexpr int MARKER_NUMBER = 6;
551};
552
553Q_DECLARE_OPERATORS_FOR_FLAGS( QgsCodeEditor::Flags )
554
555// clazy:excludeall=qstring-allocations
556
557#endif
ScriptLanguage
Scripting languages.
Definition: qgis.h:2715
A text editor based on QScintilla2.
Definition: qgscodeeditor.h:93
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.
Definition: qgscodeeditor.h:40
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.
Definition: qgscodeeditor.h:58
virtual QString promptForState(int state) const =0
Returns the interactive prompt string to use for the interpreter, given a state.
virtual ~QgsCodeInterpreter()
#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(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.