28#include <QDesktopServices>
35#include "moc_qgsprovidersublayersdialog.cpp"
37using 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;
122 const QString &providerKey,
123 const QString &filePathIn,
124 const QList<QgsProviderSublayerDetails> initialDetails,
125 const QList<Qgis::LayerType> &acceptableTypes,
129 : QDialog( parent, fl )
134 const QFileInfo fileInfo( filePathIn );
135 const QString filePath = ( fileInfo.isFile() || fileInfo.isDir() ) && fileInfo.exists() ? filePathIn : QString();
138 if ( !fileName.isEmpty() )
143 setWindowTitle( fileName.isEmpty() ? tr(
"Select Items to Add" ) : u
"%1 | %2"_s.arg( tr(
"Select Items to Add" ), fileName ) );
145 mLblFilePath->setText( u
"<a href=\"%1\">%2</a>"_s.arg( QUrl::fromLocalFile( filePath ).toString(), QDir::toNativeSeparators( QFileInfo( filePath ).canonicalFilePath() ) ) );
146 mLblFilePath->setVisible( !filePath.isEmpty() );
147 mLblFilePath->setWordWrap(
true );
148 mLblFilePath->setTextInteractionFlags( Qt::TextBrowserInteraction );
149 connect( mLblFilePath, &QLabel::linkActivated,
this, [](
const QString &link ) {
150 const QUrl url( link );
151 const QFileInfo file( url.toLocalFile() );
152 if ( file.exists() && !file.isDir() )
155 QDesktopServices::openUrl( url );
159 mModel->setSublayerDetails( initialDetails );
161 mProxyModel->setSourceModel( mModel );
162 mLayersTree->setModel( mProxyModel );
164 mLayersTree->expandAll();
168 mCbxAddToGroup->setChecked( addToGroup );
169 mCbxAddToGroup->setVisible( !fileName.isEmpty() );
172 const QByteArray ba = settings.
value(
"/Windows/SubLayers/headerState" ).toByteArray();
175 mLayersTree->header()->restoreState( ba );
179 for (
int i = 0; i < mModel->columnCount(); i++ )
180 mLayersTree->resizeColumnToContents( i );
181 mLayersTree->setColumnWidth( 1, mLayersTree->columnWidth( 1 ) + 10 );
189 QList<QgsProviderSublayerDetails> res = mTask->results();
191 std::remove_if( res.begin(), res.end(), [acceptableTypes]( const QgsProviderSublayerDetails &sublayer ) { return !acceptableTypes.empty() && !acceptableTypes.contains( sublayer.type() ); } ),
195 mModel->setSublayerDetails( res );
196 mModel->setGeometryTypesResolved(
true );
198 mLayersTree->expandAll();
204 connect( mBtnSelectAll, &QAbstractButton::pressed,
this, &QgsProviderSublayersDialog::selectAll );
205 connect( mBtnDeselectAll, &QAbstractButton::pressed,
this, [
this] { mLayersTree->selectionModel()->clear(); } );
206 connect( mLayersTree->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsProviderSublayersDialog::treeSelectionChanged );
210 connect( mLayersTree, &QTreeView::doubleClicked,
this, [
this](
const QModelIndex &index ) {
211 const QModelIndex left = mLayersTree->model()->index( index.row(), 0, index.parent() );
212 if ( !( left.flags() & Qt::ItemIsSelectable ) )
215 mLayersTree->selectionModel()->select( QItemSelection( left, mLayersTree->model()->index( index.row(), mLayersTree->model()->columnCount() - 1, index.parent() ) ), QItemSelectionModel::ClearAndSelect );
216 emit layersAdded( selectedLayers() );
219 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
220 connect( mButtonBox, &QDialogButtonBox::accepted,
this, [
this] {
221 emit layersAdded( selectedLayers() );
224 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
225 mButtonBox->button( QDialogButtonBox::Ok )->setText( tr(
"Add Layers" ) );
234 mModel->addNonLayerItem( item );
241 settings.
setValue(
"/Windows/SubLayers/headerState", mLayersTree->header()->saveState() );
250 const QModelIndexList selection = mLayersTree->selectionModel()->selectedRows();
251 QList<QgsProviderSublayerDetails> selectedSublayers;
252 for (
const QModelIndex &index : selection )
254 const QModelIndex sourceIndex = mProxyModel->mapToSource( index );
257 selectedSublayers << mModel->indexToSublayer( sourceIndex );
260 return selectedSublayers;
265 const QModelIndexList selection = mLayersTree->selectionModel()->selectedRows();
266 QList<QgsProviderSublayerModel::NonLayerItem> selectedItems;
267 for (
const QModelIndex &index : selection )
269 const QModelIndex sourceIndex = mProxyModel->mapToSource( index );
272 selectedItems << mModel->indexToNonLayerItem( sourceIndex );
275 return selectedItems;
280 mGroupName = groupNameIn;
287 mCbxAddToGroup->setVisible( !mGroupName.isEmpty() );
292 if ( !mCbxAddToGroup->isChecked() )
297void QgsProviderSublayersDialog::treeSelectionChanged(
const QItemSelection &selected,
const QItemSelection & )
299 if ( mBlockSelectionChanges )
302 mBlockSelectionChanges =
true;
303 bool selectedANonLayerItem =
false;
304 QModelIndex firstSelectedNonLayerItem;
305 bool selectedALayerItem =
false;
306 for (
const QModelIndex &index : selected.indexes() )
308 if ( index.column() != 0 )
313 if ( !selectedANonLayerItem )
315 selectedANonLayerItem =
true;
316 firstSelectedNonLayerItem = index;
321 mLayersTree->selectionModel()
322 ->select( QItemSelection( mLayersTree->model()->index( index.row(), 0, index.parent() ), mLayersTree->model()->index( index.row(), mLayersTree->model()->columnCount() - 1, index.parent() ) ), QItemSelectionModel::Deselect );
327 selectedALayerItem =
true;
331 for (
int row = 0; row < mProxyModel->rowCount(); ++row )
333 const QModelIndex index = mProxyModel->index( row, 0 );
336 if ( ( selectedANonLayerItem && index != firstSelectedNonLayerItem ) || selectedALayerItem )
338 mLayersTree->selectionModel()
339 ->select( QItemSelection( mLayersTree->model()->index( index.row(), 0, index.parent() ), mLayersTree->model()->index( index.row(), mLayersTree->model()->columnCount() - 1, index.parent() ) ), QItemSelectionModel::Deselect );
344 if ( selectedANonLayerItem )
346 mLayersTree->selectionModel()
347 ->select( QItemSelection( mLayersTree->model()->index( index.row(), 0, index.parent() ), mLayersTree->model()->index( index.row(), mLayersTree->model()->columnCount() - 1, index.parent() ) ), QItemSelectionModel::Deselect );
351 mBlockSelectionChanges =
false;
353 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( !mLayersTree->selectionModel()->selectedRows().empty() );
355 mCbxAddToGroup->setEnabled( !selectedANonLayerItem );
356 mButtonBox->button( QDialogButtonBox::Ok )->setText( selectedANonLayerItem ? tr(
"Open" ) : tr(
"Add Layers" ) );
359void QgsProviderSublayersDialog::selectAll()
361 mLayersTree->selectionModel()->clear();
363 std::function<void(
const QModelIndex & )> selectAllInParent;
365 selectAllInParent = [
this, &selectAllInParent](
const QModelIndex &parent ) {
366 for (
int row = 0; row < mProxyModel->rowCount( parent ); ++row )
368 const QModelIndex index = mProxyModel->index( row, 0, parent );
371 mLayersTree->selectionModel()
372 ->select( QItemSelection( mLayersTree->model()->index( index.row(), 0, index.parent() ), mLayersTree->model()->index( index.row(), mLayersTree->model()->columnCount() - 1, index.parent() ) ), QItemSelectionModel::Select );
374 selectAllInParent( index );
377 selectAllInParent( QModelIndex() );
379 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.
static const QgsSettingsEntryBool * settingsOpenSublayersInGroup
Settings entry whether to open sublayers in a group.
static const QgsSettingsEntryBool * settingsFormatLayerName
Settings entry whether to format layer names.
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.