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" ) << tr(
"Description" ) );
64 setWindowTitle( tr(
"Select Raster Layers to Add…" ) );
65 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" ) );
69 setWindowTitle( tr(
"Select Layers to Add…" ) );
70 layersTable->setHeaderLabels( QStringList() << tr(
"Layer ID" ) << tr(
"Layer name" )
76 QPushButton *button =
new QPushButton( tr(
"Select All" ) );
77 buttonBox->addButton( button, QDialogButtonBox::ActionRole );
78 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 );
90 settings.
setValue(
"/Windows/" +
mName +
"SubLayers/headerColumnCount",
91 layersTable->columnCount() );
92 settings.
setValue(
"/Windows/" +
mName +
"SubLayers/headerState",
93 layersTable->header()->saveState() );
96 static bool _isLayerIdUnique(
int layerId, QTreeWidget *layersTable )
99 for (
int j = 0; j < layersTable->topLevelItemCount(); j++ )
101 if ( layersTable->topLevelItem( j )->text( 0 ).toInt() == layerId )
112 for (
int i = 0; i < layersTable->selectedItems().size(); i++ )
114 QTreeWidgetItem *item = layersTable->selectedItems().at( i );
117 def.
layerId = item->text( 0 ).toInt();
123 if ( !_isLayerIdUnique( def.
layerId, layersTable ) )
135 const auto constList = list;
138 QStringList elements;
139 elements << QString::number( item.layerId ) << item.layerName;
141 elements << ( item.count == -1 ? tr(
"Unknown" ) : QString::number( item.count ) );
143 elements << item.type;
145 elements << item.description;
146 layersTable->addTopLevelItem(
new SubLayerItem( elements ) );
151 QByteArray ba = settings.
value(
"/Windows/" +
mName +
"SubLayers/headerState" ).toByteArray();
152 int savedColumnCount = settings.
value(
"/Windows/" +
mName +
"SubLayers/headerColumnCount" ).toInt();
153 if ( ! ba.isNull() && savedColumnCount == layersTable->columnCount() )
155 layersTable->header()->restoreState( ba );
159 for (
int i = 0; i < layersTable->columnCount(); i++ )
160 layersTable->resizeColumnToContents( i );
161 layersTable->setColumnWidth( 1, layersTable->columnWidth( 1 ) + 10 );
171 QString promptLayers = settings.
value( QStringLiteral(
"qgis/promptForSublayers" ), 1 ).toString();
174 if ( layersTable->topLevelItemCount() == 0 )
175 return QDialog::Rejected;
178 if ( promptLayers == QLatin1String(
"no" ) )
179 return QDialog::Rejected;
180 else if ( promptLayers == QLatin1String(
"all" ) )
182 layersTable->selectAll();
183 return QDialog::Accepted;
187 if ( layersTable->topLevelItemCount() == 1 )
189 layersTable->selectAll();
190 return QDialog::Accepted;
193 layersTable->sortByColumn( 1, Qt::AscendingOrder );
194 layersTable->setSortingEnabled(
true );
199 bool overrideCursor =
nullptr != QApplication::overrideCursor();
200 if ( overrideCursor )
202 cursor = QCursor( * QApplication::overrideCursor() );
203 QApplication::restoreOverrideCursor();
207 if ( mShowAddToGroupCheckbox )
209 mCheckboxAddToGroup->setVisible(
true );
210 bool addToGroup = settings.
value( QStringLiteral(
"/qgis/openSublayersInGroup" ),
false ).toBool();
211 mCheckboxAddToGroup->setChecked( addToGroup );
214 int ret = QDialog::exec();
215 if ( overrideCursor )
216 QApplication::setOverrideCursor( cursor );
218 if ( mShowAddToGroupCheckbox )
219 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...
bool mShowDescription
Whether to show description in the table.
A structure that defines layers for the purpose of this dialog.