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 );
131 mBrowserView->setSettingsSection( objectName().toLower() );
132 mBrowserView->setBrowserModel( mModel );
133 mBrowserView->setModel( mProxyModel );
134 mBrowserView->setSortingEnabled(
true );
135 mBrowserView->sortByColumn( 0, Qt::AscendingOrder );
137 mBrowserView->setTextElideMode( Qt::ElideNone );
138 mBrowserView->header()->setSectionResizeMode( 0, QHeaderView::ResizeToContents );
139 mBrowserView->header()->setStretchLastSection(
false );
142 connect( mBrowserView->selectionModel(), &QItemSelectionModel::selectionChanged,
152 mPropertiesWidgetEnabled = settings.
value( settingsSection() +
"/propertiesWidgetEnabled",
false ).toBool();
153 mActionPropertiesWidget->setChecked( mPropertiesWidgetEnabled );
154 mPropertiesWidget->setVisible(
false );
156 mPropertiesWidgetHeight = settings.
value( settingsSection() +
"/propertiesWidgetHeight" ).toFloat();
157 QList<int> sizes = mSplitter->sizes();
158 int total = sizes.value( 0 ) + sizes.value( 1 );
159 int height =
static_cast<int>( total * mPropertiesWidgetHeight );
161 sizes << total - height << height;
162 mSplitter->setSizes( sizes );
168 void QgsBrowserDockWidget::itemDoubleClicked(
const QModelIndex &index )
179 if ( provider->handleDoubleClick( item, context ) )
187 if ( mBrowserView->isExpanded( index ) )
188 mBrowserView->collapse( index );
190 mBrowserView->expand( index );
196 QModelIndex index = mProxyModel->mapToSource( mBrowserView->indexAt( pt ) );
201 const QModelIndexList selection = mBrowserView->selectionModel()->selectedIndexes();
202 QList< QgsDataItem * > selectedItems;
203 selectedItems.reserve( selection.size() );
204 for (
const QModelIndex &selectedIndex : selection )
208 selectedItems << selectedItem;
211 QMenu *menu =
new QMenu(
this );
213 const QList<QMenu *> menus = item->
menus( menu );
214 QList<QAction *> actions = item->
actions( menu );
216 if ( !menus.isEmpty() )
218 for ( QMenu *mn : menus )
224 if ( !actions.isEmpty() )
226 if ( !menu->actions().isEmpty() )
227 menu->addSeparator();
229 menu->addActions( actions );
237 provider->populateContextMenu( item, menu, selectedItems, context );
240 if ( menu->actions().isEmpty() )
246 menu->popup( mBrowserView->mapToGlobal( pt ) );
251 QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
267 QString directory = QFileDialog::getExistingDirectory(
this, tr(
"Add directory to favorites" ) );
268 if ( !directory.isEmpty() )
294 mModel->
removeFavorite( mProxyModel->mapToSource( mBrowserView->currentIndex() ) );
299 refreshModel( QModelIndex() );
302 void QgsBrowserDockWidget::refreshModel(
const QModelIndex &index )
304 if ( mModel && mProxyModel )
321 for (
int i = 0; i < mModel->
rowCount( index ); i++ )
323 QModelIndex idx = mModel->
index( i, 0, index );
324 QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
329 if ( mBrowserView->isExpanded( proxyIdx ) || mBrowserView->hasExpandedDescendant( proxyIdx ) || ( child && child->
capabilities2() &
QgsDataItem::Fast ) )
344 void QgsBrowserDockWidget::addLayer(
QgsLayerItem *layerItem )
356 QgsDebugMsg( QStringLiteral(
"rowCount() = %1" ).arg( mModel->
rowCount( mProxyModel->mapToSource( index ) ) ) );
364 QApplication::setOverrideCursor( Qt::WaitCursor );
365 emit
openFile( projectItem->
path(), QStringLiteral(
"project" ) );
366 QApplication::restoreOverrideCursor();
375 QApplication::setOverrideCursor( Qt::WaitCursor );
376 addLayer( layerItem );
377 QApplication::restoreOverrideCursor();
386 QApplication::setOverrideCursor( Qt::WaitCursor );
389 QModelIndexList list = mBrowserView->selectionModel()->selectedIndexes();
390 std::sort( list.begin(), list.end() );
393 const auto constList = list;
394 for (
const QModelIndex &index : constList )
401 emit
openFile( projectItem->
path(), QStringLiteral(
"project" ) );
403 QApplication::restoreOverrideCursor();
409 for (
int i = list.size() - 1; i >= 0; i-- )
416 addLayer( layerItem );
420 QApplication::restoreOverrideCursor();
425 QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
438 QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
445 QgsBrowserPropertiesDialog *dialog =
new QgsBrowserPropertiesDialog( settingsSection(),
this );
446 dialog->setAttribute( Qt::WA_DeleteOnClose );
447 dialog->setItem( item, createContext() );
454 QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
462 QStringList fastScanDirs = settings.
value( QStringLiteral(
"qgis/scanItemsFastScanUris" ),
463 QStringList() ).toStringList();
464 int idx = fastScanDirs.indexOf( item->
path() );
467 fastScanDirs.removeAt( idx );
471 fastScanDirs << item->
path();
473 settings.
setValue( QStringLiteral(
"qgis/scanItemsFastScanUris" ), fastScanDirs );
479 mWidgetFilter->setVisible( visible );
482 mLeFilter->setText( QString() );
487 mLeFilter->setFocus();
493 QString filter = mLeFilter->text();
506 if ( !action || ! mProxyModel )
509 mProxyModel->
setFilterSyntax( static_cast< QgsBrowserProxyModel::FilterSyntax >( action->data().toInt() ) );
519 int QgsBrowserDockWidget::selectedItemsCount()
521 QItemSelectionModel *selectionModel = mBrowserView->selectionModel();
522 if ( selectionModel )
524 return selectionModel->selectedIndexes().size();
539 Q_UNUSED( deselected )
540 if ( mPropertiesWidgetEnabled )
542 setPropertiesWidget();
546 void QgsBrowserDockWidget::clearPropertiesWidget()
548 while ( mPropertiesLayout->count() > 0 )
550 delete mPropertiesLayout->itemAt( 0 )->widget();
552 mPropertiesWidget->setVisible(
false );
555 void QgsBrowserDockWidget::setPropertiesWidget()
557 clearPropertiesWidget();
558 QItemSelectionModel *selectionModel = mBrowserView->selectionModel();
559 if ( selectionModel )
561 QModelIndexList indexes = selectionModel->selectedIndexes();
562 if ( indexes.size() == 1 )
564 QModelIndex index = mProxyModel->mapToSource( indexes.value( 0 ) );
567 QgsBrowserPropertiesWidget *propertiesWidget = QgsBrowserPropertiesWidget::createWidget( item, context, mPropertiesWidget );
568 if ( propertiesWidget )
570 propertiesWidget->setCondensedMode(
true );
571 mPropertiesLayout->addWidget( propertiesWidget );
575 mPropertiesWidget->setVisible( mPropertiesLayout->count() > 0 );
580 mPropertiesWidgetEnabled = enable;
581 if ( enable && selectedItemsCount() == 1 )
583 setPropertiesWidget();
587 clearPropertiesWidget();
593 if ( index.isValid() )
595 QModelIndex proxyIndex = mProxyModel->mapFromSource( index );
596 mBrowserView->expand( proxyIndex );
597 mBrowserView->setCurrentIndex( proxyIndex );
603 QList<int> sizes = mSplitter->sizes();
604 float total = sizes.value( 0 ) + sizes.value( 1 );
605 mPropertiesWidgetHeight = total > 0 ? sizes.value( 1 ) / total : 0;
virtual QList< QMenu * > menus(QWidget *parent)
Returns the list of menus available for this item.
void connectionsChanged()
Connections changed in the browser, forwarded to the widget and used to notify the provider dialogs o...
void hidePath(QgsDataItem *item)
Hide the given path in the browser model.
This class is a composition of two QSettings instances:
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static QgsDataItemGuiProviderRegistry * dataItemGuiProviderRegistry()
Returns the global data item GUI provider registry, used for tracking providers which affect the brow...
#define Q_NOWARN_DEPRECATED_PUSH
A bar for displaying non-blocking messages to the user.
void setBrowserModel(QgsBrowserModel *model)
Sets the underlying browser model.
virtual void depopulate()
Remove children recursively and set as not populated. This is used when refreshing collapsed items...
A model for showing available data sources and other items in a structured tree.
QList< QgsDataItemGuiProvider * > providers() const
Returns the list of available providers.
static QgsGui * instance()
Returns a pointer to the singleton instance.
void setFilterSyntax(FilterSyntax syntax)
Sets the filter syntax.
bool initialized() const
Returns true if the model has been initialized.
#define QgsDebugMsgLevel(str, level)
Regular expression filtering.
virtual bool handleDoubleClick()
Called when a user double clicks on the item.
void setFilterString(const QString &filter)
Sets the filter string to use when filtering items in the model.
void initialize()
Delayed initialization, needed because the provider registry must be already populated.
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index...
void removeFavorite(const QModelIndex &index)
Removes a favorite directory from its corresponding model index.
Abstract base class for providers which affect how QgsDataItem items behave within the application GU...
void setFilterCaseSensitivity(Qt::CaseSensitivity sensitivity)
Sets whether item filtering should be case sensitive.
A directory: contains subdirectories and layers.
Base class for all items in the model.
Encapsulates the context in which a QgsDataItem is shown within the application GUI.
Can create children. Even items without this capability may have children, but cannot create them...
#define Q_NOWARN_DEPRECATED_POP
void addFavoriteDirectory(const QString &directory, const QString &name=QString())
Adds a directory to the favorites group.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QgsMimeDataUtils::Uri mimeUri() const override
Returns mime URI for the data item.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
void setMessageBar(QgsMessageBar *bar)
Sets the associated message bar.
void cleared()
Emitted when the widget is cleared.
virtual QList< QAction * > actions(QWidget *parent)
Returns the list of actions available for this item.
QString dirPath() const
Returns the full path to the directory the item represents.
void refresh(const QString &path)
Refresh item specified by path.
Standard string filtering.
A QSortFilterProxyModel subclass for filtering and sorting browser model items.
QList< QgsMimeDataUtils::Uri > UriList
Represents a QGIS project.
Item that represents a layer that can be opened with one of the providers.
Data item that can be used to represent QGIS projects.
QgsDataItem * dataItem(const QModelIndex &index) const
Returns the data item at the specified proxy index, or nullptr if no item exists at the index...
CreateChildren() is fast enough to be run in main thread when refreshing items, most root items (wms...
void setMessageBar(QgsMessageBar *bar)
Sets message bar that will be passed in QgsDataItemGuiContext to data items.
virtual Capabilities capabilities2() const
Returns the capabilities for the data item.