31 #include <QDomDocument> 32 #include <QDomElement> 33 #include <QFontMetricsF> 41 , mNumUnitsPerSegment( 0 )
42 , mFontColor( QColor( 0, 0, 0 ) )
44 , mSegmentMillimeters( 0.0 )
47 , mLineJoinStyle( Qt::MiterJoin )
48 , mLineCapStyle( Qt::SquareCap )
61 Q_UNUSED( itemStyle );
203 return composerMapRect.
width();
291 mPen = QPen( Qt::black );
294 mPen.setWidthF( 1.0 );
296 mBrush.setColor( Qt::black );
297 mBrush.setStyle( Qt::SolidPattern );
300 mBrush2.setStyle( Qt::SolidPattern );
304 QString defaultFontString = settings.value(
"/Composer/defaultFont" ).toString();
305 if ( !defaultFontString.isEmpty() )
307 mFont.setFamily( defaultFontString );
309 mFont.setPointSizeF( 12.0 );
322 double upperMagnitudeMultiplier = 1.0;
323 double widthInSelectedUnits =
mapWidth();
324 double initialUnitsPerSegment = widthInSelectedUnits / 10.0;
331 upperMagnitudeMultiplier = 1.0;
337 if ( initialUnitsPerSegment > 1000.0 )
339 upperMagnitudeMultiplier = 1000.0;
344 upperMagnitudeMultiplier = 1.0;
351 if ( initialUnitsPerSegment > 5419.95 )
353 upperMagnitudeMultiplier = 5419.95;
358 upperMagnitudeMultiplier = 1.0;
365 upperMagnitudeMultiplier = 1;
371 double segmentWidth = initialUnitsPerSegment / upperMagnitudeMultiplier;
372 int segmentMagnitude = floor( log10( segmentWidth ) );
373 double unitsPerSegment = upperMagnitudeMultiplier * ( pow( 10.0, segmentMagnitude ) );
374 double multiplier = floor(( widthInSelectedUnits / ( unitsPerSegment * 10.0 ) ) / 2.5 ) * 2.5;
376 if ( multiplier > 0 )
378 unitsPerSegment = unitsPerSegment * multiplier;
400 if ( rect().
height() > box.height() )
403 box.setHeight( rect().
height() );
410 if ( newRect.width() < box.width() )
412 newRect.setWidth( box.width() );
414 if ( newRect.height() < box.height() )
416 newRect.setHeight( box.height() );
425 if ( rectangle.height() > box.height() )
428 box.setHeight( rectangle.height() );
430 box.moveTopLeft( rectangle.topLeft() );
436 if ( newRect.width() < box.width() )
438 newRect.setWidth( box.width() );
440 if ( newRect.height() < box.height() )
442 newRect.setHeight( box.height() );
455 QgsComposerItem::update();
474 posWidthList.clear();
481 posWidthList.push_back( qMakePair( mCurrentXCoord, leftSegmentSize ) );
482 mCurrentXCoord += leftSegmentSize;
499 if ( styleName ==
"Single Box" )
503 else if ( styleName ==
"Double Box" )
507 else if ( styleName ==
"Line Ticks Middle" || styleName ==
"Line Ticks Down" || styleName ==
"Line Ticks Up" )
510 if ( styleName ==
"Line Ticks Middle" )
514 else if ( styleName ==
"Line Ticks Down" )
518 else if ( styleName ==
"Line Ticks Up" )
524 else if ( styleName ==
"Numeric" )
574 QDomElement composerScaleBarElem = doc.createElement(
"ComposerScaleBar" );
575 composerScaleBarElem.setAttribute(
"height", QString::number(
mHeight ) );
576 composerScaleBarElem.setAttribute(
"labelBarSpace", QString::number(
mLabelBarSpace ) );
577 composerScaleBarElem.setAttribute(
"boxContentSpace", QString::number(
mBoxContentSpace ) );
578 composerScaleBarElem.setAttribute(
"numSegments",
mNumSegments );
580 composerScaleBarElem.setAttribute(
"numUnitsPerSegment", QString::number(
mNumUnitsPerSegment ) );
581 composerScaleBarElem.setAttribute(
"segmentMillimeters", QString::number(
mSegmentMillimeters ) );
583 composerScaleBarElem.setAttribute(
"font",
mFont.toString() );
584 composerScaleBarElem.setAttribute(
"outlineWidth", QString::number(
mPen.widthF() ) );
585 composerScaleBarElem.setAttribute(
"unitLabel",
mUnitLabeling );
586 composerScaleBarElem.setAttribute(
"units",
mUnits );
593 composerScaleBarElem.setAttribute(
"style",
mStyle->
name() );
605 QDomElement fillColorElem = doc.createElement(
"fillColor" );
606 QColor fillColor =
mBrush.color();
607 fillColorElem.setAttribute(
"red", QString::number( fillColor.red() ) );
608 fillColorElem.setAttribute(
"green", QString::number( fillColor.green() ) );
609 fillColorElem.setAttribute(
"blue", QString::number( fillColor.blue() ) );
610 fillColorElem.setAttribute(
"alpha", QString::number( fillColor.alpha() ) );
611 composerScaleBarElem.appendChild( fillColorElem );
614 QDomElement fillColor2Elem = doc.createElement(
"fillColor2" );
615 QColor fillColor2 =
mBrush2.color();
616 fillColor2Elem.setAttribute(
"red", QString::number( fillColor2.red() ) );
617 fillColor2Elem.setAttribute(
"green", QString::number( fillColor2.green() ) );
618 fillColor2Elem.setAttribute(
"blue", QString::number( fillColor2.blue() ) );
619 fillColor2Elem.setAttribute(
"alpha", QString::number( fillColor2.alpha() ) );
620 composerScaleBarElem.appendChild( fillColor2Elem );
623 QDomElement strokeColorElem = doc.createElement(
"strokeColor" );
624 QColor strokeColor =
mPen.color();
625 strokeColorElem.setAttribute(
"red", QString::number( strokeColor.red() ) );
626 strokeColorElem.setAttribute(
"green", QString::number( strokeColor.green() ) );
627 strokeColorElem.setAttribute(
"blue", QString::number( strokeColor.blue() ) );
628 strokeColorElem.setAttribute(
"alpha", QString::number( strokeColor.alpha() ) );
629 composerScaleBarElem.appendChild( strokeColorElem );
632 QDomElement fontColorElem = doc.createElement(
"textColor" );
633 fontColorElem.setAttribute(
"red", QString::number(
mFontColor.red() ) );
634 fontColorElem.setAttribute(
"green", QString::number(
mFontColor.green() ) );
635 fontColorElem.setAttribute(
"blue", QString::number(
mFontColor.blue() ) );
636 fontColorElem.setAttribute(
"alpha", QString::number(
mFontColor.alpha() ) );
637 composerScaleBarElem.appendChild( fontColorElem );
640 composerScaleBarElem.setAttribute(
"alignment", QString::number((
int )
mAlignment ) );
642 elem.appendChild( composerScaleBarElem );
643 return _writeXML( composerScaleBarElem, doc );
648 if ( itemElem.isNull() )
653 mHeight = itemElem.attribute(
"height",
"5.0" ).toDouble();
654 mLabelBarSpace = itemElem.attribute(
"labelBarSpace",
"3.0" ).toDouble();
655 mBoxContentSpace = itemElem.attribute(
"boxContentSpace",
"1.0" ).toDouble();
656 mNumSegments = itemElem.attribute(
"numSegments",
"2" ).toInt();
661 mPen.setWidthF( itemElem.attribute(
"outlineWidth",
"1.0" ).toDouble() );
667 QString fontString = itemElem.attribute(
"font",
"" );
668 if ( !fontString.isEmpty() )
670 mFont.fromString( fontString );
675 QDomNodeList fillColorList = itemElem.elementsByTagName(
"fillColor" );
676 if ( fillColorList.size() > 0 )
678 QDomElement fillColorElem = fillColorList.at( 0 ).toElement();
679 bool redOk, greenOk, blueOk, alphaOk;
680 int fillRed, fillGreen, fillBlue, fillAlpha;
682 fillRed = fillColorElem.attribute(
"red" ).toDouble( &redOk );
683 fillGreen = fillColorElem.attribute(
"green" ).toDouble( &greenOk );
684 fillBlue = fillColorElem.attribute(
"blue" ).toDouble( &blueOk );
685 fillAlpha = fillColorElem.attribute(
"alpha" ).toDouble( &alphaOk );
687 if ( redOk && greenOk && blueOk && alphaOk )
689 mBrush.setColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
694 mBrush.setColor( QColor( itemElem.attribute(
"brushColor",
"#000000" ) ) );
698 QDomNodeList fillColor2List = itemElem.elementsByTagName(
"fillColor2" );
699 if ( fillColor2List.size() > 0 )
701 QDomElement fillColor2Elem = fillColor2List.at( 0 ).toElement();
702 bool redOk, greenOk, blueOk, alphaOk;
703 int fillRed, fillGreen, fillBlue, fillAlpha;
705 fillRed = fillColor2Elem.attribute(
"red" ).toDouble( &redOk );
706 fillGreen = fillColor2Elem.attribute(
"green" ).toDouble( &greenOk );
707 fillBlue = fillColor2Elem.attribute(
"blue" ).toDouble( &blueOk );
708 fillAlpha = fillColor2Elem.attribute(
"alpha" ).toDouble( &alphaOk );
710 if ( redOk && greenOk && blueOk && alphaOk )
712 mBrush2.setColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
717 mBrush2.setColor( QColor( itemElem.attribute(
"brush2Color",
"#ffffff" ) ) );
721 QDomNodeList strokeColorList = itemElem.elementsByTagName(
"strokeColor" );
722 if ( strokeColorList.size() > 0 )
724 QDomElement strokeColorElem = strokeColorList.at( 0 ).toElement();
725 bool redOk, greenOk, blueOk, alphaOk;
726 int strokeRed, strokeGreen, strokeBlue, strokeAlpha;
728 strokeRed = strokeColorElem.attribute(
"red" ).toDouble( &redOk );
729 strokeGreen = strokeColorElem.attribute(
"green" ).toDouble( &greenOk );
730 strokeBlue = strokeColorElem.attribute(
"blue" ).toDouble( &blueOk );
731 strokeAlpha = strokeColorElem.attribute(
"alpha" ).toDouble( &alphaOk );
733 if ( redOk && greenOk && blueOk && alphaOk )
735 mPen.setColor( QColor( strokeRed, strokeGreen, strokeBlue, strokeAlpha ) );
740 mPen.setColor( QColor( itemElem.attribute(
"penColor",
"#000000" ) ) );
744 QDomNodeList textColorList = itemElem.elementsByTagName(
"textColor" );
745 if ( textColorList.size() > 0 )
747 QDomElement textColorElem = textColorList.at( 0 ).toElement();
748 bool redOk, greenOk, blueOk, alphaOk;
749 int textRed, textGreen, textBlue, textAlpha;
751 textRed = textColorElem.attribute(
"red" ).toDouble( &redOk );
752 textGreen = textColorElem.attribute(
"green" ).toDouble( &greenOk );
753 textBlue = textColorElem.attribute(
"blue" ).toDouble( &blueOk );
754 textAlpha = textColorElem.attribute(
"alpha" ).toDouble( &alphaOk );
756 if ( redOk && greenOk && blueOk && alphaOk )
758 mFontColor = QColor( textRed, textGreen, textBlue, textAlpha );
763 mFontColor.setNamedColor( itemElem.attribute(
"fontColor",
"#000000" ) );
769 QString styleString = itemElem.attribute(
"style",
"" );
770 setStyle(
tr( styleString.toLocal8Bit().data() ) );
776 int mapId = itemElem.attribute(
"mapId",
"-1" ).toInt();
791 QDomNodeList composerItemList = itemElem.elementsByTagName(
"ComposerItem" );
792 if ( composerItemList.size() > 0 )
794 QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
811 move( -( widthAfter - width ) / 2.0, 0 );
815 move( -( widthAfter - width ), 0 );
Double box with alternating colors.
A rectangle specified with double values.
double mLabelBarSpace
Space between bar and Text labels.
double mHeight
Height of bars/lines.
double mNumUnitsPerSegment
Size of a segment (in map units)
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.
void setFont(const QFont &font)
A scale bar style that draws text in the form of '1:XXXXX'.
void itemChanged()
Emitted when the item changes.
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 paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Reimplementation of QCanvasItem::paint.
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.
QBrush mBrush2
Secondary fill.
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.
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)
Draws additional graphics on selected items.
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)
bool writeXML(QDomElement &elem, QDomDocument &doc) const override
stores state in Dom element
int mNumSegmentsLeft
Number of segments on left side.
bool readXML(const QDomElement &itemElem, const QDomDocument &doc) override
sets state from Dom document
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
void setBackgroundEnabled(const bool drawBackground)
Set whether this item has a Background drawn around it or not.
A class to represent a point.
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.
static double textWidthMM(const QFont &font, const QString &text)
Calculate font width in millimeters for a string, including workarounds for QT font rendering issues...
QgsComposition * mComposition
int mNumSegments
Number of segments on right side.
QRectF evalItemRect(const QRectF &newRect, const bool resizeOnly=false)
Evaluates an item's bounding rect to consider data defined position and size of item and reference po...
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.
UnitType
Map units that qgis supports.
QString mUnitLabeling
Labeling of map units.
static Qt::PenJoinStyle decodePenJoinStyle(QString str)
void setSceneRect(const QRectF &rectangle) override
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
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)
const QgsComposerMap * getComposerMapById(const int id) const
Returns the composer map with specified id.
double width() const
Width of the rectangle.
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)