17
17
18
18
#include " qgswmsconfigparser.h"
19
19
#include " qgsmaplayer.h"
20
+ #include " qgsmaplayerregistry.h"
20
21
#include " qgsmapserviceexception.h"
21
22
22
23
#include " qgscomposerlabel.h"
30
31
#include " qgslayertreegroup.h"
31
32
#include " qgslayertreelayer.h"
32
33
34
+ #include " qgsrendererv2.h"
35
+ #include " qgsvectordataprovider.h"
36
+ #include " qgsvectorlayer.h"
37
+
33
38
QgsWMSConfigParser::QgsWMSConfigParser ()
34
39
{
35
40
@@ -40,7 +45,7 @@ QgsWMSConfigParser::~QgsWMSConfigParser()
40
45
41
46
}
42
47
43
- QgsComposition* QgsWMSConfigParser::createPrintComposition ( const QString& composerTemplate, QgsMapRenderer* mapRenderer, const QMap< QString, QString >& parameterMap ) const
48
+ QgsComposition* QgsWMSConfigParser::createPrintComposition ( const QString& composerTemplate, QgsMapRenderer* mapRenderer, const QMap< QString, QString >& parameterMap, QStringList& highlightLayers ) const
44
49
{
45
50
QList<QgsComposerMap*> composerMaps;
46
51
QList<QgsComposerLegend*> composerLegends;
@@ -133,56 +138,74 @@ QgsComposition* QgsWMSConfigParser::createPrintComposition( const QString& compo
133
138
}
134
139
135
140
// layers / styles
136
- QString layers = parameterMap.value ( mapId + " :LAYERS" );
137
- QString styles = parameterMap.value ( mapId + " :STYLES" );
138
- if ( !layers.isEmpty () )
141
+ QStringList layerSet;
142
+ if ( currentMap->keepLayerSet () )
143
+ {
144
+ layerSet = currentMap->layerSet ();
145
+ }
146
+ else
139
147
{
140
- QStringList layerSet;
141
- QStringList wmsLayerList = layers.split ( " ," , QString::SkipEmptyParts );
148
+ QString layers = parameterMap.value ( mapId + " :LAYERS" );
149
+ QString styles = parameterMap.value ( mapId + " :STYLES" );
150
+
151
+
152
+ if ( layers.isEmpty () )
153
+ {
154
+ layers = parameterMap.value ( " LAYERS" );
155
+ styles = parameterMap.value ( " STYLES" );
156
+ }
157
+
158
+ QStringList wmsLayerList = layers.split ( " ," );
142
159
QStringList wmsStyleList;
143
160
144
161
if ( !styles.isEmpty () )
145
162
{
146
- wmsStyleList = styles.split ( " ," , QString::SkipEmptyParts );
163
+ wmsStyleList = styles.split ( " ," );
147
164
}
148
165
149
166
for ( int i = 0 ; i < wmsLayerList.size (); ++i )
150
167
{
151
- QString wmsLayer = wmsLayerList.at ( i );
152
168
QString styleName;
153
169
if ( wmsStyleList.size () > i )
154
170
{
155
171
styleName = wmsStyleList.at ( i );
156
172
}
157
173
158
- bool allowCaching = true ;
159
- if ( wmsLayerList.count ( wmsLayer ) > 1 )
160
- {
161
- allowCaching = false ;
162
- }
163
-
164
- QList<QgsMapLayer*> layerList = mapLayerFromStyle ( wmsLayer, styleName, allowCaching );
165
- int listIndex;
166
- for ( listIndex = 0 ; listIndex < layerList.size (); listIndex++ )
174
+ foreach ( QgsMapLayer *layer, mapLayerFromStyle ( wmsLayerList.at ( i ), styleName ) )
167
175
{
168
- QgsMapLayer* layer = layerList.at ( listIndex );
169
176
if ( layer )
170
177
{
171
178
layerSet.push_back ( layer->id () );
172
179
}
173
180
}
174
181
}
182
+ }
183
+
184
+ // save layer list prior to adding highlight layers
185
+ QStringList bkLayerSet = layerSet;
186
+
187
+ // add highlight layers
188
+ highlightLayers.append ( addHighlightLayers ( parameterMap, layerSet, mapId + " :" ) );
175
189
176
- currentMap->setLayerSet ( layerSet );
177
- currentMap->setKeepLayerSet ( true );
190
+ currentMap->setLayerSet ( layerSet );
191
+ currentMap->setKeepLayerSet ( true );
192
+
193
+ // remove highlight layers from the composer legends
194
+ QList< QgsComposerLegend* >::iterator legendIt = composerLegends.begin ();
195
+ for ( ; legendIt != composerLegends.end (); ++legendIt )
196
+ {
197
+ if (( *legendIt )->model ()->autoUpdate () )
198
+ {
199
+ ( *legendIt )->model ()->setLayerSet ( bkLayerSet );
200
+ }
178
201
}
179
202
180
203
// grid space x / y
181
204
currentMap->grid ()->setIntervalX ( parameterMap.value ( mapId + " :GRID_INTERVAL_X" ).toDouble () );
182
205
currentMap->grid ()->setIntervalY ( parameterMap.value ( mapId + " :GRID_INTERVAL_Y" ).toDouble () );
183
206
}
184
- // update legend
185
- // if it has an auto-update model
207
+ // update legend
208
+ // if it has an auto-update model
186
209
Q_FOREACH ( QgsComposerLegend* currentLegend, composerLegends )
187
210
{
188
211
if ( !currentLegend )
@@ -243,7 +266,7 @@ QgsComposition* QgsWMSConfigParser::createPrintComposition( const QString& compo
243
266
}
244
267
}
245
268
246
- // replace label text
269
+ // replace label text
247
270
Q_FOREACH ( QgsComposerLabel *currentLabel, composerLabels )
248
271
{
249
272
QString title = parameterMap.value ( currentLabel->id ().toUpper () );
@@ -263,7 +286,7 @@ QgsComposition* QgsWMSConfigParser::createPrintComposition( const QString& compo
263
286
currentLabel->setText ( title );
264
287
}
265
288
266
- // replace html url
289
+ // replace html url
267
290
Q_FOREACH ( const QgsComposerHtml *currentHtml, composerHtmls )
268
291
{
269
292
QgsComposerHtml * html = const_cast <QgsComposerHtml *>( currentHtml );
@@ -294,3 +317,252 @@ QgsComposition* QgsWMSConfigParser::createPrintComposition( const QString& compo
294
317
295
318
return c;
296
319
}
320
+
321
+ QStringList QgsWMSConfigParser::addHighlightLayers ( const QMap<QString, QString>& parameterMap, QStringList& layerSet, const QString& parameterPrefix )
322
+ {
323
+ QStringList highlightLayers, geomSplit, symbolSplit, labelSplit, labelFontSplit, labelSizeSplit,
324
+ labelWeightSplit, labelColorSplit, labelBufferColorSplit, labelBufferSizeSplit;
325
+ highlightParameters ( parameterMap, parameterPrefix, geomSplit, symbolSplit, labelSplit, labelFontSplit, labelSizeSplit, labelWeightSplit,
326
+ labelColorSplit, labelBufferColorSplit, labelBufferSizeSplit );
327
+
328
+ if ( geomSplit.isEmpty () || symbolSplit.isEmpty () )
329
+ {
330
+ return highlightLayers;
331
+ }
332
+
333
+ QString crsString = parameterMap.contains ( " CRS" ) ? parameterMap.value ( " CRS" ) : parameterMap.value ( " SRS" );
334
+
335
+ int nHighlights = qMin ( geomSplit.size (), symbolSplit.size () );
336
+ for ( int i = 0 ; i < nHighlights; ++i )
337
+ {
338
+ // create geometry
339
+ QScopedPointer<QgsGeometry> geom ( QgsGeometry::fromWkt ( geomSplit.at ( i ) ) );
340
+ if ( !geom.data () )
341
+ {
342
+ continue ;
343
+ }
344
+
345
+ // create renderer from sld
346
+ QString sld = symbolSplit[i];
347
+ QDomDocument sldDoc;
348
+ if ( !sldDoc.setContent ( symbolSplit[i], true ) )
349
+ {
350
+ continue ;
351
+ }
352
+
353
+ QString errorMsg;
354
+ QScopedPointer<QgsFeatureRendererV2> renderer ( QgsFeatureRendererV2::loadSld ( sldDoc.documentElement (), geom.data ()->type (), errorMsg ) );
355
+ if ( !renderer.data () )
356
+ {
357
+ continue ;
358
+ }
359
+
360
+ // add label settings
361
+ QString labelString;
362
+ if ( i < labelSplit.size () )
363
+ {
364
+ labelString = labelSplit.at ( i );
365
+ }
366
+
367
+ QScopedPointer<QgsVectorLayer> layer ( createHighlightLayer ( i, crsString, geom.take (), labelString, labelSizeSplit, labelColorSplit, labelWeightSplit, labelFontSplit,
368
+ labelBufferSizeSplit, labelBufferColorSplit ) );
369
+ if ( !layer.data () )
370
+ {
371
+ continue ;
372
+ }
373
+
374
+ layer->setRendererV2 ( renderer.take () );
375
+ layerSet.prepend ( layer.data ()->id () );
376
+ highlightLayers.append ( layer.data ()->id () );
377
+ QgsMapLayerRegistry::instance ()->addMapLayers ( QList<QgsMapLayer *>() << layer.take () );
378
+ }
379
+ return highlightLayers;
380
+ }
381
+
382
+ QgsVectorLayer* QgsWMSConfigParser::createHighlightLayer ( int i, const QString& crsString, QgsGeometry* geom, const QString& labelString, const QStringList& labelSizeSplit, const QStringList& labelColorSplit,
383
+ const QStringList& labelWeightSplit, const QStringList& labelFontSplit, const QStringList& labelBufferSizeSplit,
384
+ const QStringList& labelBufferColorSplit )
385
+ {
386
+ if ( !geom )
387
+ {
388
+ return 0 ;
389
+ }
390
+
391
+ QGis::GeometryType geomType = geom->type ();
392
+ QString typeName = QString ( QGis::featureType ( geom->wkbType () ) ).replace ( " WKB" , " " );
393
+ QString url = typeName + " ?crs=" + crsString;
394
+ if ( !labelString.isEmpty () )
395
+ {
396
+ url += " &field=label:string" ;
397
+ if ( geomType == QGis::Polygon )
398
+ {
399
+ url += " &field=x:double&field=y:double&field=hali:string&field=vali:string" ;
400
+ }
401
+ }
402
+
403
+ QgsVectorLayer* layer = new QgsVectorLayer ( url, " highlight_" + QString::number ( i ), " memory" );
404
+ if ( !layer->isValid () )
405
+ {
406
+ delete layer;
407
+ return 0 ;
408
+ }
409
+
410
+ QgsFeature fet ( layer->pendingFields () );
411
+ if ( !labelString.isEmpty () )
412
+ {
413
+ fet.setAttribute ( 0 , labelString );
414
+ if ( geomType == QGis::Polygon )
415
+ {
416
+ QgsGeometry* point = geom->pointOnSurface ();
417
+ if ( point )
418
+ {
419
+ QgsPoint pt = point->asPoint ();
420
+ fet.setAttribute ( 1 , pt.x () );
421
+ fet.setAttribute ( 2 , pt.y () );
422
+ fet.setAttribute ( 3 , " Center" );
423
+ fet.setAttribute ( 4 , " Half" );
424
+ }
425
+ }
426
+
427
+ layer->setCustomProperty ( " labeling/fieldName" , " label" );
428
+ layer->setCustomProperty ( " labeling/enabled" , " true" );
429
+ layer->setCustomProperty ( " labeling" , " pal" );
430
+ // give highest priority to highlight layers and make sure the labels are always drawn
431
+ layer->setCustomProperty ( " labeling/priority" , " 10" );
432
+ layer->setCustomProperty ( " labeling/displayAll" , " true" );
433
+
434
+ // fontsize?
435
+ if ( i < labelSizeSplit.size () )
436
+ {
437
+ layer->setCustomProperty ( " labeling/fontSize" , labelSizeSplit.at ( i ) );
438
+ }
439
+ // font color
440
+ if ( i < labelColorSplit.size () )
441
+ {
442
+ QColor c ( labelColorSplit.at ( i ) );
443
+ layer->setCustomProperty ( " labeling/textColorR" , c.red () );
444
+ layer->setCustomProperty ( " labeling/textColorG" , c.green () );
445
+ layer->setCustomProperty ( " labeling/textColorB" , c.blue () );
446
+ }
447
+ // font weight
448
+ if ( i < labelWeightSplit.size () )
449
+ {
450
+ layer->setCustomProperty ( " labeling/fontWeight" , labelWeightSplit.at ( i ) );
451
+ }
452
+
453
+ // font family list
454
+ if ( i < labelFontSplit.size () )
455
+ {
456
+ layer->setCustomProperty ( " labeling/fontFamily" , labelFontSplit.at ( i ) );
457
+ }
458
+
459
+ // buffer
460
+ if ( i < labelBufferSizeSplit.size () )
461
+ {
462
+ layer->setCustomProperty ( " labeling/bufferSize" , labelBufferSizeSplit.at ( i ) );
463
+ }
464
+
465
+ // buffer color
466
+ if ( i < labelBufferColorSplit.size () )
467
+ {
468
+ QColor c ( labelBufferColorSplit.at ( i ) );
469
+ layer->setCustomProperty ( " labeling/bufferColorR" , c.red () );
470
+ layer->setCustomProperty ( " labeling/bufferColorG" , c.green () );
471
+ layer->setCustomProperty ( " labeling/bufferColorB" , c.blue () );
472
+ }
473
+
474
+ // placement
475
+ int placement = 0 ;
476
+ switch ( geomType )
477
+ {
478
+ case QGis::Point:
479
+ placement = 0 ;
480
+ layer->setCustomProperty ( " labeling/dist" , 2 );
481
+ layer->setCustomProperty ( " labeling/placementFlags" , 0 );
482
+ break ;
483
+ case QGis::Polygon:
484
+ layer->setCustomProperty ( " labeling/dataDefinedProperty9" , 1 );
485
+ layer->setCustomProperty ( " labeling/dataDefinedProperty10" , 2 );
486
+ layer->setCustomProperty ( " labeling/dataDefinedProperty11" , 3 );
487
+ layer->setCustomProperty ( " labeling/dataDefinedProperty12" , 4 );
488
+ break ;
489
+ default :
490
+ placement = 2 ; // parallel placement for line
491
+ layer->setCustomProperty ( " labeling/dist" , 2 );
492
+ layer->setCustomProperty ( " labeling/placementFlags" , 10 );
493
+ }
494
+ layer->setCustomProperty ( " labeling/placement" , placement );
495
+ }
496
+
497
+ fet.setGeometry ( geom );
498
+ layer->dataProvider ()->addFeatures ( QgsFeatureList () << fet );
499
+ return layer;
500
+ }
501
+
502
+ void QgsWMSConfigParser::highlightParameters ( const QMap<QString, QString>& parameterMap, const QString& parameterPrefix, QStringList& geom, QStringList& symbol,
503
+ QStringList& label, QStringList& labelFont, QStringList& labelSize, QStringList& labelWeight, QStringList& labelColor,
504
+ QStringList& labelBufferColor, QStringList& labelBufferSize )
505
+ {
506
+ QString geomParam = parameterMap.value ( parameterPrefix + " HIGHLIGHT_GEOM" );
507
+ QString symbolParam = parameterMap.value ( parameterPrefix + " HIGHLIGHT_SYMBOL" );
508
+ if ( geomParam.isEmpty () || symbolParam.isEmpty () )
509
+ {
510
+ return ;
511
+ }
512
+ QString labelParam = parameterMap.value ( parameterPrefix + " HIGHLIGHT_LABELSTRING" );
513
+ QString labelFontParam = parameterMap.value ( parameterPrefix + " HIGHLIGHT_LABELFONT" );
514
+ QString labelSizeParam = parameterMap.value ( parameterPrefix + " HIGHLIGHT_LABELSIZE" );
515
+ QString labelWeightParam = parameterMap.value ( parameterPrefix + " HIGHLIGHT_LABELWEIGHT" );
516
+ QString labelColorParam = parameterMap.value ( parameterPrefix + " HIGHLIGHT_LABELCOLOR" );
517
+ QString labelBufferColorParam = parameterMap.value ( parameterPrefix + " HIGHLIGHT_LABELBUFFERCOLOR" );
518
+ QString labelBufferSizeParam = parameterMap.value ( parameterPrefix + " HIGHLIGHT_LABELBUFFERSIZE" );
519
+
520
+ geom = geomParam.split ( " ;" );
521
+ symbol = symbolParam.split ( " ;" );
522
+
523
+ label.clear ();
524
+ labelFont.clear ();
525
+ labelSize.clear ();
526
+ labelWeight.clear ();
527
+ labelColor.clear ();
528
+ labelBufferColor.clear ();
529
+ labelBufferSize.clear ();
530
+
531
+ if ( !labelParam.isEmpty () )
532
+ {
533
+ label = labelParam.split ( " ;" );
534
+ }
535
+ if ( !labelFontParam.isEmpty () )
536
+ {
537
+ labelFont = labelFontParam.split ( " ;" );
538
+ }
539
+ if ( !labelSizeParam.isEmpty () )
540
+ {
541
+ labelSize = labelSizeParam.split ( " ;" );
542
+ }
543
+ if ( !labelWeightParam.isEmpty () )
544
+ {
545
+ labelWeight = labelWeightParam.split ( " ;" );
546
+ }
547
+ if ( !labelColorParam.isEmpty () )
548
+ {
549
+ labelColor = labelColorParam.split ( " ;" );
550
+ }
551
+ if ( !labelBufferColorParam.isEmpty () )
552
+ {
553
+ labelBufferColor = labelBufferColorParam.split ( " ;" );
554
+ }
555
+ if ( !labelBufferSizeParam.isEmpty () )
556
+ {
557
+ labelBufferSize = labelBufferSizeParam.split ( " ;" );
558
+ }
559
+ }
560
+
561
+ void QgsWMSConfigParser::removeHighlightLayers ( const QStringList& layerIds )
562
+ {
563
+ QStringList::const_iterator idIt = layerIds.constBegin ();
564
+ for ( ; idIt != layerIds.constEnd (); ++idIt )
565
+ {
566
+ QgsMapLayerRegistry::instance ()->removeMapLayers ( QStringList () << *idIt );
567
+ }
568
+ }
0 commit comments