20 #include <QTableWidgetItem> 21 #include <QPushButton> 24 class SubLayerItem :
public QTreeWidgetItem
27 SubLayerItem(
const QStringList &strings,
int type = QTreeWidgetItem::Type )
28 : QTreeWidgetItem( strings, type )
31 bool operator <(
const QTreeWidgetItem &other )
const override 34 int col = treeWidget()->sortColumn();
36 if ( col == 0 || ( col > 0 && d->
countColumn() == col ) )
37 return text( col ).toInt() < other.text( col ).toInt();
39 return text( col ) < other.text( col );
45 QWidget *parent, Qt::WindowFlags fl )
46 : QDialog( parent, fl )
53 setWindowTitle( tr(
"Select Vector Layers to Add…" ) );
54 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" )
55 << tr(
"Number of features" ) << tr(
"Geometry type" ) );
61 setWindowTitle( tr(
"Select Raster Layers to Add…" ) );
62 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" ) );
66 setWindowTitle( tr(
"Select Layers to Add…" ) );
67 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" )
73 QPushButton *button =
new QPushButton( tr(
"Select All" ) );
74 buttonBox->addButton( button, QDialogButtonBox::ActionRole );
75 connect( button, &QAbstractButton::pressed, layersTable, &QTreeView::selectAll );
82 mCheckboxAddToGroup =
new QCheckBox( tr(
"Add layers to a group" ),
this );
83 buttonBox->addButton( mCheckboxAddToGroup, QDialogButtonBox::ActionRole );
84 mCheckboxAddToGroup->setVisible(
false );
91 settings.
setValue(
"/Windows/" +
mName +
"SubLayers/headerState",
92 layersTable->header()->saveState() );
95 static bool _isLayerIdUnique(
int layerId, QTreeWidget *layersTable )
98 for (
int j = 0; j < layersTable->topLevelItemCount(); j++ )
100 if ( layersTable->topLevelItem( j )->text( 0 ).toInt() == layerId )
111 for (
int i = 0; i < layersTable->selectedItems().size(); i++ )
113 QTreeWidgetItem *item = layersTable->selectedItems().at( i );
116 def.
layerId = item->text( 0 ).toInt();
122 if ( !_isLayerIdUnique( def.
layerId, layersTable ) )
136 QStringList elements;
139 elements << ( item.
count == -1 ? tr(
"Unknown" ) : QString::number( item.
count ) );
141 elements << item.
type;
142 layersTable->addTopLevelItem(
new SubLayerItem( elements ) );
147 QByteArray ba = settings.
value(
"/Windows/" +
mName +
"SubLayers/headerState" ).toByteArray();
150 layersTable->header()->restoreState( ba );
154 for (
int i = 0; i < layersTable->columnCount(); i++ )
155 layersTable->resizeColumnToContents( i );
156 layersTable->setColumnWidth( 1, layersTable->columnWidth( 1 ) + 10 );
166 QString promptLayers = settings.
value( QStringLiteral(
"qgis/promptForSublayers" ), 1 ).toString();
169 if ( layersTable->topLevelItemCount() == 0 )
170 return QDialog::Rejected;
173 if ( promptLayers == QLatin1String(
"no" ) )
174 return QDialog::Rejected;
175 else if ( promptLayers == QLatin1String(
"all" ) )
177 layersTable->selectAll();
178 return QDialog::Accepted;
182 if ( layersTable->topLevelItemCount() == 1 )
184 layersTable->selectAll();
185 return QDialog::Accepted;
188 layersTable->sortByColumn( 1, Qt::AscendingOrder );
189 layersTable->setSortingEnabled(
true );
194 bool overrideCursor =
nullptr != QApplication::overrideCursor();
195 if ( overrideCursor )
197 cursor = QCursor( * QApplication::overrideCursor() );
198 QApplication::restoreOverrideCursor();
202 if ( mShowAddToGroupCheckbox )
204 mCheckboxAddToGroup->setVisible(
true );
205 bool addToGroup = settings.
value( QStringLiteral(
"/qgis/openSublayersInGroup" ),
false ).toBool();
206 mCheckboxAddToGroup->setChecked( addToGroup );
209 int ret = QDialog::exec();
210 if ( overrideCursor )
211 QApplication::setOverrideCursor( cursor );
213 if ( mShowAddToGroupCheckbox )
214 settings.
setValue( QStringLiteral(
"/qgis/openSublayersInGroup" ), mCheckboxAddToGroup->isChecked() );
QString layerName
Name of the layer (not necessarily unique)
int layerId
Identifier of the layer (one unique layer id may have multiple types though)
This class is a composition of two QSettings instances:
QList< QgsSublayersDialog::LayerDefinition > LayerDefinitionList
List of layer definitions for the purpose of this dialog.
bool mShowType
Whether to show type in the table.
bool mShowCount
Whether to show number of features in the table.
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
int count
Number of features (might be unused)
void populateLayerTable(const LayerDefinitionList &list)
Populate the table with layers.
QString type
Extra type depending on the use (e.g. geometry type for vector sublayers)
QgsSublayersDialog(ProviderType providerType, const QString &name, QWidget *parent=nullptr, Qt::WindowFlags fl=nullptr)
Constructor for QgsSublayersDialog.
~QgsSublayersDialog() override
int countColumn() const
Return column with count or -1.
LayerDefinitionList selection()
Returns list of selected layers.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
A structure that defines layers for the purpose of this dialog.