Skip to content

Commit b042d8e

Browse files
committed
use QgsLayerTree for the selection of layers in the offline editing plugin
1 parent 9cdb640 commit b042d8e

File tree

4 files changed

+88
-67
lines changed

4 files changed

+88
-67
lines changed

src/plugins/offline_editing/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ INCLUDE_DIRECTORIES(
4848
${SQLITE3_INCLUDE_DIR}
4949
${CMAKE_CURRENT_BINARY_DIR}
5050
../../core
51+
../../core/layertree
5152
../../core/raster
5253
../../gui
54+
../../gui/layertree
5355
..
5456
${GEOS_INCLUDE_DIR}
5557
)

src/plugins/offline_editing/offline_editing_plugin_gui.cpp

+38-50
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818

1919
#include "offline_editing_plugin_gui.h"
2020

21-
#include <qgscontexthelp.h>
22-
#include <qgsmaplayer.h>
23-
#include <qgsmaplayerregistry.h>
24-
#include <qgsvectordataprovider.h>
25-
#include <qgsvectorlayer.h>
21+
#include "qgscontexthelp.h"
22+
#include "qgslayertree.h"
23+
#include "qgslayertreemodel.h"
24+
#include "qgsmaplayer.h"
25+
#include "qgsmaplayerregistry.h"
26+
#include "qgsproject.h"
27+
#include "qgsvectordataprovider.h"
28+
#include "qgsvectorlayer.h"
2629

2730
#include <QFileDialog>
2831
#include <QMessageBox>
@@ -36,17 +39,23 @@ QgsOfflineEditingPluginGui::QgsOfflineEditingPluginGui( QWidget* parent /*= 0*/,
3639
restoreState();
3740

3841
mOfflineDbFile = "offline.sqlite";
39-
ui_offlineDataPath->setText( QDir( mOfflineDataPath ).absoluteFilePath( mOfflineDbFile ) );
42+
mOfflineDataPathLineEdit->setText( QDir( mOfflineDataPath ).absoluteFilePath( mOfflineDbFile ) );
4043

41-
updateLayerList( checkboxShowEditableLayers->isChecked() );
44+
QgsLayerTreeGroup* rootNode = QgsLayerTree::toGroup( QgsProject::instance()->layerTreeRoot()->clone() );
45+
QgsLayerTreeModel* treeModel = new QgsLayerTreeModel( rootNode, this );
46+
treeModel->setFlag( QgsLayerTreeModel::ShowLegend, false );
47+
treeModel->setFlag( QgsLayerTreeModel::AllowNodeChangeVisibility, true );
48+
mLayerTree->setModel( treeModel );
49+
50+
connect( mSelectAllButton, SIGNAL( clicked() ), this, SLOT( selectAll() ) );
51+
connect( mUnselectAllButton, SIGNAL( clicked() ), this, SLOT( unSelectAll() ) );
4252
}
4353

4454
QgsOfflineEditingPluginGui::~QgsOfflineEditingPluginGui()
4555
{
4656
QSettings settings;
4757
settings.setValue( "Plugin-OfflineEditing/geometry", saveGeometry() );
4858
settings.setValue( "Plugin-OfflineEditing/offline_data_path", mOfflineDataPath );
49-
settings.setValue( "Plugin-OfflineEditing/onlyEditableLayers", checkboxShowEditableLayers->isChecked() );
5059
}
5160

5261
QString QgsOfflineEditingPluginGui::offlineDataPath()
@@ -64,37 +73,7 @@ QStringList& QgsOfflineEditingPluginGui::selectedLayerIds()
6473
return mSelectedLayerIds;
6574
}
6675

