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