selection_rendering_fix.diff

Patch to implement suggested change to rendering - Chris Crook, 2010-04-13 05:49 PM

Download (20.6 KB)

View differences:

src/core/qgsvectorlayer.cpp (working copy)
706 706

  
707 707
  mRendererV2->startRender( rendererContext, this );
708 708

  
709
  QgsSingleSymbolRendererV2* selRenderer = NULL;
710
  if ( !mSelectedFeatureIds.isEmpty() )
711
  {
712
    selRenderer = new QgsSingleSymbolRendererV2( QgsSymbolV2::defaultSymbol( geometryType() ) );
713
    selRenderer->symbol()->setColor( QgsRenderer::selectionColor() );
714
    selRenderer->setVertexMarkerAppearance( currentVertexMarkerType(), currentVertexMarkerSize() );
715
    selRenderer->startRender( rendererContext, this );
716
  }
717

  
718 709
#ifndef Q_WS_MAC
719 710
  int totalFeatures = pendingFeatureCount();
720 711
  int featureCount = 0;
......
748 739
      bool drawMarker = ( mEditable && ( !vertexMarkerOnlyForSelection || sel ) );
749 740

  
750 741
      // render feature
751
      if ( sel )
752
        selRenderer->renderFeature( fet, rendererContext, -1, drawMarker );
753
      else
754
        mRendererV2->renderFeature( fet, rendererContext, -1, drawMarker );
742
      mRendererV2->renderFeature( fet, rendererContext, -1, sel, drawMarker );
755 743

  
756 744
      // labeling - register feature
757 745
      if ( labeling && mRendererV2->symbolForFeature( fet ) != NULL )
......
772 760
    ++featureCount;
773 761
#endif //Q_WS_MAC
774 762
  }
775

  
776
  stopRendererV2( rendererContext, selRenderer );
777 763
}
778 764

  
779 765
void QgsVectorLayer::drawRendererV2Levels( QgsRenderContext& rendererContext, bool labeling )
......
886 872

  
887 873
        try
888 874
        {
889
          if ( sel )
890
            selRenderer->renderFeature( *fit, rendererContext, -1, drawMarker );
891
          else
892
            mRendererV2->renderFeature( *fit, rendererContext, layer, drawMarker );
875
          mRendererV2->renderFeature( *fit, rendererContext, layer, sel, drawMarker );
893 876
        }
894 877
        catch ( const QgsCsException &cse )
895 878
        {
src/core/symbology-ng/qgsfillsymbollayerv2.cpp (working copy)
47 47
{
48 48
  mColor.setAlphaF( context.alpha() );
49 49
  mBrush = QBrush( mColor, mBrushStyle );
50
  QColor selColor = selectionColor();
51
  // selColor.setAlphaF( context.alpha() );
52
  mSelBrush = QBrush( selColor );
53
  if( selectFillStyle )	 mSelBrush.setStyle( mBrushStyle );
50 54
  mBorderColor.setAlphaF( context.alpha() );
51 55
  mPen = QPen( mBorderColor );
52 56
  mPen.setStyle( mBorderStyle );
......
65 69
    return;
66 70
  }
67 71

  
68
  p->setBrush( mBrush );
72
  p->setBrush( context.selected() ? mSelBrush : mBrush );
69 73
  p->setPen( mPen );
70 74

  
71 75
  _renderPolygon( p, points, rings );
......
209 213
  {
210 214
    return;
211 215
  }
216
  p->setPen( QPen( Qt::NoPen ) );
217
  if( context.selected())
218
  {
219
	  QColor selColor = selectionColor();
220
	  if( ! selectionIsOpaque ) selColor.setAlphaF( context.alpha() );
221
	  p->setBrush( QBrush(selColor));
222
	  _renderPolygon(p,points,rings);
223
  }
212 224
  p->setBrush( mBrush );
213
  p->setPen( QPen( Qt::NoPen ) );
214 225
  _renderPolygon( p, points, rings );
215 226
  if ( mOutline )
216 227
  {
217
    mOutline->renderPolyline( points, context.renderContext() );
228
    mOutline->renderPolyline( points, context.renderContext(), -1, selectFillBorder && context.selected() );
218 229
    if ( rings )
219 230
    {
220 231
      QList<QPolygonF>::const_iterator ringIt = rings->constBegin();
221 232
      for ( ; ringIt != rings->constEnd(); ++ringIt )
222 233
      {
223
        mOutline->renderPolyline( *ringIt, context.renderContext() );
234
        mOutline->renderPolyline( *ringIt, context.renderContext(), -1, selectFillBorder && context.selected() );
224 235
      }
225 236
    }
226 237
  }
src/core/symbology-ng/qgsfillsymbollayerv2.h (working copy)
54 54

  
55 55
  protected:
56 56
    QBrush mBrush;
57
	QBrush mSelBrush;
57 58
    Qt::BrushStyle mBrushStyle;
58 59
    QColor mBorderColor;
59 60
    Qt::PenStyle mBorderStyle;
src/core/symbology-ng/qgslinesymbollayerv2.cpp (working copy)
83 83
  }
