• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MerginMaps / input / 6655250127

26 Oct 2023 02:02PM UTC coverage: 62.234% (-0.07%) from 62.308%
6655250127

push

github

web-flow
Merge pull request #2867 from MerginMaps/other_elems

HTML, Text, Spacer "other form widgets" + "show label"

7483 of 12024 relevant lines covered (62.23%)

122.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

2.96
/app/projectwizard.cpp
1
/***************************************************************************
2
 *                                                                         *
3
 *   This program is free software; you can redistribute it and/or modify  *
4
 *   it under the terms of the GNU General Public License as published by  *
5
 *   the Free Software Foundation; either version 2 of the License, or     *
6
 *   (at your option) any later version.                                   *
7
 *                                                                         *
8
 ***************************************************************************/
9

10
#include "projectwizard.h"
11
#include "inpututils.h"
12
#include "coreutils.h"
13

14
#include "qgsproject.h"
15
#include "qgsvectortilelayer.h"
16
#include "qgsvectorlayer.h"
17
#include "qgsvectorfilewriter.h"
18
#include "qgsdatetimefieldformatter.h"
19
#include "qgsmarkersymbollayer.h"
20
#include "qgis.h"
21
#include "qgssymbollayer.h"
22
#include "qgssymbol.h"
23
#include "qgsmarkersymbol.h"
24
#include "qgssinglesymbolrenderer.h"
25
#include "inpututils.h"
26
#include "coreutils.h"
27

28
const QString TILES_URL = QStringLiteral( "https://tiles.merginmaps.com" );
29

30
ProjectWizard::ProjectWizard( const QString &dataDir, QObject *parent )
17✔
31
  : QObject( parent )
32
  , mDataDir( dataDir )
51✔
33
{
34

35
  mSettings = std::unique_ptr<QgsMapSettings>( new QgsMapSettings );
17✔
36
}
17✔
37

38
QgsVectorLayer *ProjectWizard::createGpkgLayer( QString const &projectDir, QList<FieldConfiguration> const &fieldsConfig )
×
39
{
40
  QString gpkgName( QStringLiteral( "data" ) );
×
41
  QString projectGpkgPath( QString( "%1/%2.%3" ).arg( projectDir ).arg( gpkgName ).arg( "gpkg" ) );
×
42
  QString layerName( QStringLiteral( "Survey" ) );
×
43
  QgsCoordinateReferenceSystem layerCrs( LAYER_CRS_ID );
×
44
  QgsFields predefinedFields = createFields( fieldsConfig );
×
45

46
  // Write layer as gpkg
47
  QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "PointZ?crs=%1" ).arg( LAYER_CRS_ID ), layerName, "memory" );
×
48
  layer->startEditing();
×
49
  layer->setCrs( layerCrs );
×
50
  for ( QgsField f : predefinedFields )
×
51
  {
52
    layer->addAttribute( f );
×
53
  }
×
54
  layer->updateFields();
×
55
  layer->commitChanges();
×
56

57
  QgsVectorFileWriter::SaveVectorOptions options;
×
58
  options.driverName = QStringLiteral( "GPKG" );
×
59
  options.layerName = layerName;
×
60
  options.fileEncoding = QStringLiteral( "UTF-8" );
×
61

62
  QString errorMessage;
×
63
  QgsVectorFileWriter::writeAsVectorFormatV3(
×
64
    layer,
65
    projectGpkgPath,
66
    layer->transformContext(),
×
67
    options,
68
    &errorMessage,
69
    nullptr,
70
    nullptr );
71

72
  // Check and configure layer
73
  QgsVectorLayer *l = new QgsVectorLayer( projectGpkgPath, layerName, "ogr" );
×
74

75
  Q_ASSERT( l->isValid() );
×
76

77
  l->setCrs( layerCrs );
×
78
  for ( int i = 0; i < l->fields().count(); ++i )
