QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgshistorywidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgshistorywidget.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 "qgshistorywidget.h"
17#include "moc_qgshistorywidget.cpp"
18#include "qgsgui.h"
20#include "qgshistoryentrynode.h"
21#include "qgssettings.h"
22#include "qgsnative.h"
23
24#include <QTextBrowser>
25#include <QtGlobal>
26#include <QMenu>
27#include <QFileInfo>
28#include <QDesktopServices>
29
30QgsHistoryWidget::QgsHistoryWidget( const QString &providerId, Qgis::HistoryProviderBackends backends, QgsHistoryProviderRegistry *registry, const QgsHistoryWidgetContext &context, QWidget *parent )
31 : QgsPanelWidget( parent )
32 , mContext( context )
33{
34 setupUi( this );
35
36 mModel = new QgsHistoryEntryModel( providerId, backends, registry, mContext, this );
37 mProxyModel = new QgsHistoryEntryProxyModel( this );
38 mProxyModel->setSourceModel( mModel );
39
40 mTreeView->setModel( mProxyModel );
41
42 mFilterEdit->setShowClearButton( true );
43 mFilterEdit->setShowSearchIcon( true );
44 connect( mFilterEdit, &QLineEdit::textChanged, mProxyModel, &QgsHistoryEntryProxyModel::setFilter );
45 connect( mTreeView->selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsHistoryWidget::currentItemChanged );
46 connect( mTreeView, &QTreeView::doubleClicked, this, &QgsHistoryWidget::nodeDoubleClicked );
47 mTreeView->setExpandsOnDoubleClick( false );
48
49 mTreeView->setContextMenuPolicy( Qt::CustomContextMenu );
50 connect( mTreeView, &QWidget::customContextMenuRequested, this, &QgsHistoryWidget::showNodeContextMenu );
51
52 // expand first group (usually most recent date group)
53 const QModelIndex firstGroup = mProxyModel->index( 0, 0, QModelIndex() );
54 mTreeView->expand( firstGroup );
55
56 QgsSettings settings;
57 mSplitter->restoreState( settings.value( QStringLiteral( "history/splitterState%1" ).arg( providerId ) ).toByteArray() );
58
59 connect( mSplitter, &QSplitter::splitterMoved, this, [providerId, this]
60 {
61 QgsSettings settings;
62 settings.setValue( QStringLiteral( "history/splitterState%1" ).arg( providerId ), mSplitter->saveState() );
63 } );
64}
65
66void QgsHistoryWidget::currentItemChanged( const QModelIndex &selected, const QModelIndex & )
67{
68 QWidget *newWidget = nullptr;
69 if ( QgsHistoryEntryNode *node = mModel->index2node( mProxyModel->mapToSource( selected ) ) )
70 {
71 newWidget = node->createWidget( mContext );
72 if ( !newWidget )
73 {
74 const QString html = node->html( mContext );
75 if ( !html.isEmpty() )
76 {
77 QTextBrowser *htmlBrowser = new QTextBrowser();
78 htmlBrowser->setOpenLinks( false );
79 htmlBrowser->setHtml( html );
80 connect( htmlBrowser, &QTextBrowser::anchorClicked, this, &QgsHistoryWidget::urlClicked );
81
82 newWidget = htmlBrowser;
83 }
84 }
85 if ( newWidget )
86 {
87 mContainerStackedWidget->addWidget( newWidget );
88 mContainerStackedWidget->setCurrentWidget( newWidget );
89 }
90 }
91
92 if ( !newWidget )
93 {
94 //remove current widget, if any
95 if ( mContainerStackedWidget->count() > 1 )
96 {
97 mContainerStackedWidget->removeWidget( mContainerStackedWidget->widget( 1 ) );
98 mContainerStackedWidget->setCurrentIndex( 0 );
99 }
100 }
101}
102
103void QgsHistoryWidget::nodeDoubleClicked( const QModelIndex &index )
104{
105 if ( QgsHistoryEntryNode *node = mModel->index2node( mProxyModel->mapToSource( index ) ) )
106 {
107 if ( node->doubleClicked( mContext ) )
108 return; // double-click handled
109 }
110
111 // otherwise double-clicks expands/collapses the node
112 if ( mTreeView->isExpanded( index ) )
113 mTreeView->collapse( index );
114 else
115 mTreeView->expand( index );
116}
117
118void QgsHistoryWidget::showNodeContextMenu( const QPoint &pos )
119{
120 if ( QgsHistoryEntryNode *node = mModel->index2node( mProxyModel->mapToSource( mTreeView->currentIndex() ) ) )
121 {
122 QMenu *menu = new QMenu();
123
124 node->populateContextMenu( menu, mContext );
125 if ( !menu->isEmpty() )
126 {
127 menu->exec( mTreeView->mapToGlobal( pos ) );
128 }
129 delete menu;
130 }
131}
132
133void QgsHistoryWidget::urlClicked( const QUrl &url )
134{
135 const QFileInfo file( url.toLocalFile() );
136 if ( file.exists() && !file.isDir() )
137 QgsGui::nativePlatformInterface()->openFileExplorerAndSelectFile( url.toLocalFile() );
138 else
139 QDesktopServices::openUrl( url );
140}
141
142//
143// QgsHistoryEntryProxyModel
144//
145
147QgsHistoryEntryProxyModel::QgsHistoryEntryProxyModel( QObject *parent )
148 : QSortFilterProxyModel( parent )
149{
150 setDynamicSortFilter( true );
151 setRecursiveFilteringEnabled( true );
152}
153
154void QgsHistoryEntryProxyModel::setFilter( const QString &filter )
155{
156 if ( filter == mFilter )
157 return;
158
159 mFilter = filter;
160 invalidateFilter();
161}
162
163bool QgsHistoryEntryProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
164{
165 if ( mFilter.isEmpty() )
166 return true;
167
168 const QModelIndex sourceIndex = sourceModel()->index( source_row, 0, source_parent );
169 if ( QgsHistoryEntryNode *node = qobject_cast< QgsHistoryEntryModel * >( sourceModel() )->index2node( sourceIndex ) )
170 {
171 if ( !node->matchesString( mFilter ) )
172 {
173 return false;
174 }
175 }
176 return true;
177}
QFlags< HistoryProviderBackend > HistoryProviderBackends
Definition qgis.h:3259
static QgsNative * nativePlatformInterface()
Returns the global native interface, which offers abstraction to the host OS's underlying public inte...
Definition qgsgui.cpp:84
An item model representing history entries in a hierarchical tree structure.
QgsHistoryEntryNode * index2node(const QModelIndex &index) const
Returns node for given index.
Base class for nodes representing a QgsHistoryEntry.
The QgsHistoryProviderRegistry is a registry for objects which track user history (i....
Contains settings which reflect the context in which a history widget is shown, e....
QgsHistoryWidget(const QString &providerId=QString(), Qgis::HistoryProviderBackends backends=Qgis::HistoryProviderBackend::LocalProfile, QgsHistoryProviderRegistry *registry=nullptr, const QgsHistoryWidgetContext &context=QgsHistoryWidgetContext(), QWidget *parent=nullptr)
Constructor for QgsHistoryWidget, with the specified parent widget.
Base class for any widget that can be shown as a inline panel.
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.