84 84
  mPen.setJoinStyle( mPenJoinStyle );
85 85
  mPen.setCapStyle( mPenCapStyle );
86

  
87
  mSelPen = mPen;
88
  QColor selColor = selectionColor();
89
  if( ! selectionIsOpaque ) selColor.setAlphaF(context.alpha());
90
  mSelPen.setColor(selColor);
86 91
}
87 92

  
88 93
void QgsSimpleLineSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
......
97 102
    return;
98 103
  }
99 104

  
100
  p->setPen( mPen );
105
  p->setPen( context.selected() ? mSelPen : mPen );
101 106
  if ( mOffset == 0 )
102 107
  {
103 108
    p->drawPolyline( points );
......
309 314
    // draw first marker
310 315
    if ( first )
311 316
    {
312
      mMarker->renderPoint( lastPt, rc );
317
      mMarker->renderPoint( lastPt, rc, -1, context.selected() );
313 318
      first = false;
314 319
    }
315 320

  
......
319 324
      // "c" is 1 for regular point or in interval (0,1] for begin of line segment
320 325
      lastPt += c * diff;
321 326
      lengthLeft -= painterUnitInterval;
322
      mMarker->renderPoint( lastPt, rc );
327
      mMarker->renderPoint( lastPt, rc, -1, context.selected() );
323 328
      c = 1; // reset c (if wasn't 1 already)
324 329
    }
325 330

  
......
401 406
  QColor penColor = mColor;
402 407
  penColor.setAlphaF( context.alpha() );
403 408
  mPen.setColor( penColor );
409
  QColor selColor = selectionColor();
410
  if( ! selectionIsOpaque ) selColor.setAlphaF( context.alpha() );
411
  mSelPen.setColor(selColor);
404 412
}
405 413

  
406 414
void QgsLineDecorationSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
......
442 450
  QPointF p2_1 = p2 - QPointF( size * cos( angle1 ), size * sin( angle1 ) );
443 451
  QPointF p2_2 = p2 - QPointF( size * cos( angle2 ), size * sin( angle2 ) );
444 452

  
445
  p->setPen( mPen );
453
  p->setPen( context.selected() ? mSelPen : mPen );
446 454
  p->drawLine( p2, p2_1 );
447 455
  p->drawLine( p2, p2_2 );
448 456
}
src/core/symbology-ng/qgslinesymbollayerv2.h (working copy)
64 64
    Qt::PenJoinStyle mPenJoinStyle;
65 65
    Qt::PenCapStyle mPenCapStyle;
66 66
    QPen mPen;
67
	QPen mSelPen;
67 68
    double mOffset;
68 69
    //use a custom dash dot pattern instead of the predefined ones
69 70
    bool mUseCustomDashPattern;
......
159 160

  
160 161
  protected:
161 162
    QPen mPen;
163
	QPen mSelPen;
