Skip to content

Commit 538cf4c

Browse files
committedApr 22, 2023
Add QgsHistoryWidgetContext for future api flexibility
1 parent f3f0946 commit 538cf4c

18 files changed

+178
-23
lines changed
 

‎python/gui/auto_generated/history/qgshistoryentrymodel.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ An item model representing history entries in a hierarchical tree structure.
2626
QgsHistoryEntryModel( const QString &providerId = QString(),
2727
Qgis::HistoryProviderBackends backends = Qgis::HistoryProviderBackend::LocalProfile,
2828
QgsHistoryProviderRegistry *registry = 0,
29+
const QgsHistoryWidgetContext &context = QgsHistoryWidgetContext(),
2930
QObject *parent /TransferThis/ = 0 );
3031
%Docstring
3132
Constructor for QgsHistoryEntryModel, with the specified ``parent`` object.

‎python/gui/auto_generated/history/qgshistoryentrynode.sip.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ Returns the node's data for the specified model ``role``.
4545
Returns the number of child nodes owned by this node.
4646
%End
4747

48-
virtual QString html() const;
48+
virtual QString html( const QgsHistoryWidgetContext &context ) const;
4949
%Docstring
5050
Returns a HTML formatted text string which should be shown to a user when
5151
selecting the node.
5252

5353
Subclasses should implement this method or :py:func:`~QgsHistoryEntryNode.createWidget`, but not both.
5454
%End
5555

56-
virtual QWidget *createWidget() /Factory/;
56+
virtual QWidget *createWidget( const QgsHistoryWidgetContext &context ) /Factory/;
5757
%Docstring
5858
Returns a new widget which should be shown to users when selecting the node.
5959

