21 #include <QTableWidgetItem> 22 #include <QPushButton> 25 class SubLayerItem :
public QTreeWidgetItem
28 SubLayerItem(
const QStringList &strings,
int type = QTreeWidgetItem::Type )
29 : QTreeWidgetItem( strings, type )
32 bool operator <(
const QTreeWidgetItem &other )
const override 35 int col = treeWidget()->sortColumn();
37 if ( col == 0 || ( col > 0 && d->
countColumn() == col ) )
38 return text( col ).toInt() < other.text( col ).toInt();
40 return text( col ) < other.text( col );
46 QWidget *parent, Qt::WindowFlags fl )
47 : QDialog( parent, fl )
55 setWindowTitle( tr(
"Select Vector Layers to Add…" ) );
56 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" )
57 << tr(
"Number of features" ) << tr(
"Geometry type" ) );
63 setWindowTitle( tr(
"Select Raster Layers to Add…" ) );
64 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" ) );
68 setWindowTitle( tr(
"Select Layers to Add…" ) );
69 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" )
75 QPushButton *button =
new QPushButton( tr(
"Select All" ) );
76 buttonBox->addButton( button, QDialogButtonBox::ActionRole );
77 connect( button, &QAbstractButton::pressed, layersTable, &QTreeView::selectAll );
81 mCheckboxAddToGroup =
new QCheckBox( tr(
"Add layers to a group" ),
this );
82 buttonBox->addButton( mCheckboxAddToGroup, QDialogButtonBox::ActionRole );
83 mCheckboxAddToGroup->setVisible(
false );
89 settings.
setValue(
"/Windows/" +
mName +
"SubLayers/headerState",
90 layersTable->header()->saveState() );
93 static bool _isLayerIdUnique(
int layerId, QTreeWidget *layersTable )
96 for (
int j = 0; j < layersTable->topLevelItemCount(); j++ )
98 if ( layersTable->topLevelItem( j )->text( 0 ).toInt() == layerId )
109 for (
int i = 0; i < layersTable->selectedItems().size(); i++ )
111 QTreeWidgetItem *item = layersTable->selectedItems().at( i );
114 def.
layerId = item->text( 0 ).toInt();
120 if ( !_isLayerIdUnique( def.
layerId, layersTable ) )
132 const auto constList = list;
135 QStringList elements;
136 elements << QString::number( item.layerId ) << item.layerName;
138 elements << ( item.count == -1 ? tr(
"Unknown" ) : QString::number( item.count ) );
140 elements << item.type;
141 layersTable->addTopLevelItem(
new SubLayerItem( elements ) );
146 QByteArray ba = settings.
value(
"/Windows/" +
mName +
"SubLayers/headerState" ).toByteArray();
149 layersTable->header()->restoreState( ba );
153 for (
int i = 0; i < layersTable->columnCount(); i++ )
154 layersTable->resizeColumnToContents( i );
155 layersTable->setColumnWidth( 1, layersTable->columnWidth( 1 ) + 10 );
165 QString promptLayers = settings.
value( QStringLiteral(
"qgis/promptForSublayers" ), 1 ).toString();
168 if ( layersTable->topLevelItemCount() == 0 )
169 return QDialog::Rejected;
172 if ( promptLayers == QLatin1String(
"no" ) )
173 return QDialog::Rejected;
174 else if ( promptLayers == QLatin1String(
"all" ) )
176 layersTable->selectAll();
177 return QDialog::Accepted;
181 if ( layersTable->topLevelItemCount() == 1 )
183 layersTable->selectAll();
184 return QDialog::Accepted;
187 layersTable->sortByColumn( 1, Qt::AscendingOrder );
188 layersTable->setSortingEnabled(
true );
193 bool overrideCursor =
nullptr != QApplication::overrideCursor();
194 if ( overrideCursor )
196 cursor = QCursor( * QApplication::overrideCursor() );
197 QApplication::restoreOverrideCursor();
201 if ( mShowAddToGroupCheckbox )
203 mCheckboxAddToGroup->setVisible(
true );
204 bool addToGroup = settings.
value( QStringLiteral(
"/qgis/openSublayersInGroup" ),
false ).toBool();
205 mCheckboxAddToGroup->setChecked( addToGroup );
208 int ret = QDialog::exec();
209 if ( overrideCursor )
210 QApplication::setOverrideCursor( cursor );
212 if ( mShowAddToGroupCheckbox )
213 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:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
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 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
Returns column with count or -1.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
LayerDefinitionList selection()
Returns list of selected layers.
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...
A structure that defines layers for the purpose of this dialog.