QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  ***************************************************************************/
15 #include "qgsbrowserdockwidget.h"
16 #include "qgsbrowserdockwidget_p.h"
17 #include "qgsbrowserwidget.h"
18 #include "qgsbrowserproxymodel.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 
27 QgsBrowserDockWidget::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 
86 void 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 
101 void 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 
116 bool 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 
206 void QgsBrowserDockWidget::setCaseSensitive( bool caseSensitive )
207 {
208  mWidget->setCaseSensitive( caseSensitive );
209 }
210 
211 void QgsBrowserDockWidget::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
212 {
213  mWidget->selectionChanged( selected, deselected );
214 }
215 
217 {
218  mWidget->enablePropertiesWidget( enable );
219 }
220 
221 void QgsBrowserDockWidget::setActiveIndex( const QModelIndex &index )
222 {
223  mWidget->setActiveIndex( index );
224 }
225 
227 {
228 }
QgsBrowserDockWidget::setMessageBar
void setMessageBar(QgsMessageBar *bar)
Sets a message bar to use alongside the dock widget.
Definition: qgsbrowserdockwidget.cpp:91
QgsBrowserDockWidget::showFilterWidget
Q_DECL_DEPRECATED void showFilterWidget(bool visible)
Show/hide filter widget.
Definition: qgsbrowserdockwidget.cpp:186
QgsBrowserDockWidget::showProperties
Q_DECL_DEPRECATED void showProperties()
Show the layer properties.
Definition: qgsbrowserdockwidget.cpp:156
qgsdirectoryitem.h
QgsDataItem::path
QString path() const
Definition: qgsdataitem.h:354
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:161
QgsBrowserDockWidget::setActiveIndex
Q_DECL_DEPRECATED void setActiveIndex(const QModelIndex &index)
Sets the selection to index and expand it.
Definition: qgsbrowserdockwidget.cpp:221
QgsBrowserDockWidget::setFilterSyntax
Q_DECL_DEPRECATED void setFilterSyntax(QAction *)
Sets filter syntax.
Definition: qgsbrowserdockwidget.cpp:201
Qgis::BrowserItemType::Layer
@ Layer
Represents a map layer.
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:61
QgsBrowserDockWidget::~QgsBrowserDockWidget
~QgsBrowserDockWidget() override
qgsbrowserdockwidget.h
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsBrowserDockWidget::toggleFastScan
Q_DECL_DEPRECATED void toggleFastScan()
Toggle fast scan.
Definition: qgsbrowserdockwidget.cpp:161
QgsBrowserWidget::messageBar
QgsMessageBar * messageBar()
Returns the message bar associated with the widget.
Definition: qgsbrowserwidget.cpp:272
QgsBrowserDockWidget::updateProjectHome
Q_DECL_DEPRECATED void updateProjectHome()
Update project home directory.
Definition: qgsbrowserdockwidget.cpp:196
QgsBrowserDockWidget::splitterMoved
Q_DECL_DEPRECATED void splitterMoved()
Splitter has been moved.
Definition: qgsbrowserdockwidget.cpp:226
QgsDirectoryItem::dirPath
QString dirPath() const
Returns the full path to the directory the item represents.
Definition: qgsdirectoryitem.h:86
QgsBrowserWidget::connectionsChanged
void connectionsChanged()
Connections changed in the browser.
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:2820
QgsBrowserWidget::updateProjectHome
void updateProjectHome()
Update project home directory.
Definition: qgsbrowserwidget.cpp:431
qgslayeritem.h
QgsBrowserWidget::handleDropUriList
void handleDropUriList(const QgsMimeDataUtils::UriList &)
Emitted when drop uri list needs to be handled.
QgsBrowserModel::removeFavorite
void removeFavorite(const QModelIndex &index)
Removes a favorite directory from its corresponding model index.
Definition: qgsbrowsermodel.cpp:748
QgsProjectItem
Data item that can be used to represent QGIS projects.
Definition: qgsprojectitem.h:28
QgsDirectoryItem
A directory: contains subdirectories and layers.
Definition: qgsdirectoryitem.h:33
QgsBrowserDockWidget::setCaseSensitive
Q_DECL_DEPRECATED void setCaseSensitive(bool caseSensitive)
Sets filter case sensitivity.
Definition: qgsbrowserdockwidget.cpp:206
QgsBrowserDockWidget::messageBar
QgsMessageBar * messageBar()
Returns the message bar associated with the dock.
Definition: qgsbrowserdockwidget.cpp:96
QgsBrowserWidget::setActiveIndex
void setActiveIndex(const QModelIndex &index)
Sets the selection to index and expands it.
Definition: qgsbrowserwidget.cpp:532
QgsBrowserDockWidget::connectionsChanged
void connectionsChanged()
Connections changed in the browser.
QgsBrowserWidget::setDisabledDataItemsKeys
void setDisabledDataItemsKeys(const QStringList &filter)
Sets the customization for data items based on item's data provider key.
Definition: qgsbrowserwidget.cpp:277
QgsBrowserDockWidget::addFavorite
Q_DECL_DEPRECATED void addFavorite()
Add current item to favorite.
Definition: qgsbrowserdockwidget.cpp:59
QgsBrowserDockWidget::enablePropertiesWidget
Q_DECL_DEPRECATED void enablePropertiesWidget(bool enable)
Enable/disable properties widget.
Definition: qgsbrowserdockwidget.cpp:216
QgsMessageBar
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:60
QgsSettings::setValue
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Definition: qgssettings.cpp:279
QgsBrowserDockWidget::removeFavorite
Q_DECL_DEPRECATED void removeFavorite()
Remove from favorite.
Definition: qgsbrowserdockwidget.cpp:106
QgsBrowserDockWidget::setFilter
Q_DECL_DEPRECATED void setFilter()
Apply filter to the model.
Definition: qgsbrowserdockwidget.cpp:191
QgsBrowserDockWidget::openFile
void openFile(const QString &fileName, const QString &fileTypeHint=QString())
Emitted when a file needs to be opened.
QgsBrowserDockWidget::refresh
void refresh()
Refresh the browser model and view.
Definition: qgsbrowserdockwidget.cpp:111
QgsBrowserDockWidget::selectionChanged
Q_DECL_DEPRECATED void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
Selection has changed.
Definition: qgsbrowserdockwidget.cpp:211
QgsBrowserDockWidget::setDisabledDataItemsKeys
void setDisabledDataItemsKeys(const QStringList &filter)
Sets the customization for data items based on item's data provider key.
Definition: qgsbrowserdockwidget.cpp:101
qgsbrowserwidget.h
QgsBrowserModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition: qgsbrowsermodel.cpp:378
QgsBrowserDockWidget::QgsBrowserDockWidget
QgsBrowserDockWidget(const QString &name, QgsBrowserGuiModel *browserModel, QWidget *parent=nullptr)
Constructor for QgsBrowserDockWidget.
Definition: qgsbrowserdockwidget.cpp:27
qgsbrowserdockwidget_p.h
QgsBrowserDockWidget::addLayerAtIndex
Q_DECL_DEPRECATED bool addLayerAtIndex(const QModelIndex &index)
Adds the layer corresponding to the specified model index.
Definition: qgsbrowserdockwidget.cpp:116
QgsBrowserDockWidget::browserWidget
QgsBrowserWidget * browserWidget()
Returns a pointer to the QgsBrowserWidget used by the dock widget.
Definition: qgsbrowserdockwidget.cpp:49
qgsbrowserguimodel.h
QgsDockWidget
QgsDockWidget subclass with more fine-grained control over how the widget is closed or opened.
Definition: qgsdockwidget.h:31
QgsBrowserModel::addFavoriteDirectory
void addFavoriteDirectory(const QString &directory, const QString &name=QString())
Adds a directory to the favorites group.
Definition: qgsbrowsermodel.cpp:742
qgsbrowserproxymodel.h
qgsprojectitem.h
QgsLayerItem
Item that represents a layer that can be opened with one of the providers.
Definition: qgslayeritem.h:29
Qgis::BrowserItemType::Project
@ Project
Represents a QGIS project.
QgsBrowserDockWidget::handleDropUriList
void handleDropUriList(const QgsMimeDataUtils::UriList &)
Emitted when drop uri list needs to be handled.
QgsBrowserDockWidget::addSelectedLayers
Q_DECL_DEPRECATED void addSelectedLayers()
Add selected layers to the project.
Definition: qgsbrowserdockwidget.cpp:146
QgsBrowserWidget::setMessageBar
void setMessageBar(QgsMessageBar *bar)
Sets a message bar to use alongside the widget.
Definition: qgsbrowserwidget.cpp:266
QgsDataItem
Base class for all items in the model.
Definition: qgsdataitem.h:45
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:2819
QgsBrowserDockWidget::hideItem
Q_DECL_DEPRECATED void hideItem()
Hide current item.
Definition: qgsbrowserdockwidget.cpp:151
QgsBrowserModel::dataItem
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index.
Definition: qgsbrowsermodel.cpp:695
QgsDataItem::type
Qgis::BrowserItemType type() const
Definition: qgsdataitem.h:324
QgsBrowserGuiModel
A model for showing available data sources and other items in a structured tree.
Definition: qgsbrowserguimodel.h:36
QgsBrowserWidget::openFile
void openFile(const QString &fileName, const QString &fileTypeHint=QString())
Emitted when a file needs to be opened.
QgsBrowserDockWidget::showContextMenu
Q_DECL_DEPRECATED void showContextMenu(QPoint)
Show context menu.
Definition: qgsbrowserdockwidget.cpp:54
QgsBrowserDockWidget::addFavoriteDirectory
Q_DECL_DEPRECATED void addFavoriteDirectory()
Add directory from file dialog to favorite.
Definition: qgsbrowserdockwidget.cpp:75
Qgis::BrowserItemType::Directory
@ Directory
Represents a file directory.
QgsBrowserWidget
A widget showing a browser tree view along with toolbar and toggleable properties pane.
Definition: qgsbrowserwidget.h:39