×
79
  {
80
    QgsField f = l->fields().at( i );
×
81
    QgsEditorWidgetSetup setup = InputUtils::getEditorWidgetSetup( f, findWidgetTypeByFieldName( f.name(), fieldsConfig ) );
×
82
    l->setEditorWidgetSetup( i, setup );
×
83
  }
×
84
  l->setRenderer( surveyLayerRenderer() );
×
85

86
  return l;
×
87
}
×
88

89
void ProjectWizard::createProject( QString const &projectName, FieldsModel *fieldsModel )
×
90
{
91
  if ( !CoreUtils::isValidName( projectName ) )
×
92
  {
93
    emit projectCreationFailed( tr( "Project name contains invalid characters" ) );
×
94
    return;
×
95
  }
96

97
  QString projectDir = CoreUtils::createUniqueProjectDirectory( mDataDir, projectName );
×
98
  QString projectFilepath( QString( "%1/%2.%3" ).arg( projectDir ).arg( projectName ).arg( "qgz" ) );
×
99
  QString gpkgName( QStringLiteral( "data" ) );
×
100
  QString projectGpkgPath( QString( "%1/%2.%3" ).arg( projectDir ).arg( gpkgName ).arg( "gpkg" ) );
×
101

102
  QgsProject project;
×
103

104
  // add layers
105
  QgsDataSourceUri dsUri;
×
106
  dsUri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
×
107
  dsUri.setParam( QStringLiteral( "url" ), QStringLiteral( "%1/data/default/{z}/{x}/{y}.pbf" ).arg( TILES_URL ) );
×
108
  dsUri.setParam( QStringLiteral( "styleUrl" ), QStringLiteral( "%1/styles/default.json" ).arg( TILES_URL ) );
×
109
  dsUri.setParam( QStringLiteral( "zmin" ), QStringLiteral( "0" ) );
×
110
  dsUri.setParam( QStringLiteral( "zmax" ), QStringLiteral( "14" ) );
×
111
  QgsVectorTileLayer *bgLayer = new QgsVectorTileLayer( dsUri.encodedUri(), QStringLiteral( "OpenMapTiles (OSM)" ) );
×
112
  bool ok;
113
  QString error = bgLayer->loadDefaultStyle( ok );
×
114
  QgsLayerMetadata metadata;
×
115
  metadata.setRights( QStringList() << QStringLiteral( "© OpenMapTiles © OpenStreetMap contributors" ) );
×
116
  bgLayer->setMetadata( metadata );
×
117
  QgsVectorLayer *layer = createGpkgLayer( projectDir, fieldsModel->fields() );
×
118
  QList<QgsMapLayer *> layers;
×
119
  layers << layer << bgLayer;
×
120
  project.addMapLayers( layers );
×
121

122
  // Configurate mapSettings
123
  QgsCoordinateReferenceSystem projectCrs( PROJECT_CRS_ID );
×
124
  mSettings->setExtent( bgLayer->extent() );
×
125
  mSettings->setEllipsoid( "WGS84" );
×
126
  mSettings->setDestinationCrs( projectCrs );
×
127
  mSettings->setLayers( layers );
×
128

129
  // Using writeProject signal to append mapCanvas project setting
130
  connect( &project, &QgsProject::writeProject,
×
131
           this, &ProjectWizard::writeMapCanvasSetting );
132

133
  project.setCrs( projectCrs );
×
134
  project.writePath( projectGpkgPath );
×
135
  project.write( projectFilepath );
×
136

137
  emit notify( tr( "Project %1 created" ).arg( projectName ) );
×
138
  emit projectCreated( projectDir, projectName );
×
139
}
×
140

141
void ProjectWizard::writeMapCanvasSetting( QDomDocument &doc )
×
142
{
143
  QDomNodeList nl = doc.elementsByTagName( QStringLiteral( "qgis" ) );
×
144
  if ( !nl.count() )
×
145
  {
146
    QgsDebugError( QStringLiteral( "Unable to find qgis element in project file" ) );
147
    return;
×
148
  }
149
  QDomNode qgisNode = nl.item( 0 );  // there should only be one, so zeroth element OK
×
150

151
  QDomElement mapcanvasNode = doc.createElement( QStringLiteral( "mapcanvas" ) );
×
152
  mapcanvasNode.setAttribute( QStringLiteral( "annotationsVisible" ), false );
×
153
  qgisNode.appendChild( mapcanvasNode );
×
154
  mSettings->writeXml( mapcanvasNode, doc );
×
155
}
×
156

