24#include <QDesktopServices>
28#include <QTextBrowser>
31#include "moc_qgshistorywidget.cpp"
33using namespace Qt::StringLiterals;
40 mContext.setHistoryWidget(
this );
43 mProxyModel =
new QgsHistoryEntryProxyModel(
this );
44 mProxyModel->setSourceModel( mModel );
46 mTreeView->setModel( mProxyModel );
48 mFilterEdit->setShowClearButton(
true );
49 mFilterEdit->setShowSearchIcon(
true );
50 connect( mFilterEdit, &QLineEdit::textChanged, mProxyModel, &QgsHistoryEntryProxyModel::setFilter );
51 connect( mTreeView->selectionModel(), &QItemSelectionModel::currentChanged,
this, &QgsHistoryWidget::currentItemChanged );
52 connect( mTreeView, &QTreeView::doubleClicked,
this, &QgsHistoryWidget::nodeDoubleClicked );
53 mTreeView->setExpandsOnDoubleClick(
false );
55 mTreeView->setContextMenuPolicy( Qt::CustomContextMenu );
56 connect( mTreeView, &QWidget::customContextMenuRequested,
this, &QgsHistoryWidget::showNodeContextMenu );
59 const QModelIndex firstGroup = mProxyModel->index( 0, 0, QModelIndex() );
60 mTreeView->expand( firstGroup );
63 mSplitter->restoreState( settings.
value( u
"history/splitterState%1"_s.arg( providerId ) ).toByteArray() );
65 connect( mSplitter, &QSplitter::splitterMoved,
this, [providerId,
this] {
67 settings.
setValue( u
"history/splitterState%1"_s.arg( providerId ), mSplitter->saveState() );
71void QgsHistoryWidget::currentItemChanged(
const QModelIndex &selected,
const QModelIndex & )
73 QWidget *newWidget =
nullptr;
76 newWidget = node->createWidget( mContext );
79 const QString html = node->html( mContext );
80 if ( !html.isEmpty() )
82 QTextBrowser *htmlBrowser =
new QTextBrowser();
83 htmlBrowser->setOpenLinks(
false );
84 htmlBrowser->setHtml( html );
85 connect( htmlBrowser, &QTextBrowser::anchorClicked,
this, &QgsHistoryWidget::urlClicked );
87 newWidget = htmlBrowser;
92 mContainerStackedWidget->addWidget( newWidget );
93 mContainerStackedWidget->setCurrentWidget( newWidget );
100 if ( mContainerStackedWidget->count() > 1 )
102 mContainerStackedWidget->removeWidget( mContainerStackedWidget->widget( 1 ) );
103 mContainerStackedWidget->setCurrentIndex( 0 );
108void QgsHistoryWidget::nodeDoubleClicked(
const QModelIndex &index )
110 if ( QgsHistoryEntryNode *node = mModel->index2node( mProxyModel->mapToSource( index ) ) )
112 if ( node->doubleClicked( mContext ) )
117 if ( mTreeView->isExpanded( index ) )
118 mTreeView->collapse( index );
120 mTreeView->expand( index );
123void QgsHistoryWidget::showNodeContextMenu(
const QPoint &pos )
125 if ( QgsHistoryEntryNode *node = mModel->index2node( mProxyModel->mapToSource( mTreeView->currentIndex() ) ) )
127 QMenu *menu =
new QMenu();
129 node->populateContextMenu( menu, mContext );
130 if ( !menu->isEmpty() )
132 menu->exec( mTreeView->mapToGlobal( pos ) );
138void QgsHistoryWidget::urlClicked(
const QUrl &url )
140 const QFileInfo file( url.toLocalFile() );
141 if ( file.exists() && !file.isDir() )
144 QDesktopServices::openUrl( url );
152QgsHistoryEntryProxyModel::QgsHistoryEntryProxyModel( QObject *parent )
153 : QSortFilterProxyModel( parent )
155 setDynamicSortFilter(
true );
156 setRecursiveFilteringEnabled(
true );
159void QgsHistoryEntryProxyModel::setFilter(
const QString &filter )
161 if ( filter == mFilter )
168bool QgsHistoryEntryProxyModel::filterAcceptsRow(
int source_row,
const QModelIndex &source_parent )
const
170 if ( mFilter.isEmpty() )
173 const QModelIndex sourceIndex = sourceModel()->index( source_row, 0, source_parent );
174 if (
QgsHistoryEntryNode *node = qobject_cast<QgsHistoryEntryModel *>( sourceModel() )->index2node( sourceIndex ) )
176 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.