QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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 "qgis.h"
22#include "qgspanelwidget.h"
23#include "qgssettingstree.h"
24
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
29// qscintilla includes
30#include <Qsci/qsciapis.h>
31#include "qgis_sip.h"
32#include "qgis_gui.h"
33
34#include <QMap>
35
37class QToolButton;
38class QCheckBox;
40
41SIP_IF_MODULE( HAVE_QSCI_SIP )
42
43
48class GUI_EXPORT QgsCodeInterpreter
49{
50 public:
52
58 int exec( const QString &command );
59
66 virtual int currentState() const { return mState; }
67
72 virtual QString promptForState( int state ) const = 0;
73
74 protected:
81 virtual int execCommandImpl( const QString &command ) = 0;
82
83 private:
84 int mState = 0;
85};
86
87
88// TODO QGIS 5.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#ifndef SIP_RUN
106
107 static inline QgsSettingsTreeNode *sTreeCodeEditor = QgsSettingsTree::sTreeGui->createChildNode( u"code-editor"_s );
109#endif
110
116 enum class Mode
117 {
118 ScriptEditor,
119 OutputDisplay,
120 CommandInput,
121 };
122 Q_ENUM( Mode )
123
124
132 {
133 LineNumbers = 0,
134 ErrorIndicators = 1,
135 FoldingControls = 2,
136 };
137 Q_ENUM( MarginRole )
138
139
144 enum class Flag : int SIP_ENUM_BASETYPE( IntFlag )
145 {
146 CodeFolding = 1 << 0,
147 ImmediatelyUpdateHistory
148 = 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
174 QWidget *parent SIP_TRANSFERTHIS = nullptr,
175 const QString &title = QString(),
176 bool folding = false,
177 bool margin = false,
179 QgsCodeEditor::Mode mode = QgsCodeEditor::Mode::ScriptEditor
180 );
181
186 void setTitle( const QString &title );
187
193 virtual Qgis::ScriptLanguage language() const;
194
200 virtual Qgis::ScriptLanguageCapabilities languageCapabilities() const;
201
207 static QString languageToString( Qgis::ScriptLanguage language );
208
214 Q_DECL_DEPRECATED void setMarginVisible( bool margin ) SIP_DEPRECATED;
215
220 Q_DECL_DEPRECATED bool marginVisible() SIP_DEPRECATED { return mMargin; }
221
230 void setLineNumbersVisible( bool visible );
231
238 bool lineNumbersVisible() const;
239
244 void setFoldingVisible( bool folding );
245
250 bool foldingVisible();
251
257 void insertText( const QString &text );
258
270 static QColor defaultColor( QgsCodeEditorColorScheme::ColorRole role, const QString &theme = QString() );
271
281 static QColor color( QgsCodeEditorColorScheme::ColorRole role );
282
294 static void setColor( QgsCodeEditorColorScheme::ColorRole role, const QColor &color );
295
301 static QFont getMonospaceFont();
302
310 void setCustomAppearance(
311 const QString &scheme = QString(),
312 const QMap<QgsCodeEditorColorScheme::ColorRole, QColor> &customColors = QMap<QgsCodeEditorColorScheme::ColorRole, QColor>(),
313 const QString &fontFamily = QString(),
314 int fontSize = 0
315 ) SIP_SKIP;
316
323 void addWarning( int lineNumber, const QString &warning );
324
331 void clearWarnings();
332
338 QgsCodeEditor::Mode mode() const { return mMode; }
339
345 bool isCursorOnLastLine() const;
346
355 void setHistoryFilePath( const QString &path );
356
364 QStringList history() const;
365
372 QgsCodeInterpreter *interpreter() const;
373
381 void setInterpreter( QgsCodeInterpreter *newInterpreter );
382
388 int linearPosition() const;
389
395 void setLinearPosition( int position );
396
404 int selectionStart() const;
405
413 int selectionEnd() const;
414
420 void setLinearSelection( int start, int end );
421
422 // Override QsciScintilla::callTip to handle wrapping
423 void callTip() override;
424
432 int wrapPosition( int line = -1 );
433
434
443 int editingTimeoutInterval() const;
444
445
446 public slots:
447
458 void runCommand( const QString &command, bool skipHistory = false );
459
466 virtual void moveCursorToStart();
467
474 virtual void moveCursorToEnd();
475
483 void showPreviousCommand();
484
492 void showNextCommand();
493
501 void showHistory();
502
508 void removeHistoryCommand( int index );
509
517 void clearSessionHistory();
518
526 void clearPersistentHistory();
527
533 bool writeHistoryFile();
534
542 void reformatCode();
543
551 virtual bool checkSyntax();
552
560 virtual void toggleComment();
561
567 void adjustScrollWidth();
568
569 // Override QsciScintilla::setText to adjust the scroll width
570 void setText( const QString &text ) override;
571
581 void setEditingTimeoutInterval( int timeout );
582
583 signals:
584
591
598
599
605 void helpRequested( const QString &word );
606
607
620
621 protected:
625 static bool isFixedPitch( const QFont &font );
626
627 void focusOutEvent( QFocusEvent *event ) override;
628 void keyPressEvent( QKeyEvent *event ) override;
629 void contextMenuEvent( QContextMenuEvent *event ) override;
630 bool event( QEvent *event ) override;
631 bool eventFilter( QObject *watched, QEvent *event ) override;
632
640 virtual void initializeLexer();
641
648
654 QFont lexerFont() const;
655
662
668 void updateSoftHistory();
669
677 void updatePrompt();
678
688 virtual void populateContextMenu( QMenu *menu );
689
697 virtual QString reformatCodeString( const QString &string );
698
706 virtual void showMessage( const QString &title, const QString &message, Qgis::MessageLevel level );
707
713 void toggleLineComments( const QString &commentPrefix );
714
715 private slots:
716 void onLastEditTimeout();
717
718 private:
719 void setSciWidget();
720 void updateFolding();
721 bool readHistoryFile();
722 void syncSoftHistory();
723 void updateHistory( const QStringList &commands, bool skipSoftHistory = false );
724 char getCharacter( int &pos ) const;
725
726 QString mWidgetTitle;
727 bool mMargin = false;
730
731 bool mUseDefaultSettings = true;
732 // used if above is false, inplace of values taken from QSettings:
733 bool mOverrideColors = false;
734 QString mColorScheme;
735 QMap<QgsCodeEditorColorScheme::ColorRole, QColor> mCustomColors;
736 QString mFontFamily;
737 int mFontSize = 0;
738
739 QVector<int> mWarningLines;
740
741 // for use in command input mode
742 QStringList mHistory;
743 QStringList mSoftHistory;
744 int mSoftHistoryIndex = 0;
745 QString mHistoryFilePath;
746
747 QgsCodeInterpreter *mInterpreter = nullptr;
748
749 static QMap<QgsCodeEditorColorScheme::ColorRole, QString> sColorRoleToSettingsKey;
750
751 static constexpr int MARKER_NUMBER = 6;
752
753 QTimer *mLastEditTimer = nullptr;
754};
755
757
758// clazy:excludeall=qstring-allocations
759
760#endif
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:160
bool eventFilter(QObject *watched, QEvent *event) override
void sessionHistoryCleared()
Emitted when the history of commands run in the current session is cleared.
static const QgsSettingsEntryBool * settingContextHelpHover
QgsCodeEditor::Mode mode() const
Returns the code editor mode.
Mode
Code editor modes.
@ ScriptEditor
Standard mode, allows for display and edit of entire scripts.
void contextMenuEvent(QContextMenuEvent *event) override
static constexpr int SEARCH_RESULT_INDICATOR
Indicator index for search results.
void keyPressEvent(QKeyEvent *event) override
virtual void populateContextMenu(QMenu *menu)
Called when the context menu for the widget is about to be shown, after it has been fully populated w...
QFlags< Flag > Flags
Flags controlling behavior of code editor.
void persistentHistoryCleared()
Emitted when the persistent history of commands run in the editor is cleared.
static QgsSettingsTreeNode * sTreeCodeEditor
virtual Qgis::ScriptLanguageCapabilities languageCapabilities() const
Returns the associated scripting language capabilities.
MarginRole
Margin roles.
void runPostLexerConfigurationTasks()
Performs tasks which must be run after a lexer has been set for the widget.
bool event(QEvent *event) override
virtual void showMessage(const QString &title, const QString &message, Qgis::MessageLevel level)
Shows a user facing message (eg a warning message).
virtual void initializeLexer()
Called when the dialect specific code lexer needs to be initialized (or reinitialized).
void setTitle(const QString &title)
Set the widget title.
QgsCodeEditor(QWidget *parent=nullptr, const QString &title=QString(), bool folding=false, bool margin=false, QgsCodeEditor::Flags flags=QgsCodeEditor::Flags(), QgsCodeEditor::Mode mode=QgsCodeEditor::Mode::ScriptEditor)
Construct a new code editor.
void focusOutEvent(QFocusEvent *event) override
Flag
Flags controlling behavior of code editor.
void helpRequested(const QString &word)
Emitted when documentation was requested for the specified word.
static bool isFixedPitch(const QFont &font)
Returns true if a font is a fixed pitch font.
void updateSoftHistory()
Updates the soft history by storing the current editor text in the history.
Q_DECL_DEPRECATED bool marginVisible()
Returns whether margins are in a visible state.
static QString languageToString(Qgis::ScriptLanguage language)
Returns a user-friendly, translated name of the specified script language.
virtual Qgis::ScriptLanguage language() const
Returns the associated scripting language.
QFont lexerFont() const
Returns the font to use in the lexer.
void toggleLineComments(const QString &commentPrefix)
Toggles comment for selected lines with the given comment prefix.
virtual QString reformatCodeString(const QString &string)
Applies code reformatting to a string and returns the result.
QColor lexerColor(QgsCodeEditorColorScheme::ColorRole role) const
Returns the color to use in the lexer for the specified role.
Q_DECL_DEPRECATED void setMarginVisible(bool margin)
Set margin visible state.
void updatePrompt()
Triggers an update of the interactive prompt part of the editor.
void editingTimeout()
Emitted when either:
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.
int exec(const QString &command)
Executes a command in the interpreter.
virtual ~QgsCodeInterpreter()
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
A boolean settings entry.
A tree node for the settings tree to help organizing and introspecting the tree.
static QgsSettingsTreeNode * sTreeGui
#define SIP_IF_MODULE(condition)
Definition qgis_sip.h:27
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:274
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:267
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)