Skip to content

Commit

Permalink
Add signals to history registry for entries added/updated/cleared
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 22, 2023
1 parent eb33b2e commit 2fafc28
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
Expand Up @@ -112,11 +112,18 @@ Adds an ``entry`` to the history logs.

:return: - ID of newly added entry.
- ok: will be set to ``True`` if entry was successfully added


.. seealso:: :py:func:`entryAdded`
%End

bool addEntries( const QList< QgsHistoryEntry > &entries, QgsHistoryProviderRegistry::HistoryEntryOptions options = QgsHistoryProviderRegistry::HistoryEntryOptions() );
%Docstring
Adds a list of ``entries`` to the history logs.

.. seealso:: :py:func:`addEntry`

.. seealso:: :py:func:`entryAdded`
%End

QgsHistoryEntry entry( long long id, bool &ok, Qgis::HistoryProviderBackend backend = Qgis::HistoryProviderBackend::LocalProfile ) const;
Expand Down Expand Up @@ -154,6 +161,31 @@ Returns the path to user's local history database.
bool clearHistory( Qgis::HistoryProviderBackend backend );
%Docstring
Clears the history for the specified ``backend``.

.. seealso:: :py:func:`historyCleared`
%End

signals:

void entryAdded( long long id, const QgsHistoryEntry &entry, Qgis::HistoryProviderBackend backend );
%Docstring
Emitted when an ``entry`` is added.

.. versionadded:: 3.32
%End

void entryUpdated( long long id, const QVariantMap &entry, Qgis::HistoryProviderBackend backend );
%Docstring
Emitted when an ``entry`` is updated.

.. versionadded:: 3.32
%End

void historyCleared( Qgis::HistoryProviderBackend backend );
%Docstring
Emitted when the history is cleared for a ``backend``.

.. versionadded:: 3.32
%End

};
Expand Down
5 changes: 5 additions & 0 deletions src/gui/history/qgshistoryproviderregistry.cpp
Expand Up @@ -114,6 +114,8 @@ long long QgsHistoryProviderRegistry::addEntry( const QgsHistoryEntry &entry, bo
return -1;
}
id = static_cast< int >( sqlite3_last_insert_rowid( mLocalDB.get() ) );

emit entryAdded( id, entry, Qgis::HistoryProviderBackend::LocalProfile );
}

return id;
Expand Down Expand Up @@ -192,6 +194,8 @@ bool QgsHistoryProviderRegistry::updateEntry( long long id, const QVariantMap &e
QgsDebugMsg( QStringLiteral( "Couldn't update history entry in database!" ) );
return false;
}

emit entryUpdated( id, entry, Qgis::HistoryProviderBackend::LocalProfile );
return true;
}
}
Expand Down Expand Up @@ -263,6 +267,7 @@ bool QgsHistoryProviderRegistry::clearHistory( Qgis::HistoryProviderBackend back
runEmptyQuery( QStringLiteral( "DELETE from history;" ) );
break;
}
emit historyCleared( backend );
return true;
}

Expand Down
30 changes: 30 additions & 0 deletions src/gui/history/qgshistoryproviderregistry.h
Expand Up @@ -135,11 +135,16 @@ class GUI_EXPORT QgsHistoryProviderRegistry : public QObject
* \param options options
*
* \returns ID of newly added entry.
*
* \see entryAdded()
*/
long long addEntry( const QgsHistoryEntry &entry, bool &ok SIP_OUT, QgsHistoryProviderRegistry::HistoryEntryOptions options = QgsHistoryProviderRegistry::HistoryEntryOptions() );

/**
* Adds a list of \a entries to the history logs.
*
* \see addEntry()
* \see entryAdded()
*/
bool addEntries( const QList< QgsHistoryEntry > &entries, QgsHistoryProviderRegistry::HistoryEntryOptions options = QgsHistoryProviderRegistry::HistoryEntryOptions() );

Expand Down Expand Up @@ -177,9 +182,34 @@ class GUI_EXPORT QgsHistoryProviderRegistry : public QObject

/**
* Clears the history for the specified \a backend.
*
* \see historyCleared()
*/
bool clearHistory( Qgis::HistoryProviderBackend backend );

signals:

/**
* Emitted when an \a entry is added.
*
* \since QGIS 3.32
*/
void entryAdded( long long id, const QgsHistoryEntry &entry, Qgis::HistoryProviderBackend backend );

/**
* Emitted when an \a entry is updated.
*
* \since QGIS 3.32
*/
void entryUpdated( long long id, const QVariantMap &entry, Qgis::HistoryProviderBackend backend );

/**
* Emitted when the history is cleared for a \a backend.
*
* \since QGIS 3.32
*/
void historyCleared( Qgis::HistoryProviderBackend backend );

private:

