QGIS API Documentation 3.41.0-Master (d2aaa9c6e02)
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;
37
38SIP_IF_MODULE( HAVE_QSCI_SIP )
39
40
45class GUI_EXPORT QgsCodeInterpreter
46{
47 public:
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:
78 virtual int execCommandImpl( const QString &command ) = 0;
79
80 private:
81 int mState = 0;
82};
83
84
85// TODO QGIS 4.0 -- Consider making QgsCodeEditor inherit QWidget only,
86// with a separate getter for the QsciScintilla child widget. This
87// would give us more flexibility to add functionality to the base
88// QgsCodeEditor class, eg adding a message bar or other child widgets
89// to the editor widget. For now this extra functionality lives in
90// the QgsCodeEditorWidget wrapper widget.
91
97class GUI_EXPORT QgsCodeEditor : public QsciScintilla
98{
99 Q_OBJECT
100
101 public:
102#ifndef SIP_RUN
103
104 static inline QgsSettingsTreeNode *sTreeCodeEditor = QgsSettingsTree::sTreeGui->createChildNode( QStringLiteral( "code-editor" ) );
106#endif
107
113 enum class Mode
114 {
115 ScriptEditor,
116 OutputDisplay,
117 CommandInput,
118 };
119 Q_ENUM( Mode )
120
121
129 {
130 LineNumbers = 0,
131 ErrorIndicators = 1,
132 FoldingControls = 2,
133 };
134 Q_ENUM( MarginRole )
135
136
141 enum class Flag : int SIP_ENUM_BASETYPE( IntFlag )
142 {
143 CodeFolding = 1 << 0,
144 ImmediatelyUpdateHistory = 1 << 1,
145 };
146 Q_ENUM( Flag )
147
148
153 Q_DECLARE_FLAGS( Flags, Flag )
154 Q_FLAG( Flags )
155
157 static constexpr int SEARCH_RESULT_INDICATOR = QsciScintilla::INDIC_MAX - 1;
158
169 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 );
170
175 void setTitle( const QString &title );
176
182 virtual Qgis::ScriptLanguage language() const;
183
189 virtual Qgis::ScriptLanguageCapabilities languageCapabilities() const;
190
196 static QString languageToString( Qgis::ScriptLanguage language );
197
203 Q_DECL_DEPRECATED void setMarginVisible( bool margin ) SIP_DEPRECATED;
204
209 Q_DECL_DEPRECATED bool marginVisible() SIP_DEPRECATED { return mMargin; }
210
219 void setLineNumbersVisible( bool visible );
220
227 bool lineNumbersVisible() const;
228
233 void setFoldingVisible( bool folding );
234
239 bool foldingVisible();
240
246 void insertText( const QString &text );
247
259 static QColor defaultColor( QgsCodeEditorColorScheme::ColorRole role, const QString &theme = QString() );
260
270 static QColor color( QgsCodeEditorColorScheme::ColorRole role );
271
283 static void setColor( QgsCodeEditorColorScheme::ColorRole role, const QColor &color );
284
290 static QFont getMonospaceFont();
291
299 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;
300
307 void addWarning( int lineNumber, const QString &warning );
308
315 void clearWarnings();
316
322 QgsCodeEditor::Mode mode() const { return mMode; }
323
329 bool isCursorOnLastLine() const;
330
339 void setHistoryFilePath( const QString &path );
340
348 QStringList history() const;
349
356 QgsCodeInterpreter *interpreter() const;
357
365 void setInterpreter( QgsCodeInterpreter *newInterpreter );
366
372 int linearPosition() const;
373
379 void setLinearPosition( int position );
380
388 int selectionStart() const;
389
397 int selectionEnd() const;
398
404 void setLinearSelection( int start, int end );
405
406 // Override QsciScintilla::callTip to handle wrapping
407 virtual void callTip() override;
408
416 int wrapPosition( int line = -1 );
417
418
427 int editingTimeoutInterval() const;
428
429
430 public slots:
431
442 void runCommand( const QString &command, bool skipHistory = false );
443
450 virtual void moveCursorToStart();
451
458 virtual void moveCursorToEnd();
459
467 void showPreviousCommand();
468
476 void showNextCommand();
477
485 void showHistory();
486
492 void removeHistoryCommand( int index );
493
501 void clearSessionHistory();
502
510 void clearPersistentHistory();
511
517 bool writeHistoryFile();
518
526 void reformatCode();
527
535 virtual bool checkSyntax();
536
544 virtual void toggleComment();
545
551 void adjustScrollWidth();
552
553 // Override QsciScintilla::setText to adjust the scroll width
554 void setText( const QString &text ) override;
555
565 void setEditingTimeoutInterval( int timeout );
566
567 signals:
568
575
582
583
589 void helpRequested( const QString &word );
590
591
604
605 protected:
609 static bool isFixedPitch( const QFont &font );
610
611 void focusOutEvent( QFocusEvent *event ) override;
612 void keyPressEvent( QKeyEvent *event ) override;
613 void contextMenuEvent( QContextMenuEvent *event ) override;
614 bool eventFilter( QObject *watched, QEvent *event ) override;
622 virtual void initializeLexer();
623
629 QColor lexerColor( QgsCodeEditorColorScheme::ColorRole role ) const;
630
636 QFont lexerFont() const;
637
643 void runPostLexerConfigurationTasks();
644
650 void updateSoftHistory();
651
659 void updatePrompt();
660
670 virtual void populateContextMenu( QMenu *menu );
671
679 virtual QString reformatCodeString( const QString &string );
680
688 virtual void showMessage( const QString &title, const QString &message, Qgis::MessageLevel level );
689
690 private slots:
691 void onLastEditTimeout();
692
693 private:
694 void setSciWidget();
695 void updateFolding();
696 bool readHistoryFile();
697 void syncSoftHistory();
698 void updateHistory( const QStringList &commands, bool skipSoftHistory = false );
699 char getCharacter( int &pos ) const;
700
701 QString mWidgetTitle;
702 bool mMargin = false;
705
706 bool mUseDefaultSettings = true;
707 // used if above is false, inplace of values taken from QSettings:
708 bool mOverrideColors = false;
709 QString mColorScheme;
710 QMap<QgsCodeEditorColorScheme::ColorRole, QColor> mCustomColors;
711 QString mFontFamily;
712 int mFontSize = 0;
713
714 QVector<int> mWarningLines;
715
716 // for use in command input mode
717 QStringList mHistory;
718 QStringList mSoftHistory;
719 int mSoftHistoryIndex = 0;
720 QString mHistoryFilePath;
721
722 QgsCodeInterpreter *mInterpreter = nullptr;
723
724 static QMap<QgsCodeEditorColorScheme::ColorRole, QString> sColorRoleToSettingsKey;
725
726 static constexpr int MARKER_NUMBER = 6;
727
728 QTimer *mLastEditTimer = nullptr;
729};
730
732
733// clazy:excludeall=qstring-allocations
734
735#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.
static const QgsSettingsEntryBool * settingContextHelpHover
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.
void helpRequested(const QString &word)
Emitted when documentation was requested for the specified word.
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.
virtual ~QgsCodeInterpreter()
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
A boolean settings entry.
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)