30 #include <QDomDocument>
31 #include <QDomElement>
32 #include <QFontMetricsF>
40 , mNumUnitsPerSegment( 0 )
41 , mFontColor( QColor( 0, 0, 0 ) )
43 , mSegmentMillimeters( 0.0 )
46 , mLineJoinStyle( Qt::MiterJoin )
47 , mLineCapStyle( Qt::SquareCap )
60 Q_UNUSED( itemStyle );
198 return composerMapRect.
width();
273 mPen = QPen( QColor( 0, 0, 0 ) );
276 mPen.setWidthF( 1.0 );
278 mBrush.setColor( QColor( 0, 0, 0 ) );
279 mBrush.setStyle( Qt::SolidPattern );
283 QString defaultFontString = settings.value(
"/Composer/defaultFont" ).toString();
284 if ( !defaultFontString.isEmpty() )
286 mFont.setFamily( defaultFontString );
288 mFont.setPointSizeF( 12.0 );
301 double upperMagnitudeMultiplier = 1.0;
302 double widthInSelectedUnits =
mapWidth();
303 double initialUnitsPerSegment = widthInSelectedUnits / 10.0;
310 upperMagnitudeMultiplier = 1.0;
316 if ( initialUnitsPerSegment > 1000.0 )
318 upperMagnitudeMultiplier = 1000.0;
323 upperMagnitudeMultiplier = 1.0;
330 if ( initialUnitsPerSegment > 5419.95 )
332 upperMagnitudeMultiplier = 5419.95;
337 upperMagnitudeMultiplier = 1.0;
344 upperMagnitudeMultiplier = 1;
350 double segmentWidth = initialUnitsPerSegment / upperMagnitudeMultiplier;
351 int segmentMagnitude = floor( log10( segmentWidth ) );
352 double unitsPerSegment = upperMagnitudeMultiplier * ( pow( 10.0, segmentMagnitude ) );
353 double multiplier = floor(( widthInSelectedUnits / ( unitsPerSegment * 10.0 ) ) / 2.5 ) * 2.5;
355 if ( multiplier > 0 )
357 unitsPerSegment = unitsPerSegment * multiplier;
389 QgsComposerItem::update();
408 posWidthList.clear();
414 posWidthList.push_back( qMakePair( mCurrentXCoord,
mSegmentMillimeters / mNumSegmentsLeft ) );
432 if ( styleName ==
"Single Box" )
436 else if ( styleName ==
"Double Box" )
440 else if ( styleName ==
"Line Ticks Middle" || styleName ==
"Line Ticks Down" || styleName ==
"Line Ticks Up" )
443 if ( styleName ==
"Line Ticks Middle" )
447 else if ( styleName ==
"Line Ticks Down" )
451 else if ( styleName ==
"Line Ticks Up" )
457 else if ( styleName ==
"Numeric" )
507 QDomElement composerScaleBarElem = doc.createElement(
"ComposerScaleBar" );
508 composerScaleBarElem.setAttribute(
"height", QString::number(
mHeight ) );
509 composerScaleBarElem.setAttribute(
"labelBarSpace", QString::number(
mLabelBarSpace ) );
510 composerScaleBarElem.setAttribute(
"boxContentSpace", QString::number(
mBoxContentSpace ) );
511 composerScaleBarElem.setAttribute(
"numSegments",
mNumSegments );
513 composerScaleBarElem.setAttribute(
"numUnitsPerSegment", QString::number(
mNumUnitsPerSegment ) );
514 composerScaleBarElem.setAttribute(
"segmentMillimeters", QString::number(
mSegmentMillimeters ) );
516 composerScaleBarElem.setAttribute(
"font",
mFont.toString() );
517 composerScaleBarElem.setAttribute(
"outlineWidth", QString::number(
mPen.widthF() ) );
518 composerScaleBarElem.setAttribute(
"unitLabel",
mUnitLabeling );
519 composerScaleBarElem.setAttribute(
"units",
mUnits );
526 composerScaleBarElem.setAttribute(
"style",
mStyle->
name() );
536 composerScaleBarElem.setAttribute(
"brushColor",
mBrush.color().name() );
537 composerScaleBarElem.setAttribute(
"penColor",
mPen.color().name() );
538 composerScaleBarElem.setAttribute(
"fontColor",
mFontColor.name() );
541 composerScaleBarElem.setAttribute(
"alignment", QString::number((
int )
mAlignment ) );
543 elem.appendChild( composerScaleBarElem );
544 return _writeXML( composerScaleBarElem, doc );
549 if ( itemElem.isNull() )
554 mHeight = itemElem.attribute(
"height",
"5.0" ).toDouble();
555 mLabelBarSpace = itemElem.attribute(
"labelBarSpace",
"3.0" ).toDouble();
556 mBoxContentSpace = itemElem.attribute(
"boxContentSpace",
"1.0" ).toDouble();
557 mNumSegments = itemElem.attribute(
"numSegments",
"2" ).toInt();
562 mPen.setWidthF( itemElem.attribute(
"outlineWidth",
"1.0" ).toDouble() );
568 QString fontString = itemElem.attribute(
"font",
"" );
569 if ( !fontString.isEmpty() )
571 mFont.fromString( fontString );
576 mBrush.setColor( QColor( itemElem.attribute(
"brushColor",
"#000000" ) ) );
577 mPen.setColor( QColor( itemElem.attribute(
"penColor",
"#000000" ) ) );
578 mFontColor.setNamedColor( itemElem.attribute(
"fontColor",
"#000000" ) );
583 QString styleString = itemElem.attribute(
"style",
"" );
584 setStyle(
tr( styleString.toLocal8Bit().data() ) );
590 int mapId = itemElem.attribute(
"mapId",
"-1" ).toInt();
605 QDomNodeList composerItemList = itemElem.elementsByTagName(
"ComposerItem" );
606 if ( composerItemList.size() > 0 )
608 QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
625 move( -( widthAfter - width ) / 2.0, 0 );
629 move( -( widthAfter - width ), 0 );
Double box with alternating colors.
A rectangle specified with double values.
ScaleBarUnits
Added in version 1.9.
double mLabelBarSpace
Space between bar and Text labels.
double mHeight
Height of bars/lines.
double mNumUnitsPerSegment
Size of a segment (in map units)
bool writeXML(QDomElement &elem, QDomDocument &doc) const
stores state in Dom element
void setUnits(ScaleBarUnits u)
void setLineCapStyle(Qt::PenCapStyle style)
Sets cap style used when drawing the lines in the scalebar.
double mNumMapUnitsPerScaleBarUnit
Number of map units per scale bar units (e.g.
bool readXML(const QDomElement &itemElem, const QDomDocument &doc)
sets state from Dom document
void setFont(const QFont &font)
A scale bar style that draws text in the form of '1:XXXXX'.
Qt::PenJoinStyle mLineJoinStyle
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
void setSourceCrs(long srsid)
sets source spatial reference system (by QGIS CRS)
void setUnitLabeling(const QString &label)
void applyDefaultSize(ScaleBarUnits u=Meters)
Apply default size (scale bar 1/5 of map item width)
void setAlignment(Alignment a)
void applyDefaultSettings()
Apply default settings.
A item that forms part of a map composition.
void setNumSegments(int nSegments)
void segmentPositions(QList< QPair< double, double > > &posWidthList) const
Returns the x - positions of the segment borders (in item coordinates) and the width of the segment...
virtual QRectF calculateBoxSize() const
void setNumSegmentsLeft(int nSegmentsLeft)
bool hasCrsTransformEnabled() const
returns true if projections are enabled for this layer set
ScaleBarUnits units() const
virtual void drawFrame(QPainter *p)
Draw black frame around item.
QgsScaleBarStyle * mStyle
Scalebar style.
virtual QString name() const =0
bool setEllipsoid(const QString &ellipsoid)
sets ellipsoid by its acronym
void update()
Adjusts box size and calls QgsComposerItem::update()
A scale bar that draws segments using short ticks.
void setNumMapUnitsPerScaleBarUnit(double d)
void adjustBoxSize()
Sets box size suitable to content.
void itemChanged()
Used e.g.
bool _readXML(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document.
double mSegmentMillimeters
Width of a segment (in mm)
void setLineJoinStyle(Qt::PenJoinStyle style)
Sets join style used when drawing the lines in the scalebar.
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
double xMaximum() const
Get the x maximum value (right side of rectangle)
static Qt::PenCapStyle decodePenCapStyle(QString str)
virtual void drawSelectionBoxes(QPainter *p)
Draw selection boxes around item.
double textWidthMillimeters(const QFont &font, const QString &text) const
Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE...
Qt::PenCapStyle mLineCapStyle
void setStyle(const QString &styleName)
Sets style by name.
virtual void draw(QPainter *p, double xOffset=0) const =0
Draws the style.
const QgsCoordinateReferenceSystem & destinationCrs() const
returns CRS of destination coordinate reference system
static QString encodePenJoinStyle(Qt::PenJoinStyle style)
QgsComposerScaleBar(QgsComposition *composition)
Alignment
Added in version 1.8.
int mNumSegmentsLeft
Number of segments on left side.
QgsComposition * mComposition
A class to represent a point geometry.
const QgsComposerMap * composerMap() const
Graphics scene for map printing.
Object representing map window.
QString style() const
Returns style name.
QgsRectangle * currentMapExtent()
Returns a pointer to the current map extent, which is either the original user specified extent or th...
void setComposerMap(const QgsComposerMap *map)
void refreshSegmentMillimeters()
Calculates with of a segment in mm and stores it in mSegmentMillimeters.
void invalidateCurrentMap()
Sets mCompositionMap to 0 if the map is deleted.
void setBoxContentSpace(double space)
int id() const
Get identification number.
General purpose distance and area calculator.
int mNumSegments
Number of segments on right side.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget)
Reimplementation of QCanvasItem::paint.
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
virtual void drawBackground(QPainter *p)
Draw background.
static QgsProject * instance()
access to canonical QgsProject instance
virtual void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
double measureLine(const QList< QgsPoint > &points)
measures line
double mBoxContentSpace
Space between content and item box.
static double fromUnitToUnitFactor(QGis::UnitType fromUnit, QGis::UnitType toUnit)
Returns the conversion factor between the specified units.
QString mUnitLabeling
Labeling of map units.
static Qt::PenJoinStyle decodePenJoinStyle(QString str)
double mapWidth() const
Returns diagonal of composer map in selected units (map units / meters / feet / nautical miles) ...
const QgsComposerMap * mComposerMap
Reference to composer map object.
void setNumUnitsPerSegment(double units)
double width() const
Width of the rectangle.
const QgsComposerMap * getComposerMapById(int id) const
Returns the composer map with specified id.
void move(double dx, double dy)
Moves item in canvas coordinates.
Scalebar style that draws a single box with alternating color for the segments.
void correctXPositionAlignment(double width, double widthAfter)
Moves scalebar position to the left / right depending on alignment and change in item width...
double xMinimum() const
Get the x minimum value (left side of rectangle)
void setEllipsoidalMode(bool flag)
sets whether coordinates must be projected to ellipsoid before measuring
void setTickPosition(TickPosition p)
QString firstLabelString() const
Returns string of first label (important for drawing, labeling, size calculation. ...
static QString encodePenCapStyle(Qt::PenCapStyle style)