/**
Expand Down
40 changes: 40 additions & 0 deletions tests/src/python/test_qgshistoryproviderregistry.py
Expand Up @@ -12,11 +12,14 @@
import qgis # NOQA switch sip api
from qgis.PyQt import sip
from qgis.PyQt.QtCore import QDate, QDateTime
from qgis.PyQt.QtTest import QSignalSpy

from qgis.core import Qgis
from qgis.gui import (
QgsAbstractHistoryProvider,
QgsHistoryEntry,
QgsHistoryProviderRegistry,
QgsGui
)
from qgis.testing import start_app, unittest

Expand All @@ -37,6 +40,11 @@ def id(self) -> str:

class TestQgsHistoryProviderRegistry(unittest.TestCase):

@classmethod
def setUpClass(cls):
# trigger metatype registration
QgsGui.instance()

def test_entry(self):
"""
Test QgsHistoryEntry
Expand Down Expand Up @@ -92,12 +100,25 @@ def test_registry_entries(self):
reg = QgsHistoryProviderRegistry(useMemoryDatabase=True)
self.assertFalse(reg.queryEntries())

added_spy = QSignalSpy(reg.entryAdded)
updated_spy = QSignalSpy(reg.entryUpdated)

id_1, ok = reg.addEntry('my provider', {'some var': 5, 'other_var': [1, 2, 3], 'final_var': {'a': 'b'}},
QgsHistoryProviderRegistry.HistoryEntryOptions())
self.assertTrue(ok)

self.assertEqual(len(added_spy), 1)
self.assertEqual(added_spy[-1][0], id_1)
self.assertEqual(added_spy[-1][1].providerId, 'my provider')
self.assertEqual(added_spy[-1][1].entry, {'some var': 5, 'other_var': [1, 2, 3], 'final_var': {'a': 'b'}})

id_2, ok = reg.addEntry('my provider 2', {'some var': 6},
QgsHistoryProviderRegistry.HistoryEntryOptions())
self.assertTrue(ok)
self.assertEqual(len(added_spy), 2)
self.assertEqual(added_spy[-1][0], id_2)
self.assertEqual(added_spy[-1][1].providerId, 'my provider 2')
self.assertEqual(added_spy[-1][1].entry, {'some var': 6})

self.assertEqual(len(reg.queryEntries()), 2)
self.assertEqual(reg.queryEntries()[0].providerId, 'my provider')
Expand All @@ -124,6 +145,11 @@ def test_registry_entries(self):
entry = QgsHistoryEntry('my provider 3', QDateTime(2021, 1, 2, 3, 4, 5), {'var': 7})
id_3, ok = reg.addEntry(entry)
self.assertTrue(ok)
self.assertEqual(len(added_spy), 3)
self.assertEqual(added_spy[-1][0], id_3)
self.assertEqual(added_spy[-1][1].providerId, 'my provider 3')
self.assertEqual(added_spy[-1][1].entry, {'var': 7})

self.assertEqual(len(reg.queryEntries()), 3)
self.assertEqual(reg.queryEntries()[0].providerId, 'my provider')
self.assertEqual(reg.queryEntries()[0].timestamp.date(), QDateTime.currentDateTime().date())
Expand Down Expand Up @@ -163,12 +189,19 @@ def test_registry_entries(self):

# update an entry
self.assertTrue(reg.updateEntry(id_1, {'new_props': 54}))
self.assertEqual(len(updated_spy), 1)
self.assertEqual(updated_spy[-1][0], id_1)
self.assertEqual(updated_spy[-1][1], {'new_props': 54})

entry, ok = reg.entry(id_1)
self.assertEqual(entry.providerId, 'my provider')
self.assertEqual(entry.timestamp.date(), QDateTime.currentDateTime().date())
self.assertEqual(entry.entry, {'new_props': 54})

clear_spy = QSignalSpy(reg.historyCleared)
self.assertTrue(reg.clearHistory(Qgis.HistoryProviderBackend.LocalProfile))
self.assertEqual(len(clear_spy), 1)

self.assertFalse(reg.queryEntries())

# bulk add entries
Expand All @@ -177,6 +210,13 @@ def test_registry_entries(self):
QgsHistoryEntry('my provider 5', QDateTime(2021, 1, 2, 3, 4, 5), {'var': 8})
]))
self.assertEqual(len(reg.queryEntries()), 2)

self.assertEqual(len(added_spy), 5)
self.assertEqual(added_spy[-2][1].providerId, 'my provider 4')
self.assertEqual(added_spy[-2][1].entry, {'var': 7})
self.assertEqual(added_spy[-1][1].providerId, 'my provider 5')
self.assertEqual(added_spy[-1][1].entry, {'var': 8})

self.assertEqual(reg.queryEntries()[0].providerId, 'my provider 4')
self.assertEqual(reg.queryEntries()[0].entry, {'var': 7})

Expand Down

0 comments on commit 2fafc28

Please sign in to comment.