67-
void QgsOfflineEditingPluginGui::updateLayerList( bool filterEditableLayers )
68-
{
69-
ui_layerList->clear();
70-
71-
QMap<QString, QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
72-
for ( QMap<QString, QgsMapLayer*>::iterator layer_it = mapLayers.begin() ; layer_it != mapLayers.end(); ++layer_it )
73-
{
74-
if ( layer_it.value()->type() == QgsMapLayer::VectorLayer )
75-
{
76-
QgsVectorLayer* layer = qobject_cast<QgsVectorLayer*>( layer_it.value() );
77-
78-
bool showLayer = true;
79-
if ( filterEditableLayers )
80-
{
81-
int cap = layer->dataProvider()->capabilities();
82-
showLayer = ( cap & QgsVectorDataProvider::AddFeatures ) &&
83-
( cap & QgsVectorDataProvider::DeleteFeatures ) &&
84-
( cap & QgsVectorDataProvider::ChangeAttributeValues ) &&
85-
( cap & QgsVectorDataProvider::AddAttributes ) &&
86-
( cap & QgsVectorDataProvider::ChangeGeometries );
87-
}
88-
if ( showLayer )
89-
{
90-
QListWidgetItem* item = new QListWidgetItem( layer->name(), ui_layerList );
91-
item->setData( Qt::UserRole, QVariant( layer_it.key() ) );
92-
}
93-
}
94-
}
95-
}
96-
97-
void QgsOfflineEditingPluginGui::on_butBrowse_clicked()
76+
void QgsOfflineEditingPluginGui::on_mBrowseButton_clicked()
9877
{
9978
QString fileName = QFileDialog::getSaveFileName( this,
10079
tr( "Select target database for offline data" ),
@@ -110,16 +89,10 @@ void QgsOfflineEditingPluginGui::on_butBrowse_clicked()
11089
}
11190
mOfflineDbFile = QFileInfo( fileName ).fileName();
11291
mOfflineDataPath = QFileInfo( fileName ).absolutePath();
113-
ui_offlineDataPath->setText( fileName );
92+
mOfflineDataPathLineEdit->setText( fileName );
11493
}
11594
}
11695

117-
void QgsOfflineEditingPluginGui::on_checkboxShowEditableLayers_stateChanged( int state )
118-
{
119-
Q_UNUSED( state );
120-
updateLayerList( checkboxShowEditableLayers->isChecked() );
121-
}
122-
12396
void QgsOfflineEditingPluginGui::on_buttonBox_accepted()
12497
{
12598
if ( QFile( QDir( mOfflineDataPath ).absoluteFilePath( mOfflineDbFile ) ).exists() )
@@ -137,10 +110,13 @@ void QgsOfflineEditingPluginGui::on_buttonBox_accepted()
137110
}
138111

139112
mSelectedLayerIds.clear();
140-
QList<QListWidgetItem*> layers = ui_layerList->selectedItems();
141-
for ( QList<QListWidgetItem*>::const_iterator it = layers.begin(); it != layers.end(); ++it )
113+
foreach ( QgsLayerTreeLayer* nodeLayer, mLayerTree->layerTreeModel()->rootGroup()->findLayers() )
142114
{
143-
mSelectedLayerIds.append(( *it )->data( Qt::UserRole ).toString() );
115+
if ( nodeLayer->isVisible() )
116+
{
117+
QgsDebugMsg(nodeLayer->layerId());
118+
mSelectedLayerIds.append( nodeLayer->layerId() );
119+
}
144120
}
145121

146122
accept();
@@ -162,5 +138,17 @@ void QgsOfflineEditingPluginGui::restoreState()
162138
QSettings settings;
163139
mOfflineDataPath = settings.value( "Plugin-OfflineEditing/offline_data_path", QDir().absolutePath() ).toString();
164140
restoreGeometry( settings.value( "Plugin-OfflineEditing/geometry" ).toByteArray() );
165-
checkboxShowEditableLayers->setChecked( settings.value( "Plugin-OfflineEditing/onlyEditableLayers", true ).toBool() );
141+
}
142+
143+
void QgsOfflineEditingPluginGui::selectAll()
144+
{
145+
foreach ( QgsLayerTreeLayer* nodeLayer, mLayerTree->layerTreeModel()->rootGroup()->findLayers() )
146+
nodeLayer->setVisible( Qt::Checked );
147+
}
148+
149+
150+
void QgsOfflineEditingPluginGui::unSelectAll()
151+
{
152+
foreach ( QgsLayerTreeLayer* nodeLayer, mLayerTree->layerTreeModel()->rootGroup()->findLayers() )
153+
nodeLayer->setVisible( Qt::Unchecked );
166154
}

