17#include "moc_qgsprovidersublayersdialog.cpp"
30#include <QDesktopServices>
40 if ( !
index.isValid() )
43 QgsProviderSublayerModelNode *node = index2node(
index );
47 if ( QgsProviderSublayerModelSublayerNode *sublayerNode =
dynamic_cast<QgsProviderSublayerModelSublayerNode *
>( node ) )
59 return tr(
"Scanning…" );
90 if ( !
index.isValid() )
103 return Qt::ItemFlags();
111 mGeometryTypesResolved = resolved;
116 : QDialog( parent, fl )
121 const QFileInfo fileInfo( filePathIn );
122 const QString filePath = ( fileInfo.isFile() || fileInfo.isDir() ) && fileInfo.exists() ? filePathIn : QString();
125 if ( !fileName.isEmpty() )
130 setWindowTitle( fileName.isEmpty() ? tr(
"Select Items to Add" ) : QStringLiteral(
"%1 | %2" ).arg( tr(
"Select Items to Add" ), fileName ) );
132 mLblFilePath->setText( QStringLiteral(
"<a href=\"%1\">%2</a>" )
133 .arg( QUrl::fromLocalFile( filePath ).toString(), QDir::toNativeSeparators( QFileInfo( filePath ).canonicalFilePath() ) ) );
134 mLblFilePath->setVisible( !filePath.isEmpty() );
135 mLblFilePath->setWordWrap(
true );
136 mLblFilePath->setTextInteractionFlags( Qt::TextBrowserInteraction );
137 connect( mLblFilePath, &QLabel::linkActivated,
this, [=](
const QString &link ) {
138 const QUrl url( link );
139 const QFileInfo file( url.toLocalFile() );
140 if ( file.exists() && !file.isDir() )
143 QDesktopServices::openUrl( url );
147 mModel->setSublayerDetails( initialDetails );
149 mProxyModel->setSourceModel( mModel );
150 mLayersTree->setModel( mProxyModel );
152 mLayersTree->expandAll();
155 const bool addToGroup = settings.
value( QStringLiteral(
"/qgis/openSublayersInGroup" ),
false ).toBool();
156 mCbxAddToGroup->setChecked( addToGroup );
157 mCbxAddToGroup->setVisible( !fileName.isEmpty() );
160 const QByteArray ba = settings.
value(
"/Windows/SubLayers/headerState" ).toByteArray();
163 mLayersTree->header()->restoreState( ba );
167 for (
int i = 0; i < mModel->columnCount(); i++ )
168 mLayersTree->resizeColumnToContents( i );
169 mLayersTree->setColumnWidth( 1, mLayersTree->columnWidth( 1 ) + 10 );
177 QList<QgsProviderSublayerDetails> res = mTask->results();
178 res.erase( std::remove_if( res.begin(), res.end(), [acceptableTypes]( const QgsProviderSublayerDetails &sublayer ) {
179 return !acceptableTypes.empty() && !acceptableTypes.contains( sublayer.type() );
183 mModel->setSublayerDetails( res );
184 mModel->setGeometryTypesResolved(
true );
186 mLayersTree->expandAll();
192 connect( mBtnSelectAll, &QAbstractButton::pressed,
this, &QgsProviderSublayersDialog::selectAll );
193 connect( mBtnDeselectAll, &QAbstractButton::pressed,
this, [=] { mLayersTree->selectionModel()->clear(); } );
194 connect( mLayersTree->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsProviderSublayersDialog::treeSelectionChanged );
198 connect( mLayersTree, &QTreeView::doubleClicked,
this, [=](
const QModelIndex &index ) {
199 const QModelIndex left = mLayersTree->model()->index( index.row(), 0, index.parent() );
200 if ( !( left.flags() & Qt::ItemIsSelectable ) )
203 mLayersTree->selectionModel()->select( QItemSelection( left, mLayersTree->model()->index( index.row(), mLayersTree->model()->columnCount() - 1, index.parent() ) ), QItemSelectionModel::ClearAndSelect );
204 emit layersAdded( selectedLayers() );
207 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
208 connect( mButtonBox, &QDialogButtonBox::accepted,
this, [=] {
209 emit layersAdded( selectedLayers() );
212 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
213 mButtonBox->button( QDialogButtonBox::Ok )->setText( tr(
"Add Layers" ) );
229 settings.
setValue(
"/Windows/SubLayers/headerState", mLayersTree->header()->saveState() );
230 settings.
setValue( QStringLiteral(
"/qgis/openSublayersInGroup" ), mCbxAddToGroup->isChecked() );
238 const QModelIndexList selection = mLayersTree->selectionModel()->selectedRows();
239 QList<QgsProviderSublayerDetails> selectedSublayers;
240 for (
const QModelIndex &index : selection )
242 const QModelIndex sourceIndex = mProxyModel->mapToSource( index );
248 return selectedSublayers;
253 const QModelIndexList selection = mLayersTree->selectionModel()->selectedRows();
254 QList<QgsProviderSublayerModel::NonLayerItem> selectedItems;
255 for (
const QModelIndex &index : selection )
257 const QModelIndex sourceIndex = mProxyModel->mapToSource( index );
263 return selectedItems;
268 mGroupName = groupNameIn;
270 if ( settings.
value( QStringLiteral(
"qgis/formatLayerName" ),
false ).toBool() )
275 mCbxAddToGroup->setVisible( !mGroupName.isEmpty() );
280 if ( !mCbxAddToGroup->isChecked() )
285void QgsProviderSublayersDialog::treeSelectionChanged(
const QItemSelection &selected,
const QItemSelection & )
287 if ( mBlockSelectionChanges )
290 mBlockSelectionChanges =
true;
291 bool selectedANonLayerItem =
false;
292 QModelIndex firstSelectedNonLayerItem;
293 bool selectedALayerItem =
false;
294 for (
const QModelIndex &index : selected.indexes() )
296 if ( index.column() != 0 )
301 if ( !selectedANonLayerItem )
303 selectedANonLayerItem =
true;
304 firstSelectedNonLayerItem = index;
309 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 );
314 selectedALayerItem =
true;
318 for (
int row = 0; row < mProxyModel->rowCount(); ++row )
320 const QModelIndex index = mProxyModel->index( row, 0 );
323 if ( ( selectedANonLayerItem && index != firstSelectedNonLayerItem ) || selectedALayerItem )
325 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 );
330 if ( selectedANonLayerItem )
332 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 );
336 mBlockSelectionChanges =
false;
338 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( !mLayersTree->selectionModel()->selectedRows().empty() );
340 mCbxAddToGroup->setEnabled( !selectedANonLayerItem );
341 mButtonBox->button( QDialogButtonBox::Ok )->setText( selectedANonLayerItem ? tr(
"Open" ) : tr(
"Add Layers" ) );
344void QgsProviderSublayersDialog::selectAll()
346 mLayersTree->selectionModel()->clear();
348 std::function<void(
const QModelIndex & )> selectAllInParent;
350 selectAllInParent = [
this, &selectAllInParent](
const QModelIndex &parent ) {
351 for (
int row = 0; row < mProxyModel->rowCount( parent ); ++row )
353 const QModelIndex index = mProxyModel->index( row, 0, parent );
356 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 );
358 selectAllInParent( index );
361 selectAllInParent( QModelIndex() );
363 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.