Skip to content

Commit

Permalink
Add capacity for history nodes to show custom widgets when selected
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 22, 2023
1 parent 34ce4d7 commit 167d4e1
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
16 changes: 16 additions & 0 deletions python/gui/auto_generated/history/qgshistoryentrynode.sip.in
Expand Up @@ -45,6 +45,22 @@ 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;
%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/;
%Docstring
Returns a new widget which should be shown to users when selecting the node.

If a ``None`` is returned, the node's :py:func:`~QgsHistoryEntryNode.html` method will be called instead to
create the node's content.
%End


private:
QgsHistoryEntryNode( const QgsHistoryEntryNode &other );
Expand Down
2 changes: 1 addition & 1 deletion src/gui/history/qgshistoryentrynode.cpp
Expand Up @@ -26,7 +26,6 @@ int QgsHistoryEntryNode::childCount() const
return 0;
}

#if 0
QString QgsHistoryEntryNode::html() const
{
return QString();
Expand All @@ -37,6 +36,7 @@ QWidget *QgsHistoryEntryNode::createWidget()
return nullptr;
}

#if 0
QList<QAction *> QgsHistoryEntryNode::actions( QWidget * )
{
return {};
Expand Down
4 changes: 2 additions & 2 deletions src/gui/history/qgshistoryentrynode.h
Expand Up @@ -66,8 +66,6 @@ class GUI_EXPORT QgsHistoryEntryNode
*/
virtual int childCount() const;

#if 0 // currently unused

/**
* Returns a HTML formatted text string which should be shown to a user when
* selecting the node.
Expand All @@ -84,6 +82,8 @@ class GUI_EXPORT QgsHistoryEntryNode
*/
virtual QWidget *createWidget() SIP_FACTORY;

#if 0 // currently unused

/**
* Returns a list of actions which users can trigger to interact with the history
* entry. Buttons corresponding to each action will be automatically created and
Expand Down
27 changes: 27 additions & 0 deletions src/gui/history/qgshistorywidget.cpp
Expand Up @@ -16,6 +16,7 @@
#include "qgshistorywidget.h"
#include "qgsgui.h"
#include "qgshistoryentrymodel.h"
#include "qgshistoryentrynode.h"

QgsHistoryWidget::QgsHistoryWidget( const QString &providerId, Qgis::HistoryProviderBackends backends, QgsHistoryProviderRegistry *registry, QWidget *parent )
: QgsPanelWidget( parent )
Expand All @@ -25,4 +26,30 @@ QgsHistoryWidget::QgsHistoryWidget( const QString &providerId, Qgis::HistoryProv
mModel = new QgsHistoryEntryModel( providerId, backends, registry, this );
mTreeView->setModel( mModel );

connect( mTreeView->selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsHistoryWidget::currentItemChanged );

}

void QgsHistoryWidget::currentItemChanged( const QModelIndex &selected, const QModelIndex & )
{
QWidget *newWidget = nullptr;
if ( QgsHistoryEntryNode *node = mModel->index2node( selected ) )
{
newWidget = node->createWidget();
if ( newWidget )
{
mContainerStackedWidget->addWidget( newWidget );
mContainerStackedWidget->setCurrentWidget( newWidget );
}
}

if ( !newWidget )
{
//remove current widget, if any
if ( mContainerStackedWidget->count() > 1 )
{
mContainerStackedWidget->removeWidget( mContainerStackedWidget->widget( 1 ) );
mContainerStackedWidget->setCurrentIndex( 0 );
}
}
}
3 changes: 3 additions & 0 deletions src/gui/history/qgshistorywidget.h
Expand Up @@ -50,6 +50,9 @@ class GUI_EXPORT QgsHistoryWidget : public QgsPanelWidget, private Ui::QgsHistor
QgsHistoryProviderRegistry *registry = nullptr,
QWidget *parent = nullptr );

private slots:

void currentItemChanged( const QModelIndex &selected, const QModelIndex &previous );

private:

Expand Down
12 changes: 11 additions & 1 deletion src/ui/qgshistorywidgetbase.ui
Expand Up @@ -42,11 +42,21 @@
<bool>false</bool>
</attribute>
</widget>
<widget class="QWidget" name="mEntryContainerWidget" native="true"/>
<widget class="QStackedWidget" name="mContainerStackedWidget">
<widget class="QWidget" name="mEntryContainerStackedWidgetPage1" native="true"/>
</widget>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsPanelWidget</class>
<extends>QWidget</extends>
<header>qgspanelwidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

0 comments on commit 167d4e1

Please sign in to comment.