157
QgsFields ProjectWizard::createFields( const QList<FieldConfiguration> fieldsConfig ) const
×
158
{
159

160
  QgsFields fields;
×
161
  for ( const FieldConfiguration &fc : fieldsConfig )
×
162
  {
163
    QString type = widgetToType( fc.widgetType );
×
164
    QVariant::Type qtype = parseType( type );
×
165
    QgsField field( fc.attributeName, qtype, type );
×
166
    fields.append( field );
×
167
  }
×
168
  return fields;
×
169
}
×
170

171
QgsSingleSymbolRenderer *ProjectWizard::surveyLayerRenderer()
×
172
{
173
  QgsSimpleMarkerSymbolLayer *markerLayer = new QgsSimpleMarkerSymbolLayer( Qgis::MarkerShape::Circle );
×
174
  markerLayer->setSize( 3.0 );
×
175
  markerLayer->setFillColor( QColor( "#d73027" ) );
×
176
  markerLayer->setStrokeColor( QColor( "#e8e8e8" ) );
×
177
  markerLayer->setStrokeWidth( 0.4 );
×
178
  QgsMarkerSymbol *symbol = new QgsMarkerSymbol( QgsSymbolLayerList() << markerLayer );
×
179
  return new QgsSingleSymbolRenderer( symbol );
×
180
}
181

182
QVariant::Type ProjectWizard::parseType( const QString &type ) const
×
183
{
184
  if ( type == QLatin1String( "text" ) )
×
185
    return QVariant::String;
×
186
  else if ( type == QLatin1String( "integer" ) )
×
187
    return QVariant::Int;
×
188
  else if ( type == QLatin1String( "integer64" ) )
×
189
    return QVariant::Int;
×
190
  else if ( type == QLatin1String( "real" ) )
×
191
    return QVariant::Double;
×
192
  else if ( type == QLatin1String( "date" ) )
×
193
    return QVariant::Date;
×
194
  else if ( type == QLatin1String( "datetime" ) )
×
195
    return QVariant::DateTime;
×
196
  else if ( type == QLatin1String( "bool" ) )
×
197
    return QVariant::Bool;
×
198
  else if ( type == QLatin1String( "binary" ) )
×
199
    return QVariant::ByteArray;
×
200

201
  return QVariant::Invalid;
×
202
}
203

204
QString ProjectWizard::widgetToType( const QString &widgetType ) const
×
205
{
206
  if ( widgetType == QStringLiteral( "TextEdit" ) )
×
207
    return QStringLiteral( "text" );
×
208
  else if ( widgetType == QStringLiteral( "Range" ) )
×
209
    return QStringLiteral( "integer" );
×
210
  else if ( widgetType == QStringLiteral( "DateTime" ) )
×
211
    return QStringLiteral( "datetime" );
×
212
  else if ( widgetType == QStringLiteral( "CheckBox" ) )
×
213
    return QStringLiteral( "bool" );
×
214
  else if ( widgetType == QStringLiteral( "ExternalResource" ) )
×
215
    return QStringLiteral( "text" );
×
216

217
  return QStringLiteral( "text" );
×
218
}
219

220
QString ProjectWizard::findWidgetTypeByFieldName( const QString name, const QList<FieldConfiguration> fieldsConfig ) const
×
221
{
222

223
  for ( int i = 0; i < fieldsConfig.count(); ++i )
×
224
  {
225
    if ( fieldsConfig.at( i ).attributeName == name )
×
226
      return fieldsConfig.at( i ).widgetType;
×
227
  }
228
  return QString( "TextEdit" );
×
229
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc