/
offline_editing_plugin_gui.cpp
269 lines (236 loc) · 8.99 KB
/
offline_editing_plugin_gui.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/***************************************************************************
offline_editing_plugin_gui.cpp
Offline Editing Plugin
a QGIS plugin
--------------------------------------
Date : 08-Jul-2010
Copyright : (C) 2010 by Sourcepole
Email : info at sourcepole.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "offline_editing_plugin_gui.h"
#include "qgshelp.h"
#include "qgslayertree.h"
#include "qgslayertreemodel.h"
#include "qgsmaplayer.h"
#include "qgsproject.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgssettings.h"
#include <QFileDialog>
#include <QMessageBox>
QgsSelectLayerTreeModel::QgsSelectLayerTreeModel( QgsLayerTree *rootNode, QObject *parent )
: QgsLayerTreeModel( rootNode, parent )
{
setFlag( QgsLayerTreeModel::ShowLegend, false );
setFlag( QgsLayerTreeModel::AllowNodeChangeVisibility, true );
}
int QgsSelectLayerTreeModel::columnCount( const QModelIndex &parent ) const
{
return QgsLayerTreeModel::columnCount( parent ) + 1;
}
QVariant QgsSelectLayerTreeModel::data( const QModelIndex &index, int role ) const
{
QgsLayerTreeNode *node = index2node( index );
if ( index.column() == 0 )
{
if ( role == Qt::CheckStateRole )
{
if ( QgsLayerTree::isLayer( node ) )
{
QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
return nodeLayer->isVisible() ? Qt::Checked : Qt::Unchecked;
}
else if ( QgsLayerTree::isGroup( node ) )
{
QgsLayerTreeGroup *nodeGroup = QgsLayerTree::toGroup( node );
return nodeGroup->isVisible() ? Qt::Checked : Qt::Unchecked;
}
else
{
return QVariant();
}
}
}
else
{
if ( QgsLayerTree::isLayer( node ) && index.column() > 0 )
{
QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
if ( nodeLayer->layer()->dataProvider()->name() == QStringLiteral( "WFS" ) )
{
switch ( role )
{
case Qt::ToolTipRole:
return tr( "The source of this layer is a <b>WFS</b> server.<br>"
"Some WFS layers are not suitable for offline<br>"
"editing due to unstable primary keys<br>"
"please check with your system administrator<br>"
"if this WFS layer can be used for offline<br>"
"editing." );
break;
case Qt::DecorationRole:
return QgsApplication::getThemeIcon( "/mIconWarning.svg" );
break;
}
}
}
return QVariant();
}
return QgsLayerTreeModel::data( index, role );
}
QgsOfflineEditingPluginGui::QgsOfflineEditingPluginGui( QWidget *parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
{
setupUi( this );
connect( mBrowseButton, &QPushButton::clicked, this, &QgsOfflineEditingPluginGui::mBrowseButton_clicked );
connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsOfflineEditingPluginGui::buttonBox_accepted );
connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsOfflineEditingPluginGui::buttonBox_rejected );
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsOfflineEditingPluginGui::showHelp );
restoreState();
mOfflineDbFile = QStringLiteral( "offline.gpkg" );
mOfflineDataPathLineEdit->setText( QDir( mOfflineDataPath ).absoluteFilePath( mOfflineDbFile ) );
QgsLayerTree *rootNode = QgsProject::instance()->layerTreeRoot()->clone();
QgsLayerTreeModel *treeModel = new QgsSelectLayerTreeModel( rootNode, this );
mLayerTree->setModel( treeModel );
mLayerTree->header()->setResizeMode( QHeaderView::ResizeToContents );
connect( mSelectAllButton, &QAbstractButton::clicked, this, &QgsOfflineEditingPluginGui::selectAll );
connect( mDeselectAllButton, &QAbstractButton::clicked, this, &QgsOfflineEditingPluginGui::deSelectAll );
connect( mSelectDatatypeCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsOfflineEditingPluginGui::datatypeChanged );
}
QgsOfflineEditingPluginGui::~QgsOfflineEditingPluginGui()
{
QgsSettings settings;
settings.setValue( QStringLiteral( "OfflineEditing/geometry" ), saveGeometry(), QgsSettings::Section::Plugins );
settings.setValue( QStringLiteral( "OfflineEditing/offline_data_path" ), mOfflineDataPath, QgsSettings::Section::Plugins );
}
QString QgsOfflineEditingPluginGui::offlineDataPath()
{
return mOfflineDataPath;
}
QString QgsOfflineEditingPluginGui::offlineDbFile()
{
return mOfflineDbFile;
}
QStringList QgsOfflineEditingPluginGui::selectedLayerIds()
{
return mSelectedLayerIds;
}
bool QgsOfflineEditingPluginGui::onlySelected() const
{
return mOnlySelectedCheckBox->checkState() == Qt::Checked;
}
bool QgsOfflineEditingPluginGui::isGeopackage() const
{
return mSelectDatatypeCombo->currentIndex() == 0;
}
void QgsOfflineEditingPluginGui::mBrowseButton_clicked()
{
if ( isGeopackage() )
{
//GeoPackage
QString fileName = QFileDialog::getSaveFileName( this,
tr( "Select target database for offline data" ),
QDir( mOfflineDataPath ).absoluteFilePath( mOfflineDbFile ),
tr( "GeoPackage" ) + " (*.gpkg);;"
+ tr( "All files" ) + " (*.*)" );
if ( !fileName.isEmpty() )
{
if ( !fileName.endsWith( QLatin1String( ".gpkg" ), Qt::CaseInsensitive ) )
{
fileName += QLatin1String( ".gpkg" );
}
mOfflineDbFile = QFileInfo( fileName ).fileName();
mOfflineDataPath = QFileInfo( fileName ).absolutePath();
mOfflineDataPathLineEdit->setText( fileName );
}
}
else
{
//Spacialite
QString fileName = QFileDialog::getSaveFileName( this,
tr( "Select target database for offline data" ),
QDir( mOfflineDataPath ).absoluteFilePath( mOfflineDbFile ),
tr( "SpatiaLite DB" ) + " (*.sqlite);;"
+ tr( "All files" ) + " (*.*)" );
if ( !fileName.isEmpty() )
{
if ( !fileName.endsWith( QLatin1String( ".sqlite" ), Qt::CaseInsensitive ) )
{
fileName += QLatin1String( ".sqlite" );
}
mOfflineDbFile = QFileInfo( fileName ).fileName();
mOfflineDataPath = QFileInfo( fileName ).absolutePath();
mOfflineDataPathLineEdit->setText( fileName );
}
}
}
void QgsOfflineEditingPluginGui::buttonBox_accepted()
{
if ( QFile( QDir( mOfflineDataPath ).absoluteFilePath( mOfflineDbFile ) ).exists() )
{
QMessageBox msgBox;
msgBox.setWindowTitle( tr( "Offline Editing Plugin" ) );
msgBox.setText( tr( "Converting to offline project." ) );
msgBox.setInformativeText( tr( "Offline database file '%1' exists. Overwrite?" ).arg( mOfflineDbFile ) );
msgBox.setStandardButtons( QMessageBox::Yes | QMessageBox::Cancel );
msgBox.setDefaultButton( QMessageBox::Cancel );
if ( msgBox.exec() != QMessageBox::Yes )
{
return;
}
}
mSelectedLayerIds.clear();
Q_FOREACH ( QgsLayerTreeLayer *nodeLayer, mLayerTree->layerTreeModel()->rootGroup()->findLayers() )
{
if ( nodeLayer->isVisible() )
{
mSelectedLayerIds.append( nodeLayer->layerId() );
}
}
accept();
}
void QgsOfflineEditingPluginGui::buttonBox_rejected()
{
reject();
}
void QgsOfflineEditingPluginGui::showHelp()
{
QgsHelp::openHelp( QStringLiteral( "plugins/plugins_offline_editing.html" ) );
}
void QgsOfflineEditingPluginGui::restoreState()
{
QgsSettings settings;
mOfflineDataPath = settings.value( QStringLiteral( "OfflineEditing/offline_data_path" ), QDir::homePath(), QgsSettings::Section::Plugins ).toString();
restoreGeometry( settings.value( QStringLiteral( "OfflineEditing/geometry" ), QgsSettings::Section::Plugins ).toByteArray() );
}
void QgsOfflineEditingPluginGui::selectAll()
{
Q_FOREACH ( QgsLayerTreeLayer *nodeLayer, mLayerTree->layerTreeModel()->rootGroup()->findLayers() )
nodeLayer->setItemVisibilityChecked( true );
}
void QgsOfflineEditingPluginGui::deSelectAll()
{
Q_FOREACH ( QgsLayerTreeLayer *nodeLayer, mLayerTree->layerTreeModel()->rootGroup()->findLayers() )
nodeLayer->setItemVisibilityChecked( false );
}
void QgsOfflineEditingPluginGui::datatypeChanged( int index )
{
if ( index == 0 )
{
//GeoPackage
mOfflineDbFile = QStringLiteral( "offline.gpkg" );
}
else
{
//Spatialite
mOfflineDbFile = QStringLiteral( "offline.sqlite" );
}
mOfflineDataPathLineEdit->setText( QDir( mOfflineDataPath ).absoluteFilePath( mOfflineDbFile ) );
}