162 164

  
163 165
};
164 166

  
src/core/symbology-ng/qgsmarkersymbollayerv2.cpp (working copy)
68 68
  mBrush = QBrush( mColor );
69 69
  mPen = QPen( mBorderColor );
70 70
  mPen.setWidthF( context.outputLineWidth( mPen.widthF() ) );
71
  QColor selColor = selectionColor();
72
  mSelBrush = QBrush( selColor );
73
  mSelPen = QPen( selColor == mColor ? selColor : mBorderColor );
74
  mSelPen.setWidthF(mPen.widthF());
71 75

  
72 76
  mPolygon.clear();
73 77

  
......
148 152
  else
149 153
  {
150 154
    // some markers can't be drawn as a polygon (circle, cross)
155
    // For these set the selected border color to the selected color
156

  
157
    if( mName != "circle" ) mSelPen.setColor(selColor);
151 158
  }
152 159

  
153 160
  // rotate if needed
......
176 183
  drawMarker( &p, context );
177 184
  p.end();
178 185

  
186
  // Construct the selected version of the Cache
187

  
188
  mSelCache = QImage( QSize( imageSize, imageSize ), QImage::Format_ARGB32_Premultiplied );
189
  mSelCache.fill( 0 );
190

  
191
  p.begin( &mSelCache );
192
  p.setRenderHint( QPainter::Antialiasing );
193
  p.setBrush( mSelBrush );
194
  p.setPen( mSelPen );
195
  p.translate( QPointF( center, center ) );
196
  drawMarker( &p, context );
197
  p.end();
198

  
199
  // Check that the selected version is different.  If not, then re-render, 
200
  // filling the background with the selection colour and using the normal
201
  // colours for the symbol .. could be ugly!
202

  
203
  if( mSelCache == mCache )
204
  { 
205
     p.begin( &mSelCache );
206
     p.setRenderHint( QPainter::Antialiasing );
207
     p.fillRect(0,0,imageSize,imageSize,selColor);
208
     p.setBrush( mBrush );
209
     p.setPen( mPen );
210
     p.translate( QPointF( center, center ) );
211
     drawMarker( &p, context );
212
     p.end();
213
  }
214

  
179 215
  //opacity
180 216
  if ( context.alpha() < 1.0 )
181 217
  {
182 218
    QgsSymbolLayerV2Utils::multiplyImageOpacity( &mCache, context.alpha() );
219
    if( ! selectionIsOpaque ) QgsSymbolLayerV2Utils::multiplyImageOpacity( &mSelCache, context.alpha() );
183 220
  }
184 221
}
185 222

  
......
203 240
  //p->translate(point);
204 241

  
205 242
  //drawMarker(p);
206
  //mCache.save("/home/marco/tmp/marker.png", "PNG");
207
  double s = mCache.width() / context.renderContext().rasterScaleFactor();
243
  //mCache.save("/home/marco/tmp/marker.png","PNG");
244
  QImage &img = context.selected() ? mSelCache : mCache;
245
  double s = img.width() / context.renderContext().rasterScaleFactor();
208 246
  p->drawImage( QRectF( point.x() - s / 2.0 + context.outputLineWidth( mOffset.x() ),
209 247
                        point.y() - s / 2.0 + context.outputLineWidth( mOffset.y() ),
210
                        s, s ), mCache );
248
                        s, s ), img );
211 249
  //p->restore();
212 250
}
213 251

  
......
328 366
  QSvgRenderer renderer( mPath );
329 367
  QPainter painter( &mPicture );
330 368
  renderer.render( &painter, rect );
369
  double selPictureSize = pictureSize*1.2;
370
  QPainter selPainter( &mSelPicture );
371
  selPainter.setRenderHint( QPainter::Antialiasing );
372
  selPainter.setBrush(QBrush(selectionColor()));
373
  selPainter.setPen( Qt::NoPen );
374
  selPainter.drawEllipse(QPointF(0,0),pictureSize*0.6,pictureSize*0.6);
375
  renderer.render(&selPainter,rect);
