QGIS API Documentation 3.38.0-Grenoble (exported)
Loading...
Searching...
No Matches
qgsprocessinghistorywidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessinghistorywidget.cpp
3 ------------------------
4 Date : April 2023
5 Copyright : (C) 2023 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 "qgshistorywidget.h"
18#include "qgsgui.h"
20#include "qgshelp.h"
21#include "qgsfileutils.h"
22#include "qgshistoryentry.h"
23
24#include <QVBoxLayout>
25#include <QMessageBox>
26#include <QFileDialog>
27#include <QTextStream>
28#include <QDialogButtonBox>
29#include <QPushButton>
30
32 : QgsPanelWidget( parent )
33{
34 mHistoryWidget = new QgsHistoryWidget( QStringLiteral( "processing" ) );
35 QVBoxLayout *vl = new QVBoxLayout();
36 vl->setContentsMargins( 0, 0, 0, 0 );
37 vl->addWidget( mHistoryWidget );
38 setLayout( vl );
39}
40
42{
43 if ( QMessageBox::question( this,
44 tr( "Clear History" ),
45 tr( "Are you sure you want to clear the Processing history?" ),
46 QMessageBox::Yes | QMessageBox::No,
47 QMessageBox::No
48 ) == QMessageBox::Yes )
49 {
51 }
52}
53
55{
56 QgsHelp::openHelp( QStringLiteral( "processing/history.html" ) );
57}
58
60{
61 QString fileName = QFileDialog::getSaveFileName( this,
62 tr( "Save File" ),
63 QDir::homePath(),
64 tr( "Log files (*.log *.LOG)" ) );
65 // return dialog focus on Mac
66 activateWindow();
67 raise();
68
69 if ( fileName.isEmpty() )
70 return;
71
72 fileName = QgsFileUtils::ensureFileNameHasExtension( fileName, { QStringLiteral( "log" ) } );
73
74 const QList< QgsHistoryEntry > entries = QgsGui::historyProviderRegistry()->queryEntries( QDateTime(), QDateTime(), QStringLiteral( "processing" ) );
75
76 const QString logSeparator = QStringLiteral( "|~|" );
77 QFile logFile( fileName );
78 if ( logFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
79 {
80 QTextStream logOut( &logFile );
81 for ( const QgsHistoryEntry &entry : entries )
82 {
83 logOut << QStringLiteral( "ALGORITHM%1%2%3%4\n" ).arg( logSeparator,
84 entry.timestamp.toString( "yyyy-MM-dd HH:mm:ss" ),
85 logSeparator,
86 entry.entry.value( QStringLiteral( "python_command" ) ).toString() );
87 }
88 }
89}
90
91
92//
93// QgsProcessingHistoryDialog
94//
95
97 : QDialog( parent )
98{
99 setObjectName( QStringLiteral( "QgsProcessingHistoryDialog" ) );
101
102 setWindowTitle( tr( "Processing History" ) );
103
104 QVBoxLayout *vl = new QVBoxLayout();
105 mWidget = new QgsProcessingHistoryWidget();
106 vl->addWidget( mWidget, 1 );
107
108 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Close | QDialogButtonBox::Help );
109
110 QPushButton *clearButton = new QPushButton( tr( "Clear" ) );
111 clearButton->setToolTip( tr( "Clear history" ) );
112 mButtonBox->addButton( clearButton, QDialogButtonBox::ActionRole );
113
114 QPushButton *saveButton = new QPushButton( tr( "Save As…" ) );
115 saveButton->setToolTip( tr( "Save history" ) );
116 mButtonBox->addButton( saveButton, QDialogButtonBox::ActionRole );
117
118 connect( clearButton, &QPushButton::clicked, mWidget, &QgsProcessingHistoryWidget::clearHistory );
119 connect( saveButton, &QPushButton::clicked, mWidget, &QgsProcessingHistoryWidget::saveLog );
120 connect( mButtonBox->button( QDialogButtonBox::Help ), &QPushButton::clicked, mWidget, &QgsProcessingHistoryWidget::openHelp );
121 connect( mButtonBox->button( QDialogButtonBox::Close ), &QPushButton::clicked, mWidget, [ = ]() { close(); } );
122
123 vl->addWidget( mButtonBox );
124
125 setLayout( vl );
126}
@ LocalProfile
Local profile.
static QString ensureFileNameHasExtension(const QString &fileName, const QStringList &extensions)
Ensures that a fileName ends with an extension from the provided list of extensions.
static QgsGui * instance()
Returns a pointer to the singleton instance.
Definition qgsgui.cpp:73
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:194
static QgsHistoryProviderRegistry * historyProviderRegistry()
Returns the global history provider registry, used for tracking history providers.
Definition qgsgui.cpp:184
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
Encapsulates a history entry.
bool clearHistory(Qgis::HistoryProviderBackend backend, const QString &providerId=QString())
Clears the history for the specified backend.
QList< QgsHistoryEntry > queryEntries(const QDateTime &start=QDateTime(), const QDateTime &end=QDateTime(), const QString &providerId=QString(), Qgis::HistoryProviderBackends backends=Qgis::HistoryProviderBackend::LocalProfile) const
Queries history entries which occurred between the specified start and end times.
A widget showing entries from a QgsHistoryProviderRegistry.
Base class for any widget that can be shown as a inline panel.
QgsProcessingHistoryDialog(QWidget *parent=nullptr)
Constructor for QgsProcessingHistoryDialog.
A widget for showing Processing algorithm execution history.
void saveLog()
Interactively allows users to save the history log.
QgsProcessingHistoryWidget(QWidget *parent=nullptr)
Constructor for QgsProcessingHistoryWidget, with the specified parent widget.
void clearHistory()
Clears the Processing history (after user confirmation).
void openHelp()
Opens helps for the widget.