QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgscodeeditorhistorydialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscodeeditorhistorydialog.cpp
3 ----------------------
4 begin : October 2022
5 copyright : (C) 2022 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgscodeeditorhistorydialog.cpp"
18#include "qgscodeeditor.h"
19#include <QStandardItemModel>
20#include <QShortcut>
21
23 : QDialog( parent )
24 , mEditor( editor )
25{
26 setupUi( this );
27
28 if ( mEditor )
29 {
30 setWindowTitle( tr( "%1 Console - Command History" ).arg( QgsCodeEditor::languageToString( mEditor->language() ) ) );
31 }
32
33 listView->setToolTip( tr( "Double-click on item to execute" ) );
34
35 mModel = new CodeHistoryModel( listView );
36 listView->setModel( mModel );
37
38 reloadHistory();
39
40 QShortcut *deleteShortcut = new QShortcut( QKeySequence( Qt::Key_Delete ), this );
41 connect( deleteShortcut, &QShortcut::activated, this, &QgsCodeEditorHistoryDialog::deleteItem );
42 connect( listView, &QListView::doubleClicked, this, &QgsCodeEditorHistoryDialog::runCommand );
43 connect( mButtonReloadHistory, &QPushButton::clicked, this, & QgsCodeEditorHistoryDialog::reloadHistory );
44 connect( mButtonSaveHistory, &QPushButton::clicked, this, & QgsCodeEditorHistoryDialog::saveHistory );
45 connect( mButtonRunHistory, &QPushButton::clicked, this, &QgsCodeEditorHistoryDialog::executeSelectedHistory );
46}
47
48void QgsCodeEditorHistoryDialog::executeSelectedHistory()
49{
50 if ( !mEditor )
51 return;
52
53 QModelIndexList selection = listView->selectionModel()->selectedIndexes();
54 std::sort( selection.begin(), selection.end() );
55 for ( const QModelIndex &index : std::as_const( selection ) )
56 {
57 mEditor->runCommand( index.data( Qt::DisplayRole ).toString() );
58 }
59}
60
61void QgsCodeEditorHistoryDialog::runCommand( const QModelIndex &index )
62{
63 if ( !mEditor )
64 return;
65
66 mEditor->runCommand( index.data( Qt::DisplayRole ).toString() );
67}
68
69void QgsCodeEditorHistoryDialog::saveHistory()
70{
71 if ( !mEditor )
72 return;
73
74 mEditor->writeHistoryFile();
75}
76
77void QgsCodeEditorHistoryDialog::reloadHistory()
78{
79 if ( mEditor )
80 {
81 mModel->setStringList( mEditor->history() );
82 }
83
84 listView->scrollToBottom();
85 listView->setCurrentIndex( mModel->index( mModel->rowCount() - 1, 0 ) );
86}
87
88void QgsCodeEditorHistoryDialog::deleteItem()
89{
90 const QModelIndexList selection = listView->selectionModel()->selectedRows();
91 if ( selection.empty() )
92 return;
93
94 QList< int > selectedRows;
95 selectedRows.reserve( selection.size() );
96 for ( const QModelIndex &index : selection )
97 selectedRows << index.row();
98 std::sort( selectedRows.begin(), selectedRows.end(), std::greater< int >() );
99
100 for ( int row : std::as_const( selectedRows ) )
101 {
102 if ( mEditor )
103 mEditor->removeHistoryCommand( row );
104
105 // Remove row from the command history dialog
106 mModel->removeRow( row );
107 }
108}
109
111CodeHistoryModel::CodeHistoryModel( QObject *parent )
112 : QStringListModel( parent )
113{
115}
116
117QVariant CodeHistoryModel::data( const QModelIndex &index, int role ) const
118{
119 if ( role == Qt::FontRole )
120 {
121 return mFont;
122 }
123
124 return QStringListModel::data( index, role );
125}
QgsCodeEditorHistoryDialog(QgsCodeEditor *editor, QWidget *parent=nullptr)
Constructor for QgsCodeEditorHistoryDialog.
A text editor based on QScintilla2.
static QFont getMonospaceFont()
Returns the monospaced font to use for code editors.
static QString languageToString(Qgis::ScriptLanguage language)
Returns a user-friendly, translated name of the specified script language.