17#include "moc_qgssublayersdialog.cpp"
23#include <QTableWidgetItem>
27class SubLayerItem :
public QTreeWidgetItem
30 SubLayerItem(
const QStringList &strings,
int type = QTreeWidgetItem::Type )
31 : QTreeWidgetItem( strings, type )
34 bool operator<(
const QTreeWidgetItem &other )
const override
37 const int col = treeWidget()->sortColumn();
39 if ( col == 0 || ( col > 0 && d->
countColumn() == col ) )
40 return text( col ).toInt() < other.text( col ).toInt();
42 return text( col ) < other.text( col );
48 : QDialog( parent, fl )
55 switch ( providerType )
58 title = tr(
"Select Vector Layers to Add…" );
59 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" ) << tr(
"Number of features" ) << tr(
"Geometry type" ) << tr(
"Description" ) );
65 title = tr(
"Select Raster Layers to Add…" );
66 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" ) );
69 title = tr(
"Select Mesh Layers to Add…" );
70 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Mesh name" ) );
73 title = tr(
"Select Layers to Add…" );
74 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" ) << tr(
"Type" ) );
79 const QString dataSourceFilePath = dataSourceUriParsed.value( QStringLiteral(
"path" ) ).toString();
80 const QString filePath = dataSourceFilePath.isEmpty() ? dataSourceUri : dataSourceFilePath;
81 const QString fileName = QFileInfo( filePath ).fileName();
83 setWindowTitle( fileName.isEmpty() ? title : QStringLiteral(
"%1 | %2" ).arg( title, fileName ) );
84 mLblFilePath->setText( QDir::toNativeSeparators( QFileInfo( filePath ).canonicalFilePath() ) );
85 mLblFilePath->setVisible( !fileName.isEmpty() );
88 connect( mBtnSelectAll, &QAbstractButton::pressed, layersTable, &QTreeView::selectAll );
89 connect( mBtnDeselectAll, &QAbstractButton::pressed,
this, &QgsSublayersDialog::mBtnDeselectAll_pressed );
90 connect( layersTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsSublayersDialog::layersTable_selectionChanged );
92 mCbxAddToGroup->setVisible(
false );
98 settings.
setValue(
"/Windows/" +
mName +
"SubLayers/headerColumnCount", layersTable->columnCount() );
99 settings.
setValue(
"/Windows/" +
mName +
"SubLayers/headerState", layersTable->header()->saveState() );
102static bool _isLayerIdUnique(
int layerId, QTreeWidget *layersTable )
105 for (
int j = 0; j < layersTable->topLevelItemCount(); j++ )
107 if ( layersTable->topLevelItem( j )->text( 0 ).toInt() == layerId )
118 for (
int i = 0; i < layersTable->selectedItems().size(); i++ )
120 QTreeWidgetItem *item = layersTable->selectedItems().at( i );
123 def.
layerId = item->text( 0 ).toInt();
129 if ( !_isLayerIdUnique( def.
layerId, layersTable ) )
141 const auto constList = list;
144 QStringList elements;
145 elements << QString::number( item.layerId ) << item.
layerName;
149 elements << item.type;
151 elements << item.description;
152 layersTable->addTopLevelItem(
new SubLayerItem( elements ) );
157 const QByteArray ba = settings.
value(
"/Windows/" +
mName +
"SubLayers/headerState" ).toByteArray();
158 const int savedColumnCount = settings.
value(
"/Windows/" +
mName +
"SubLayers/headerColumnCount" ).toInt();
159 if ( !ba.isNull() && savedColumnCount == layersTable->columnCount() )
161 layersTable->header()->restoreState( ba );
165 for (
int i = 0; i < layersTable->columnCount(); i++ )
166 layersTable->resizeColumnToContents( i );
167 layersTable->setColumnWidth( 1, layersTable->columnWidth( 1 ) + 10 );
180 if ( layersTable->topLevelItemCount() == 0 )
181 return QDialog::Rejected;
183 layersTable->selectAll();
187 return QDialog::Rejected;
189 return QDialog::Accepted;
192 if ( layersTable->topLevelItemCount() == 1 )
193 return QDialog::Accepted;
195 layersTable->sortByColumn( 1, Qt::AscendingOrder );
196 layersTable->setSortingEnabled(
true );
201 const bool overrideCursor =
nullptr != QApplication::overrideCursor();
202 if ( overrideCursor )
204 cursor = QCursor( *QApplication::overrideCursor() );
205 QApplication::restoreOverrideCursor();
209 if ( mShowAddToGroupCheckbox )
211 mCbxAddToGroup->setVisible(
true );
212 const bool addToGroup = settings.
value( QStringLiteral(
"/qgis/openSublayersInGroup" ),
false ).toBool();
213 mCbxAddToGroup->setChecked( addToGroup );
216 const int ret = QDialog::exec();
217 if ( overrideCursor )
218 QApplication::setOverrideCursor( cursor );
220 if ( mShowAddToGroupCheckbox )
221 settings.
setValue( QStringLiteral(
"/qgis/openSublayersInGroup" ), mCbxAddToGroup->isChecked() );
225void QgsSublayersDialog::layersTable_selectionChanged(
const QItemSelection &,
const QItemSelection & )
227 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( layersTable->selectedItems().length() > 0 );
230void QgsSublayersDialog::mBtnDeselectAll_pressed()
232 layersTable->selectionModel()->clear();
SublayerPromptMode
Specifies how to handle layer sources with multiple sublayers.
@ AlwaysAsk
Always ask users to select from available sublayers, if sublayers are present.
@ NeverAskLoadAll
Never ask users to select sublayers, instead automatically load all available sublayers.
@ NeverAskSkip
Never ask users to select sublayers, instead don't load anything.
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.
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.
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on an enum.
bool mShowType
Whether to show type in the table.
QList< QgsSublayersDialog::LayerDefinition > LayerDefinitionList
List of layer definitions for the purpose of this dialog.
Q_DECL_DEPRECATED 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.
@ Uncounted
Feature count not yet computed.
@ UnknownCount
Provider returned an unknown feature count.
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)