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