18 #include <QAbstractTextDocumentLayout>
19 #include <QHeaderView>
22 #include <QToolButton>
23 #include <QFileDialog>
24 #include <QPlainTextDocumentLayout>
25 #include <QSortFilterProxyModel>
38 #include "qgsnative.h"
46 #include <QDragEnterEvent>
50 , mModel( browserModel )
51 , mPropertiesWidgetEnabled( false )
52 , mPropertiesWidgetHeight( 0 )
56 mContents->layout()->setContentsMargins( 0, 0, 0, 0 );
57 mContents->layout()->setMargin( 0 );
58 static_cast< QVBoxLayout *
>( mContents->layout() )->setSpacing( 0 );
60 setWindowTitle( name );
62 mBrowserView =
new QgsDockBrowserTreeView(
this );
63 mLayoutBrowser->addWidget( mBrowserView );
65 mWidgetFilter->hide();
66 mLeFilter->setPlaceholderText( tr(
"Type here to filter visible items…" ) );
69 QMenu *menu =
new QMenu(
this );
70 menu->setSeparatorsCollapsible(
false );
71 mBtnFilterOptions->setMenu( menu );
72 QAction *action =
new QAction( tr(
"Case Sensitive" ), menu );
73 action->setData(
"case" );
74 action->setCheckable(
true );
75 action->setChecked(
false );
77 menu->addAction( action );
78 QActionGroup *group =
new QActionGroup( menu );
79 action =
new QAction( tr(
"Filter Pattern Syntax" ), group );
80 action->setSeparator(
true );
81 menu->addAction( action );
82 action =
new QAction( tr(
"Normal" ), group );
84 action->setCheckable(
true );
85 action->setChecked(
true );
86 menu->addAction( action );
87 action =
new QAction( tr(
"Wildcard(s)" ), group );
89 action->setCheckable(
true );
90 menu->addAction( action );
91 action =
new QAction( tr(
"Regular Expression" ), group );
93 action->setCheckable(
true );
94 menu->addAction( action );
96 mBrowserView->setExpandsOnDoubleClick(
false );
100 connect( mActionCollapse, &QAction::triggered, mBrowserView, &QgsDockBrowserTreeView::collapseAll );
108 connect( mBrowserView, &QgsDockBrowserTreeView::doubleClicked,
this, &QgsBrowserDockWidget::itemDoubleClicked );
115 settings.
setValue( settingsSection() +
"/propertiesWidgetEnabled", mPropertiesWidgetEnabled );
117 settings.
setValue( settingsSection() +
"/propertiesWidgetHeight", mPropertiesWidgetHeight );
132 mBrowserView->setSettingsSection( objectName().toLower() );
133 mBrowserView->setBrowserModel( mModel );
134 mBrowserView->setModel( mProxyModel );
135 mBrowserView->setSortingEnabled(
true );
136 mBrowserView->sortByColumn( 0, Qt::AscendingOrder );
138 mBrowserView->setTextElideMode( Qt::ElideNone );
139 mBrowserView->header()->setSectionResizeMode( 0, QHeaderView::ResizeToContents );
140 mBrowserView->header()->setStretchLastSection(
false );
143 connect( mBrowserView->selectionModel(), &QItemSelectionModel::selectionChanged,
153 mPropertiesWidgetEnabled = settings.
value( settingsSection() +
"/propertiesWidgetEnabled",
false ).toBool();
154 mActionPropertiesWidget->setChecked( mPropertiesWidgetEnabled );
155 mPropertiesWidget->setVisible(
false );
157 mPropertiesWidgetHeight = settings.
value( settingsSection() +
"/propertiesWidgetHeight" ).toFloat();
158 QList<int> sizes = mSplitter->sizes();
159 int total = sizes.value( 0 ) + sizes.value( 1 );
160 int height =
static_cast<int>( total * mPropertiesWidgetHeight );
162 sizes << total - height << height;
163 mSplitter->setSizes( sizes );
169 void QgsBrowserDockWidget::itemDoubleClicked(
const QModelIndex &index )
180 if ( provider->handleDoubleClick( item, context ) )
188 if ( mBrowserView->isExpanded( index ) )
189 mBrowserView->collapse( index );
191 mBrowserView->expand( index );
197 QModelIndex index = mProxyModel->mapToSource( mBrowserView->indexAt( pt ) );
202 const QModelIndexList selection = mBrowserView->selectionModel()->selectedIndexes();
203 QList< QgsDataItem * > selectedItems;
204 selectedItems.reserve( selection.size() );
205 for (
const QModelIndex &selectedIndex : selection )
209 selectedItems << selectedItem;
212 QMenu *menu =
new QMenu(
this );
214 const QList<QMenu *> menus = item->
menus( menu );
215 QList<QAction *> actions = item->
actions( menu );
217 if ( !menus.isEmpty() )
219 for ( QMenu *mn : menus )
225 if ( !actions.isEmpty() )
227 if ( !menu->actions().isEmpty() )
228 menu->addSeparator();
230 menu->addActions( actions );
238 provider->populateContextMenu( item, menu, selectedItems, context );
241 if ( menu->actions().isEmpty() )
247 menu->popup( mBrowserView->mapToGlobal( pt ) );
252 QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
268 QString directory = QFileDialog::getExistingDirectory(
this, tr(
"Add directory to favorites" ) );
269 if ( !directory.isEmpty() )
295 mDisabledDataItemsKeys = filter;
305 mModel->
removeFavorite( mProxyModel->mapToSource( mBrowserView->currentIndex() ) );
310 refreshModel( QModelIndex() );
313 void QgsBrowserDockWidget::refreshModel(
const QModelIndex &index )
315 if ( mModel && mProxyModel )
332 for (
int i = 0; i < mModel->
rowCount( index ); i++ )
334 QModelIndex idx = mModel->
index( i, 0, index );
335 QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
340 if ( mBrowserView->isExpanded( proxyIdx ) || mBrowserView->hasExpandedDescendant( proxyIdx ) || ( child && child->
capabilities2() &
QgsDataItem::Fast ) )
355 void QgsBrowserDockWidget::addLayer(
QgsLayerItem *layerItem )
367 QgsDebugMsg( QStringLiteral(
"rowCount() = %1" ).arg( mModel->
rowCount( mProxyModel->mapToSource( index ) ) ) );
372 QgsProjectItem *projectItem = qobject_cast<QgsProjectItem *>( item );
375 QApplication::setOverrideCursor( Qt::WaitCursor );
376 emit
openFile( projectItem->
path(), QStringLiteral(
"project" ) );
377 QApplication::restoreOverrideCursor();
383 QgsLayerItem *layerItem = qobject_cast<QgsLayerItem *>( item );
386 QApplication::setOverrideCursor( Qt::WaitCursor );
387 addLayer( layerItem );
388 QApplication::restoreOverrideCursor();
397 QApplication::setOverrideCursor( Qt::WaitCursor );
400 QModelIndexList list = mBrowserView->selectionModel()->selectedIndexes();
401 std::sort( list.begin(), list.end() );
404 const auto constList = list;
405 for (
const QModelIndex &index : constList )
410 QgsProjectItem *projectItem = qobject_cast<QgsProjectItem *>( item );
412 emit
openFile( projectItem->
path(), QStringLiteral(
"project" ) );
414 QApplication::restoreOverrideCursor();
420 for (
int i = list.size() - 1; i >= 0; i-- )
425 QgsLayerItem *layerItem = qobject_cast<QgsLayerItem *>( item );
427 addLayer( layerItem );
431 QApplication::restoreOverrideCursor();
436 QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
449 QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
456 QgsBrowserPropertiesDialog *dialog =
new QgsBrowserPropertiesDialog( settingsSection(),
this );
457 dialog->setAttribute( Qt::WA_DeleteOnClose );
458 dialog->setItem( item, createContext() );
465 QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
473 QStringList fastScanDirs = settings.
value( QStringLiteral(
"qgis/scanItemsFastScanUris" ),
474 QStringList() ).toStringList();
475 int idx = fastScanDirs.indexOf( item->
path() );
478 fastScanDirs.removeAt( idx );
482 fastScanDirs << item->
path();
484 settings.
setValue( QStringLiteral(
"qgis/scanItemsFastScanUris" ), fastScanDirs );
490 mWidgetFilter->setVisible( visible );
493 mLeFilter->setText( QString() );
498 mLeFilter->setFocus();
504 QString filter = mLeFilter->text();
517 if ( !action || ! mProxyModel )
530 int QgsBrowserDockWidget::selectedItemsCount()
532 QItemSelectionModel *selectionModel = mBrowserView->selectionModel();
533 if ( selectionModel )
535 return selectionModel->selectedIndexes().size();
550 Q_UNUSED( deselected )
551 if ( mPropertiesWidgetEnabled )
553 setPropertiesWidget();
557 void QgsBrowserDockWidget::clearPropertiesWidget()
559 while ( mPropertiesLayout->count() > 0 )
561 delete mPropertiesLayout->itemAt( 0 )->widget();
563 mPropertiesWidget->setVisible(
false );
566 void QgsBrowserDockWidget::setPropertiesWidget()
568 clearPropertiesWidget();
569 QItemSelectionModel *selectionModel = mBrowserView->selectionModel();
570 if ( selectionModel )
572 QModelIndexList indexes = selectionModel->selectedIndexes();
573 if ( indexes.size() == 1 )
575 QModelIndex index = mProxyModel->mapToSource( indexes.value( 0 ) );
578 QgsBrowserPropertiesWidget *propertiesWidget = QgsBrowserPropertiesWidget::createWidget( item, context, mPropertiesWidget );
579 if ( propertiesWidget )
581 propertiesWidget->setCondensedMode(
true );
582 mPropertiesLayout->addWidget( propertiesWidget );
586 mPropertiesWidget->setVisible( mPropertiesLayout->count() > 0 );
591 mPropertiesWidgetEnabled = enable;
592 if ( enable && selectedItemsCount() == 1 )
594 setPropertiesWidget();
598 clearPropertiesWidget();
604 if ( index.isValid() )
606 QModelIndex proxyIndex = mProxyModel->mapFromSource( index );
607 mBrowserView->expand( proxyIndex );
608 mBrowserView->setCurrentIndex( proxyIndex );
614 QList<int> sizes = mSplitter->sizes();
615 float total = sizes.value( 0 ) + sizes.value( 1 );
616 mPropertiesWidgetHeight = total > 0 ? sizes.value( 1 ) / total : 0;