QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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 "qgscodeeditor.h"
18#include <QStandardItemModel>
19#include <QShortcut>
20
22 : QDialog( parent )
23 , mEditor( editor )
24{
25 setupUi( this );
26
27 if ( mEditor )
28 {
29 setWindowTitle( tr( "%1 Console - Command History" ).arg( QgsCodeEditor::languageToString( mEditor->language() ) ) );
30 }
31
32 listView->setToolTip( tr( "Double-click on item to execute" ) );
33
34 mModel = new CodeHistoryModel( listView );
35 listView->setModel( mModel );
36
37 reloadHistory();
38
39 QShortcut *deleteShortcut = new QShortcut( QKeySequence( Qt::Key_Delete ), this );
40 connect( deleteShortcut, &QShortcut::activated, this, &QgsCodeEditorHistoryDialog::deleteItem );
41 connect( listView, &QListView::doubleClicked, this, &QgsCodeEditorHistoryDialog::runCommand );
42 connect( mButtonReloadHistory, &QPushButton::clicked, this, & QgsCodeEditorHistoryDialog::reloadHistory );
43 connect( mButtonSaveHistory, &QPushButton::clicked, this, & QgsCodeEditorHistoryDialog::saveHistory );
44 connect( mButtonRunHistory, &QPushButton::clicked, this, &QgsCodeEditorHistoryDialog::executeSelectedHistory );
45}
46
47void QgsCodeEditorHistoryDialog::executeSelectedHistory()
48{
49 if ( !mEditor )
50 return;
51
52 QModelIndexList selection = listView->selectionModel()->selectedIndexes();
53 std::sort( selection.begin(), selection.end() );
54 for ( const QModelIndex &index : std::as_const( selection ) )
55 {
56 mEditor->runCommand( index.data( Qt::DisplayRole ).toString() );
57 }
58}
59
60void QgsCodeEditorHistoryDialog::runCommand( const QModelIndex &index )
61{
62 if ( !mEditor )
63 return;
64
65 mEditor->runCommand( index.data( Qt::DisplayRole ).toString() );
66}
67
68void QgsCodeEditorHistoryDialog::saveHistory()
69{
70 if ( !mEditor )
71 return;
72
73 mEditor->writeHistoryFile();
74}
75
76void QgsCodeEditorHistoryDialog::reloadHistory()
77{
78 if ( mEditor )
79 {
80 mModel->setStringList( mEditor->history() );
81 }
82
83 listView->scrollToBottom();
84 listView->setCurrentIndex( mModel->index( mModel->rowCount() - 1, 0 ) );
85}
86
87void QgsCodeEditorHistoryDialog::deleteItem()
88{
89 const QModelIndexList selection = listView->selectionModel()->selectedRows();
90 if ( selection.empty() )
91 return;
92
93 QList< int > selectedRows;
94 selectedRows.reserve( selection.size() );
95 for ( const QModelIndex &index : selection )
96 selectedRows << index.row();
97 std::sort( selectedRows.begin(), selectedRows.end(), std::greater< int >() );
98
99 for ( int row : std::as_const( selectedRows ) )
100 {
101 if ( mEditor )
102 mEditor->removeHistoryCommand( row );
103
104 // Remove row from the command history dialog
105 mModel->removeRow( row );
106 }
107}
108
110CodeHistoryModel::CodeHistoryModel( QObject *parent )
111 : QStringListModel( parent )
112{
114}
115
116QVariant CodeHistoryModel::data( const QModelIndex &index, int role ) const
117{
118 if ( role == Qt::FontRole )
119 {
120 return mFont;
121 }
122
123 return QStringListModel::data( index, role );
124}
QgsCodeEditorHistoryDialog(QgsCodeEditor *editor, QWidget *parent=nullptr)
Constructor for QgsCodeEditorHistoryDialog.
A text editor based on QScintilla2.
Definition: qgscodeeditor.h:93
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.