Skip to content

Commit 071ac3b

Browse files
author
mhugent
committedSep 22, 2009
Show point symbol in rotation preview and a little arrow. Also fixed a bug in QgsSingleSymbolRenderer::clone
git-svn-id: http://svn.osgeo.org/qgis/trunk@11700 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 209488e commit 071ac3b

File tree

5 files changed

+88
-28
lines changed

5 files changed

+88
-28
lines changed
 

‎src/app/qgsmaptoolrotatepointsymbols.cpp

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
QgsMapToolRotatePointSymbols::QgsMapToolRotatePointSymbols( QgsMapCanvas* canvas ): QgsMapToolEdit( canvas ), \
2828
mActiveLayer( 0 ), mFeatureNumber( 0 ), mCurrentMouseAzimut( 0.0 ), mCurrentRotationFeature( 0.0 ), \
29-
mRotating( false ), mRotationItem( 0 ), mCtrlPressed(false)
29+
mRotating( false ), mRotationItem( 0 ), mCtrlPressed( false )
3030
{
3131

3232
}
@@ -123,13 +123,13 @@ void QgsMapToolRotatePointSymbols::canvasPressEvent( QMouseEvent * e )
123123
}
124124

125125
mCurrentRotationFeature = attIt.value().toDouble();
126-
createPixmapItem();
126+
createPixmapItem( pointFeature );
127127
if ( mRotationItem )
128128
{
129129
mRotationItem->setPointLocation( snapResults.at( 0 ).snappedVertex );
130130
}
131131
mCurrentMouseAzimut = calculateAzimut( e->pos() );
132-
setPixmapItemRotation( (int)(mCurrentMouseAzimut) );
132+
setPixmapItemRotation(( int )( mCurrentMouseAzimut ) );
133133
mRotating = true;
134134
}
135135

@@ -165,17 +165,17 @@ void QgsMapToolRotatePointSymbols::canvasMoveEvent( QMouseEvent * e )
165165

166166
//if shift-modifier is pressed, round to 15 degrees
167167
int displayValue;
168-
if(e->modifiers() & Qt::ControlModifier)
168+
if ( e->modifiers() & Qt::ControlModifier )
169169
{
170-
displayValue = roundTo15Degrees(mCurrentRotationFeature);
170+
displayValue = roundTo15Degrees( mCurrentRotationFeature );
171171
mCtrlPressed = true;
172172
}
173173
else
174174
{
175-
displayValue = (int)(mCurrentRotationFeature);
175+
displayValue = ( int )( mCurrentRotationFeature );
176176
mCtrlPressed = false;
177177
}
178-
setPixmapItemRotation(displayValue);
178+
setPixmapItemRotation( displayValue );
179179
}
180180

181181
void QgsMapToolRotatePointSymbols::canvasReleaseEvent( QMouseEvent * e )
@@ -187,13 +187,13 @@ void QgsMapToolRotatePointSymbols::canvasReleaseEvent( QMouseEvent * e )
187187

188188
//write mCurrentRotationFeature to all rotation attributes of feature (mFeatureNumber)
189189
int rotation;
190-
if(mCtrlPressed) //round to 15 degrees
190+
if ( mCtrlPressed ) //round to 15 degrees
191191
{
192-
rotation = roundTo15Degrees(mCurrentRotationFeature);
192+
rotation = roundTo15Degrees( mCurrentRotationFeature );
193193
}
194194
else
195195
{
196-
rotation = (int)mCurrentRotationFeature;
196+
rotation = ( int )mCurrentRotationFeature;
197197
}
198198

199199
QList<int>::const_iterator it = mCurrentRotationAttributes.constBegin();
@@ -255,15 +255,51 @@ double QgsMapToolRotatePointSymbols::calculateAzimut( const QPoint& mousePos )
255255
{
256256
int dx = mousePos.x() - mSnappedPoint.x();
257257
int dy = mousePos.y() - mSnappedPoint.y();
258-
return 180 - atan2( (double) dx, (double) dy ) * 180.0 / M_PI;
258+
return 180 - atan2(( double ) dx, ( double ) dy ) * 180.0 / M_PI;
259259
}
260260