‎python/gui/auto_generated/history/qgshistoryprovider.sip.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ Abstract base class for objects which track user history (i.e. operations perfor
3737
Returns the provider's unique id, which is used to associate existing history entries with the provider.
3838
%End
3939

40-
virtual QgsHistoryEntryNode *createNodeForEntry( const QgsHistoryEntry &entry ) /Factory/;
40+
virtual QgsHistoryEntryNode *createNodeForEntry( const QgsHistoryEntry &entry, const QgsHistoryWidgetContext &context ) /Factory/;
4141
%Docstring
4242
Creates a new history node for the given ``entry``.
4343

4444
.. versionadded:: 3.32
4545
%End
4646

47-
virtual void updateNodeForEntry( QgsHistoryEntryNode *node, const QgsHistoryEntry &entry );
47+
virtual void updateNodeForEntry( QgsHistoryEntryNode *node, const QgsHistoryEntry &entry, const QgsHistoryWidgetContext &context );
4848
%Docstring
4949
Updates an existing history ``node`` for the given ``entry``.
5050

‎python/gui/auto_generated/history/qgshistorywidget.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ A widget showing entries from a :py:class:`QgsHistoryProviderRegistry`.
2727
QgsHistoryWidget( const QString &providerId = QString(),
2828
Qgis::HistoryProviderBackends backends = Qgis::HistoryProviderBackend::LocalProfile,
2929
QgsHistoryProviderRegistry *registry = 0,
30+
const QgsHistoryWidgetContext &context = QgsHistoryWidgetContext(),
3031
QWidget *parent = 0 );
3132
%Docstring
3233
Constructor for QgsHistoryWidget, with the specified ``parent`` widget.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/gui/history/qgshistorywidgetcontext.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
class QgsHistoryWidgetContext
13+
{
14+
%Docstring(signature="appended")
15+
Contains settings which reflect the context in which a history widget is shown, e.g., an associated message bar.
16+
17+
.. versionadded:: 3.32
18+
%End
19+
20+
%TypeHeaderCode
21+
#include "qgshistorywidgetcontext.h"
22+
%End
23+
public:
24+
25+
QgsHistoryWidgetContext();
26+
%Docstring
27+
Constructor for QgsHistoryWidgetContext.
28+
%End
29+
30+
void setMessageBar( QgsMessageBar *bar );
31+
%Docstring
32+
Sets the message ``bar`` associated with the widget. This allows the widget to push feedback messages
33+
to the appropriate message bar.
34+
35+
.. seealso:: :py:func:`messageBar`
36+
%End
37+
38+
QgsMessageBar *messageBar() const;
39+
%Docstring
40+
Returns the message bar associated with the widget.
41+
42+
.. seealso:: :py:func:`setMessageBar`
43+
%End
44+
45+
};
46+
47+
/************************************************************************
48+
* This file has been generated automatically from *
49+
* *
50+
* src/gui/history/qgshistorywidgetcontext.h *
51+
* *
52+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
53+
************************************************************************/

‎python/gui/gui_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@
347347
%Include auto_generated/history/qgshistoryprovider.sip
348348
%Include auto_generated/history/qgshistoryproviderregistry.sip
349349
%Include auto_generated/history/qgshistorywidget.sip
350+
%Include auto_generated/history/qgshistorywidgetcontext.sip
350351
%Include auto_generated/labeling/qgslabellineanchorwidget.sip
351352
%Include auto_generated/labeling/qgslabelobstaclesettingswidget.sip
352353
%Include auto_generated/labeling/qgslabelsettingswidgetbase.sip

‎src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ set(QGIS_GUI_SRCS
242242
history/qgshistoryprovider.cpp
243243
history/qgshistoryproviderregistry.cpp
244244
history/qgshistorywidget.cpp
245+
history/qgshistorywidgetcontext.cpp
245246

246247
labeling/qgslabelengineconfigdialog.cpp
247248
labeling/qgslabelinggui.cpp
@@ -1152,6 +1153,7 @@ set(QGIS_GUI_HDRS
11521153
history/qgshistoryprovider.h
11531154
history/qgshistoryproviderregistry.h
11541155
history/qgshistorywidget.h
1156+
history/qgshistorywidgetcontext.h
11551157

11561158
labeling/qgslabelengineconfigdialog.h
11571159
labeling/qgslabelinggui.h

‎src/gui/history/qgshistoryentrymodel.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ class QgsHistoryEntryDateGroupNode : public QgsHistoryEntryGroup
8080
};
8181
///@endcond
8282

83-
QgsHistoryEntryModel::QgsHistoryEntryModel( const QString &providerId, Qgis::HistoryProviderBackends backends, QgsHistoryProviderRegistry *registry, QObject *parent )
83+
QgsHistoryEntryModel::QgsHistoryEntryModel( const QString &providerId, Qgis::HistoryProviderBackends backends, QgsHistoryProviderRegistry *registry, const QgsHistoryWidgetContext &context, QObject *parent )
8484
: QAbstractItemModel( parent )
85+
, mContext( context )
8586
, mRegistry( registry ? registry : QgsGui::historyProviderRegistry() )
8687
, mProviderId( providerId )
8788
, mBackends( backends )
@@ -96,7 +97,7 @@ QgsHistoryEntryModel::QgsHistoryEntryModel( const QString &providerId, Qgis::His
9697
if ( !provider )
9798
continue;
9899

99-
if ( QgsHistoryEntryNode *node = provider->createNodeForEntry( entry.entry ) )
100+
if ( QgsHistoryEntryNode *node = provider->createNodeForEntry( entry.entry, mContext ) )
100101
{
101102
mIdToNodeHash.insert( entry.id, node );
102103
mRootNode->addEntryNode( entry, node, nullptr );
@@ -202,7 +203,7 @@ void QgsHistoryEntryModel::entryAdded( long long id, const QgsHistoryEntry &entr
202203
if ( !provider )
203204
return;
204205

205-
if ( QgsHistoryEntryNode *node = provider->createNodeForEntry( entry.entry ) )
206+
if ( QgsHistoryEntryNode *node = provider->createNodeForEntry( entry.entry, mContext ) )
206207
{
207208
mIdToNodeHash.insert( id, node );
208209
mRootNode->addEntryNode( entry, node, this );
@@ -228,7 +229,7 @@ void QgsHistoryEntryModel::entryUpdated( long long id, const QVariantMap &entry,
228229

229230
const QModelIndex nodeIndex = node2index( node );
230231
const int existingChildRows = node->childCount();
231-
provider->updateNodeForEntry( node, historyEntry );
232+
provider->updateNodeForEntry( node, historyEntry, mContext );
232233
const int newChildRows = node->childCount();
233234

234235
if ( newChildRows < existingChildRows )

‎src/gui/history/qgshistoryentrymodel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgis_gui.h"
2020
#include "qgis_sip.h"
2121
#include "qgis.h"
22+
#include "qgshistorywidgetcontext.h"
2223
#include <QAbstractItemModel>
2324

2425
class QWidget;
@@ -57,6 +58,7 @@ class GUI_EXPORT QgsHistoryEntryModel : public QAbstractItemModel
5758
QgsHistoryEntryModel( const QString &providerId = QString(),
5859
Qgis::HistoryProviderBackends backends = Qgis::HistoryProviderBackend::LocalProfile,
5960
QgsHistoryProviderRegistry *registry = nullptr,
61+
const QgsHistoryWidgetContext &context = QgsHistoryWidgetContext(),
6062
QObject *parent SIP_TRANSFERTHIS = nullptr );
6163

6264
~QgsHistoryEntryModel() override;
@@ -86,6 +88,8 @@ class GUI_EXPORT QgsHistoryEntryModel : public QAbstractItemModel
8688
QModelIndex node2index( QgsHistoryEntryNode *node ) const;
8789
QModelIndex indexOfParentNode( QgsHistoryEntryNode *parentNode ) const;
8890

91+
QgsHistoryWidgetContext mContext;
92+
8993
std::unique_ptr< QgsHistoryEntryRootNode > mRootNode;
9094
QgsHistoryProviderRegistry *mRegistry = nullptr;
9195
QString mProviderId;

‎src/gui/history/qgshistoryentrynode.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ int QgsHistoryEntryNode::childCount() const
2626
return 0;
2727
}
2828

29-
QString QgsHistoryEntryNode::html() const
29+
QString QgsHistoryEntryNode::html( const QgsHistoryWidgetContext & ) const
3030
{
3131
return QString();
3232
}
3333

34-
QWidget *QgsHistoryEntryNode::createWidget()
34+
QWidget *QgsHistoryEntryNode::createWidget( const QgsHistoryWidgetContext & )
3535
{
3636
return nullptr;
3737
}

‎src/gui/history/qgshistoryentrynode.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
class QWidget;
2828
class QAction;
2929
class QgsHistoryEntryGroup;
30+
class QgsHistoryWidgetContext;
3031

3132
/**
3233
* Base class for nodes representing a QgsHistoryEntry.
@@ -72,15 +73,15 @@ class GUI_EXPORT QgsHistoryEntryNode
7273
*
7374
* Subclasses should implement this method or createWidget(), but not both.
7475
*/
75-
virtual QString html() const;
76+
virtual QString html( const QgsHistoryWidgetContext &context ) const;
7677

7778
/**
7879
* Returns a new widget which should be shown to users when selecting the node.
7980
*
8081
* If a NULLPTR is returned, the node's html() method will be called instead to
8182
* create the node's content.
8283
*/
83-
virtual QWidget *createWidget() SIP_FACTORY;
84+
virtual QWidget *createWidget( const QgsHistoryWidgetContext &context ) SIP_FACTORY;
8485

8586
#if 0 // currently unused
8687

@@ -91,7 +92,7 @@ class GUI_EXPORT QgsHistoryEntryNode
9192
*
9293
* Actions should be parented to the specified \a parent widget.
9394
*/
94-
virtual QList< QAction * > actions( QWidget *parent );
95+
virtual QList< QAction * > actions( const QgsHistoryWidgetContext &context, QWidget *parent );
9596

9697
#endif
9798

‎src/gui/history/qgshistoryprovider.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
QgsAbstractHistoryProvider::~QgsAbstractHistoryProvider() = default;
2020

21-
QgsHistoryEntryNode *QgsAbstractHistoryProvider::createNodeForEntry( const QgsHistoryEntry & )
21+
QgsHistoryEntryNode *QgsAbstractHistoryProvider::createNodeForEntry( const QgsHistoryEntry &, const QgsHistoryWidgetContext & )
2222
{
2323
return nullptr;
2424
}
2525

26-
void QgsAbstractHistoryProvider::updateNodeForEntry( QgsHistoryEntryNode *, const QgsHistoryEntry & )
26+
void QgsAbstractHistoryProvider::updateNodeForEntry( QgsHistoryEntryNode *, const QgsHistoryEntry &, const QgsHistoryWidgetContext & )
2727
{
2828

2929
}

‎src/gui/history/qgshistoryprovider.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
class QgsHistoryEntryNode;
2525
class QgsHistoryEntry;
26+
class QgsHistoryWidgetContext;
2627

2728
/**
2829
* Abstract base class for objects which track user history (i.e. operations performed through the GUI).
@@ -57,14 +58,14 @@ class GUI_EXPORT QgsAbstractHistoryProvider
5758
*
5859
* \since QGIS 3.32
5960
*/
60-
virtual QgsHistoryEntryNode *createNodeForEntry( const QgsHistoryEntry &entry ) SIP_FACTORY;
61+
virtual QgsHistoryEntryNode *createNodeForEntry( const QgsHistoryEntry &entry, const QgsHistoryWidgetContext &context ) SIP_FACTORY;
6162

6263
/**
6364
* Updates an existing history \a node for the given \a entry.
6465
*
6566
* \since QGIS 3.32
6667
*/
67-
virtual void updateNodeForEntry( QgsHistoryEntryNode *node, const QgsHistoryEntry &entry );
68+
virtual void updateNodeForEntry( QgsHistoryEntryNode *node, const QgsHistoryEntry &entry, const QgsHistoryWidgetContext &context );
6869

6970
};
7071

‎src/gui/history/qgshistorywidget.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020

2121
#include <QTextBrowser>
2222

23-
QgsHistoryWidget::QgsHistoryWidget( const QString &providerId, Qgis::HistoryProviderBackends backends, QgsHistoryProviderRegistry *registry, QWidget *parent )
23+
QgsHistoryWidget::QgsHistoryWidget( const QString &providerId, Qgis::HistoryProviderBackends backends, QgsHistoryProviderRegistry *registry, const QgsHistoryWidgetContext &context, QWidget *parent )
2424
: QgsPanelWidget( parent )
25+
, mContext( context )
2526
{
2627
setupUi( this );
2728

28-
mModel = new QgsHistoryEntryModel( providerId, backends, registry, this );
29+
mModel = new QgsHistoryEntryModel( providerId, backends, registry, mContext, this );
2930
mProxyModel = new QgsHistoryEntryProxyModel( this );
3031
mProxyModel->setSourceModel( mModel );
3132

@@ -42,10 +43,10 @@ void QgsHistoryWidget::currentItemChanged( const QModelIndex &selected, const QM
4243
QWidget *newWidget = nullptr;
4344
if ( QgsHistoryEntryNode *node = mModel->index2node( mProxyModel->mapToSource( selected ) ) )
4445
{
45-
newWidget = node->createWidget();
46+
newWidget = node->createWidget( mContext );
4647
if ( !newWidget )
4748
{
48-
const QString html = node->html();
49+
const QString html = node->html( mContext );
4950
if ( !html.isEmpty() )
5051
{
5152
QTextBrowser *htmlBrowser = new QTextBrowser();

‎src/gui/history/qgshistorywidget.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
#include "qgis_gui.h"
2121
#include "ui_qgshistorywidgetbase.h"
2222
#include "qgspanelwidget.h"
23+
#include "qgshistorywidgetcontext.h"
2324

2425
#include <QSortFilterProxyModel>
2526

2627
class QgsHistoryProviderRegistry;
2728
class QgsHistoryEntryModel;
29+
class QgsMessageBar;
2830

2931
#ifndef SIP_RUN
3032

@@ -71,6 +73,7 @@ class GUI_EXPORT QgsHistoryWidget : public QgsPanelWidget, private Ui::QgsHistor
7173
QgsHistoryWidget( const QString &providerId = QString(),
7274
Qgis::HistoryProviderBackends backends = Qgis::HistoryProviderBackend::LocalProfile,
7375
QgsHistoryProviderRegistry *registry = nullptr,
76+
const QgsHistoryWidgetContext &context = QgsHistoryWidgetContext(),
7477
QWidget *parent = nullptr );
7578

7679
private slots:
@@ -81,6 +84,7 @@ class GUI_EXPORT QgsHistoryWidget : public QgsPanelWidget, private Ui::QgsHistor
8184

8285
QgsHistoryEntryModel *mModel = nullptr;
8386
QgsHistoryEntryProxyModel *mProxyModel = nullptr;
87+
QgsHistoryWidgetContext mContext;
8488

8589
};
8690

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/***************************************************************************
2+
qgshistorywidgetcontext.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+
16+
#include "qgshistorywidgetcontext.h"
17+
18+
void QgsHistoryWidgetContext::setMessageBar( QgsMessageBar *bar )
19+
{
20+
mMessageBar = bar;
21+
}
22+
23+
QgsMessageBar *QgsHistoryWidgetContext::messageBar() const
24+
{
25+
return mMessageBar;
26+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/***************************************************************************
2+
qgshistorywidgetcontext.h
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+
16+
#ifndef QGSHISTORYWIDGETCONTEXT_H
17+
#define QGSHISTORYWIDGETCONTEXT_H
18+
19+
#include "qgis.h"
20+
#include "qgis_gui.h"
21+
22+
class QgsMessageBar;
23+
24+
/**
25+
* \ingroup gui
26+
* \class QgsHistoryWidgetContext
27+
* \brief Contains settings which reflect the context in which a history widget is shown, e.g., an associated message bar.
28+
*
29+
* \since QGIS 3.32
30+
*/
31+
class GUI_EXPORT QgsHistoryWidgetContext
32+
{
33+
public:
34+
35+
/**
36+
* Constructor for QgsHistoryWidgetContext.
37+
*/
38+
QgsHistoryWidgetContext() = default;
39+
40+
/**
41+
* Sets the message \a bar associated with the widget. This allows the widget to push feedback messages
42+
* to the appropriate message bar.
43+
* \see messageBar()
44+
*/
45+
void setMessageBar( QgsMessageBar *bar );
46+
47+
/**
48+
* Returns the message bar associated with the widget.
49+
* \see setMessageBar()
50+
*/
51+
QgsMessageBar *messageBar() const;
52+
53+
private:
54+
55+
QgsMessageBar *mMessageBar = nullptr;
56+
57+
};
58+
59+
#endif // QGSHISTORYWIDGETCONTEXT_H

‎tests/src/python/test_qgshistoryproviderregistry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ class TestHistoryProvider(QgsAbstractHistoryProvider):
4949
def id(self) -> str:
5050
return 'test_provider'
5151

52-
def createNodeForEntry(self, entry):
52+
def createNodeForEntry(self, entry, context):
5353
return TestEntryNode(entry.entry)
5454

55-
def updateNodeForEntry(self, node, entry):
55+
def updateNodeForEntry(self, node, entry, context):
5656
node.val = entry.entry
5757

5858
new_child_node = TestEntryNode('my child')

0 commit comments

Comments
 (0)
Please sign in to comment.