331 376
}
332 377

  
333 378
void QgsSvgMarkerSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
......
350 395
  if ( mAngle != 0 )
351 396
    p->rotate( mAngle );
352 397

  
353
  p->drawPicture( 0, 0, mPicture );
398
  QPicture &pct = context.selected() ? mSelPicture : mPicture;
399
  p->drawPicture( 0, 0, pct );
354 400

  
355 401
  if ( mAngle != 0 )
356 402
    p->rotate( -mAngle );
......
529 575
void QgsFontMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context )
530 576
{
531 577
  QPainter* p = context.renderContext().painter();
532
  QColor penColor = mColor;
578
  QColor penColor = context.selected() ? selectionColor() : mColor;
533 579
  penColor.setAlphaF( context.alpha() );
534 580
  p->setPen( penColor );
535 581
  p->setFont( mFont );
src/core/symbology-ng/qgsmarkersymbollayerv2.h (working copy)
59 59
    QPolygonF mPolygon;
60 60
    QString mName;
61 61
    QImage mCache;
62
	QPen mSelPen;
63
	QBrush mSelBrush;
64
	QImage mSelCache;
62 65
};
63 66

  
64 67
//////////
......
110 113

  
111 114
    QString mPath;
112 115
    QPicture mPicture;
116
	QPicture mSelPicture;
113 117
};
114 118

  
115 119

  
src/core/symbology-ng/qgsrendererv2.cpp (working copy)
151 151
}
152 152

  
153 153

  
154
void QgsFeatureRendererV2::renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer, bool drawVertexMarker )
154
void QgsFeatureRendererV2::renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer, bool selected, bool drawVertexMarker )
155 155
{
156 156
  QgsSymbolV2* symbol = symbolForFeature( feature );
157 157
  if ( symbol == NULL )
......
172 172
      }
173 173
      QPointF pt;
174 174
      _getPoint( pt, context, geom->asWkb() );
175
      (( QgsMarkerSymbolV2* )symbol )->renderPoint( pt, context, layer );
175
      (( QgsMarkerSymbolV2* )symbol )->renderPoint( pt, context, layer, selected );
176 176

  
177 177
      //if ( drawVertexMarker )
178 178
      //  renderVertexMarker( pt, context );
......
189 189
      }
190 190
      QPolygonF pts;
191 191
      _getLineString( pts, context, geom->asWkb() );
192
      (( QgsLineSymbolV2* )symbol )->renderPolyline( pts, context, layer );
192
      (( QgsLineSymbolV2* )symbol )->renderPolyline( pts, context, layer, selected );
193 193

  
194 194
      if ( drawVertexMarker )
195 195
        renderVertexMarkerPolyline( pts, context );
......
207 207
      QPolygonF pts;
208 208
      QList<QPolygonF> holes;
209 209
      _getPolygon( pts, holes, context, geom->asWkb() );
210
      (( QgsFillSymbolV2* )symbol )->renderPolygon( pts, ( holes.count() ? &holes : NULL ), context, layer );
210
      (( QgsFillSymbolV2* )symbol )->renderPolygon( pts, ( holes.count() ? &holes : NULL ), context, layer, selected );
211 211

  
212 212
      if ( drawVertexMarker )
213 213
        renderVertexMarkerPolygon( pts, ( holes.count() ? &holes : NULL ), context );
......
231 231
      for ( unsigned int i = 0; i < num; ++i )
