Skip to content

Commit

Permalink
use symbologyv2 for lines and add marker symbols (in addition to Cross)
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennesky committed Jun 18, 2012
1 parent 0181324 commit 25ee41c
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 47 deletions.
114 changes: 110 additions & 4 deletions src/app/qgsdecorationgrid.cpp
Expand Up @@ -27,6 +27,8 @@
#include "qgspoint.h"
#include "qgsproject.h"
#include "qgssymbollayerv2utils.h" //for pointOnLineWithDistance
#include "qgssymbolv2.h" //for symbology
#include "qgsrendercontext.h"

#include <QPainter>
#include <QAction>
Expand All @@ -39,6 +41,7 @@
#include <QMenu>
#include <QFile>
#include <QLocale>
#include <QDomDocument>

//non qt includes
#include <cmath>
Expand All @@ -55,12 +58,29 @@ QgsDecorationGrid::QgsDecorationGrid( QObject* parent )
: QgsDecorationItem( parent )
{
setName( "Grid" );
mLineSymbol = 0;
mMarkerSymbol = 0;
projectRead();
}

QgsDecorationGrid::~QgsDecorationGrid()
{
if ( mLineSymbol )
delete mLineSymbol;
if ( mMarkerSymbol )
delete mMarkerSymbol;
}

void QgsDecorationGrid::setLineSymbol( QgsLineSymbolV2* symbol )
{
delete mLineSymbol;
mLineSymbol = symbol;
}

void QgsDecorationGrid::setMarkerSymbol( QgsMarkerSymbolV2* symbol )
{
delete mMarkerSymbol;
mMarkerSymbol = symbol;
}

void QgsDecorationGrid::projectRead()
Expand All @@ -79,6 +99,34 @@ void QgsDecorationGrid::projectRead()
mGridAnnotationFont.fromString( QgsProject::instance()->readEntry( mNameConfig, "/AnnotationFont", "" ) );
mAnnotationFrameDistance = QgsProject::instance()->readDoubleEntry( mNameConfig, "/AnnotationFrameDistance", 0 );
mGridAnnotationPrecision = QgsProject::instance()->readNumEntry( mNameConfig, "/AnnotationPrecision", 3 );

// read symbol info from xml
QDomDocument doc;
QDomElement elem;
QString xml;
xml = QgsProject::instance()->readEntry( mNameConfig, "/LineSymbol" );
if ( xml != "" )
{
doc.setContent( xml );
elem = doc.documentElement();
if ( mLineSymbol )
delete mLineSymbol;
mLineSymbol = dynamic_cast<QgsLineSymbolV2*>( QgsSymbolLayerV2Utils::loadSymbol( elem ) );
if ( ! mLineSymbol )
mLineSymbol = new QgsLineSymbolV2();
}
xml = QgsProject::instance()->readEntry( mNameConfig, "/MarkerSymbol" );
if ( xml != "" )
{
doc.setContent( xml );
elem = doc.documentElement();
if ( mMarkerSymbol )
delete mMarkerSymbol;
mMarkerSymbol = dynamic_cast<QgsMarkerSymbolV2*>( QgsSymbolLayerV2Utils::loadSymbol( elem ) );
if ( ! mMarkerSymbol )
mMarkerSymbol = new QgsMarkerSymbolV2();
}

}

void QgsDecorationGrid::saveToProject()
Expand All @@ -97,6 +145,25 @@ void QgsDecorationGrid::saveToProject()
QgsProject::instance()->writeEntry( mNameConfig, "/AnnotationFont", mGridAnnotationFont.toString() );
QgsProject::instance()->writeEntry( mNameConfig, "/AnnotationFrameDistance", mAnnotationFrameDistance );
QgsProject::instance()->writeEntry( mNameConfig, "/AnnotationPrecision", mGridAnnotationPrecision );

// write symbol info to xml
QDomDocument doc;
QDomElement elem;
if ( mLineSymbol )
{
elem = QgsSymbolLayerV2Utils::saveSymbol( "line symbol", mLineSymbol, doc );
doc.appendChild( elem );
// FIXME this works, but XML will not be valid as < is replaced by &lt;
QgsProject::instance()->writeEntry( mNameConfig, "/LineSymbol", doc.toString() );
}
if ( mLineSymbol )
{
doc.setContent( QString( "" ) );
elem = QgsSymbolLayerV2Utils::saveSymbol( "marker symbol", mMarkerSymbol, doc );
doc.appendChild( elem );
QgsProject::instance()->writeEntry( mNameConfig, "/MarkerSymbol", doc.toString() );
}

}


Expand All @@ -115,7 +182,7 @@ void QgsDecorationGrid::render( QPainter * p )
if ( ! mEnabled )
return;

p->setPen( mGridPen );
// p->setPen( mGridPen );