261-
void QgsMapToolRotatePointSymbols::createPixmapItem()
261+
void QgsMapToolRotatePointSymbols::createPixmapItem( QgsFeature& f )
262262
{
263-
delete mRotationItem;
264-
mRotationItem = new QgsPointRotationItem( mCanvas );
265-
mRotationItem->setSymbol( QgsApplication::defaultThemePath() + "mActionArrowUp.png" );
266-
mCanvas->scene()->addItem( mRotationItem );
263+
if ( !mCanvas )
264+
{
265+
return;
266+
}
267+
268+
if ( mActiveLayer && mActiveLayer->renderer() )
269+
{
270+
//get the image that is used for that symbol, but without point rotation
271+
QImage pointImage;
272+
//copy renderer
273+
QgsRenderer* r = mActiveLayer->renderer()->clone();
274+
275+
//set all symbol fields of the cloned renderer to -1. Very ugly but necessary
276+
QList<QgsSymbol*> symbolList( r->symbols() );
277+
QList<QgsSymbol*>::iterator it = symbolList.begin();
278+
for ( ; it != symbolList.end(); ++it )
279+
{
280+
( *it )->setRotationClassificationField( -1 );
281+
}
282+
283+
284+
//get reference to current render context
285+
QgsMapRenderer* mapRenderer = mCanvas->mapRenderer();
286+
if ( !mapRenderer )
287+
{
288+
delete r;
289+
return;
290+
}
291+
QgsRenderContext* renderContext = mCanvas->mapRenderer()->rendererContext(); //todo: check if pointers are not 0
292+
if ( !renderContext )
293+
{
294+
delete r;
295+
return;
296+
}
297+
298+
r->renderFeature( *renderContext, f, &pointImage, false );
299+
mRotationItem = new QgsPointRotationItem( mCanvas );
300+
mRotationItem->setSymbol( pointImage );
301+
delete r;
302+
}
267303
}
268304

269305
void QgsMapToolRotatePointSymbols::setPixmapItemRotation( double rotation )
@@ -272,9 +308,9 @@ void QgsMapToolRotatePointSymbols::setPixmapItemRotation( double rotation )
272308
mRotationItem->update();
273309
}
274310

275-
int QgsMapToolRotatePointSymbols::roundTo15Degrees(double n)
311+
int QgsMapToolRotatePointSymbols::roundTo15Degrees( double n )
276312
{
277-
int m = (int)(n / 15.0 + 0.5);
278-
return (m * 15);
313+
int m = ( int )( n / 15.0 + 0.5 );
314+
return ( m * 15 );
279315
}
280316

‎src/app/qgsmaptoolrotatepointsymbols.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define QGSMAPTOOLROTATEPOINTSYMBOLS_H
1818

1919
#include "qgsmaptooledit.h"
20+
#include "qgsfeature.h"
2021

2122
class QgsPointRotationItem;
2223

@@ -61,12 +62,12 @@ class QgsMapToolRotatePointSymbols: public QgsMapToolEdit
6162
void drawArrow( double azimut ) const;
6263
/**Calculates the azimut between mousePos and mSnappedPoint*/
6364
double calculateAzimut( const QPoint& mousePos );
64-
/**Create item that shows rotation to the user*/
65-
void createPixmapItem();
65+
/**Create item with the point symbol for a specific feature. This will be used to show the rotation to the user*/
66+
void createPixmapItem( QgsFeature& f );
6667
/**Sets the rotation of the pixmap item*/
6768
void setPixmapItemRotation( double rotation );
6869
/**Rounds value to 15 degree integer (used if ctrl pressed)*/
69-
static int roundTo15Degrees(double n);
70+
static int roundTo15Degrees( double n );
7071
};
7172

7273
#endif // QGSMAPTOOLROTATEPOINTSYMBOLS_H

