18 #include "qgssettings.h"
22 #include <QTableWidgetItem>
23 #include <QPushButton>
26 class SubLayerItem :
public QTreeWidgetItem
29 SubLayerItem(
const QStringList &strings,
int type = QTreeWidgetItem::Type )
30 : QTreeWidgetItem( strings, type )
33 bool operator <(
const QTreeWidgetItem &other )
const override
36 int col = treeWidget()->sortColumn();
38 if ( col == 0 || ( col > 0 && d->
countColumn() == col ) )
39 return text( col ).toInt() < other.text( col ).toInt();
41 return text( col ) < other.text( col );
50 const QString &dataSourceUri )
51 : QDialog( parent, fl )
58 switch ( providerType )
61 title = tr(
"Select Vector Layers to Add…" );
62 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" )
63 << tr(
"Number of features" ) << tr(
"Geometry type" ) << tr(
"Description" ) );
69 title = tr(
"Select Raster Layers to Add…" );
70 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" ) );
73 title = tr(
"Select Mesh Layers to Add…" );
74 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Mesh name" ) );
77 title = tr(
"Select Layers to Add…" );
78 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" )
84 const QString dataSourceFilePath = dataSourceUriParsed.value( QStringLiteral(
"path" ) ).toString();
85 const QString filePath = dataSourceFilePath.isEmpty() ? dataSourceUri : dataSourceFilePath;
86 const QString fileName = QFileInfo( filePath ).fileName();
88 setWindowTitle( fileName.isEmpty() ? title : QStringLiteral(
"%1 | %2" ).arg( title, fileName ) );
89 mLblFilePath->setText( QDir::toNativeSeparators( QFileInfo( filePath ).canonicalFilePath() ) );
90 mLblFilePath->setVisible( ! fileName.isEmpty() );
93 connect( mBtnSelectAll, &QAbstractButton::pressed, layersTable, &QTreeView::selectAll );
94 connect( mBtnDeselectAll, &QAbstractButton::pressed,
this, &QgsSublayersDialog::mBtnDeselectAll_pressed );
95 connect( layersTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsSublayersDialog::layersTable_selectionChanged );
97 mCbxAddToGroup->setVisible(
false );
102 QgsSettings settings;
103 settings.setValue(
"/Windows/" +
mName +
"SubLayers/headerColumnCount",
104 layersTable->columnCount() );
105 settings.setValue(
"/Windows/" +
mName +
"SubLayers/headerState",
106 layersTable->header()->saveState() );
109 static bool _isLayerIdUnique(
int layerId, QTreeWidget *layersTable )
112 for (
int j = 0; j < layersTable->topLevelItemCount(); j++ )
114 if ( layersTable->topLevelItem( j )->text( 0 ).toInt() == layerId )
125 for (
int i = 0; i < layersTable->selectedItems().size(); i++ )
127 QTreeWidgetItem *item = layersTable->selectedItems().at( i );
130 def.
layerId = item->text( 0 ).toInt();
136 if ( !_isLayerIdUnique( def.
layerId, layersTable ) )
148 const auto constList = list;
151 QStringList elements;
152 elements << QString::number( item.layerId ) << item.layerName;
154 elements << ( item.count == -1 ? tr(
"Unknown" ) : QString::number( item.count ) );
156 elements << item.type;
158 elements << item.description;
159 layersTable->addTopLevelItem(
new SubLayerItem( elements ) );
163 QgsSettings settings;
164 QByteArray ba = settings.value(
"/Windows/" +
mName +
"SubLayers/headerState" ).toByteArray();
165 int savedColumnCount = settings.value(
"/Windows/" +
mName +
"SubLayers/headerColumnCount" ).toInt();
166 if ( ! ba.isNull() && savedColumnCount == layersTable->columnCount() )
168 layersTable->header()->restoreState( ba );
172 for (
int i = 0; i < layersTable->columnCount(); i++ )
173 layersTable->resizeColumnToContents( i );
174 layersTable->setColumnWidth( 1, layersTable->columnWidth( 1 ) + 10 );
183 QgsSettings settings;
184 PromptMode promptLayers = settings.enumValue( QStringLiteral(
"qgis/promptForSublayers" ),
PromptAlways );
187 if ( layersTable->topLevelItemCount() == 0 )
188 return QDialog::Rejected;
190 layersTable->selectAll();
194 return QDialog::Rejected;
196 return QDialog::Accepted;
199 if ( layersTable->topLevelItemCount() == 1 )
200 return QDialog::Accepted;
202 layersTable->sortByColumn( 1, Qt::AscendingOrder );
203 layersTable->setSortingEnabled(
true );
208 bool overrideCursor =
nullptr != QApplication::overrideCursor();
209 if ( overrideCursor )
211 cursor = QCursor( * QApplication::overrideCursor() );
212 QApplication::restoreOverrideCursor();
216 if ( mShowAddToGroupCheckbox )
218 mCbxAddToGroup->setVisible(
true );
219 bool addToGroup = settings.value( QStringLiteral(
"/qgis/openSublayersInGroup" ),
false ).toBool();
220 mCbxAddToGroup->setChecked( addToGroup );
223 int ret = QDialog::exec();
224 if ( overrideCursor )
225 QApplication::setOverrideCursor( cursor );
227 if ( mShowAddToGroupCheckbox )
228 settings.setValue( QStringLiteral(
"/qgis/openSublayersInGroup" ), mCbxAddToGroup->isChecked() );
232 void QgsSublayersDialog::layersTable_selectionChanged(
const QItemSelection &,
const QItemSelection & )
234 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( layersTable->selectedItems().length() > 0 );
237 void QgsSublayersDialog::mBtnDeselectAll_pressed()
239 layersTable->selectionModel()->clear();
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...
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
bool mShowType
Whether to show type in the table.
QList< QgsSublayersDialog::LayerDefinition > LayerDefinitionList
List of layer definitions for the purpose of this dialog.
PromptMode
Prompt behavior of the QgsSublayersDialog.
@ PromptNever
never prompt, will not load anything
@ PromptAlways
always ask if there are existing sublayers
@ PromptLoadAll
never prompt, but load all sublayers
QgsSublayersDialog(ProviderType providerType, const QString &name, QWidget *parent=nullptr, Qt::WindowFlags fl=Qt::WindowFlags(), const QString &dataSourceUri=QString())
Constructor for QgsSublayersDialog.
LayerDefinitionList selection()
Returns list of selected layers.
bool mShowCount
Whether to show number of features in the table.
bool mShowDescription
Whether to show description in the table.
~QgsSublayersDialog() override
void populateLayerTable(const LayerDefinitionList &list)
Populate the table with layers.
int countColumn() const
Returns column with count or -1.
QString mName
Provider type name.
bool operator<(const QVariant &v1, const QVariant &v2)
Compares two QVariant values and returns whether the first is less than the second.
A structure that defines layers for the purpose of this dialog.
QString layerName
Name of the layer (not necessarily unique)
QString type
Extra type depending on the use (e.g. geometry type for vector sublayers)
int layerId
Identifier of the layer (one unique layer id may have multiple types though)