Skip to content

Commit d232cde

Browse files
committedJul 4, 2018
[processing] Move recent algorithm log to c++ class
1 parent d66d1ee commit d232cde

File tree

10 files changed

+315
-0
lines changed

10 files changed

+315
-0
lines changed
 
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/gui/processing/qgsprocessingrecentalgorithmlog.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
class QgsProcessingRecentAlgorithmLog : QObject
13+
{
14+
%Docstring
15+
A log for tracking recently used processing algorithms.
16+
17+
QgsProcessingRecentAlgorithmLog is not usually directly created, instead
18+
use the instance accessible through :py:func:`QgsGui.processingRecentAlgorithmLog()`
19+
20+
The log contents are saved and restored via QgsSettings.
21+
22+
.. note::
23+
24+
Not stable API
25+
26+
.. versionadded:: 3.4
27+
%End
28+
29+
%TypeHeaderCode
30+
#include "qgsprocessingrecentalgorithmlog.h"
31+
%End
32+
public:
33+
34+
QgsProcessingRecentAlgorithmLog( QObject *parent = 0 );
35+
%Docstring
36+
Constructor for QgsProcessingRecentAlgorithmLog, with the specified
37+
``parent`` object.
38+
%End
39+
40+
QStringList recentAlgorithmIds() const;
41+
%Docstring
42+
Returns a list of the IDs of recently used processing algorithms, where the
43+
first item in the list is the most recently used algorithm.
44+
%End
45+
46+
void push( const QString &id );
47+
%Docstring
48+
Pushes the algorithm with matching ``id`` to the top of the recently used
49+
algorithm list.
50+
51+
If this changes the list of recent algorithm IDs then the changed() signal
52+
will be emitted.
53+
%End
54+
55+
signals:
56+
57+
void changed();
58+
%Docstring
59+
Emitted when the list of recently used algorithms is changed, e.g. when
60+
a new algorithm ID is pushed to the list (see push()).
61+
%End
62+
63+
};
64+
65+
66+
/************************************************************************
67+
* This file has been generated automatically from *
68+
* *
69+
* src/gui/processing/qgsprocessingrecentalgorithmlog.h *
70+
* *
71+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
72+
************************************************************************/

‎python/gui/auto_generated/qgsgui.sip.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ Returns the global layout item GUI registry, used for registering the GUI behavi
6767
Returns the global processing gui registry, used for registering the GUI behavior of processing algorithms.
6868

6969
.. versionadded:: 3.2
70+
%End
71+
72+
static QgsProcessingRecentAlgorithmLog *processingRecentAlgorithmLog();
73+
%Docstring
74+
Returns the global processing recent algorithm log, used for tracking recently used processing algorithms.
75+
76+
.. versionadded:: 3.4
7077
%End
7178

7279
static void enableAutoGeometryRestore( QWidget *widget, const QString &key = QString() );

‎python/gui/gui_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,5 @@
315315
%Include auto_generated/locator/qgslocatorwidget.sip
316316
%Include auto_generated/processing/qgsprocessingalgorithmconfigurationwidget.sip
317317
%Include auto_generated/processing/qgsprocessingalgorithmdialogbase.sip
318+
%Include auto_generated/processing/qgsprocessingrecentalgorithmlog.sip
318319
%Include auto_generated/qgsadvanceddigitizingcanvasitem.sip

