48 , mMessageBar( messageBar )
52 mEditor->installEventFilter(
this );
53 installEventFilter(
this );
55 QVBoxLayout *vl =
new QVBoxLayout();
56 vl->setContentsMargins( 0, 0, 0, 0 );
58 vl->addWidget(
editor, 1 );
62 QGridLayout *layout =
new QGridLayout( mEditor );
63 layout->setContentsMargins( 0, 0, 0, 0 );
64 layout->addItem(
new QSpacerItem( 20, 40, QSizePolicy::Policy::Minimum, QSizePolicy::Policy::Expanding ), 1, 0, 1, 1 );
67 QSizePolicy sizePolicy = QSizePolicy( QSizePolicy::Policy::Minimum, QSizePolicy::Policy::Fixed );
68 mMessageBar->setSizePolicy( sizePolicy );
69 layout->addWidget( mMessageBar, 0, 0, 1, 1 );
72 mFindWidget =
new QWidget();
73 QGridLayout *layoutFind =
new QGridLayout();
74 layoutFind->setContentsMargins( 0, 2, 0, 0 );
75 layoutFind->setSpacing( 1 );
77 if ( !mEditor->isReadOnly() )
79 mShowReplaceBarButton =
new QToolButton();
80 mShowReplaceBarButton->setToolTip( tr(
"Replace" ) );
81 mShowReplaceBarButton->setCheckable(
true );
82 mShowReplaceBarButton->setAutoRaise(
true );
84 layoutFind->addWidget( mShowReplaceBarButton, 0, 0 );
91 mLineEditFind->setPlaceholderText( tr(
"Enter text to find…" ) );
92 layoutFind->addWidget( mLineEditFind, 0, mShowReplaceBarButton ? 1 : 0 );
96 mLineEditReplace->setPlaceholderText( tr(
"Replace…" ) );
97 layoutFind->addWidget( mLineEditReplace, 1, mShowReplaceBarButton ? 1 : 0 );
99 QHBoxLayout *findButtonLayout =
new QHBoxLayout();
100 findButtonLayout->setContentsMargins( 0, 0, 0, 0 );
101 findButtonLayout->setSpacing( 1 );
102 mCaseSensitiveButton =
new QToolButton();
103 mCaseSensitiveButton->setToolTip( tr(
"Case Sensitive" ) );
104 mCaseSensitiveButton->setCheckable(
true );
105 mCaseSensitiveButton->setAutoRaise(
true );
107 findButtonLayout->addWidget( mCaseSensitiveButton );
109 mWholeWordButton =
new QToolButton( );
110 mWholeWordButton->setToolTip( tr(
"Whole Word" ) );
111 mWholeWordButton->setCheckable(
true );
112 mWholeWordButton->setAutoRaise(
true );
114 findButtonLayout->addWidget( mWholeWordButton );
116 mRegexButton =
new QToolButton( );
117 mRegexButton->setToolTip( tr(
"Use Regular Expressions" ) );
118 mRegexButton->setCheckable(
true );
119 mRegexButton->setAutoRaise(
true );
121 findButtonLayout->addWidget( mRegexButton );
123 mWrapAroundButton =
new QToolButton();
124 mWrapAroundButton->setToolTip( tr(
"Wrap Around" ) );
125 mWrapAroundButton->setCheckable(
true );
126 mWrapAroundButton->setAutoRaise(
true );
128 findButtonLayout->addWidget( mWrapAroundButton );
130 mFindPrevButton =
new QToolButton();
131 mFindPrevButton->setEnabled(
false );
132 mFindPrevButton->setToolTip( tr(
"Find Previous" ) );
134 mFindPrevButton->setAutoRaise(
true );
135 findButtonLayout->addWidget( mFindPrevButton );
137 mFindNextButton =
new QToolButton();
138 mFindNextButton->setEnabled(
false );
139 mFindNextButton->setToolTip( tr(
"Find Next" ) );
141 mFindNextButton->setAutoRaise(
true );
142 findButtonLayout->addWidget( mFindNextButton );
144 connect( mLineEditFind, &QLineEdit::returnPressed,
this, &QgsCodeEditorWidget::findNext );
145 connect( mLineEditFind, &QLineEdit::textChanged,
this, &QgsCodeEditorWidget::textSearchChanged );
146 connect( mFindNextButton, &QToolButton::clicked,
this, &QgsCodeEditorWidget::findNext );
147 connect( mFindPrevButton, &QToolButton::clicked,
this, &QgsCodeEditorWidget::findPrevious );
148 connect( mCaseSensitiveButton, &QToolButton::toggled,
this, &QgsCodeEditorWidget::updateSearch );
149 connect( mWholeWordButton, &QToolButton::toggled,
this, &QgsCodeEditorWidget::updateSearch );
150 connect( mRegexButton, &QToolButton::toggled,
this, &QgsCodeEditorWidget::updateSearch );
151 connect( mWrapAroundButton, &QCheckBox::toggled,
this, &QgsCodeEditorWidget::updateSearch );
153 QShortcut *findShortcut =
new QShortcut( QKeySequence::StandardKey::Find, mEditor );
154 findShortcut->setContext( Qt::ShortcutContext::WidgetWithChildrenShortcut );
157 QShortcut *findNextShortcut =
new QShortcut( QKeySequence::StandardKey::FindNext,
this );
158 findNextShortcut->setContext( Qt::ShortcutContext::WidgetWithChildrenShortcut );
159 connect( findNextShortcut, &QShortcut::activated,
this, &QgsCodeEditorWidget::findNext );
161 QShortcut *findPreviousShortcut =
new QShortcut( QKeySequence::StandardKey::FindPrevious,
this );
162 findPreviousShortcut->setContext( Qt::ShortcutContext::WidgetWithChildrenShortcut );
163 connect( findPreviousShortcut, &QShortcut::activated,
this, &QgsCodeEditorWidget::findPrevious );
165 if ( !mEditor->isReadOnly() )
167 QShortcut *replaceShortcut =
new QShortcut( QKeySequence::StandardKey::Replace,
this );
168 replaceShortcut->setContext( Qt::ShortcutContext::WidgetWithChildrenShortcut );
169 connect( replaceShortcut, &QShortcut::activated,
this, [ = ]
172 const bool show = mLineEditReplace->isHidden();
182 QShortcut *closeFindShortcut =
new QShortcut( Qt::Key::Key_Escape,
this );
183 closeFindShortcut->setContext( Qt::ShortcutContext::WidgetWithChildrenShortcut );
184 connect( closeFindShortcut, &QShortcut::activated,
this, [
this]
190 layoutFind->addLayout( findButtonLayout, 0, mShowReplaceBarButton ? 2 : 1 );
192 QHBoxLayout *replaceButtonLayout =
new QHBoxLayout();
193 replaceButtonLayout->setContentsMargins( 0, 0, 0, 0 );
194 replaceButtonLayout->setSpacing( 1 );
196 mReplaceButton =
new QToolButton();
197 mReplaceButton->setText( tr(
"Replace" ) );
198 mReplaceButton->setEnabled(
false );
199 connect( mReplaceButton, &QToolButton::clicked,
this, &QgsCodeEditorWidget::replace );
200 replaceButtonLayout->addWidget( mReplaceButton );
202 mReplaceAllButton =
new QToolButton();
203 mReplaceAllButton->setText( tr(
"Replace All" ) );
204 mReplaceAllButton->setEnabled(
false );
205 connect( mReplaceAllButton, &QToolButton::clicked,
this, &QgsCodeEditorWidget::replaceAll );
206 replaceButtonLayout->addWidget( mReplaceAllButton );
208 layoutFind->addLayout( replaceButtonLayout, 1, mShowReplaceBarButton ? 2 : 1 );
210 QToolButton *closeFindButton =
new QToolButton(
this );
211 closeFindButton->setToolTip( tr(
"Close" ) );
213 closeFindButton->setStyleSheet(
214 "QToolButton { border:none; background-color: rgba(0, 0, 0, 0); }"
215 "QToolButton::menu-button { border:none; background-color: rgba(0, 0, 0, 0); }" );
216 closeFindButton->setCursor( Qt::PointingHandCursor );
220 closeFindButton->setIconSize( QSize( iconSize, iconSize ) );
221 closeFindButton->setFixedSize( QSize( iconSize, iconSize ) );
222 connect( closeFindButton, &QAbstractButton::clicked,
this, [
this]
227 layoutFind->addWidget( closeFindButton, 0, mShowReplaceBarButton ? 3 : 2 );
229 layoutFind->setColumnStretch( mShowReplaceBarButton ? 1 : 0, 1 );
231 mFindWidget->setLayout( layoutFind );
232 vl->addWidget( mFindWidget );
239 mHighlightController = std::make_unique< QgsScrollBarHighlightController >();
240 mHighlightController->setScrollArea( mEditor );
537 const QString accessToken =
QgsSettings().
value(
"pythonConsole/accessTokenGithub", QString() ).toString();
538 if ( accessToken.isEmpty() )
541 mMessageBar->
pushWarning( QString(), tr(
"GitHub personal access token must be generated (see IDE Options)" ) );
545 QString defaultFileName;
549 defaultFileName = QStringLiteral(
"pyqgis_snippet.py" );
553 defaultFileName = QStringLiteral(
"qgis_snippet.css" );
557 defaultFileName = QStringLiteral(
"qgis_snippet" );
561 defaultFileName = QStringLiteral(
"qgis_snippet.html" );
565 defaultFileName = QStringLiteral(
"qgis_snippet.js" );
569 defaultFileName = QStringLiteral(
"qgis_snippet.json" );
573 defaultFileName = QStringLiteral(
"qgis_snippet.r" );
577 defaultFileName = QStringLiteral(
"qgis_snippet.sql" );
581 defaultFileName = QStringLiteral(
"qgis_snippet.bat" );
585 defaultFileName = QStringLiteral(
"qgis_snippet.sh" );
589 defaultFileName = QStringLiteral(
"qgis_snippet.txt" );
592 const QString filename = mFilePath.isEmpty() ? defaultFileName : QFileInfo( mFilePath ).fileName();
594 const QString contents = mEditor->hasSelectedText() ? mEditor->selectedText() : mEditor->text();
595 const QVariantMap data
597 { QStringLiteral(
"description" ),
"Gist created by PyQGIS Console"},
598 { QStringLiteral(
"public" ), isPublic },
599 { QStringLiteral(
"files" ), QVariantMap{ {filename, QVariantMap{{ QStringLiteral(
"content" ), contents }} } } }
602 QNetworkRequest request;
603 request.setUrl( QUrl( QStringLiteral(
"https://api.github.com/gists" ) ) );
604 request.setRawHeader(
"Authorization", QStringLiteral(
"token %1" ).arg( accessToken ).toLocal8Bit() );
605 request.setHeader( QNetworkRequest::ContentTypeHeader, QLatin1String(
"application/json" ) );
606 request.setAttribute( QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::RedirectPolicy::NoLessSafeRedirectPolicy );
610 connect( reply, &QNetworkReply::finished,
this, [
this, reply]
612 if ( reply->error() == QNetworkReply::NoError )
614 const QVariantMap replyJson = QgsJsonUtils::parseJson( reply->readAll() ).toMap();
615 const QString link = replyJson.value( QStringLiteral(
"html_url" ) ).toString();
616 QDesktopServices::openUrl( QUrl( link ) );
621 mMessageBar->pushCritical( QString(), tr(
"Connection error: %1" ).arg( reply->errorString() ) );
623 reply->deleteLater();