‎src/app/qgspointrotationitem.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void QgsPointRotationItem::paint( QPainter * painter )
5353
double h, dAngel;
5454
if ( mPixmap.width() > 0 && mPixmap.height() > 0 )
5555
{
56-
h = sqrt( (double) mPixmap.width() * mPixmap.width() + mPixmap.height() * mPixmap.height() ) / 2; //the half of the item diagonal
56+
h = sqrt(( double ) mPixmap.width() * mPixmap.width() + mPixmap.height() * mPixmap.height() ) / 2; //the half of the item diagonal
5757
dAngel = acos( mPixmap.width() / ( h * 2 ) ) * 180 / M_PI; //the diagonal angel of the original rect
5858
x = h * cos(( mRotation - dAngel ) * M_PI / 180 );
5959
y = h * sin(( mRotation - dAngel ) * M_PI / 180 );
@@ -69,7 +69,7 @@ void QgsPointRotationItem::paint( QPainter * painter )
6969
QFontMetricsF fm( mFont );
7070
painter->fillRect( mPixmap.width(), 0, mItemSize.width() - mPixmap.width(), mItemSize.height(), QColor( Qt::white ) );
7171
painter->setFont( mFont );
72-
painter->drawText( mPixmap.width(), mPixmap.height() / 2.0 + fm.height() / 2.0, QString::number( mRotation) );
72+
painter->drawText( mPixmap.width(), mPixmap.height() / 2.0 + fm.height() / 2.0, QString::number( mRotation ) );
7373
}
7474

7575
void QgsPointRotationItem::setPointLocation( const QgsPoint& p )
@@ -78,10 +78,24 @@ void QgsPointRotationItem::setPointLocation( const QgsPoint& p )
7878
setPos( transformedPoint.x() - mPixmap.width() / 2.0, transformedPoint.y() - mPixmap.height() / 2.0 );
7979
}
8080

81-
void QgsPointRotationItem::setSymbol( const QString& symbolPath )
81+
void QgsPointRotationItem::setSymbol( const QImage& symbolImage )
8282
{
83-
mPixmap = QPixmap( symbolPath );
83+
mPixmap = QPixmap::fromImage( symbolImage );
8484
QFontMetricsF fm( mFont );
85+
86+
//draw arrow
87+
QPainter p( &mPixmap );
88+
QPen pen;
89+
pen.setWidth( 1 );
90+
pen.setColor( QColor( Qt::red ) );
91+
p.setPen( pen );
92+
int halfItemWidth = mPixmap.width() / 2;
93+
int quarterItemHeight = mPixmap.height() / 4;
94+
p.drawLine( halfItemWidth, mPixmap.height(), halfItemWidth, 0 );
95+
p.drawLine( halfItemWidth, 0, mPixmap.width() / 4, quarterItemHeight );
96+
p.drawLine( halfItemWidth, 0, mPixmap.width() * 0.75, quarterItemHeight );
97+
98+
//set item size
8599
mItemSize.setWidth( mPixmap.width() + fm.width( "360" ) );
86100
double pixmapHeight = mPixmap.height();
87101
double fontHeight = fm.height();

‎src/app/qgspointrotationitem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class QgsPointRotationItem: public QgsMapCanvasItem
3636
Units are degrees, starting from north direction, clockwise direction*/
3737
void setSymbolRotation( int r ) {mRotation = r;}
3838

39-
/**Sets a symbol from image file*/
40-
void setSymbol( const QString& symbolPath );
39+
/**Sets rotation symbol from image (takes ownership)*/
40+
void setSymbol( const QImage& symbolImage );
4141

4242
private:
4343
QgsPointRotationItem();

‎src/core/renderer/qgssinglesymbolrenderer.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ QgsSingleSymbolRenderer& QgsSingleSymbolRenderer::operator=( const QgsSingleSymb
7272

7373
for ( QMap<QString, QgsSymbol *>::const_iterator it = other.mSymbols.begin(); it != other.mSymbols.end(); it++ )
7474
mSymbols[ it.key()] = new QgsSymbol( *it.value() );
75+
76+
if ( mSymbols.size() > 0 )
77+
{
78+
mSymbol0 = mSymbols[0];
79+
}
80+
else
81+
{
82+
mSymbol0 = 0;
83+
}
7584
}
7685
updateSymbolAttributes();
7786
return *this;

0 commit comments

Comments
 (0)
Please sign in to comment.