QGIS API Documentation 3.99.0-Master (8e76e220402)
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
18#include "qgsfileutils.h"
19#include "qgsgui.h"
20#include "qgshelp.h"
21#include "qgshistoryentry.h"
23#include "qgshistorywidget.h"
25
26#include <QDialogButtonBox>
27#include <QFileDialog>
28#include <QMessageBox>
29#include <QPushButton>
30#include <QString>
31#include <QTextStream>
32#include <QVBoxLayout>
33
34#include "moc_qgsprocessinghistorywidget.cpp"
35
36using namespace Qt::StringLiterals;
37
39 : QgsPanelWidget( parent )
40{
41 mHistoryWidget = new QgsHistoryWidget( u"processing"_s );
42 QVBoxLayout *vl = new QVBoxLayout();
43 vl->setContentsMargins( 0, 0, 0, 0 );
44 vl->addWidget( mHistoryWidget );
45 setLayout( vl );
46}
47
49{
50 if ( QMessageBox::question( this, tr( "Clear History" ), tr( "Are you sure you want to clear the Processing history?" ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes )
51 {
53 }
54}
55
57{
58 QgsHelp::openHelp( u"processing/history.html"_s );
59}
60
62{
63 QString fileName = QFileDialog::getSaveFileName( this, tr( "Save File" ), QDir::homePath(), tr( "Log files (*.log *.LOG)" ) );
64 // return dialog focus on Mac
65 activateWindow();
66 raise();
67
68 if ( fileName.isEmpty() )
69 return;
70
71 fileName = QgsFileUtils::ensureFileNameHasExtension( fileName, { u"log"_s } );
72
73 const QList<QgsHistoryEntry> entries = QgsGui::historyProviderRegistry()->queryEntries( QDateTime(), QDateTime(), u"processing"_s );
74
75 const QString logSeparator = u"|~|"_s;
76 QFile logFile( fileName );
77 if ( logFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
78 {
79 QTextStream logOut( &logFile );
80 for ( const QgsHistoryEntry &entry : entries )
81 {
82 logOut << u"ALGORITHM%1%2%3%4\n"_s.arg( logSeparator, entry.timestamp.toString( "yyyy-MM-dd HH:mm:ss" ), logSeparator, entry.entry.value( u"python_command"_s ).toString() );
83 }
84 }
85}
86
87
88//
89// QgsProcessingHistoryDialog
90//
91
93 : QDialog( parent )
94{
95 setObjectName( u"QgsProcessingHistoryDialog"_s );
97
98 setWindowTitle( tr( "Processing History" ) );
99
100 QVBoxLayout *vl = new QVBoxLayout();
101
102 mMessageBar = new QgsMessageBar();
103 vl->addWidget( mMessageBar );
104
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, [this]() { close(); } );
122 auto *provider = qgis::down_cast<QgsProcessingHistoryProvider *>( QgsGui::historyProviderRegistry()->providerById( u"processing"_s ) );
123 connect( provider, &QgsProcessingHistoryProvider::showMessage, mMessageBar, [this]( const QString &message ) { mMessageBar->pushMessage( message, Qgis::MessageLevel::Warning ); } );
124
125 vl->addWidget( mButtonBox );
126
127 setLayout( vl );
128}
@ Warning
Warning message.
Definition qgis.h:161
@ LocalProfile
Local profile.
Definition qgis.h:3575
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:93
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:224
static QgsHistoryProviderRegistry * historyProviderRegistry()
Returns the global history provider registry, used for tracking history providers.
Definition qgsgui.cpp:214
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:41
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.
A bar for displaying non-blocking messages to the user.
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as an inline panel.
QgsProcessingHistoryDialog(QWidget *parent=nullptr)
Constructor for QgsProcessingHistoryDialog.
void showMessage(const QString &message)
Emitted when the provider needs to display a message.
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.