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 const 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 );
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;
156 ? tr(
"Unknown" ) : QString::number( item.count ) );
158 elements << item.type;
160 elements << item.description;
161 layersTable->addTopLevelItem(
new SubLayerItem( elements ) );
166 const QByteArray ba = settings.
value(
"/Windows/" +
mName +
"SubLayers/headerState" ).toByteArray();
167 const int savedColumnCount = settings.
value(
"/Windows/" +
mName +
"SubLayers/headerColumnCount" ).toInt();
168 if ( ! ba.isNull() && savedColumnCount == layersTable->columnCount() )
170 layersTable->header()->restoreState( ba );
174 for (
int i = 0; i < layersTable->columnCount(); i++ )
175 layersTable->resizeColumnToContents( i );
176 layersTable->setColumnWidth( 1, layersTable->columnWidth( 1 ) + 10 );
189 if ( layersTable->topLevelItemCount() == 0 )
190 return QDialog::Rejected;
192 layersTable->selectAll();
196 return QDialog::Rejected;
198 return QDialog::Accepted;
201 if ( layersTable->topLevelItemCount() == 1 )
202 return QDialog::Accepted;
204 layersTable->sortByColumn( 1, Qt::AscendingOrder );
205 layersTable->setSortingEnabled(
true );
210 const bool overrideCursor =
nullptr != QApplication::overrideCursor();
211 if ( overrideCursor )
213 cursor = QCursor( * QApplication::overrideCursor() );
214 QApplication::restoreOverrideCursor();
218 if ( mShowAddToGroupCheckbox )
220 mCbxAddToGroup->setVisible(
true );
221 const bool addToGroup = settings.
value( QStringLiteral(
"/qgis/openSublayersInGroup" ),
false ).toBool();
222 mCbxAddToGroup->setChecked( addToGroup );
225 const int ret = QDialog::exec();
226 if ( overrideCursor )
227 QApplication::setOverrideCursor( cursor );
229 if ( mShowAddToGroupCheckbox )
230 settings.
setValue( QStringLiteral(
"/qgis/openSublayersInGroup" ), mCbxAddToGroup->isChecked() );
234 void QgsSublayersDialog::layersTable_selectionChanged(
const QItemSelection &,
const QItemSelection & )
236 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( layersTable->selectedItems().length() > 0 );
239 void QgsSublayersDialog::mBtnDeselectAll_pressed()
241 layersTable->selectionModel()->clear();