Skip to content

Commit

Permalink
Start implementation of QgsDxfPaintDevice / Engine to export svg mark…
Browse files Browse the repository at this point in the history
…ers to dxf
  • Loading branch information
mhugent committed Nov 22, 2013
1 parent 5bd7083 commit 78fbee3
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -160,6 +160,9 @@ SET(QGIS_CORE_SRCS
composer/qgscomposerhtml.cpp
composer/qgscomposermultiframe.cpp
composer/qgscomposition.cpp

dxf/qgsdxfpaintdevice.cpp
dxf/qgsdxfpaintengine.cpp

pal/costcalculator.cpp
pal/feature.cpp
Expand Down Expand Up @@ -549,6 +552,7 @@ ENDIF (QT_MOBILITY_LOCATION_FOUND)
INCLUDE_DIRECTORIES(
${CMAKE_CURRENT_SOURCE_DIR}
composer
dxf
pal
raster
renderer
Expand Down
84 changes: 84 additions & 0 deletions src/core/dxf/qgsdxfpaintdevice.cpp
@@ -0,0 +1,84 @@
/***************************************************************************
qgsdxpaintdevice.cpp
--------------------
begin : November 2013
copyright : (C) 2013 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsdxfpaintdevice.h"

QgsDxfPaintDevice::QgsDxfPaintDevice( QgsDxfExport* dxf ): QPaintDevice(), mPaintEngine( 0 )
{
mPaintEngine = new QgsDxfPaintEngine( this, dxf );
}

QgsDxfPaintDevice::~QgsDxfPaintDevice()
{
delete mPaintEngine;
}

QPaintEngine* QgsDxfPaintDevice::paintEngine() const
{
return mPaintEngine;
}

int QgsDxfPaintDevice::metric( PaintDeviceMetric metric ) const
{
switch ( metric )
{
case QPaintDevice::PdmWidth:
return mDrawingSize.width();
case QPaintDevice::PdmHeight:
return mDrawingSize.height();
case QPaintDevice::PdmWidthMM:
return mDrawingSize.width();
case QPaintDevice::PdmHeightMM:
return mDrawingSize.height();
case QPaintDevice::PdmNumColors:
return INT_MAX;
case QPaintDevice::PdmDepth:
return 32;
case QPaintDevice::PdmDpiX:
case QPaintDevice::PdmDpiY:
case QPaintDevice::PdmPhysicalDpiX:
case QPaintDevice::PdmPhysicalDpiY:
return 96;
}
return 0;
}

double QgsDxfPaintDevice::widthScaleFactor() const
{
if ( !mDrawingSize.isValid() || mRectangle.isEmpty() )
{
return 1.0;
}

double widthFactor = mRectangle.width() / mDrawingSize.width();
double heightFactor = mRectangle.height() / mDrawingSize.height();
return ( widthFactor + heightFactor ) / 2.0;
}

QPointF QgsDxfPaintDevice::dxfCoordinates( const QPointF& pt )
{
if ( !mDrawingSize.isValid() || mRectangle.isEmpty() )
{
return QPointF( pt.x(), pt.y() );
}

double x = mRectangle.left() + pt.x() * ( mRectangle.width() / mDrawingSize.width() );
double y = mRectangle.bottom() - pt.y() * ( mRectangle.height() / mDrawingSize.height() );
return QPointF( x, y );
}


59 changes: 59 additions & 0 deletions src/core/dxf/qgsdxfpaintdevice.h
@@ -0,0 +1,59 @@
/***************************************************************************
qgsdxpaintdevice.h
------------------
begin : November 2013
copyright : (C) 2013 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSDXFPAINTDEVICE_H
#define QGSDXFPAINTDEVICE_H

#include <QPaintDevice>
#include "qgsdxfpaintengine.h"

class QgsDxfExport;
class QPaintEngine;

/**A paint device for drawing into dxf files*/

class QgsDxfPaintDevice: public QPaintDevice
{
public:
QgsDxfPaintDevice( QgsDxfExport* dxf );
~QgsDxfPaintDevice();

QPaintEngine* paintEngine() const;

void setDrawingSize( const QSizeF& size ) { mDrawingSize = size; }
void setOutputSize( const QRectF& r ) { mRectangle = r; }

/**Returns scale factor for line width*/
double widthScaleFactor() const;

/**Converts a point from device coordinates to dxf coordinates*/
QPointF dxfCoordinates( const QPointF& pt );

/*int height() const { return mDrawingSize.height(); }
int width() const { return mDrawingSize.width(); }*/

int metric( PaintDeviceMetric metric ) const;


private:
QgsDxfPaintEngine* mPaintEngine;

QSizeF mDrawingSize; //size (in source coordinates)
QRectF mRectangle; //size (in dxf coordinates)
};

#endif // QGSDXFPAINTDEVICE_H
89 changes: 89 additions & 0 deletions src/core/dxf/qgsdxfpaintengine.cpp
@@ -0,0 +1,89 @@
/***************************************************************************
qgsdxpaintengine.cpp
--------------------
begin : November 2013
copyright : (C) 2013 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsdxfpaintengine.h"
#include "qgsdxfexport.h"
#include "qgslogger.h"

QgsDxfPaintEngine::QgsDxfPaintEngine( const QgsDxfPaintDevice* dxfDevice, QgsDxfExport* dxf ): QPaintEngine( QPaintEngine::AllFeatures /*QPaintEngine::PainterPaths | QPaintEngine::PaintOutsidePaintEvent*/ )
, mPaintDevice( dxfDevice ), mDxf( dxf )
{

}

QgsDxfPaintEngine::~QgsDxfPaintEngine()
{

}

bool QgsDxfPaintEngine::begin( QPaintDevice* pdev )
{
Q_UNUSED( pdev );
return true;
}

bool QgsDxfPaintEngine::end()
{
return true;
}

QPaintEngine::Type QgsDxfPaintEngine::type() const
{
return QPaintEngine::User;
}

void QgsDxfPaintEngine::drawPixmap( const QRectF& r, const QPixmap& pm, const QRectF& sr )
{
Q_UNUSED( r ); Q_UNUSED( pm ); Q_UNUSED( sr );
}

void QgsDxfPaintEngine::updateState( const QPaintEngineState& state )
{
if ( state.state() | QPaintEngine::DirtyTransform )
{
mTransform = state.transform();
}
if ( state.state() | QPaintEngine::DirtyPen )
{
mPen = state.pen();
}
}

void QgsDxfPaintEngine::drawPolygon( const QPointF* points, int pointCount, PolygonDrawMode mode )
{
QgsDebugMsg( "***********************Dxf paint engine: drawing polygon*********************" );
}

void QgsDxfPaintEngine::drawRects( const QRectF * rects, int rectCount )
{
QgsDebugMsg( "***********************Dxf paint engine: drawing rects*********************" );
}

void QgsDxfPaintEngine::drawEllipse( const QRectF& rect )
{
QgsDebugMsg( "***********************Dxf paint engine: drawing ellipse*********************" );
}

void QgsDxfPaintEngine::drawPath( const QPainterPath& path )
{
QgsDebugMsg( "***********************Dxf paint engine: drawing path*********************" );
}

void QgsDxfPaintEngine::drawLines( const QLineF* lines, int lineCount )
{
QgsDebugMsg( "***********************Dxf paint engine: drawing path*********************" );
}
54 changes: 54 additions & 0 deletions src/core/dxf/qgsdxfpaintengine.h
@@ -0,0 +1,54 @@
/***************************************************************************
qgsdxpaintengine.h
------------------
begin : November 2013
copyright : (C) 2013 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSDXFPAINTENGINE_H
#define QGSDXFPAINTENGINE_H

#include <QPaintEngine>

class QgsDxfExport;
class QgsDxfPaintDevice;

class QgsDxfPaintEngine: public QPaintEngine
{
public:
QgsDxfPaintEngine( const QgsDxfPaintDevice* dxfDevice, QgsDxfExport* dxf );
~QgsDxfPaintEngine();

bool begin( QPaintDevice* pdev );
bool end();
QPaintEngine::Type type() const;
void updateState( const QPaintEngineState& state );

void drawPixmap( const QRectF& r, const QPixmap& pm, const QRectF& sr );

void drawPolygon( const QPointF * points, int pointCount, PolygonDrawMode mode );
void drawRects( const QRectF * rects, int rectCount );
void drawEllipse( const QRectF& rect );
void drawPath( const QPainterPath& path );
void drawLines( const QLineF* lines, int lineCount );

private:
const QgsDxfPaintDevice* mPaintDevice;
QgsDxfExport* mDxf;

//painter state information
QTransform mTransform;
QPen mPen;
};

#endif // QGSDXFPAINTENGINE_H
20 changes: 20 additions & 0 deletions src/core/symbology-ng/qgsmarkersymbollayerv2.cpp
Expand Up @@ -17,6 +17,7 @@
#include "qgssymbollayerv2utils.h"

#include "qgsdxfexport.h"
#include "qgsdxfpaintdevice.h"
#include "qgsexpression.h"
#include "qgsrendercontext.h"
#include "qgslogger.h"
Expand Down Expand Up @@ -1165,6 +1166,25 @@ QgsSymbolLayerV2* QgsSvgMarkerSymbolLayerV2::createFromSld( QDomElement &element
return m;
}

void QgsSvgMarkerSymbolLayerV2::writeDxf( QgsDxfExport& e, double mmMapUnitScaleFactor ) const
{
QSvgRenderer r( mPath );
if ( !r.isValid() )
{
return;
}

QgsDxfPaintDevice pd( &e );
pd.setDrawingSize( QSizeF( r.defaultSize() ) );
double size = mSize * mmMapUnitScaleFactor ;
pd.setOutputSize( QRectF( 0, 0, size, size ) );
QPainter p;

p.begin( &pd );
r.render( &p );
p.end();
}

//////////

QgsFontMarkerSymbolLayerV2::QgsFontMarkerSymbolLayerV2( QString fontFamily, QChar chr, double pointSize, QColor color, double angle )
Expand Down
2 changes: 2 additions & 0 deletions src/core/symbology-ng/qgsmarkersymbollayerv2.h
Expand Up @@ -159,6 +159,8 @@ class CORE_EXPORT QgsSvgMarkerSymbolLayerV2 : public QgsMarkerSymbolLayerV2
void setOutputUnit( QgsSymbolV2::OutputUnit unit );
QgsSymbolV2::OutputUnit outputUnit() const;

void writeDxf( QgsDxfExport& e, double mmMapUnitScaleFactor ) const;

protected:
QString mPath;

Expand Down

0 comments on commit 78fbee3

Please sign in to comment.