src/plugins/offline_editing/offline_editing_plugin_gui.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ class QgsOfflineEditingPluginGui : public QDialog, private Ui::QgsOfflineEditing
3434
QString offlineDbFile();
3535
QStringList& selectedLayerIds();
3636

37+
public slots:
38+
/** change the selection of layers in the list */
39+
void selectAll();
40+
void unSelectAll();
41+
3742
private:
38-
void updateLayerList( bool filterEditableLayers );
3943
void saveState();
4044
void restoreState();
4145

@@ -44,8 +48,7 @@ class QgsOfflineEditingPluginGui : public QDialog, private Ui::QgsOfflineEditing
4448
QStringList mSelectedLayerIds;
4549

4650
private slots:
47-
void on_butBrowse_clicked();
48-
void on_checkboxShowEditableLayers_stateChanged( int state );
51+
void on_mBrowseButton_clicked();
4952
void on_buttonBox_accepted();
5053
void on_buttonBox_rejected();
5154
void on_buttonBox_helpRequested();

src/plugins/offline_editing/offline_editing_plugin_guibase.ui

+42-14
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
</widget>
3030
</item>
3131
<item>
32-
<widget class="QLineEdit" name="ui_offlineDataPath">
32+
<widget class="QLineEdit" name="mOfflineDataPathLineEdit">
3333
<property name="readOnly">
3434
<bool>true</bool>
3535
</property>
3636
</widget>
3737
</item>
3838
<item>
39-
<widget class="QPushButton" name="butBrowse">
39+
<widget class="QPushButton" name="mBrowseButton">
4040
<property name="text">
4141
<string>Browse...</string>
4242
</property>
@@ -54,26 +54,47 @@
5454
</widget>
5555
</item>
5656
<item>
57-
<widget class="QListWidget" name="ui_layerList">
57+
<widget class="QgsLayerTreeView" name="mLayerTree">
5858
<property name="selectionMode">
59-
<enum>QAbstractItemView::MultiSelection</enum>
59+
<enum>QAbstractItemView::SingleSelection</enum>
6060
</property>
6161
</widget>
6262
</item>
63+
<item>
64+
<layout class="QHBoxLayout" name="horizontalLayout_3">
65+
<item>
66+
<widget class="QPushButton" name="mSelectAllButton">
67+
<property name="text">
68+
<string>select all</string>
69+
</property>
70+
</widget>
71+
</item>
72+
<item>
73+
<spacer name="horizontalSpacer">
74+
<property name="orientation">
75+
<enum>Qt::Horizontal</enum>
76+
</property>
77+
<property name="sizeHint" stdset="0">
78+
<size>
79+
<width>40</width>
80+
<height>20</height>
81+
</size>
82+
</property>
83+
</spacer>
84+
</item>
85+
<item>
86+
<widget class="QPushButton" name="mUnselectAllButton">
87+
<property name="text">
88+
<string>unselect all</string>
89+
</property>
90+
</widget>
91+
</item>
92+
</layout>
93+
</item>
6394
</layout>
6495
</item>
6596
<item>
6697
<layout class="QHBoxLayout" name="horizontalLayout_2">
67-
<item>
68-
<widget class="QCheckBox" name="checkboxShowEditableLayers">
69-
<property name="text">
70-
<string>Show only editable layers</string>
71-
</property>
72-
<property name="checked">
73-
<bool>true</bool>
74-
</property>
75-
</widget>
76-
</item>
7798
<item>
7899
<widget class="QDialogButtonBox" name="buttonBox">
79100
<property name="orientation">
@@ -89,6 +110,13 @@
89110
</layout>
90111
</widget>
91112
<layoutdefault spacing="6" margin="11"/>
113+
<customwidgets>
114+
<customwidget>
115+
<class>QgsLayerTreeView</class>
116+
<extends>QTreeView</extends>
117+
<header>qgslayertreeview.h</header>
118+
</customwidget>
119+
</customwidgets>
92120
<resources/>
93121
<connections/>
94122
</ui>

0 commit comments

Comments
 (0)