232 232
      {
233 233
        ptr = _getPoint( pt, context, ptr );
234
        (( QgsMarkerSymbolV2* )symbol )->renderPoint( pt, context, layer );
234
        (( QgsMarkerSymbolV2* )symbol )->renderPoint( pt, context, layer, selected );
235 235

  
236 236
        //if ( drawVertexMarker )
237 237
        //  renderVertexMarker( pt, context );
......
256 256
      for ( unsigned int i = 0; i < num; ++i )
257 257
      {
258 258
        ptr = _getLineString( pts, context, ptr );
259
        (( QgsLineSymbolV2* )symbol )->renderPolyline( pts, context, layer );
259
        (( QgsLineSymbolV2* )symbol )->renderPolyline( pts, context, layer, selected );
260 260

  
261 261
        if ( drawVertexMarker )
262 262
          renderVertexMarkerPolyline( pts, context );
......
282 282
      for ( unsigned int i = 0; i < num; ++i )
283 283
      {
284 284
        ptr = _getPolygon( pts, holes, context, ptr );
285
        (( QgsFillSymbolV2* )symbol )->renderPolygon( pts, ( holes.count() ? &holes : NULL ), context, layer );
285
        (( QgsFillSymbolV2* )symbol )->renderPolygon( pts, ( holes.count() ? &holes : NULL ), context );
286 286

  
287 287
        if ( drawVertexMarker )
288 288
          renderVertexMarkerPolygon( pts, ( holes.count() ? &holes : NULL ), context );
src/core/symbology-ng/qgsrendererv2.h (working copy)
73 73

  
74 74
    virtual QgsFeatureRendererV2* clone() = 0;
75 75

  
76
    virtual void renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool drawVertexMarker = false );
76
    virtual void renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false );
77 77

  
78 78
    //! for debugging
79 79
    virtual QString dump();
src/core/symbology-ng/qgssymbollayerv2.cpp (working copy)
1 1

  
2 2
#include "qgssymbollayerv2.h"
3 3

  
4
#include "qgsrenderer.h"
4 5
#include "qgsrendercontext.h"
5 6

  
6 7
#include <QSize>
......
10 11

  
11 12

  
12 13

  
14
QColor QgsSymbolLayerV2::selectionColor()
15
{
16
	return QgsRenderer::selectionColor();
17
}
18

  
13 19
QgsMarkerSymbolLayerV2::QgsMarkerSymbolLayerV2( bool locked )
14 20
    : QgsSymbolLayerV2( QgsSymbolV2::Marker, locked )
15 21
{
src/core/symbology-ng/qgssymbollayerv2.h (working copy)
18 18
class QgsSymbolV2;
19 19

  
20 20

  
21

  
22 21
class CORE_EXPORT QgsSymbolLayerV2
23 22
{
24 23
  public:
......
53 52
    void setRenderingPass( int renderingPass ) { mRenderingPass = renderingPass; }
54 53
    int renderingPass() const { return mRenderingPass; }
55 54

  
55
	// Colour used for selections
56

  
57
	static QColor selectionColor();
58

  
56 59
  protected:
57 60
    QgsSymbolLayerV2( QgsSymbolV2::SymbolType type, bool locked = false )
58 61
        : mType( type ), mLocked( locked ), mRenderingPass( 0 ) {}
......
61 64
    bool mLocked;
62 65
    QColor mColor;
63 66
    int mRenderingPass;
67

  
68
	// Configuration of selected symbology implementation
69
    static const bool selectionIsOpaque = true;  // Selection ignores symbol alpha
70
    static const bool selectFillBorder = false;  // Fill symbol layer also selects border symbology
71
	static const bool selectFillStyle = false;   // Fill symbol uses symbol layer style..
72

  
64 73
};
65 74

  
66 75
//////////////////////
src/core/symbology-ng/qgssymbolv2.cpp (working copy)
244 244

  
245 245
////////////////////
246 246

  
247
QgsSymbolV2RenderContext::QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u, qreal alpha )
248
    : mRenderContext( c ), mOutputUnit( u ), mAlpha( alpha )
247
QgsSymbolV2RenderContext::QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u, qreal alpha, bool selected )
248
    : mRenderContext( c ), mOutputUnit( u ), mAlpha( alpha ), mSelected( selected )
249 249
{
250 250

  
251 251
}
......
338 338
  return maxSize;