QList< QPair< double, QLineF > > verticalLines;
yGridLines( verticalLines, p );
Expand All @@ -130,17 +197,34 @@ void QgsDecorationGrid::render( QPainter * p )
//simpler approach: draw vertical lines first, then horizontal ones
if ( mGridStyle == QgsDecorationGrid::Solid )
{
if ( ! mLineSymbol )
return;

QgsRenderContext context;
context.setPainter( p );
mLineSymbol->startRender( context, 0 );

for ( ; vIt != verticalLines.constEnd(); ++vIt )
{
p->drawLine( vIt->second );
// p->drawLine( vIt->second );
// need to convert QLineF to QPolygonF ...
QVector<QPointF> poly;
poly << vIt->second.p1() << vIt->second.p2();
mLineSymbol->renderPolyline( QPolygonF( poly ), 0, context );
}

for ( ; hIt != horizontalLines.constEnd(); ++hIt )
{
p->drawLine( hIt->second );
// p->drawLine( hIt->second );
// need to convert QLineF to QPolygonF ...
QVector<QPointF> poly;
poly << hIt->second.p1() << hIt->second.p2();
mLineSymbol->renderPolyline( QPolygonF( poly ), 0, context );
}

mLineSymbol->stopRender( context );
}
else //cross
else if ( mGridStyle == QgsDecorationGrid::Cross )
{
QPointF intersectionPoint, crossEnd1, crossEnd2;
for ( ; vIt != verticalLines.constEnd(); ++vIt )
Expand Down Expand Up @@ -186,8 +270,30 @@ void QgsDecorationGrid::render( QPainter * p )
crossEnd1 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( hIt->second.p2(), hIt->second.p1(), mCrossLength );
p->drawLine( hIt->second.p2(), crossEnd1 );
}
}
else //marker
{
if ( ! mMarkerSymbol )
return;

QgsRenderContext context;
context.setPainter( p );
mMarkerSymbol->startRender( context, 0 );

QPointF intersectionPoint, crossEnd1, crossEnd2;
for ( ; vIt != verticalLines.constEnd(); ++vIt )
{
//test for intersection with every horizontal line
hIt = horizontalLines.constBegin();
for ( ; hIt != horizontalLines.constEnd(); ++hIt )
{
if ( hIt->second.intersect( vIt->second, &intersectionPoint ) == QLineF::BoundedIntersection )
{
mMarkerSymbol->renderPoint( intersectionPoint, 0, context );
}
}
}
mMarkerSymbol->stopRender( context );
}

// p->setClipRect( thisPaintRect , Qt::NoClip );
Expand Down
16 changes: 15 additions & 1 deletion src/app/qgsdecorationgrid.h
Expand Up @@ -21,6 +21,8 @@
#include "qgsdecorationitem.h"

class QPainter;
class QgsLineSymbolV2;
class QgsMarkerSymbolV2;

#include <QColor>
#include <QPen>
Expand All @@ -38,7 +40,8 @@ class QgsDecorationGrid: public QgsDecorationItem
enum GridStyle
{
Solid = 0, //solid lines
Cross //only draw line crossings
Cross = 1, //only draw line crossings
Marker = 2 //user-defined marker
};

enum GridAnnotationPosition
Expand Down Expand Up @@ -126,6 +129,14 @@ class QgsDecorationGrid: public QgsDecorationItem
void setCrossLength( double l ) {mCrossLength = l;}
double crossLength() {return mCrossLength;}

/**Set symbol that is used to draw grid lines. Takes ownership*/
void setLineSymbol( QgsLineSymbolV2* symbol );
const QgsLineSymbolV2* lineSymbol() const { return mLineSymbol; }

/**Set symbol that is used to draw markers. Takes ownership*/
void setMarkerSymbol( QgsMarkerSymbolV2* symbol );
const QgsMarkerSymbolV2* markerSymbol() const { return mMarkerSymbol; }

public slots:
//! set values on the gui when a project is read or the gui first loaded
void projectRead();
Expand Down Expand Up @@ -175,6 +186,9 @@ class QgsDecorationGrid: public QgsDecorationItem
/**The length of the cross sides for mGridStyle Cross*/
double mCrossLength;

QgsLineSymbolV2* mLineSymbol;
QgsMarkerSymbolV2* mMarkerSymbol;

/**Draw coordinates for mGridAnnotationType Coordinate
@param p drawing painter
@param hLines horizontal coordinate lines in item coordinates
Expand Down
80 changes: 38 additions & 42 deletions src/ui/qgsdecorationgriddialog.ui
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>511</width>
<height>353</height>
<width>531</width>
<height>349</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -18,7 +18,7 @@
<enum>QLayout::SetMinimumSize</enum>
</property>
<item>
<layout class="QGridLayout" name="gridLayout_3">
<layout class="QGridLayout" name="gridLayout_3" columnminimumwidth="125,0,0,0,0">
<item row="1" column="0">
<widget class="QLabel" name="mGridTypeLabel">
<property name="accessibleName">
Expand Down Expand Up @@ -139,45 +139,15 @@
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="mLineWidthLabel">
<widget class="QLabel" name="mLineSymbolLabel">
<property name="text">
<string>Line width</string>
<string>Line symbol</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="7" column="2">
<widget class="QDoubleSpinBox" name="mLineWidthSpinBox">
<property name="decimals">
<number>5</number>
</property>
</widget>
</item>
<item row="8" column="0">
<widget class="QLabel" name="mLineColorLabel">
<property name="text">
<string>Line color</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="8" column="2">
<widget class="QgsColorButton" name="mLineColorButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="5" column="3">
<spacer name="horizontalSpacer">
<property name="orientation">
Expand Down Expand Up @@ -296,6 +266,39 @@
</layout>
</widget>
</item>
<item row="8" column="0">
<widget class="QLabel" name="mMarkerSymbolLabel">
<property name="text">
<string>Marker symbol</string>
</property>
</widget>
</item>
<item row="7" column="2">
<widget class="QPushButton" name="mLineSymbolButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="8" column="2">
<widget class="QPushButton" name="mMarkerSymbolButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand All @@ -310,13 +313,6 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsColorButton</class>
<extends>QToolButton</extends>
<header>qgscolorbutton.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

0 comments on commit 25ee41c

Please sign in to comment.