Skip to content
Permalink
Browse files

use symbologyv2 for lines and add marker symbols (in addition to Cross)

  • Loading branch information
etiennesky committed Jun 18, 2012
1 parent 0181324 commit 25ee41c3c6babb3da5eb1a1b96288f812406ba65
Showing with 163 additions and 47 deletions.
  1. +110 −4 src/app/qgsdecorationgrid.cpp
  2. +15 −1 src/app/qgsdecorationgrid.h
  3. +38 −42 src/ui/qgsdecorationgriddialog.ui
@@ -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>
@@ -39,6 +41,7 @@
#include <QMenu>
#include <QFile>
#include <QLocale>
#include <QDomDocument>

//non qt includes
#include <cmath>
@@ -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()
@@ -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()
@@ -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() );
}

}


@@ -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 );
@@ -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 )
@@ -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 );
@@ -21,6 +21,8 @@
#include "qgsdecorationitem.h"

class QPainter;
class QgsLineSymbolV2;
class QgsMarkerSymbolV2;

#include <QColor>
#include <QPen>
@@ -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
@@ -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();
@@ -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
@@ -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">
@@ -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">
@@ -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">
@@ -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>
@@ -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.
You can’t perform that action at this time.