-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processing] Move recent algorithm log to c++ class
- Loading branch information
1 parent
d66d1ee
commit d232cde
Showing
10 changed files
with
315 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
python/gui/auto_generated/processing/qgsprocessingrecentalgorithmlog.sip.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/gui/processing/qgsprocessingrecentalgorithmlog.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
|
||
class QgsProcessingRecentAlgorithmLog : QObject | ||
{ | ||
%Docstring | ||
A log for tracking recently used processing algorithms. | ||
|
||
QgsProcessingRecentAlgorithmLog is not usually directly created, instead | ||
use the instance accessible through :py:func:`QgsGui.processingRecentAlgorithmLog()` | ||
|
||
The log contents are saved and restored via QgsSettings. | ||
|
||
.. note:: | ||
|
||
Not stable API | ||
|
||
.. versionadded:: 3.4 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgsprocessingrecentalgorithmlog.h" | ||
%End | ||
public: | ||
|
||
QgsProcessingRecentAlgorithmLog( QObject *parent = 0 ); | ||
%Docstring | ||
Constructor for QgsProcessingRecentAlgorithmLog, with the specified | ||
``parent`` object. | ||
%End | ||
|
||
QStringList recentAlgorithmIds() const; | ||
%Docstring | ||
Returns a list of the IDs of recently used processing algorithms, where the | ||
first item in the list is the most recently used algorithm. | ||
%End | ||
|
||
void push( const QString &id ); | ||
%Docstring | ||
Pushes the algorithm with matching ``id`` to the top of the recently used | ||
algorithm list. | ||
|
||
If this changes the list of recent algorithm IDs then the changed() signal | ||
will be emitted. | ||
%End | ||
|
||
signals: | ||
|
||
void changed(); | ||
%Docstring | ||
Emitted when the list of recently used algorithms is changed, e.g. when | ||
a new algorithm ID is pushed to the list (see push()). | ||
%End | ||
|
||
}; | ||
|
||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/gui/processing/qgsprocessingrecentalgorithmlog.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/*************************************************************************** | ||
qgsprocessingrecentalgorithmlog.cpp | ||
------------------------------------ | ||
Date : July 2018 | ||
Copyright : (C) 2018 Nyall Dawson | ||
Email : nyall dot dawson at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsprocessingrecentalgorithmlog.h" | ||
#include "qgssettings.h" | ||
|
||
///@cond PRIVATE | ||
|
||
const int MAX_LOG_LENGTH = 5; | ||
|
||
QgsProcessingRecentAlgorithmLog::QgsProcessingRecentAlgorithmLog( QObject *parent ) | ||
: QObject( parent ) | ||
{ | ||
QgsSettings settings; | ||
mRecentAlgorithmIds = settings.value( QStringLiteral( "processing/recentAlgorithms" ), QVariant(), QgsSettings::Gui ).toStringList(); | ||
} | ||
|
||
QStringList QgsProcessingRecentAlgorithmLog::recentAlgorithmIds() const | ||
{ | ||
return mRecentAlgorithmIds; | ||
} | ||
|
||
void QgsProcessingRecentAlgorithmLog::push( const QString &id ) | ||
{ | ||
const QStringList previous = mRecentAlgorithmIds; | ||
mRecentAlgorithmIds.removeAll( id ); | ||
mRecentAlgorithmIds.insert( 0, id ); | ||
if ( mRecentAlgorithmIds.length() > MAX_LOG_LENGTH ) | ||
mRecentAlgorithmIds = mRecentAlgorithmIds.mid( 0, MAX_LOG_LENGTH ); | ||
|
||
QgsSettings settings; | ||
settings.setValue( QStringLiteral( "processing/recentAlgorithms" ), mRecentAlgorithmIds, QgsSettings::Gui ); | ||
|
||
if ( previous != mRecentAlgorithmIds ) | ||
emit changed(); | ||
} | ||
|
||
///@endcond |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/*************************************************************************** | ||
qgsprocessingrecentalgorithmlog.h | ||
---------------------------------- | ||
Date : July 2018 | ||
Copyright : (C) 2018 Nyall Dawson | ||
Email : nyall dot dawson at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSPROCESSINGRECENTALGORITHMLOG_H | ||
#define QGSPROCESSINGRECENTALGORITHMLOG_H | ||
|
||
#include "qgis.h" | ||
#include "qgis_gui.h" | ||
|
||
///@cond NOT_STABLE | ||
|
||
/** | ||
* \ingroup gui | ||
* \brief A log for tracking recently used processing algorithms. | ||
* | ||
* QgsProcessingRecentAlgorithmLog is not usually directly created, instead | ||
* use the instance accessible through QgsGui::processingRecentAlgorithmLog(). | ||
* | ||
* The log contents are saved and restored via QgsSettings. | ||
* | ||
* \note Not stable API | ||
* \since QGIS 3.4 | ||
*/ | ||
class GUI_EXPORT QgsProcessingRecentAlgorithmLog : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
|
||
/** | ||
* Constructor for QgsProcessingRecentAlgorithmLog, with the specified | ||
* \a parent object. | ||
*/ | ||
QgsProcessingRecentAlgorithmLog( QObject *parent = nullptr ); | ||
|
||
/** | ||
* Returns a list of the IDs of recently used processing algorithms, where the | ||
* first item in the list is the most recently used algorithm. | ||
*/ | ||
QStringList recentAlgorithmIds() const; | ||
|
||
/** | ||
* Pushes the algorithm with matching \a id to the top of the recently used | ||
* algorithm list. | ||
* | ||
* If this changes the list of recent algorithm IDs then the changed() signal | ||
* will be emitted. | ||
*/ | ||
void push( const QString &id ); | ||
|
||
signals: | ||
|
||
/** | ||
* Emitted when the list of recently used algorithms is changed, e.g. when | ||
* a new algorithm ID is pushed to the list (see push()). | ||
*/ | ||
void changed(); | ||
|
||
private: | ||
|
||
QStringList mRecentAlgorithmIds; | ||
|
||
}; | ||
|
||
///@endcond | ||
|
||
#endif // QGSPROCESSINGRECENTALGORITHMLOG_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# -*- coding: utf-8 -*- | ||
"""QGIS Unit tests for QgsProcessingRecentAlgorithmLog. | ||
.. note:: This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
""" | ||
__author__ = 'Nyall Dawson' | ||
__date__ = '2018-07' | ||
__copyright__ = 'Copyright 2018, The QGIS Project' | ||
# This will get replaced with a git SHA1 when you do a git archive | ||
__revision__ = '$Format:%H$' | ||
|
||
from qgis.PyQt.QtCore import QCoreApplication | ||
from qgis.core import QgsSettings | ||
from qgis.gui import QgsProcessingRecentAlgorithmLog, QgsGui | ||
from qgis.testing import start_app, unittest | ||
from qgis.PyQt.QtTest import QSignalSpy | ||
|
||
start_app() | ||
|
||
|
||
class TestQgsProcessingRecentAlgorithmLog(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
"""Run before all tests""" | ||
QCoreApplication.setOrganizationName("QGIS_Test") | ||
QCoreApplication.setOrganizationDomain("QGIS_TestPyQgsNewGeoPackageLayerDialog.com") | ||
QCoreApplication.setApplicationName("QGIS_TestPyQgsNewGeoPackageLayerDialog") | ||
QgsSettings().clear() | ||
|
||
def test_log(self): | ||
log = QgsProcessingRecentAlgorithmLog() | ||
self.assertFalse(log.recentAlgorithmIds()) | ||
spy = QSignalSpy(log.changed) | ||
|
||
log.push('test') | ||
self.assertEqual(log.recentAlgorithmIds(), ['test']) | ||
self.assertEqual(len(spy), 1) | ||
log.push('test') | ||
self.assertEqual(log.recentAlgorithmIds(), ['test']) | ||
self.assertEqual(len(spy), 1) | ||
|
||
log.push('test2') | ||
self.assertEqual(log.recentAlgorithmIds(), ['test2', 'test']) | ||
self.assertEqual(len(spy), 2) | ||
|
||
log.push('test') | ||
self.assertEqual(log.recentAlgorithmIds(), ['test', 'test2']) | ||
self.assertEqual(len(spy), 3) | ||
|
||
log.push('test3') | ||
self.assertEqual(log.recentAlgorithmIds(), ['test3', 'test', 'test2']) | ||
self.assertEqual(len(spy), 4) | ||
|
||
log.push('test4') | ||
self.assertEqual(log.recentAlgorithmIds(), ['test4', 'test3', 'test', 'test2']) | ||
self.assertEqual(len(spy), 5) | ||
|
||
log.push('test5') | ||
self.assertEqual(log.recentAlgorithmIds(), ['test5', 'test4', 'test3', 'test', 'test2']) | ||
self.assertEqual(len(spy), 6) | ||
|
||
log.push('test6') | ||
self.assertEqual(log.recentAlgorithmIds(), ['test6', 'test5', 'test4', 'test3', 'test']) | ||
self.assertEqual(len(spy), 7) | ||
|
||
log.push('test3') | ||
self.assertEqual(log.recentAlgorithmIds(), ['test3', 'test6', 'test5', 'test4', 'test']) | ||
self.assertEqual(len(spy), 8) | ||
|
||
log.push('test3') | ||
self.assertEqual(log.recentAlgorithmIds(), ['test3', 'test6', 'test5', 'test4', 'test']) | ||
self.assertEqual(len(spy), 8) | ||
|
||
# test that log has been saved to QgsSettings | ||
log2 = QgsProcessingRecentAlgorithmLog() | ||
self.assertEqual(log2.recentAlgorithmIds(), ['test3', 'test6', 'test5', 'test4', 'test']) | ||
|
||
def test_gui_instance(self): | ||
self.assertIsNotNone(QgsGui.instance().processingRecentAlgorithmLog()) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |