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 );
51 const QString &dataSourceUri )
52 : QDialog( parent, fl )
59 switch ( providerType )
62 title = tr(
"Select Vector Layers to Add…" );
63 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" )
64 << tr(
"Number of features" ) << tr(
"Geometry type" ) << tr(
"Description" ) );
70 title = tr(
"Select Raster Layers to Add…" );
71 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" ) );
74 title = tr(
"Select Mesh Layers to Add…" );
75 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Mesh name" ) );
78 title = tr(
"Select Layers to Add…" );
79 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" )
85 const QString dataSourceFilePath = dataSourceUriParsed.value( QStringLiteral(
"path" ) ).toString();
86 const QString filePath = dataSourceFilePath.isEmpty() ? dataSourceUri : dataSourceFilePath;
87 const QString fileName = QFileInfo( filePath ).fileName();
89 setWindowTitle( fileName.isEmpty() ? title : QStringLiteral(
"%1 | %2" ).arg( title, fileName ) );
90 mLblFilePath->setText( QDir::toNativeSeparators( QFileInfo( filePath ).canonicalFilePath() ) );
91 mLblFilePath->setVisible( ! fileName.isEmpty() );
94 connect( mBtnSelectAll, &QAbstractButton::pressed, layersTable, &QTreeView::selectAll );
95 connect( mBtnDeselectAll, &QAbstractButton::pressed,
this, &QgsSublayersDialog::mBtnDeselectAll_pressed );
96 connect( layersTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsSublayersDialog::layersTable_selectionChanged );
98 mCbxAddToGroup->setVisible(
false );
104 settings.
setValue(
"/Windows/" +
mName +
"SubLayers/headerColumnCount",
105 layersTable->columnCount() );
106 settings.
setValue(
"/Windows/" +
mName +
"SubLayers/headerState",
107 layersTable->header()->saveState() );
110static bool _isLayerIdUnique(
int layerId, QTreeWidget *layersTable )
113 for (
int j = 0; j < layersTable->topLevelItemCount(); j++ )
115 if ( layersTable->topLevelItem( j )->text( 0 ).toInt() == layerId )
126 for (
int i = 0; i < layersTable->selectedItems().size(); i++ )
128 QTreeWidgetItem *item = layersTable->selectedItems().at( i );
131 def.
layerId = item->text( 0 ).toInt();
137 if ( !_isLayerIdUnique( def.
layerId, layersTable ) )
149 const auto constList = list;
152 QStringList elements;
153 elements << QString::number( item.layerId ) << item.
layerName;
157 ? tr(
"Unknown" ) : QString::number( item.count ) );
159 elements << item.type;
161 elements << item.description;
162 layersTable->addTopLevelItem(
new SubLayerItem( elements ) );
167 const QByteArray ba = settings.
value(
"/Windows/" +
mName +
"SubLayers/headerState" ).toByteArray();
168 const int savedColumnCount = settings.
value(
"/Windows/" +
mName +
"SubLayers/headerColumnCount" ).toInt();
169 if ( ! ba.isNull() && savedColumnCount == layersTable->columnCount() )
171 layersTable->header()->restoreState( ba );
175 for (
int i = 0; i < layersTable->columnCount(); i++ )
176 layersTable->resizeColumnToContents( i );
177 layersTable->setColumnWidth( 1, layersTable->columnWidth( 1 ) + 10 );
190 if ( layersTable->topLevelItemCount() == 0 )
191 return QDialog::Rejected;
193 layersTable->selectAll();
197 return QDialog::Rejected;
199 return QDialog::Accepted;
202 if ( layersTable->topLevelItemCount() == 1 )
203 return QDialog::Accepted;
205 layersTable->sortByColumn( 1, Qt::AscendingOrder );
206 layersTable->setSortingEnabled(
true );
211 const bool overrideCursor =
nullptr != QApplication::overrideCursor();
212 if ( overrideCursor )
214 cursor = QCursor( * QApplication::overrideCursor() );
215 QApplication::restoreOverrideCursor();
219 if ( mShowAddToGroupCheckbox )
221 mCbxAddToGroup->setVisible(
true );
222 const bool addToGroup = settings.
value( QStringLiteral(
"/qgis/openSublayersInGroup" ),
false ).toBool();
223 mCbxAddToGroup->setChecked( addToGroup );
226 const int ret = QDialog::exec();
227 if ( overrideCursor )
228 QApplication::setOverrideCursor( cursor );
230 if ( mShowAddToGroupCheckbox )
231 settings.
setValue( QStringLiteral(
"/qgis/openSublayersInGroup" ), mCbxAddToGroup->isChecked() );
235void QgsSublayersDialog::layersTable_selectionChanged(
const QItemSelection &,
const QItemSelection & )
237 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( layersTable->selectedItems().length() > 0 );
240void QgsSublayersDialog::mBtnDeselectAll_pressed()
242 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)