17#include "moc_qgsprovidersublayersdialog.cpp"
30#include <QDesktopServices>
41 if ( !
index.isValid() )
44 QgsProviderSublayerModelNode *node = index2node(
index );
48 if ( QgsProviderSublayerModelSublayerNode *sublayerNode =
dynamic_cast<QgsProviderSublayerModelSublayerNode *
>( node ) )
60 return tr(
"Scanning…" );
91 if ( !
index.isValid() )
104 return Qt::ItemFlags();
112 mGeometryTypesResolved = resolved;
117 : QDialog( parent, fl )
122 const QFileInfo fileInfo( filePathIn );
123 const QString filePath = ( fileInfo.isFile() || fileInfo.isDir() ) && fileInfo.exists() ? filePathIn : QString();
126 if ( !fileName.isEmpty() )
131 setWindowTitle( fileName.isEmpty() ? tr(
"Select Items to Add" ) : QStringLiteral(
"%1 | %2" ).arg( tr(
"Select Items to Add" ), fileName ) );
133 mLblFilePath->setText( QStringLiteral(
"<a href=\"%1\">%2</a>" )
134 .arg( QUrl::fromLocalFile( filePath ).toString(), QDir::toNativeSeparators( QFileInfo( filePath ).canonicalFilePath() ) ) );
135 mLblFilePath->setVisible( !filePath.isEmpty() );
136 mLblFilePath->setWordWrap(
true );
137 mLblFilePath->setTextInteractionFlags( Qt::TextBrowserInteraction );
138 connect( mLblFilePath, &QLabel::linkActivated,
this, [ = ](
const QString & link )
140 const QUrl url( link );
141 const QFileInfo file( url.toLocalFile() );
142 if ( file.exists() && !file.isDir() )
145 QDesktopServices::openUrl( url );
149 mModel->setSublayerDetails( initialDetails );
151 mProxyModel->setSourceModel( mModel );
152 mLayersTree->setModel( mProxyModel );
154 mLayersTree->expandAll();
157 const bool addToGroup = settings.
value( QStringLiteral(
"/qgis/openSublayersInGroup" ),
false ).toBool();
158 mCbxAddToGroup->setChecked( addToGroup );
159 mCbxAddToGroup->setVisible( !fileName.isEmpty() );
162 const QByteArray ba = settings.
value(
"/Windows/SubLayers/headerState" ).toByteArray();
165 mLayersTree->header()->restoreState( ba );
169 for (
int i = 0; i < mModel->columnCount(); i++ )
170 mLayersTree->resizeColumnToContents( i );
171 mLayersTree->setColumnWidth( 1, mLayersTree->columnWidth( 1 ) + 10 );
180 QList< QgsProviderSublayerDetails > res = mTask->results();
181 res.erase( std::remove_if( res.begin(), res.end(), [acceptableTypes]( const QgsProviderSublayerDetails & sublayer )
183 return !acceptableTypes.empty() && !acceptableTypes.contains( sublayer.type() );
186 mModel->setSublayerDetails( res );
187 mModel->setGeometryTypesResolved(
true );
189 mLayersTree->expandAll();
195 connect( mBtnSelectAll, &QAbstractButton::pressed,
this, &QgsProviderSublayersDialog::selectAll );
196 connect( mBtnDeselectAll, &QAbstractButton::pressed,
this, [ = ] { mLayersTree->selectionModel()->clear(); } );
197 connect( mLayersTree->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsProviderSublayersDialog::treeSelectionChanged );
201 connect( mLayersTree, &QTreeView::doubleClicked,
this, [ = ](
const QModelIndex & index )
203 const QModelIndex left = mLayersTree->model()->index( index.row(), 0, index.parent() );
204 if ( !( left.flags() & Qt::ItemIsSelectable ) )
207 mLayersTree->selectionModel()->select( QItemSelection( left,
208 mLayersTree->model()->index( index.row(), mLayersTree->model()->columnCount() - 1, index.parent() ) ),
209 QItemSelectionModel::ClearAndSelect );
210 emit layersAdded( selectedLayers() );
213 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
214 connect( mButtonBox, &QDialogButtonBox::accepted,
this, [ = ]
216 emit layersAdded( selectedLayers() );
219 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
220 mButtonBox->button( QDialogButtonBox::Ok )->setText( tr(
"Add Layers" ) );
236 settings.
setValue(
"/Windows/SubLayers/headerState", mLayersTree->header()->saveState() );
237 settings.
setValue( QStringLiteral(
"/qgis/openSublayersInGroup" ), mCbxAddToGroup->isChecked() );
245 const QModelIndexList selection = mLayersTree->selectionModel()->selectedRows();
246 QList< QgsProviderSublayerDetails > selectedSublayers;
247 for (
const QModelIndex &index : selection )
249 const QModelIndex sourceIndex = mProxyModel->mapToSource( index );
255 return selectedSublayers;
260 const QModelIndexList selection = mLayersTree->selectionModel()->selectedRows();
261 QList< QgsProviderSublayerModel::NonLayerItem > selectedItems;
262 for (
const QModelIndex &index : selection )
264 const QModelIndex sourceIndex = mProxyModel->mapToSource( index );
270 return selectedItems;
275 mGroupName = groupNameIn;
277 if ( settings.
value( QStringLiteral(
"qgis/formatLayerName" ),
false ).toBool() )
282 mCbxAddToGroup->setVisible( !mGroupName.isEmpty() );
287 if ( !mCbxAddToGroup->isChecked() )
292void QgsProviderSublayersDialog::treeSelectionChanged(
const QItemSelection &selected,
const QItemSelection & )
294 if ( mBlockSelectionChanges )
297 mBlockSelectionChanges =
true;
298 bool selectedANonLayerItem =
false;
299 QModelIndex firstSelectedNonLayerItem;
300 bool selectedALayerItem =
false;
301 for (
const QModelIndex &index : selected.indexes() )
303 if ( index.column() != 0 )
308 if ( !selectedANonLayerItem )
310 selectedANonLayerItem =
true;
311 firstSelectedNonLayerItem = index;
316 mLayersTree->selectionModel()->select( QItemSelection( mLayersTree->model()->index( index.row(), 0, index.parent() ),
317 mLayersTree->model()->index( index.row(), mLayersTree->model()->columnCount() - 1, index.parent() ) ),
318 QItemSelectionModel::Deselect );
323 selectedALayerItem =
true;
327 for (
int row = 0; row < mProxyModel->rowCount(); ++row )
329 const QModelIndex index = mProxyModel->index( row, 0 );
332 if ( ( selectedANonLayerItem && index != firstSelectedNonLayerItem ) || selectedALayerItem )
334 mLayersTree->selectionModel()->select( QItemSelection( mLayersTree->model()->index( index.row(), 0, index.parent() ),
335 mLayersTree->model()->index( index.row(), mLayersTree->model()->columnCount() - 1, index.parent() ) ),
336 QItemSelectionModel::Deselect );
341 if ( selectedANonLayerItem )
343 mLayersTree->selectionModel()->select( QItemSelection( mLayersTree->model()->index( index.row(), 0, index.parent() ),
344 mLayersTree->model()->index( index.row(), mLayersTree->model()->columnCount() - 1, index.parent() ) ),
345 QItemSelectionModel::Deselect );
349 mBlockSelectionChanges =
false;
351 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( !mLayersTree->selectionModel()->selectedRows().empty() );
353 mCbxAddToGroup->setEnabled( !selectedANonLayerItem );
354 mButtonBox->button( QDialogButtonBox::Ok )->setText( selectedANonLayerItem ? tr(
"Open" ) : tr(
"Add Layers" ) );
357void QgsProviderSublayersDialog::selectAll()
359 mLayersTree->selectionModel()->clear();
361 std::function< void(
const QModelIndex & ) > selectAllInParent;
363 selectAllInParent = [
this, &selectAllInParent](
const QModelIndex & parent )
365 for (
int row = 0; row < mProxyModel->rowCount( parent ); ++row )
367 const QModelIndex index = mProxyModel->index( row, 0, parent );
370 mLayersTree->selectionModel()->select( QItemSelection( mLayersTree->model()->index( index.row(), 0, index.parent() ),
371 mLayersTree->model()->index( index.row(), mLayersTree->model()->columnCount() - 1, index.parent() ) ),
372 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.
A model for representing the sublayers present in a URI.
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
QgsProviderSublayerDetails indexToSublayer(const QModelIndex &index) const
Returns the sublayer corresponding to the given index.
@ IsNonLayerItem
true if item is a non-sublayer item (e.g. an embedded project)
QgsProviderSublayerModel::NonLayerItem indexToNonLayerItem(const QModelIndex &index) const
Returns the non layer item corresponding to the given index.
void addNonLayerItem(const QgsProviderSublayerModel::NonLayerItem &item)
Adds a non-layer item (e.g.
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.
This class is a composition of two QSettings instances:
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.