QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsbrowserwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsbrowserwidget.cpp
3 ---------------------
4 begin : July 2011
5 copyright : (C) 2011 by Martin Dobias
6 email : wonder dot sk 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#include "qgsbrowserwidget.h"
16#include "moc_qgsbrowserwidget.cpp"
17
18#include <QAbstractTextDocumentLayout>
19#include <QHeaderView>
20#include <QTreeView>
21#include <QMenu>
22#include <QToolButton>
23#include <QFileDialog>
24#include <QPlainTextDocumentLayout>
25#include <QSortFilterProxyModel>
26#include <QActionGroup>
27
28#include "qgsbrowserguimodel.h"
29#include "qgsbrowsertreeview.h"
30#include "qgslogger.h"
31#include "qgsrasterlayer.h"
32#include "qgsvectorlayer.h"
33#include "qgsproject.h"
34#include "qgssettings.h"
35#include "qgsnewnamedialog.h"
37#include "qgsgui.h"
39#include "qgsnative.h"
42#include "qgsdirectoryitem.h"
43#include "qgslayeritem.h"
44#include "qgsprojectitem.h"
46
47// browser layer properties dialog
48#include "qgsapplication.h"
49#include "qgsmapcanvas.h"
50
51#include <QDragEnterEvent>
52#include <functional>
53
55 : QgsPanelWidget( parent )
56 , mModel( browserModel )
57{
58 setupUi( this );
59
60 layout()->setContentsMargins( 0, 0, 0, 0 );
61 qgis::down_cast<QVBoxLayout *>( layout() )->setSpacing( 0 );
62
63 mBrowserView = new QgsDockBrowserTreeView( this );
64 mLayoutBrowser->addWidget( mBrowserView );
65
66 mWidgetFilter->hide();
67 mLeFilter->setPlaceholderText( tr( "Type here to filter visible items…" ) );
68 // icons from http://www.fatcow.com/free-icons License: CC Attribution 3.0
69
70 QMenu *menu = new QMenu( this );
71 menu->setSeparatorsCollapsible( false );
72 mBtnFilterOptions->setMenu( menu );
73 QAction *action = new QAction( tr( "Case Sensitive" ), menu );
74 action->setData( "case" );
75 action->setCheckable( true );
76 action->setChecked( false );
77 connect( action, &QAction::toggled, this, &QgsBrowserWidget::setCaseSensitive );
78 menu->addAction( action );
79 QActionGroup *group = new QActionGroup( menu );
80 action = new QAction( tr( "Filter Pattern Syntax" ), group );
81 action->setSeparator( true );
82 menu->addAction( action );
83 action = new QAction( tr( "Normal" ), group );
84 action->setData( QgsBrowserProxyModel::Normal );
85 action->setCheckable( true );
86 action->setChecked( true );
87 menu->addAction( action );
88 action = new QAction( tr( "Wildcard(s)" ), group );
89 action->setData( QgsBrowserProxyModel::Wildcards );
90 action->setCheckable( true );
91 menu->addAction( action );
92 action = new QAction( tr( "Regular Expression" ), group );
94 action->setCheckable( true );
95 menu->addAction( action );
96
97 mBrowserView->setExpandsOnDoubleClick( false );
98
99 connect( mActionRefresh, &QAction::triggered, this, &QgsBrowserWidget::refresh );
100 connect( mActionAddLayers, &QAction::triggered, this, &QgsBrowserWidget::addSelectedLayers );
101 connect( mActionCollapse, &QAction::triggered, mBrowserView, &QgsDockBrowserTreeView::collapseAll );
102 connect( mActionShowFilter, &QAction::triggered, this, &QgsBrowserWidget::showFilterWidget );
103 connect( mActionPropertiesWidget, &QAction::triggered, this, &QgsBrowserWidget::propertiesWidgetToggled );
104 connect( mLeFilter, &QgsFilterLineEdit::returnPressed, this, &QgsBrowserWidget::setFilter );
105 connect( mLeFilter, &QgsFilterLineEdit::cleared, this, &QgsBrowserWidget::setFilter );
106 connect( mLeFilter, &QgsFilterLineEdit::textChanged, this, &QgsBrowserWidget::setFilter );
107 connect( group, &QActionGroup::triggered, this, &QgsBrowserWidget::setFilterSyntax );
108 connect( mBrowserView, &QgsDockBrowserTreeView::customContextMenuRequested, this, &QgsBrowserWidget::showContextMenu );
109 connect( mBrowserView, &QgsDockBrowserTreeView::doubleClicked, this, &QgsBrowserWidget::itemDoubleClicked );
110 connect( mSplitter, &QSplitter::splitterMoved, this, &QgsBrowserWidget::splitterMoved );
111
112 connect( QgsGui::instance(), &QgsGui::optionsChanged, this, &QgsBrowserWidget::onOptionsChanged );
113}
114
116
117void QgsBrowserWidget::showEvent( QShowEvent *e )
118{
119 // delayed initialization of the model
120 if ( !mModel->initialized() )
121 {
122 mModel->initialize();
123 }
124 if ( !mProxyModel )
125 {
126 mProxyModel = new QgsBrowserProxyModel( this );
127 mProxyModel->setBrowserModel( mModel );
128 mProxyModel->setHiddenDataItemProviderKeyFilter( mDisabledDataItemsKeys );
129 mBrowserView->setSettingsSection( objectName().toLower() ); // to distinguish 2 or more instances of the browser
130 mBrowserView->setBrowserModel( mModel );
131 mBrowserView->setModel( mProxyModel );
132 mBrowserView->setSortingEnabled( true );
133 mBrowserView->sortByColumn( 0, Qt::AscendingOrder );
134 // provide a horizontal scroll bar instead of using ellipse (...) for longer items
135 mBrowserView->setTextElideMode( Qt::ElideNone );
136 mBrowserView->header()->setSectionResizeMode( 0, QHeaderView::ResizeToContents );
137 mBrowserView->header()->setStretchLastSection( false );
138
139 // selectionModel is created when model is set on tree
140 connect( mBrowserView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsBrowserWidget::selectionChanged );
141
142 // Forward the model changed signals to the widget
144
145 // objectName used by settingsSection() is not yet set in constructor
146 QgsSettings settings;
147 mActionPropertiesWidget->setChecked( settings.value( settingsSection() + "/propertiesWidgetEnabled", false ).toBool() );
148 mPropertiesWidget->setVisible( false ); // false until item is selected
149
150 mSplitter->restoreState( settings.value( QStringLiteral( "%1/splitterState" ).arg( settingsSection() ) ).toByteArray() );
151 }
152
153 QWidget::showEvent( e );
154}
155
156void QgsBrowserWidget::itemDoubleClicked( const QModelIndex &index )
157{
158 QgsDataItem *item = mModel->dataItem( mProxyModel->mapToSource( index ) );
159 if ( !item )
160 return;
161
162 QgsDataItemGuiContext context = createContext();
163
164 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
165 for ( QgsDataItemGuiProvider *provider : providers )
166 {
167 if ( provider->handleDoubleClick( item, context ) )
168 return;
169 }
170
171 // if no providers overrode the double-click handling for this item, we give the item itself a chance
172 if ( !item->handleDoubleClick() )
173 {
174 // double-click not handled by browser model, so use as default view expand behavior
175 if ( mBrowserView->isExpanded( index ) )
176 mBrowserView->collapse( index );
177 else
178 mBrowserView->expand( index );
179 }
180}
181
182void QgsBrowserWidget::onOptionsChanged()
183{
184 std::function<void( const QModelIndex &index )> updateItem;
185 updateItem = [this, &updateItem]( const QModelIndex &index ) {
186 if ( QgsDirectoryItem *dirItem = qobject_cast<QgsDirectoryItem *>( mModel->dataItem( index ) ) )
187 {
188 dirItem->reevaluateMonitoring();
189 }
190
191 const int rowCount = mModel->rowCount( index );
192 for ( int i = 0; i < rowCount; ++i )
193 {
194 const QModelIndex child = mModel->index( i, 0, index );
195 updateItem( child );
196 }
197 };
198
199 for ( int i = 0; i < mModel->rowCount(); ++i )
200 {
201 updateItem( mModel->index( i, 0 ) );
202 }
203}
204
205void QgsBrowserWidget::showContextMenu( QPoint pt )
206{
207 QModelIndex index = mProxyModel->mapToSource( mBrowserView->indexAt( pt ) );
208 QgsDataItem *item = mModel->dataItem( index );
209 if ( !item )
210 return;
211
212 const QModelIndexList selection = mBrowserView->selectionModel()->selectedIndexes();
213 QList<QgsDataItem *> selectedItems;
214 selectedItems.reserve( selection.size() );
215 for ( const QModelIndex &selectedIndex : selection )
216 {
217 QgsDataItem *selectedItem = mProxyModel->dataItem( selectedIndex );
218 if ( selectedItem )
219 selectedItems << selectedItem;
220 }
221
222 QMenu *menu = new QMenu( this );
223
224 const QList<QMenu *> menus = item->menus( menu );
225 QList<QAction *> actions = item->actions( menu );
226
227 if ( !menus.isEmpty() )
228 {
229 for ( QMenu *mn : menus )
230 {
231 menu->addMenu( mn );
232 }
233 }
234
235 if ( !actions.isEmpty() )
236 {
237 if ( !menu->actions().isEmpty() )
238 menu->addSeparator();
239 // add action to the menu
240 menu->addActions( actions );
241 }
242
243 QgsDataItemGuiContext context = createContext();
244
245 QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
246 std::sort( providers.begin(), providers.end(), []( QgsDataItemGuiProvider *a, QgsDataItemGuiProvider *b ) {
247 return a->precedenceWhenPopulatingMenus() < b->precedenceWhenPopulatingMenus();
248 } );
249 for ( QgsDataItemGuiProvider *provider : std::as_const( providers ) )
250 {
251 provider->populateContextMenu( item, menu, selectedItems, context );
252 }
253
254 if ( menu->actions().isEmpty() )
255 {
256 delete menu;
257 return;
258 }
259
260 menu->popup( mBrowserView->mapToGlobal( pt ) );
261}
262
264{
265 mMessageBar = bar;
266 mModel->setMessageBar( bar );
267}
268
270{
271 return mMessageBar;
272}
273
274void QgsBrowserWidget::setDisabledDataItemsKeys( const QStringList &filter )
275{
276 mDisabledDataItemsKeys = filter;
277
278 if ( !mProxyModel )
279 return;
280
281 mProxyModel->setHiddenDataItemProviderKeyFilter( mDisabledDataItemsKeys );
282}
283
285{
286 refreshModel( QModelIndex() );
287}
288
289void QgsBrowserWidget::refreshModel( const QModelIndex &index )
290{
291 if ( mModel && mProxyModel )
292 {
293 QgsDataItem *item = mModel->dataItem( index );
294 if ( item )
295 {
296 QgsDebugMsgLevel( "path = " + item->path(), 4 );
297 }
298 else
299 {
300 QgsDebugMsgLevel( QStringLiteral( "invalid item" ), 4 );
301 }
302
303 if ( item && ( item->capabilities2() & Qgis::BrowserItemCapability::Fertile ) )
304 {
305 mModel->refresh( index );
306 }
307
308 for ( int i = 0; i < mModel->rowCount( index ); i++ )
309 {
310 QModelIndex idx = mModel->index( i, 0, index );
311 QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
312 QgsDataItem *child = mModel->dataItem( idx );
313
314 // Check also expanded descendants so that the whole expanded path does not get collapsed if one item is collapsed.
315 // Fast items (usually root items) are refreshed so that when collapsed, it is obvious they are if empty (no expand symbol).
316 if ( mBrowserView->isExpanded( proxyIdx ) || mBrowserView->hasExpandedDescendant( proxyIdx ) || ( child && child->capabilities2() & Qgis::BrowserItemCapability::Fast ) )
317 {
318 refreshModel( idx );
319 }
320 else
321 {
322 if ( child && ( child->capabilities2() & Qgis::BrowserItemCapability::Fertile ) )
323 {
324 child->depopulate();
325 }
326 }
327 }
328 }
329}
330
331void QgsBrowserWidget::addLayer( QgsLayerItem *layerItem )
332{
333 if ( !layerItem )
334 return;
335
336 emit handleDropUriList( layerItem->mimeUris() );
337}
338
339void QgsBrowserWidget::addSelectedLayers()
340{
341 QApplication::setOverrideCursor( Qt::WaitCursor );
342
343 // get a sorted list of selected indexes
344 QModelIndexList list = mBrowserView->selectionModel()->selectedIndexes();
345 std::sort( list.begin(), list.end() );
346
347 // If any of the layer items are QGIS we just open and exit the loop
348 const auto constList = list;
349 for ( const QModelIndex &index : constList )
350 {
351 QgsDataItem *item = mModel->dataItem( mProxyModel->mapToSource( index ) );
352 if ( item && item->type() == Qgis::BrowserItemType::Project )
353 {
354 QgsProjectItem *projectItem = qobject_cast<QgsProjectItem *>( item );
355 if ( projectItem )
356 emit openFile( projectItem->path(), QStringLiteral( "project" ) );
357
358 QApplication::restoreOverrideCursor();
359 return;
360 }
361 }
362
363 // add items in reverse order so they are in correct order in the layers dock
364 for ( int i = list.size() - 1; i >= 0; i-- )
365 {
366 QgsDataItem *item = mModel->dataItem( mProxyModel->mapToSource( list[i] ) );
367 if ( item && item->type() == Qgis::BrowserItemType::Layer )
368 {
369 QgsLayerItem *layerItem = qobject_cast<QgsLayerItem *>( item );
370 if ( layerItem )
371 addLayer( layerItem );
372 }
373 }
374
375 QApplication::restoreOverrideCursor();
376}
377
378void QgsBrowserWidget::hideItem()
379{
380 QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
381 QgsDataItem *item = mModel->dataItem( index );
382 if ( !item )
383 return;
384
385 if ( item->type() == Qgis::BrowserItemType::Directory )
386 {
387 mModel->hidePath( item );
388 }
389}
390
391void QgsBrowserWidget::showProperties()
392{
393 QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
394 QgsDataItem *item = mModel->dataItem( index );
395 if ( !item )
396 return;
397
399 {
400 QgsBrowserPropertiesDialog *dialog = new QgsBrowserPropertiesDialog( settingsSection(), this );
401 dialog->setAttribute( Qt::WA_DeleteOnClose );
402 dialog->setItem( item, createContext() );
403 dialog->show();
404 }
405}
406
407void QgsBrowserWidget::showFilterWidget( bool visible )
408{
409 mWidgetFilter->setVisible( visible );
410 if ( !visible )
411 {
412 mLeFilter->setText( QString() );
413 setFilter();
414 }
415 else
416 {
417 mLeFilter->setFocus();
418 }
419}
420
421void QgsBrowserWidget::setFilter()
422{
423 QString filter = mLeFilter->text();
424 if ( mProxyModel )
425 mProxyModel->setFilterString( filter );
426}
427
429{
430 if ( mModel )
431 mModel->updateProjectHome();
432}
433
434void QgsBrowserWidget::setFilterSyntax( QAction *action )
435{
436 if ( !action || !mProxyModel )
437 return;
438
439 mProxyModel->setFilterSyntax( static_cast<QgsBrowserProxyModel::FilterSyntax>( action->data().toInt() ) );
440}
441
442void QgsBrowserWidget::setCaseSensitive( bool caseSensitive )
443{
444 if ( !mProxyModel )
445 return;
446 mProxyModel->setFilterCaseSensitivity( caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive );
447}
448
449int QgsBrowserWidget::selectedItemsCount()
450{
451 QItemSelectionModel *selectionModel = mBrowserView->selectionModel();
452 if ( selectionModel )
453 {
454 return selectionModel->selectedIndexes().size();
455 }
456 return 0;
457}
458
459QgsDataItemGuiContext QgsBrowserWidget::createContext()
460{
461 QgsDataItemGuiContext context;
462 context.setMessageBar( mMessageBar );
463 context.setView( mBrowserView );
464 return context;
465}
466
467void QgsBrowserWidget::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
468{
469 Q_UNUSED( selected )
470 Q_UNUSED( deselected )
471 if ( mActionPropertiesWidget->isChecked() )
472 {
473 setPropertiesWidget();
474 }
475}
476
477void QgsBrowserWidget::clearPropertiesWidget()
478{
479 while ( mPropertiesLayout->count() > 0 )
480 {
481 delete mPropertiesLayout->itemAt( 0 )->widget();
482 }
483 mPropertiesWidget->setVisible( false );
484}
485
486void QgsBrowserWidget::setPropertiesWidget()
487{
488 clearPropertiesWidget();
489 QItemSelectionModel *selectionModel = mBrowserView->selectionModel();
490 if ( selectionModel )
491 {
492 QModelIndexList indexes = selectionModel->selectedIndexes();
493 if ( indexes.size() == 1 )
494 {
495 QModelIndex index = mProxyModel->mapToSource( indexes.value( 0 ) );
496 QgsDataItem *item = mModel->dataItem( index );
497 QgsDataItemGuiContext context = createContext();
498 QgsBrowserPropertiesWidget *propertiesWidget = QgsBrowserPropertiesWidget::createWidget( item, context, mPropertiesWidget );
499 if ( propertiesWidget )
500 {
501 propertiesWidget->setCondensedMode( true );
502 mPropertiesLayout->addWidget( propertiesWidget );
503 }
504 }
505 }
506 mPropertiesWidget->setVisible( mPropertiesLayout->count() > 0 );
507}
508
509void QgsBrowserWidget::enablePropertiesWidget( bool enable )
510{
511 mActionPropertiesWidget->setChecked( enable );
512 propertiesWidgetToggled( enable );
513}
514
515void QgsBrowserWidget::propertiesWidgetToggled( bool enabled )
516{
517 if ( enabled && selectedItemsCount() == 1 )
518 {
519 setPropertiesWidget();
520 }
521 else
522 {
523 clearPropertiesWidget();
524 }
525
526 QgsSettings settings;
527 settings.setValue( settingsSection() + "/propertiesWidgetEnabled", enabled );
528}
529
530void QgsBrowserWidget::setActiveIndex( const QModelIndex &index )
531{
532 if ( index.isValid() )
533 {
534 QModelIndex proxyIndex = mProxyModel->mapFromSource( index );
535 mBrowserView->expand( proxyIndex );
536 mBrowserView->setCurrentIndex( proxyIndex );
537 }
538}
539
540void QgsBrowserWidget::splitterMoved()
541{
542 QgsSettings settings;
543 settings.setValue( QStringLiteral( "%1/splitterState" ).arg( settingsSection() ), mSplitter->saveState() );
544}
@ Fertile
Can create children. Even items without this capability may have children, but cannot create them,...
@ Fast
CreateChildren() is fast enough to be run in main thread when refreshing items, most root items (wms,...
@ Layer
Represents a map layer.
@ Project
Represents a QGIS project.
@ Directory
Represents a file directory.
A model for showing available data sources and other items in a structured tree.
void setMessageBar(QgsMessageBar *bar)
Sets message bar that will be passed in QgsDataItemGuiContext to data items.
void connectionsChanged(const QString &providerKey)
Emitted when connections for the specified providerKey have changed in the browser.
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index.
void hidePath(QgsDataItem *item)
Hide the given path in the browser model.
void refresh(const QString &path)
Refresh item specified by path.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void initialize()
Delayed initialization, needed because the provider registry must be already populated.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
bool initialized() const
Returns true if the model has been initialized.
A QSortFilterProxyModel subclass for filtering and sorting browser model items.
FilterSyntax
Filter syntax options.
@ RegularExpression
Regular expression filtering.
@ Wildcards
Wildcard filtering.
@ Normal
Standard string filtering.
void setHiddenDataItemProviderKeyFilter(const QStringList &hiddenItemsFilter)
Sets a filter to hide data items based on QgsDataItem::providerKey() associated with the item.
void setFilterString(const QString &filter)
Sets the filter string to use when filtering items in the model.
QgsDataItem * dataItem(const QModelIndex &index) const
Returns the data item at the specified proxy index, or nullptr if no item exists at the index.
void setFilterCaseSensitivity(Qt::CaseSensitivity sensitivity)
Sets whether item filtering should be case sensitive.
void setFilterSyntax(FilterSyntax syntax)
Sets the filter syntax.
void setBrowserModel(QgsBrowserModel *model)
Sets the underlying browser model.
QgsBrowserWidget(QgsBrowserGuiModel *browserModel, QWidget *parent=nullptr)
Constructor for QgsBrowserWidget.
void updateProjectHome()
Update project home directory.
void setActiveIndex(const QModelIndex &index)
Sets the selection to index and expands it.
QgsMessageBar * messageBar()
Returns the message bar associated with the widget.
void connectionsChanged()
Connections changed in the browser.
void handleDropUriList(const QgsMimeDataUtils::UriList &)
Emitted when drop uri list needs to be handled.
void showEvent(QShowEvent *event) override
~QgsBrowserWidget() override
void setMessageBar(QgsMessageBar *bar)
Sets a message bar to use alongside the widget.
void openFile(const QString &fileName, const QString &fileTypeHint=QString())
Emitted when a file needs to be opened.
void refresh()
Refreshes the browser model and view.
void setDisabledDataItemsKeys(const QStringList &filter)
Sets the customization for data items based on item's data provider key.
Encapsulates the context in which a QgsDataItem is shown within the application GUI.
void setView(QgsBrowserTreeView *view)
Sets the associated view.
void setMessageBar(QgsMessageBar *bar)
Sets the associated message bar.
QList< QgsDataItemGuiProvider * > providers() const
Returns the list of available providers.
Abstract base class for providers which affect how QgsDataItem items behave within the application GU...
Base class for all items in the model.
Definition qgsdataitem.h:46
virtual QList< QMenu * > menus(QWidget *parent)
Returns the list of menus available for this item.
virtual bool handleDoubleClick()
Called when a user double clicks on the item.
Qgis::BrowserItemType type() const
virtual QList< QAction * > actions(QWidget *parent)
Returns the list of actions available for this item.
QString path() const
virtual Qgis::BrowserItemCapabilities capabilities2() const
Returns the capabilities for the data item.
virtual void depopulate()
Remove children recursively and set as not populated. This is used when refreshing collapsed items.
A directory: contains subdirectories and layers.
void cleared()
Emitted when the widget is cleared.
void optionsChanged()
This signal is emitted whenever the application options have been changed.
static QgsGui * instance()
Returns a pointer to the singleton instance.
Definition qgsgui.cpp:79
static QgsDataItemGuiProviderRegistry * dataItemGuiProviderRegistry()
Returns the global data item GUI provider registry, used for tracking providers which affect the brow...
Definition qgsgui.cpp:180
Item that represents a layer that can be opened with one of the providers.
QgsMimeDataUtils::UriList mimeUris() const override
Returns mime URIs for the data item, most data providers will only return a single URI but some data ...
A bar for displaying non-blocking messages to the user.
Base class for any widget that can be shown as a inline panel.
Data item that can be used to represent QGIS projects.
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.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39