QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
66 if ( fileName.isEmpty() )
67 return;
68
69 fileName = QgsFileUtils::ensureFileNameHasExtension( fileName, { QStringLiteral( "log" ) } );
70
71 const QList< QgsHistoryEntry > entries = QgsGui::historyProviderRegistry()->queryEntries( QDateTime(), QDateTime(), QStringLiteral( "processing" ) );
72
73 const QString logSeparator = QStringLiteral( "|~|" );
74 QFile logFile( fileName );
75 if ( logFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
76 {
77 QTextStream logOut( &logFile );
78 for ( const QgsHistoryEntry &entry : entries )
79 {
80 logOut << QStringLiteral( "ALGORITHM%1%2%3%4\n" ).arg( logSeparator,
81 entry.timestamp.toString( "YYYY-mm-dd HH:MM:ss" ),
82 logSeparator,
83 entry.entry.value( QStringLiteral( "python_command" ) ).toString() );
84 }
85 }
86}
87
88
89//
90// QgsProcessingHistoryDialog
91//
92
94 : QDialog( parent )
95{
96 setObjectName( QStringLiteral( "QgsProcessingHistoryDialog" ) );
98
99 setWindowTitle( tr( "Processing History" ) );
100
101 QVBoxLayout *vl = new QVBoxLayout();
102 mWidget = new QgsProcessingHistoryWidget();
103 vl->addWidget( mWidget, 1 );
104
105 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Close | QDialogButtonBox::Help );
106
107 QPushButton *clearButton = new QPushButton( tr( "Clear" ) );
108 clearButton->setToolTip( tr( "Clear history" ) );
109 mButtonBox->addButton( clearButton, QDialogButtonBox::ActionRole );
110
111 QPushButton *saveButton = new QPushButton( tr( "Save As…" ) );
112 saveButton->setToolTip( tr( "Save history" ) );
113 mButtonBox->addButton( saveButton, QDialogButtonBox::ActionRole );
114
115 connect( clearButton, &QPushButton::clicked, mWidget, &QgsProcessingHistoryWidget::clearHistory );
116 connect( saveButton, &QPushButton::clicked, mWidget, &QgsProcessingHistoryWidget::saveLog );
117 connect( mButtonBox->button( QDialogButtonBox::Help ), &QPushButton::clicked, mWidget, &QgsProcessingHistoryWidget::openHelp );
118 connect( mButtonBox->button( QDialogButtonBox::Close ), &QPushButton::clicked, mWidget, [ = ]() { close(); } );
119
120 vl->addWidget( mButtonBox );
121
122 setLayout( vl );
123}
@ 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:72
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:193
static QgsHistoryProviderRegistry * historyProviderRegistry()
Returns the global history provider registry, used for tracking history providers.
Definition: qgsgui.cpp:183
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:38
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.