‎src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ SET(QGIS_GUI_SRCS
197197
processing/qgsprocessingalgorithmdialogbase.cpp
198198
processing/qgsprocessingconfigurationwidgets.cpp
199199
processing/qgsprocessingguiregistry.cpp
200+
processing/qgsprocessingrecentalgorithmlog.cpp
200201

201202
qgisinterface.cpp
202203
qgsactionmenu.cpp
@@ -717,6 +718,7 @@ SET(QGIS_GUI_MOC_HDRS
717718
processing/qgsprocessingalgorithmconfigurationwidget.h
718719
processing/qgsprocessingalgorithmdialogbase.h
719720
processing/qgsprocessingconfigurationwidgets.h
721+
processing/qgsprocessingrecentalgorithmlog.h
720722
)
721723
SET_PROPERTY(GLOBAL PROPERTY QGIS_GUI_MOC_HDRS ${QGIS_GUI_MOC_HDRS})
722724

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/***************************************************************************
2+
qgsprocessingrecentalgorithmlog.cpp
3+
------------------------------------
4+
Date : July 2018
5+
Copyright : (C) 2018 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+
16+
#include "qgsprocessingrecentalgorithmlog.h"
17+
#include "qgssettings.h"
18+
19+
///@cond PRIVATE
20+
21+
const int MAX_LOG_LENGTH = 5;
22+
23+
QgsProcessingRecentAlgorithmLog::QgsProcessingRecentAlgorithmLog( QObject *parent )
24+
: QObject( parent )
25+
{
26+
QgsSettings settings;
27+
mRecentAlgorithmIds = settings.value( QStringLiteral( "processing/recentAlgorithms" ), QVariant(), QgsSettings::Gui ).toStringList();
28+
}
29+
30+
QStringList QgsProcessingRecentAlgorithmLog::recentAlgorithmIds() const
31+
{
32+
return mRecentAlgorithmIds;
33+
}
34+
35+
void QgsProcessingRecentAlgorithmLog::push( const QString &id )
36+
{
37+
const QStringList previous = mRecentAlgorithmIds;
38+
mRecentAlgorithmIds.removeAll( id );
39+
mRecentAlgorithmIds.insert( 0, id );
40+
if ( mRecentAlgorithmIds.length() > MAX_LOG_LENGTH )
41+
mRecentAlgorithmIds = mRecentAlgorithmIds.mid( 0, MAX_LOG_LENGTH );
42+
43+
QgsSettings settings;
44+
settings.setValue( QStringLiteral( "processing/recentAlgorithms" ), mRecentAlgorithmIds, QgsSettings::Gui );
45+
46+
if ( previous != mRecentAlgorithmIds )
47+
emit changed();
48+
}
49+
50+
///@endcond
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/***************************************************************************
2+
qgsprocessingrecentalgorithmlog.h
3+
----------------------------------
4+
Date : July 2018
5+
Copyright : (C) 2018 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+
16+
#ifndef QGSPROCESSINGRECENTALGORITHMLOG_H
17+
#define QGSPROCESSINGRECENTALGORITHMLOG_H
18+
19+
#include "qgis.h"
20+
#include "qgis_gui.h"
21+
22+
///@cond NOT_STABLE
23+
24+
/**
25+
* \ingroup gui
26+
* \brief A log for tracking recently used processing algorithms.
27+
*
28+
* QgsProcessingRecentAlgorithmLog is not usually directly created, instead
29+
* use the instance accessible through QgsGui::processingRecentAlgorithmLog().
30+
*
31+
* The log contents are saved and restored via QgsSettings.
32+
*
33+
* \note Not stable API
34+
* \since QGIS 3.4
35+
*/
36+
class GUI_EXPORT QgsProcessingRecentAlgorithmLog : public QObject
37+
{
38+
Q_OBJECT
39+
40+
public:
41+
42+
/**
43+
* Constructor for QgsProcessingRecentAlgorithmLog, with the specified
44+
* \a parent object.
45+
*/
46+
QgsProcessingRecentAlgorithmLog( QObject *parent = nullptr );
47+
48+
/**
49+
* Returns a list of the IDs of recently used processing algorithms, where the
50+
* first item in the list is the most recently used algorithm.
51+
*/
52+
QStringList recentAlgorithmIds() const;
53+
54+
/**
55+
* Pushes the algorithm with matching \a id to the top of the recently used
56+
* algorithm list.
57+
*
58+
* If this changes the list of recent algorithm IDs then the changed() signal
59+
* will be emitted.
60+
*/
61+
void push( const QString &id );
62+
63+
signals:
64+
65+
/**
66+
* Emitted when the list of recently used algorithms is changed, e.g. when
67+
* a new algorithm ID is pushed to the list (see push()).
68+
*/
69+
void changed();
70+
71+
private:
72+
73+
QStringList mRecentAlgorithmIds;
74+
75+
};
76+
77+
///@endcond
78+
79+
#endif // QGSPROCESSINGRECENTALGORITHMLOG_H

‎src/gui/qgsgui.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "qgsshortcutsmanager.h"
3333
#include "qgswidgetstatehelper_p.h"
3434
#include "qgslogger.h"
35+
#include "qgsprocessingrecentalgorithmlog.h"
3536

3637
QgsGui *QgsGui::instance()
3738
{
@@ -79,6 +80,11 @@ QgsProcessingGuiRegistry *QgsGui::processingGuiRegistry()
7980
return instance()->mProcessingGuiRegistry;
8081
}
8182

83+
QgsProcessingRecentAlgorithmLog *QgsGui::processingRecentAlgorithmLog()
84+
{
85+
return instance()->mProcessingRecentAlgorithmLog;
86+
}
87+
8288
void QgsGui::enableAutoGeometryRestore( QWidget *widget, const QString &key )
8389
{
8490
if ( widget->objectName().isEmpty() )
@@ -91,6 +97,7 @@ void QgsGui::enableAutoGeometryRestore( QWidget *widget, const QString &key )
9197
QgsGui::~QgsGui()
9298
{
9399
delete mProcessingGuiRegistry;
100+
delete mProcessingRecentAlgorithmLog;
94101
delete mLayoutItemGuiRegistry;
95102
delete mLayerTreeEmbeddedWidgetRegistry;
96103
delete mEditorWidgetRegistry;
@@ -116,5 +123,6 @@ QgsGui::QgsGui()
116123
mSourceSelectProviderRegistry = new QgsSourceSelectProviderRegistry();
117124
mLayoutItemGuiRegistry = new QgsLayoutItemGuiRegistry();
118125
mWidgetStateHelper = new QgsWidgetStateHelper();
126+
mProcessingRecentAlgorithmLog = new QgsProcessingRecentAlgorithmLog();
119127
mProcessingGuiRegistry = new QgsProcessingGuiRegistry();
120128
}

‎src/gui/qgsgui.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class QgsNative;
3131
class QgsLayoutItemGuiRegistry;
3232
class QgsWidgetStateHelper;
3333
class QgsProcessingGuiRegistry;
34+
class QgsProcessingRecentAlgorithmLog;
3435

3536
/**
3637
* \ingroup gui
@@ -96,6 +97,12 @@ class GUI_EXPORT QgsGui
9697
*/
9798
static QgsProcessingGuiRegistry *processingGuiRegistry();
9899

100+
/**
101+
* Returns the global processing recent algorithm log, used for tracking recently used processing algorithms.
102+
* \since QGIS 3.4
103+
*/
104+
static QgsProcessingRecentAlgorithmLog *processingRecentAlgorithmLog();
105+
99106
/**
100107
* Register the widget to allow its position to be automatically saved and restored when open and closed.
101108
* Use this to avoid needing to call saveGeometry() and restoreGeometry() on your widget.
@@ -117,6 +124,7 @@ class GUI_EXPORT QgsGui
117124
QgsMapLayerActionRegistry *mMapLayerActionRegistry = nullptr;
118125
QgsLayoutItemGuiRegistry *mLayoutItemGuiRegistry = nullptr;
119126
QgsProcessingGuiRegistry *mProcessingGuiRegistry = nullptr;
127+
QgsProcessingRecentAlgorithmLog *mProcessingRecentAlgorithmLog = nullptr;
120128

121129
#ifdef SIP_RUN
122130
QgsGui( const QgsGui &other );

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ ADD_PYTHON_TEST(PyQgsPoint test_qgspoint.py)
140140
ADD_PYTHON_TEST(PyQgsPointClusterRenderer test_qgspointclusterrenderer.py)
141141
ADD_PYTHON_TEST(PyQgsPointDisplacementRenderer test_qgspointdisplacementrenderer.py)
142142
ADD_PYTHON_TEST(PyQgsPostgresDomain test_qgspostgresdomain.py)
143+
ADD_PYTHON_TEST(PyQgsProcessingRecentAlgorithmLog test_qgsprocessingrecentalgorithmslog.py)
143144
ADD_PYTHON_TEST(PyQgsProjectionSelectionWidgets test_qgsprojectionselectionwidgets.py)
144145
ADD_PYTHON_TEST(PyQgsProjectMetadata test_qgsprojectmetadata.py)
145146
ADD_PYTHON_TEST(PyQgsRange test_qgsrange.py)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsProcessingRecentAlgorithmLog.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Nyall Dawson'
10+
__date__ = '2018-07'
11+
__copyright__ = 'Copyright 2018, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
from qgis.PyQt.QtCore import QCoreApplication
16+
from qgis.core import QgsSettings
17+
from qgis.gui import QgsProcessingRecentAlgorithmLog, QgsGui
18+
from qgis.testing import start_app, unittest
19+
from qgis.PyQt.QtTest import QSignalSpy
20+
21+
start_app()
22+
23+
24+
class TestQgsProcessingRecentAlgorithmLog(unittest.TestCase):
25+
26+
@classmethod
27+
def setUpClass(cls):
28+
"""Run before all tests"""
29+
QCoreApplication.setOrganizationName("QGIS_Test")
30+
QCoreApplication.setOrganizationDomain("QGIS_TestPyQgsNewGeoPackageLayerDialog.com")
31+
QCoreApplication.setApplicationName("QGIS_TestPyQgsNewGeoPackageLayerDialog")
32+
QgsSettings().clear()
33+
34+
def test_log(self):
35+
log = QgsProcessingRecentAlgorithmLog()
36+
self.assertFalse(log.recentAlgorithmIds())
37+
spy = QSignalSpy(log.changed)
38+
39+
log.push('test')
40+
self.assertEqual(log.recentAlgorithmIds(), ['test'])
41+
self.assertEqual(len(spy), 1)
42+
log.push('test')
43+
self.assertEqual(log.recentAlgorithmIds(), ['test'])
44+
self.assertEqual(len(spy), 1)
45+
46+
log.push('test2')
47+
self.assertEqual(log.recentAlgorithmIds(), ['test2', 'test'])
48+
self.assertEqual(len(spy), 2)
49+
50+
log.push('test')
51+
self.assertEqual(log.recentAlgorithmIds(), ['test', 'test2'])
52+
self.assertEqual(len(spy), 3)
53+
54+
log.push('test3')
55+
self.assertEqual(log.recentAlgorithmIds(), ['test3', 'test', 'test2'])
56+
self.assertEqual(len(spy), 4)
57+
58+
log.push('test4')
59+
self.assertEqual(log.recentAlgorithmIds(), ['test4', 'test3', 'test', 'test2'])
60+
self.assertEqual(len(spy), 5)
61+
62+
log.push('test5')
63+
self.assertEqual(log.recentAlgorithmIds(), ['test5', 'test4', 'test3', 'test', 'test2'])
64+
self.assertEqual(len(spy), 6)
65+
66+
log.push('test6')
67+
self.assertEqual(log.recentAlgorithmIds(), ['test6', 'test5', 'test4', 'test3', 'test'])
68+
self.assertEqual(len(spy), 7)
69+
70+
log.push('test3')
71+
self.assertEqual(log.recentAlgorithmIds(), ['test3', 'test6', 'test5', 'test4', 'test'])
72+
self.assertEqual(len(spy), 8)
73+
74+
log.push('test3')
75+
self.assertEqual(log.recentAlgorithmIds(), ['test3', 'test6', 'test5', 'test4', 'test'])
76+
self.assertEqual(len(spy), 8)
77+
78+
# test that log has been saved to QgsSettings
79+
log2 = QgsProcessingRecentAlgorithmLog()
80+
self.assertEqual(log2.recentAlgorithmIds(), ['test3', 'test6', 'test5', 'test4', 'test'])
81+
82+
def test_gui_instance(self):
83+
self.assertIsNotNone(QgsGui.instance().processingRecentAlgorithmLog())
84+
85+
86+
if __name__ == '__main__':
87+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.