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