27#include <QDesktopServices>
34#include "moc_qgsprovidersublayersdialog.cpp"
36using namespace Qt::StringLiterals;
45 if ( !
index.isValid() )
48 QgsProviderSublayerModelNode *node = index2node(
index );
52 if ( QgsProviderSublayerModelSublayerNode *sublayerNode =
dynamic_cast<QgsProviderSublayerModelSublayerNode *
>( node ) )
64 return tr(
"Scanning…" );
95 if ( !
index.isValid() )
108 return Qt::ItemFlags();
116 mGeometryTypesResolved = resolved;
121 : QDialog( parent, fl )
126 const QFileInfo fileInfo( filePathIn );
127 const QString filePath = ( fileInfo.isFile() || fileInfo.isDir() ) && fileInfo.exists() ? filePathIn : QString();
130 if ( !fileName.isEmpty() )
135 setWindowTitle( fileName.isEmpty() ? tr(
"Select Items to Add" ) : u
"%1 | %2"_s.arg( tr(
"Select Items to Add" ), fileName ) );
137 mLblFilePath->setText( u
"<a href=\"%1\">%2</a>"_s
138 .arg( QUrl::fromLocalFile( filePath ).toString(), QDir::toNativeSeparators( QFileInfo( filePath ).canonicalFilePath() ) ) );
139 mLblFilePath->setVisible( !filePath.isEmpty() );
140 mLblFilePath->setWordWrap(
true );
141 mLblFilePath->setTextInteractionFlags( Qt::TextBrowserInteraction );
142 connect( mLblFilePath, &QLabel::linkActivated,
this, [](
const QString &link ) {
143 const QUrl url( link );
144 const QFileInfo file( url.toLocalFile() );
145 if ( file.exists() && !file.isDir() )
148 QDesktopServices::openUrl( url );
152 mModel->setSublayerDetails( initialDetails );
154 mProxyModel->setSourceModel( mModel );
155 mLayersTree->setModel( mProxyModel );
157 mLayersTree->expandAll();
160 const bool addToGroup = settings.
value( u
"/qgis/openSublayersInGroup"_s,
false ).toBool();
161 mCbxAddToGroup->setChecked( addToGroup );
162 mCbxAddToGroup->setVisible( !fileName.isEmpty() );
165 const QByteArray ba = settings.
value(
"/Windows/SubLayers/headerState" ).toByteArray();
168 mLayersTree->header()->restoreState( ba );
172 for (
int i = 0; i < mModel->columnCount(); i++ )
173 mLayersTree->resizeColumnToContents( i );
174 mLayersTree->setColumnWidth( 1, mLayersTree->columnWidth( 1 ) + 10 );
182 QList<QgsProviderSublayerDetails> res = mTask->results();
183 res.erase( std::remove_if( res.begin(), res.end(), [acceptableTypes]( const QgsProviderSublayerDetails &sublayer ) {
184 return !acceptableTypes.empty() && !acceptableTypes.contains( sublayer.type() );
188 mModel->setSublayerDetails( res );
189 mModel->setGeometryTypesResolved(
true );
191 mLayersTree->expandAll();
197 connect( mBtnSelectAll, &QAbstractButton::pressed,
this, &QgsProviderSublayersDialog::selectAll );
198 connect( mBtnDeselectAll, &QAbstractButton::pressed,
this, [
this] { mLayersTree->selectionModel()->clear(); } );
199 connect( mLayersTree->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsProviderSublayersDialog::treeSelectionChanged );
203 connect( mLayersTree, &QTreeView::doubleClicked,
this, [
this](
const QModelIndex &index ) {
204 const QModelIndex left = mLayersTree->model()->index( index.row(), 0, index.parent() );
205 if ( !( left.flags() & Qt::ItemIsSelectable ) )
208 mLayersTree->selectionModel()->select( QItemSelection( left, mLayersTree->model()->index( index.row(), mLayersTree->model()->columnCount() - 1, index.parent() ) ), QItemSelectionModel::ClearAndSelect );
209 emit layersAdded( selectedLayers() );
212 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
213 connect( mButtonBox, &QDialogButtonBox::accepted,
this, [
this] {
214 emit layersAdded( selectedLayers() );
217 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
218 mButtonBox->button( QDialogButtonBox::Ok )->setText( tr(
"Add Layers" ) );
227 mModel->addNonLayerItem( item );
234 settings.
setValue(
"/Windows/SubLayers/headerState", mLayersTree->header()->saveState() );
235 settings.
setValue( u
"/qgis/openSublayersInGroup"_s, mCbxAddToGroup->isChecked() );
243 const QModelIndexList selection = mLayersTree->selectionModel()->selectedRows();
244 QList<QgsProviderSublayerDetails> selectedSublayers;
245 for (
const QModelIndex &index : selection )
247 const QModelIndex sourceIndex = mProxyModel->mapToSource( index );
250 selectedSublayers << mModel->indexToSublayer( sourceIndex );
253 return selectedSublayers;
258 const QModelIndexList selection = mLayersTree->selectionModel()->selectedRows();
259 QList<QgsProviderSublayerModel::NonLayerItem> selectedItems;
260 for (
const QModelIndex &index : selection )
262 const QModelIndex sourceIndex = mProxyModel->mapToSource( index );
265 selectedItems << mModel->indexToNonLayerItem( sourceIndex );
268 return selectedItems;
273 mGroupName = groupNameIn;
275 if ( settings.
value( u
"qgis/formatLayerName"_s,
false ).toBool() )
280 mCbxAddToGroup->setVisible( !mGroupName.isEmpty() );
285 if ( !mCbxAddToGroup->isChecked() )
290void QgsProviderSublayersDialog::treeSelectionChanged(
const QItemSelection &selected,
const QItemSelection & )
292 if ( mBlockSelectionChanges )
295 mBlockSelectionChanges =
true;
296 bool selectedANonLayerItem =
false;
297 QModelIndex firstSelectedNonLayerItem;
298 bool selectedALayerItem =
false;
299 for (
const QModelIndex &index : selected.indexes() )
301 if ( index.column() != 0 )
306 if ( !selectedANonLayerItem )
308 selectedANonLayerItem =
true;
309 firstSelectedNonLayerItem = index;
314 mLayersTree->selectionModel()->select( QItemSelection( mLayersTree->model()->index( index.row(), 0, index.parent() ), mLayersTree->model()->index( index.row(), mLayersTree->model()->columnCount() - 1, index.parent() ) ), QItemSelectionModel::Deselect );
319 selectedALayerItem =
true;
323 for (
int row = 0; row < mProxyModel->rowCount(); ++row )
325 const QModelIndex index = mProxyModel->index( row, 0 );
328 if ( ( selectedANonLayerItem && index != firstSelectedNonLayerItem ) || selectedALayerItem )
330 mLayersTree->selectionModel()->select( QItemSelection( mLayersTree->model()->index( index.row(), 0, index.parent() ), mLayersTree->model()->index( index.row(), mLayersTree->model()->columnCount() - 1, index.parent() ) ), QItemSelectionModel::Deselect );
335 if ( selectedANonLayerItem )
337 mLayersTree->selectionModel()->select( QItemSelection( mLayersTree->model()->index( index.row(), 0, index.parent() ), mLayersTree->model()->index( index.row(), mLayersTree->model()->columnCount() - 1, index.parent() ) ), QItemSelectionModel::Deselect );
341 mBlockSelectionChanges =
false;
343 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( !mLayersTree->selectionModel()->selectedRows().empty() );
345 mCbxAddToGroup->setEnabled( !selectedANonLayerItem );
346 mButtonBox->button( QDialogButtonBox::Ok )->setText( selectedANonLayerItem ? tr(
"Open" ) : tr(
"Add Layers" ) );
349void QgsProviderSublayersDialog::selectAll()
351 mLayersTree->selectionModel()->clear();
353 std::function<void(
const QModelIndex & )> selectAllInParent;
355 selectAllInParent = [
this, &selectAllInParent](
const QModelIndex &parent ) {
356 for (
int row = 0; row < mProxyModel->rowCount( parent ); ++row )
358 const QModelIndex index = mProxyModel->index( row, 0, parent );
361 mLayersTree->selectionModel()->select( QItemSelection( mLayersTree->model()->index( index.row(), 0, index.parent() ), mLayersTree->model()->index( index.row(), mLayersTree->model()->columnCount() - 1, index.parent() ) ), QItemSelectionModel::Select );
363 selectAllInParent( index );
366 selectAllInParent( QModelIndex() );
368 mButtonBox->button( QDialogButtonBox::Ok )->setFocus();
@ SystemTable
Sublayer is a system or internal table, which should be hidden by default.
static QgsTaskManager * taskManager()
Returns the application's task manager, used for managing application wide background task handling.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
static QgsNative * nativePlatformInterface()
Returns the global native interface, which offers abstraction to the host OS's underlying public inte...
static QString formatLayerName(const QString &name)
A convenience function to capitalize and format a layer name.
Contains details about a sub layer available from a dataset.
Qgis::LayerType type() const
Returns the layer type.
Qgis::WkbType wkbType() const
Returns the layer's WKB type, or QgsWkbTypes::Unknown if the WKB type is not application or unknown.
Qgis::SublayerFlags flags() const
Returns the layer's flags, which indicate properties of the layer.
A model for representing the sublayers present in a URI for the QgsProviderSublayersDialog.
QVariant data(const QModelIndex &index, int role) const override
QgsProviderSublayerDialogModel(QObject *parent=nullptr)
Constructor.
void setGeometryTypesResolved(bool resolved)
Sets whether geometry types are resolved.
Qt::ItemFlags flags(const QModelIndex &index) const override
Contains details for a non-sublayer item to include in a QgsProviderSublayerModel.
QList< QgsProviderSublayerDetails > mSublayers
Sublayer list.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role) const override
QgsProviderSublayerModel(QObject *parent=nullptr)
Constructor for QgsProviderSublayerModel, with the specified parent object.
@ IsNonLayerItem
true if item is a non-sublayer item (e.g. an embedded project)
QModelIndex parent(const QModelIndex &index) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
int rowCount(const QModelIndex &parent) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
@ Description
Layer description.
A QSortFilterProxyModel for filtering and sorting a QgsProviderSublayerModel.
void setIncludeSystemTables(bool include)
Sets whether system and internal tables will be shown in the model.
void setFilterString(const QString &filter)
Sets the filter string used for filtering items in the model.
void setIncludeEmptyLayers(bool include)
Sets whether empty tables will be shown in the model.
A QgsTask which retrieves sublayer details for a URI.
void setGroupName(const QString &groupNameIn)
Sets an appropriate name for the layer group.
~QgsProviderSublayersDialog() override
QString groupName() const
Returns an appropriate name for the layer group.
void setNonLayerItems(const QList< QgsProviderSublayerModel::NonLayerItem > &items)
Set list of non-layer items (e.g.
QList< QgsProviderSublayerModel::NonLayerItem > selectedNonLayerItems() const
Returns the list of selected non-layer items (e.g.
QList< QgsProviderSublayerDetails > selectedLayers() const
Returns the list of selected layers.
QgsProviderSublayersDialog(const QString &uri, const QString &providerKey, const QString &filePath, const QList< QgsProviderSublayerDetails > initialDetails=QList< QgsProviderSublayerDetails >(), const QList< Qgis::LayerType > &acceptableTypes=QList< Qgis::LayerType >(), QWidget *parent SIP_TRANSFERTHIS=nullptr, Qt::WindowFlags fl=Qt::WindowFlags())
Constructor.
static bool sublayerDetailsAreIncomplete(const QList< QgsProviderSublayerDetails > &details, QgsProviderUtils::SublayerCompletenessFlags flags=QgsProviderUtils::SublayerCompletenessFlags())
Returns true if the sublayer details are incomplete, and require a more in-depth scan.
static QString suggestLayerNameFromFilePath(const QString &path)
Suggests a suitable layer name given only a file path.
Stores settings for use within QGIS.
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.
long addTask(QgsTask *task, int priority=0)
Adds a task to the manager.
void taskCompleted()
Will be emitted by task to indicate its successful completion.