339 339
}
340 340

  
341
void QgsMarkerSymbolV2::renderPoint( const QPointF& point, QgsRenderContext& context, int layer )
341
void QgsMarkerSymbolV2::renderPoint( const QPointF& point, QgsRenderContext& context, int layer, bool selected )
342 342
{
343
  QgsSymbolV2RenderContext symbolContext( context, mOutputUnit, mAlpha );
343
  QgsSymbolV2RenderContext symbolContext( context, mOutputUnit, mAlpha, selected );
344 344
  if ( layer != -1 )
345 345
  {
346 346
    if ( layer >= 0 && layer < mLayers.count() )
......
407 407
  return maxWidth;
408 408
}
409 409

  
410
void QgsLineSymbolV2::renderPolyline( const QPolygonF& points, QgsRenderContext& context, int layer )
410
void QgsLineSymbolV2::renderPolyline( const QPolygonF& points, QgsRenderContext& context, int layer, bool selected )
411 411
{
412
  QgsSymbolV2RenderContext symbolContext( context, mOutputUnit, mAlpha );
412
  QgsSymbolV2RenderContext symbolContext( context, mOutputUnit, mAlpha, selected );
413 413
  if ( layer != -1 )
414 414
  {
415 415
    if ( layer >= 0 && layer < mLayers.count() )
......
443 443
    mLayers.append( new QgsSimpleFillSymbolLayerV2() );
444 444
}
445 445

  
446
void QgsFillSymbolV2::renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsRenderContext& context, int layer )
446
void QgsFillSymbolV2::renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsRenderContext& context, int layer, bool selected )
447 447
{
448
  QgsSymbolV2RenderContext symbolContext( context, mOutputUnit, mAlpha );
448
  QgsSymbolV2RenderContext symbolContext( context, mOutputUnit, mAlpha, selected );
449 449
  if ( layer != -1 )
450 450
  {
451 451
    if ( layer >= 0 && layer < mLayers.count() )
src/core/symbology-ng/qgssymbolv2.h (working copy)
103 103
class CORE_EXPORT QgsSymbolV2RenderContext
104 104
{
105 105
  public:
106
    QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u , qreal alpha = 1.0 );
106
    QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u , qreal alpha = 1.0, bool selected = false );
107 107
    ~QgsSymbolV2RenderContext();
108 108

  
109 109
    QgsRenderContext& renderContext() { return mRenderContext; }
......
115 115
    qreal alpha() const { return mAlpha; }
116 116
    void setAlpha( qreal alpha ) { mAlpha = alpha; }
117 117

  
118
	bool selected() const { return mSelected; }
119
	void setSelected( bool selected ) { mSelected = selected; }
120

  
118 121
    double outputLineWidth( double width ) const;
119 122
    double outputPixelSize( double size ) const;
120 123

  
......
125 128
    QgsRenderContext& mRenderContext;
126 129
    QgsSymbolV2::OutputUnit mOutputUnit;
127 130
    qreal mAlpha;
131
	bool mSelected;
128 132
};
129 133

  
130 134

  
......
144 148
    void setSize( double size );
145 149
    double size();
146 150

  
147
    void renderPoint( const QPointF& point, QgsRenderContext& context, int layer = -1 );
151
    void renderPoint( const QPointF& point, QgsRenderContext& context, int layer = -1, bool selected = false );
148 152

  
149 153
    virtual QgsSymbolV2* clone() const;
150 154
};
......
159 163
    void setWidth( double width );
160 164
    double width();
161 165

  
162
    void renderPolyline( const QPolygonF& points, QgsRenderContext& context, int layer = -1 );
166
    void renderPolyline( const QPolygonF& points, QgsRenderContext& context, int layer = -1, bool selected = false );
163 167

  
164 168
    virtual QgsSymbolV2* clone() const;
165 169
};
......
171 175
  public:
172 176
    QgsFillSymbolV2( QgsSymbolLayerV2List layers = QgsSymbolLayerV2List() );
173 177

  
174
    void renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsRenderContext& context, int layer = -1 );
178
    void renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsRenderContext& context, int layer = -1, bool selected = false );
175 179

  
176 180
    virtual QgsSymbolV2* clone() const;
177 181
};