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() )
293 mModel->
removeFavorite( mProxyModel->mapToSource( mBrowserView->currentIndex() ) );
298 refreshModel( QModelIndex() );
301 void QgsBrowserDockWidget::refreshModel(
const QModelIndex &index )
303 if ( mModel && mProxyModel )
320 for (
int i = 0; i < mModel->
rowCount( index ); i++ )
322 QModelIndex idx = mModel->
index( i, 0, index );
323 QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
328 if ( mBrowserView->isExpanded( proxyIdx ) || mBrowserView->hasExpandedDescendant( proxyIdx ) || ( child && child->
capabilities2() &
QgsDataItem::Fast ) )
343 void QgsBrowserDockWidget::addLayer(
QgsLayerItem *layerItem )
355 QgsDebugMsg( QStringLiteral(
"rowCount() = %1" ).arg( mModel->
rowCount( mProxyModel->mapToSource( index ) ) ) );
363 QApplication::setOverrideCursor( Qt::WaitCursor );
364 emit
openFile( projectItem->
path(), QStringLiteral(
"project" ) );
365 QApplication::restoreOverrideCursor();
374 QApplication::setOverrideCursor( Qt::WaitCursor );
375 addLayer( layerItem );
376 QApplication::restoreOverrideCursor();
385 QApplication::setOverrideCursor( Qt::WaitCursor );
388 QModelIndexList list = mBrowserView->selectionModel()->selectedIndexes();
389 std::sort( list.begin(), list.end() );
392 Q_FOREACH (
const QModelIndex &index, list )
399 emit
openFile( projectItem->
path(), QStringLiteral(
"project" ) );
401 QApplication::restoreOverrideCursor();
407 for (
int i = list.size() - 1; i >= 0; i-- )
414 addLayer( layerItem );
418 QApplication::restoreOverrideCursor();
423 QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
436 QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
443 QgsBrowserPropertiesDialog *dialog =
new QgsBrowserPropertiesDialog( settingsSection(),
this );
444 dialog->setAttribute( Qt::WA_DeleteOnClose );
445 dialog->setItem( item );
452 QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
460 QStringList fastScanDirs = settings.
value( QStringLiteral(
"qgis/scanItemsFastScanUris" ),
461 QStringList() ).toStringList();
462 int idx = fastScanDirs.indexOf( item->
path() );
465 fastScanDirs.removeAt( idx );
469 fastScanDirs << item->
path();
471 settings.
setValue( QStringLiteral(
"qgis/scanItemsFastScanUris" ), fastScanDirs );
477 mWidgetFilter->setVisible( visible );
480 mLeFilter->setText( QString() );
485 mLeFilter->setFocus();
491 QString filter = mLeFilter->text();
504 if ( !action || ! mProxyModel )
507 mProxyModel->
setFilterSyntax( static_cast< QgsBrowserProxyModel::FilterSyntax >( action->data().toInt() ) );
517 int QgsBrowserDockWidget::selectedItemsCount()
519 QItemSelectionModel *selectionModel = mBrowserView->selectionModel();
520 if ( selectionModel )
522 return selectionModel->selectedIndexes().size();
536 Q_UNUSED( selected );
537 Q_UNUSED( deselected );
538 if ( mPropertiesWidgetEnabled )
540 setPropertiesWidget();
544 void QgsBrowserDockWidget::clearPropertiesWidget()
546 while ( mPropertiesLayout->count() > 0 )
548 delete mPropertiesLayout->itemAt( 0 )->widget();
550 mPropertiesWidget->setVisible(
false );
553 void QgsBrowserDockWidget::setPropertiesWidget()
555 clearPropertiesWidget();
556 QItemSelectionModel *selectionModel = mBrowserView->selectionModel();
557 if ( selectionModel )
559 QModelIndexList indexes = selectionModel->selectedIndexes();
560 if ( indexes.size() == 1 )
562 QModelIndex index = mProxyModel->mapToSource( indexes.value( 0 ) );
564 QgsBrowserPropertiesWidget *propertiesWidget = QgsBrowserPropertiesWidget::createWidget( item, mPropertiesWidget );
565 if ( propertiesWidget )
567 propertiesWidget->setCondensedMode(
true );
568 mPropertiesLayout->addWidget( propertiesWidget );
572 mPropertiesWidget->setVisible( mPropertiesLayout->count() > 0 );
577 mPropertiesWidgetEnabled = enable;
578 if ( enable && selectedItemsCount() == 1 )
580 setPropertiesWidget();
584 clearPropertiesWidget();
590 QList<int> sizes = mSplitter->sizes();
591 float total = sizes.value( 0 ) + sizes.value( 1 );
592 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...
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 a 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.
A model for showing available data sources and other items in a structured tree.
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 a 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...
virtual Capabilities capabilities2() const
Returns the capabilities for the data item.