24#include <QDesktopServices>
27#include <QTextBrowser>
30#include "moc_qgshistorywidget.cpp"
37 mContext.setHistoryWidget(
this );
40 mProxyModel =
new QgsHistoryEntryProxyModel(
this );
41 mProxyModel->setSourceModel( mModel );
43 mTreeView->setModel( mProxyModel );
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 );
52 mTreeView->setContextMenuPolicy( Qt::CustomContextMenu );
53 connect( mTreeView, &QWidget::customContextMenuRequested,
this, &QgsHistoryWidget::showNodeContextMenu );
56 const QModelIndex firstGroup = mProxyModel->index( 0, 0, QModelIndex() );
57 mTreeView->expand( firstGroup );
60 mSplitter->restoreState( settings.
value( QStringLiteral(
"history/splitterState%1" ).arg( providerId ) ).toByteArray() );
62 connect( mSplitter, &QSplitter::splitterMoved,
this, [providerId,
this] {
64 settings.
setValue( QStringLiteral(
"history/splitterState%1" ).arg( providerId ), mSplitter->saveState() );
68void QgsHistoryWidget::currentItemChanged(
const QModelIndex &selected,
const QModelIndex & )
70 QWidget *newWidget =
nullptr;
73 newWidget = node->createWidget( mContext );
76 const QString html = node->html( mContext );
77 if ( !html.isEmpty() )
79 QTextBrowser *htmlBrowser =
new QTextBrowser();
80 htmlBrowser->setOpenLinks(
false );
81 htmlBrowser->setHtml( html );
82 connect( htmlBrowser, &QTextBrowser::anchorClicked,
this, &QgsHistoryWidget::urlClicked );
84 newWidget = htmlBrowser;
89 mContainerStackedWidget->addWidget( newWidget );
90 mContainerStackedWidget->setCurrentWidget( newWidget );
97 if ( mContainerStackedWidget->count() > 1 )
99 mContainerStackedWidget->removeWidget( mContainerStackedWidget->widget( 1 ) );
100 mContainerStackedWidget->setCurrentIndex( 0 );
105void QgsHistoryWidget::nodeDoubleClicked(
const QModelIndex &index )
107 if ( QgsHistoryEntryNode *node = mModel->index2node( mProxyModel->mapToSource( index ) ) )
109 if ( node->doubleClicked( mContext ) )
114 if ( mTreeView->isExpanded( index ) )
115 mTreeView->collapse( index );
117 mTreeView->expand( index );
120void QgsHistoryWidget::showNodeContextMenu(
const QPoint &pos )
122 if ( QgsHistoryEntryNode *node = mModel->index2node( mProxyModel->mapToSource( mTreeView->currentIndex() ) ) )
124 QMenu *menu =
new QMenu();
126 node->populateContextMenu( menu, mContext );
127 if ( !menu->isEmpty() )
129 menu->exec( mTreeView->mapToGlobal( pos ) );
135void QgsHistoryWidget::urlClicked(
const QUrl &url )
137 const QFileInfo file( url.toLocalFile() );
138 if ( file.exists() && !file.isDir() )
141 QDesktopServices::openUrl( url );
149QgsHistoryEntryProxyModel::QgsHistoryEntryProxyModel( QObject *parent )
150 : QSortFilterProxyModel( parent )
152 setDynamicSortFilter(
true );
153 setRecursiveFilteringEnabled(
true );
156void QgsHistoryEntryProxyModel::setFilter(
const QString &filter )
158 if ( filter == mFilter )
165bool QgsHistoryEntryProxyModel::filterAcceptsRow(
int source_row,
const QModelIndex &source_parent )
const
167 if ( mFilter.isEmpty() )
170 const QModelIndex sourceIndex = sourceModel()->index( source_row, 0, source_parent );
171 if (
QgsHistoryEntryNode *node = qobject_cast<QgsHistoryEntryModel *>( sourceModel() )->index2node( sourceIndex ) )
173 if ( !node->matchesString( mFilter ) )
QFlags< HistoryProviderBackend > HistoryProviderBackends
static QgsNative * nativePlatformInterface()
Returns the global native interface, which offers abstraction to the host OS's underlying public inte...
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.
Stores settings for use within QGIS.
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.