Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add QgsHistoryWidgetContext for future api flexibility
  • Loading branch information
nyalldawson committed Apr 22, 2023
1 parent f3f0946 commit 538cf4c
Show file tree
Hide file tree
Showing 18 changed files with 178 additions and 23 deletions.
Expand Up @@ -26,6 +26,7 @@ An item model representing history entries in a hierarchical tree structure.
QgsHistoryEntryModel( const QString &providerId = QString(),
Qgis::HistoryProviderBackends backends = Qgis::HistoryProviderBackend::LocalProfile,
QgsHistoryProviderRegistry *registry = 0,
const QgsHistoryWidgetContext &context = QgsHistoryWidgetContext(),
QObject *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsHistoryEntryModel, with the specified ``parent`` object.
Expand Down
4 changes: 2 additions & 2 deletions python/gui/auto_generated/history/qgshistoryentrynode.sip.in
Expand Up @@ -45,15 +45,15 @@ Returns the node's data for the specified model ``role``.
Returns the number of child nodes owned by this node.
%End

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions python/gui/auto_generated/history/qgshistoryprovider.sip.in
Expand Up @@ -37,14 +37,14 @@ Abstract base class for objects which track user history (i.e. operations perfor
Returns the provider's unique id, which is used to associate existing history entries with the provider.
%End

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

.. versionadded:: 3.32
%End

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

Expand Down
1 change: 1 addition & 0 deletions python/gui/auto_generated/history/qgshistorywidget.sip.in
Expand Up @@ -27,6 +27,7 @@ A widget showing entries from a :py:class:`QgsHistoryProviderRegistry`.
QgsHistoryWidget( const QString &providerId = QString(),
Qgis::HistoryProviderBackends backends = Qgis::HistoryProviderBackend::LocalProfile,
QgsHistoryProviderRegistry *registry = 0,
const QgsHistoryWidgetContext &context = QgsHistoryWidgetContext(),
QWidget *parent = 0 );
%Docstring
Constructor for QgsHistoryWidget, with the specified ``parent`` widget.
Expand Down
53 changes: 53 additions & 0 deletions python/gui/auto_generated/history/qgshistorywidgetcontext.sip.in
@@ -0,0 +1,53 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/history/qgshistorywidgetcontext.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsHistoryWidgetContext
{
%Docstring(signature="appended")
Contains settings which reflect the context in which a history widget is shown, e.g., an associated message bar.

.. versionadded:: 3.32
%End

%TypeHeaderCode
#include "qgshistorywidgetcontext.h"
%End
public:

QgsHistoryWidgetContext();
%Docstring
Constructor for QgsHistoryWidgetContext.
%End

void setMessageBar( QgsMessageBar *bar );
%Docstring
Sets the message ``bar`` associated with the widget. This allows the widget to push feedback messages
to the appropriate message bar.

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

QgsMessageBar *messageBar() const;
%Docstring
Returns the message bar associated with the widget.

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

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/history/qgshistorywidgetcontext.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/gui/gui_auto.sip
Expand Up @@ -347,6 +347,7 @@
%Include auto_generated/history/qgshistoryprovider.sip
%Include auto_generated/history/qgshistoryproviderregistry.sip
%Include auto_generated/history/qgshistorywidget.sip
%Include auto_generated/history/qgshistorywidgetcontext.sip
%Include auto_generated/labeling/qgslabellineanchorwidget.sip
%Include auto_generated/labeling/qgslabelobstaclesettingswidget.sip
%Include auto_generated/labeling/qgslabelsettingswidgetbase.sip
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -242,6 +242,7 @@ set(QGIS_GUI_SRCS
history/qgshistoryprovider.cpp
history/qgshistoryproviderregistry.cpp
history/qgshistorywidget.cpp
history/qgshistorywidgetcontext.cpp

labeling/qgslabelengineconfigdialog.cpp
labeling/qgslabelinggui.cpp
Expand Down Expand Up @@ -1152,6 +1153,7 @@ set(QGIS_GUI_HDRS
history/qgshistoryprovider.h
history/qgshistoryproviderregistry.h
history/qgshistorywidget.h
history/qgshistorywidgetcontext.h

labeling/qgslabelengineconfigdialog.h
labeling/qgslabelinggui.h
Expand Down
9 changes: 5 additions & 4 deletions src/gui/history/qgshistoryentrymodel.cpp
Expand Up @@ -80,8 +80,9 @@ class QgsHistoryEntryDateGroupNode : public QgsHistoryEntryGroup
};
///@endcond

QgsHistoryEntryModel::QgsHistoryEntryModel( const QString &providerId, Qgis::HistoryProviderBackends backends, QgsHistoryProviderRegistry *registry, QObject *parent )
QgsHistoryEntryModel::QgsHistoryEntryModel( const QString &providerId, Qgis::HistoryProviderBackends backends, QgsHistoryProviderRegistry *registry, const QgsHistoryWidgetContext &context, QObject *parent )
: QAbstractItemModel( parent )
, mContext( context )
, mRegistry( registry ? registry : QgsGui::historyProviderRegistry() )
, mProviderId( providerId )
, mBackends( backends )
Expand All @@ -96,7 +97,7 @@ QgsHistoryEntryModel::QgsHistoryEntryModel( const QString &providerId, Qgis::His
if ( !provider )
continue;

if ( QgsHistoryEntryNode *node = provider->createNodeForEntry( entry.entry ) )
if ( QgsHistoryEntryNode *node = provider->createNodeForEntry( entry.entry, mContext ) )
{
mIdToNodeHash.insert( entry.id, node );
mRootNode->addEntryNode( entry, node, nullptr );
Expand Down Expand Up @@ -202,7 +203,7 @@ void QgsHistoryEntryModel::entryAdded( long long id, const QgsHistoryEntry &entr
if ( !provider )
return;

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

const QModelIndex nodeIndex = node2index( node );
const int existingChildRows = node->childCount();
provider->updateNodeForEntry( node, historyEntry );
provider->updateNodeForEntry( node, historyEntry, mContext );
const int newChildRows = node->childCount();

if ( newChildRows < existingChildRows )
Expand Down
4 changes: 4 additions & 0 deletions src/gui/history/qgshistoryentrymodel.h
Expand Up @@ -19,6 +19,7 @@
#include "qgis_gui.h"
#include "qgis_sip.h"
#include "qgis.h"
#include "qgshistorywidgetcontext.h"
#include <QAbstractItemModel>

class QWidget;
Expand Down Expand Up @@ -57,6 +58,7 @@ class GUI_EXPORT QgsHistoryEntryModel : public QAbstractItemModel
QgsHistoryEntryModel( const QString &providerId = QString(),
Qgis::HistoryProviderBackends backends = Qgis::HistoryProviderBackend::LocalProfile,
QgsHistoryProviderRegistry *registry = nullptr,
const QgsHistoryWidgetContext &context = QgsHistoryWidgetContext(),
QObject *parent SIP_TRANSFERTHIS = nullptr );

~QgsHistoryEntryModel() override;
Expand Down Expand Up @@ -86,6 +88,8 @@ class GUI_EXPORT QgsHistoryEntryModel : public QAbstractItemModel
QModelIndex node2index( QgsHistoryEntryNode *node ) const;
QModelIndex indexOfParentNode( QgsHistoryEntryNode *parentNode ) const;

QgsHistoryWidgetContext mContext;

std::unique_ptr< QgsHistoryEntryRootNode > mRootNode;
QgsHistoryProviderRegistry *mRegistry = nullptr;
QString mProviderId;
Expand Down
4 changes: 2 additions & 2 deletions src/gui/history/qgshistoryentrynode.cpp
Expand Up @@ -26,12 +26,12 @@ int QgsHistoryEntryNode::childCount() const
return 0;
}

QString QgsHistoryEntryNode::html() const
QString QgsHistoryEntryNode::html( const QgsHistoryWidgetContext & ) const
{
return QString();
}

QWidget *QgsHistoryEntryNode::createWidget()
QWidget *QgsHistoryEntryNode::createWidget( const QgsHistoryWidgetContext & )
{
return nullptr;
}
Expand Down
7 changes: 4 additions & 3 deletions src/gui/history/qgshistoryentrynode.h
Expand Up @@ -27,6 +27,7 @@
class QWidget;
class QAction;
class QgsHistoryEntryGroup;
class QgsHistoryWidgetContext;

/**
* Base class for nodes representing a QgsHistoryEntry.
Expand Down Expand Up @@ -72,15 +73,15 @@ class GUI_EXPORT QgsHistoryEntryNode
*
* Subclasses should implement this method or createWidget(), but not both.
*/
virtual QString html() const;
virtual QString html( const QgsHistoryWidgetContext &context ) const;

/**
* Returns a new widget which should be shown to users when selecting the node.
*
* If a NULLPTR is returned, the node's html() method will be called instead to
* create the node's content.
*/
virtual QWidget *createWidget() SIP_FACTORY;
virtual QWidget *createWidget( const QgsHistoryWidgetContext &context ) SIP_FACTORY;

#if 0 // currently unused

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

#endif

Expand Down
4 changes: 2 additions & 2 deletions src/gui/history/qgshistoryprovider.cpp
Expand Up @@ -18,12 +18,12 @@

QgsAbstractHistoryProvider::~QgsAbstractHistoryProvider() = default;

QgsHistoryEntryNode *QgsAbstractHistoryProvider::createNodeForEntry( const QgsHistoryEntry & )
QgsHistoryEntryNode *QgsAbstractHistoryProvider::createNodeForEntry( const QgsHistoryEntry &, const QgsHistoryWidgetContext & )
{
return nullptr;
}

void QgsAbstractHistoryProvider::updateNodeForEntry( QgsHistoryEntryNode *, const QgsHistoryEntry & )
void QgsAbstractHistoryProvider::updateNodeForEntry( QgsHistoryEntryNode *, const QgsHistoryEntry &, const QgsHistoryWidgetContext & )
{

}
5 changes: 3 additions & 2 deletions src/gui/history/qgshistoryprovider.h
Expand Up @@ -23,6 +23,7 @@

class QgsHistoryEntryNode;
class QgsHistoryEntry;
class QgsHistoryWidgetContext;

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

/**
* Updates an existing history \a node for the given \a entry.
*
* \since QGIS 3.32
*/
virtual void updateNodeForEntry( QgsHistoryEntryNode *node, const QgsHistoryEntry &entry );
virtual void updateNodeForEntry( QgsHistoryEntryNode *node, const QgsHistoryEntry &entry, const QgsHistoryWidgetContext &context );

};

Expand Down
9 changes: 5 additions & 4 deletions src/gui/history/qgshistorywidget.cpp
Expand Up @@ -20,12 +20,13 @@

#include <QTextBrowser>

QgsHistoryWidget::QgsHistoryWidget( const QString &providerId, Qgis::HistoryProviderBackends backends, QgsHistoryProviderRegistry *registry, QWidget *parent )
QgsHistoryWidget::QgsHistoryWidget( const QString &providerId, Qgis::HistoryProviderBackends backends, QgsHistoryProviderRegistry *registry, const QgsHistoryWidgetContext &context, QWidget *parent )
: QgsPanelWidget( parent )
, mContext( context )
{
setupUi( this );

mModel = new QgsHistoryEntryModel( providerId, backends, registry, this );
mModel = new QgsHistoryEntryModel( providerId, backends, registry, mContext, this );
mProxyModel = new QgsHistoryEntryProxyModel( this );
mProxyModel->setSourceModel( mModel );

Expand All @@ -42,10 +43,10 @@ void QgsHistoryWidget::currentItemChanged( const QModelIndex &selected, const QM
QWidget *newWidget = nullptr;
if ( QgsHistoryEntryNode *node = mModel->index2node( mProxyModel->mapToSource( selected ) ) )
{
newWidget = node->createWidget();
newWidget = node->createWidget( mContext );
if ( !newWidget )
{
const QString html = node->html();
const QString html = node->html( mContext );
if ( !html.isEmpty() )
{
QTextBrowser *htmlBrowser = new QTextBrowser();
Expand Down
4 changes: 4 additions & 0 deletions src/gui/history/qgshistorywidget.h
Expand Up @@ -20,11 +20,13 @@
#include "qgis_gui.h"
#include "ui_qgshistorywidgetbase.h"
#include "qgspanelwidget.h"
#include "qgshistorywidgetcontext.h"

#include <QSortFilterProxyModel>

class QgsHistoryProviderRegistry;
class QgsHistoryEntryModel;
class QgsMessageBar;

#ifndef SIP_RUN

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

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

QgsHistoryEntryModel *mModel = nullptr;
QgsHistoryEntryProxyModel *mProxyModel = nullptr;
QgsHistoryWidgetContext mContext;

};

Expand Down
26 changes: 26 additions & 0 deletions src/gui/history/qgshistorywidgetcontext.cpp
@@ -0,0 +1,26 @@
/***************************************************************************
qgshistorywidgetcontext.cpp
------------------
Date : April 2023
Copyright : (C) 2023 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 "qgshistorywidgetcontext.h"

void QgsHistoryWidgetContext::setMessageBar( QgsMessageBar *bar )
{
mMessageBar = bar;
}

QgsMessageBar *QgsHistoryWidgetContext::messageBar() const
{
return mMessageBar;
}

0 comments on commit 538cf4c

Please sign in to comment.