QGIS API Documentation 3.29.0-Master (694ebda27c)
qgsbrowserdockwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsbrowserdockwidget.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 ***************************************************************************/
17#include "qgsbrowserwidget.h"
19#include "qgsbrowserguimodel.h"
20#include "qgsdirectoryitem.h"
21#include "qgsprojectitem.h"
22#include "qgslayeritem.h"
23
24#include <QVBoxLayout>
25#include <QFileDialog>
26
27QgsBrowserDockWidget::QgsBrowserDockWidget( const QString &name, QgsBrowserGuiModel *browserModel, QWidget *parent )
28 : QgsDockWidget( parent )
29{
30 QVBoxLayout *layout = new QVBoxLayout();
31 layout->setContentsMargins( 0, 0, 0, 0 );
32 layout->setSpacing( 0 );
33 QWidget *container = new QWidget();
34 container->setLayout( layout );
35 setWidget( container );
36
37 setWindowTitle( name );
38
39 mWidget = new QgsBrowserWidget( browserModel );
40 layout->addWidget( mWidget );
41
45}
46
48
50{
51 return mWidget;
52}
53
55{
56 mWidget->showContextMenu( pt );
57}
58
60{
61 const QModelIndex index = mWidget->mProxyModel->mapToSource( mWidget->mBrowserView->currentIndex() );
62 QgsDataItem *item = mWidget->mModel->dataItem( index );
63 if ( !item )
64 return;
65
66 QgsDirectoryItem *dirItem = qobject_cast<QgsDirectoryItem *>( item );
67 if ( !dirItem )
68 return;
69
71 addFavoriteDirectory( dirItem->dirPath() );
73}
74
76{
77 const QString directory = QFileDialog::getExistingDirectory( this, tr( "Add directory to favorites" ) );
78 if ( !directory.isEmpty() )
79 {
81 addFavoriteDirectory( directory );
83 }
84}
85
86void QgsBrowserDockWidget::addFavoriteDirectory( const QString &favDir, const QString &name )
87{
88 mWidget->mModel->addFavoriteDirectory( favDir, name );
89}
90
92{
93 mWidget->setMessageBar( bar );
94}
95
97{
98 return mWidget->messageBar();
99}
100
101void QgsBrowserDockWidget::setDisabledDataItemsKeys( const QStringList &filter )
102{
103 mWidget->setDisabledDataItemsKeys( filter );
104}
105
107{
108 mWidget->mModel->removeFavorite( mWidget->mProxyModel->mapToSource( mWidget->mBrowserView->currentIndex() ) );
109}
110
112{
113 mWidget->refreshModel( QModelIndex() );
114}
115
116bool QgsBrowserDockWidget::addLayerAtIndex( const QModelIndex &index )
117{
118 QgsDebugMsg( QStringLiteral( "rowCount() = %1" ).arg( mWidget->mModel->rowCount( mWidget->mProxyModel->mapToSource( index ) ) ) );
119 QgsDataItem *item = mWidget->mModel->dataItem( mWidget->mProxyModel->mapToSource( index ) );
120
121 if ( item && item->type() == Qgis::BrowserItemType::Project )
122 {
123 QgsProjectItem *projectItem = qobject_cast<QgsProjectItem *>( item );
124 if ( projectItem )
125 {
126 QApplication::setOverrideCursor( Qt::WaitCursor );
127 emit openFile( projectItem->path(), QStringLiteral( "project" ) );
128 QApplication::restoreOverrideCursor();
129 }
130 return true;
131 }
132 else if ( item && item->type() == Qgis::BrowserItemType::Layer )
133 {
134 QgsLayerItem *layerItem = qobject_cast<QgsLayerItem *>( item );
135 if ( layerItem )
136 {
137 QApplication::setOverrideCursor( Qt::WaitCursor );
138 mWidget->addLayer( layerItem );
139 QApplication::restoreOverrideCursor();
140 }
141 return true;
142 }
143 return false;
144}
145
147{
148 mWidget->addSelectedLayers();
149}
150
152{
153 mWidget->hideItem();
154}
155
157{
158 mWidget->showProperties();
159}
160
162{
163 const QModelIndex index = mWidget->mProxyModel->mapToSource( mWidget->mBrowserView->currentIndex() );
164 QgsDataItem *item = mWidget->mModel->dataItem( index );
165 if ( ! item )
166 return;
167
168 if ( item->type() == Qgis::BrowserItemType::Directory )
169 {
170 QgsSettings settings;
171 QStringList fastScanDirs = settings.value( QStringLiteral( "qgis/scanItemsFastScanUris" ),
172 QStringList() ).toStringList();
173 const int idx = fastScanDirs.indexOf( item->path() );
174 if ( idx != -1 )
175 {
176 fastScanDirs.removeAt( idx );
177 }
178 else
179 {
180 fastScanDirs << item->path();
181 }
182 settings.setValue( QStringLiteral( "qgis/scanItemsFastScanUris" ), fastScanDirs );
183 }
184}
185
187{
188 mWidget->showFilterWidget( visible );
189}
190
192{
193 mWidget->setFilter();
194}
195
197{
198 mWidget->updateProjectHome();
199}
200
202{
203 mWidget->setFilterSyntax( action );
204}
205
207{
208 mWidget->setCaseSensitive( caseSensitive );
209}
210
211void QgsBrowserDockWidget::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
212{
213 mWidget->selectionChanged( selected, deselected );
214}
215
217{
218 mWidget->enablePropertiesWidget( enable );
219}
220
221void QgsBrowserDockWidget::setActiveIndex( const QModelIndex &index )
222{
223 mWidget->setActiveIndex( index );
224}
225
227{
228}
@ Layer
Represents a map layer.
@ Project
Represents a QGIS project.
@ Directory
Represents a file directory.
void refresh()
Refresh the browser model and view.
Q_DECL_DEPRECATED void enablePropertiesWidget(bool enable)
Enable/disable properties widget.
Q_DECL_DEPRECATED void addSelectedLayers()
Add selected layers to the project.
Q_DECL_DEPRECATED void toggleFastScan()
Toggle fast scan.
~QgsBrowserDockWidget() override
QgsBrowserDockWidget(const QString &name, QgsBrowserGuiModel *browserModel, QWidget *parent=nullptr)
Constructor for QgsBrowserDockWidget.
void connectionsChanged()
Connections changed in the browser.
Q_DECL_DEPRECATED void addFavoriteDirectory()
Add directory from file dialog to favorite.
Q_DECL_DEPRECATED void showContextMenu(QPoint)
Show context menu.
Q_DECL_DEPRECATED void showProperties()
Show the layer properties.
Q_DECL_DEPRECATED void setActiveIndex(const QModelIndex &index)
Sets the selection to index and expand it.
Q_DECL_DEPRECATED void removeFavorite()
Remove from favorite.
Q_DECL_DEPRECATED void setFilter()
Apply filter to the model.
QgsBrowserWidget * browserWidget()
Returns a pointer to the QgsBrowserWidget used by the dock widget.
QgsMessageBar * messageBar()
Returns the message bar associated with the dock.
void handleDropUriList(const QgsMimeDataUtils::UriList &)
Emitted when drop uri list needs to be handled.
Q_DECL_DEPRECATED void setCaseSensitive(bool caseSensitive)
Sets filter case sensitivity.
Q_DECL_DEPRECATED void hideItem()
Hide current item.
void setMessageBar(QgsMessageBar *bar)
Sets a message bar to use alongside the dock widget.
Q_DECL_DEPRECATED void addFavorite()
Add current item to favorite.
Q_DECL_DEPRECATED void showFilterWidget(bool visible)
Show/hide filter widget.
Q_DECL_DEPRECATED void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
Selection has changed.
Q_DECL_DEPRECATED void setFilterSyntax(QAction *)
Sets filter syntax.
Q_DECL_DEPRECATED void updateProjectHome()
Update project home directory.
void openFile(const QString &fileName, const QString &fileTypeHint=QString())
Emitted when a file needs to be opened.
Q_DECL_DEPRECATED void splitterMoved()
Splitter has been moved.
Q_DECL_DEPRECATED bool addLayerAtIndex(const QModelIndex &index)
Adds the layer corresponding to the specified model index.
void setDisabledDataItemsKeys(const QStringList &filter)
Sets the customization for data items based on item's data provider key.
A model for showing available data sources and other items in a structured tree.
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void addFavoriteDirectory(const QString &directory, const QString &name=QString())
Adds a directory to the favorites group.
void removeFavorite(const QModelIndex &index)
Removes a favorite directory from its corresponding model index.
A widget showing a browser tree view along with toolbar and toggleable properties pane.
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 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 setDisabledDataItemsKeys(const QStringList &filter)
Sets the customization for data items based on item's data provider key.
Base class for all items in the model.
Definition: qgsdataitem.h:46
Qgis::BrowserItemType type() const
Definition: qgsdataitem.h:324
QString path() const
Definition: qgsdataitem.h:354
A directory: contains subdirectories and layers.
QString dirPath() const
Returns the full path to the directory the item represents.
QgsDockWidget subclass with more fine-grained control over how the widget is closed or opened.
Definition: qgsdockwidget.h:32
Item that represents a layer that can be opened with one of the providers.
Definition: qgslayeritem.h:31
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
Data item that can be used to represent QGIS projects.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
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 Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:3647
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:3646
#define QgsDebugMsg(str)
Definition: qgslogger.h:38