QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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 // Override QsciScintilla::callTip to handle wrapping
411 virtual void callTip() override;
412
420 int wrapPosition( int line = -1 );
421
422 public slots:
423
434 void runCommand( const QString &command, bool skipHistory = false );
435
442 virtual void moveCursorToStart();
443
450 virtual void moveCursorToEnd();
451
459 void showPreviousCommand();
460
468 void showNextCommand();
469
477 void showHistory();
478
484 void removeHistoryCommand( int index );
485
493 void clearSessionHistory();
494
502 void clearPersistentHistory();
503
509 bool writeHistoryFile();
510
518 void reformatCode();
519
527 virtual bool checkSyntax();
528
536 virtual void toggleComment();
537
538 signals:
539
546
553
554 protected:
555
559 static bool isFixedPitch( const QFont &font );
560
561 void focusOutEvent( QFocusEvent *event ) override;
562 void keyPressEvent( QKeyEvent *event ) override;
563 void contextMenuEvent( QContextMenuEvent *event ) override;
564 bool eventFilter( QObject *watched, QEvent *event ) override;
565
573 virtual void initializeLexer();
574
580 QColor lexerColor( QgsCodeEditorColorScheme::ColorRole role ) const;
581
587 QFont lexerFont() const;
588
594 void runPostLexerConfigurationTasks();
595
601 void updateSoftHistory();
602
610 void updatePrompt();
611
621 virtual void populateContextMenu( QMenu *menu );
622
630 virtual QString reformatCodeString( const QString &string );
631
639 virtual void showMessage( const QString &title, const QString &message, Qgis::MessageLevel level );
640
641 private:
642
643 void setSciWidget();
644 void updateFolding();
645 bool readHistoryFile();
646 void syncSoftHistory();
647 void updateHistory( const QStringList &commands, bool skipSoftHistory = false );
648 char getCharacter( int &pos ) const;
649
650 QString mWidgetTitle;
651 bool mMargin = false;
654
655 bool mUseDefaultSettings = true;
656 // used if above is false, inplace of values taken from QSettings:
657 bool mOverrideColors = false;
658 QString mColorScheme;
659 QMap< QgsCodeEditorColorScheme::ColorRole, QColor > mCustomColors;
660 QString mFontFamily;
661 int mFontSize = 0;
662
663 QVector< int > mWarningLines;
664
665 // for use in command input mode
666 QStringList mHistory;
667 QStringList mSoftHistory;
668 int mSoftHistoryIndex = 0;
669 QString mHistoryFilePath;
670
671 QgsCodeInterpreter *mInterpreter = nullptr;
672
673 static QMap< QgsCodeEditorColorScheme::ColorRole, QString > sColorRoleToSettingsKey;
674
675 static constexpr int MARKER_NUMBER = 6;
676};
677
679
680// clazy:excludeall=qstring-allocations
681
682#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.
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)