29 #include <QDomDocument>
30 #include <QDomElement>
31 #include <QFontMetricsF>
39 , mNumUnitsPerSegment( 0 )
40 , mFontColor( QColor( 0, 0, 0 ) )
42 , mSegmentMillimeters( 0.0 )
57 Q_UNUSED( itemStyle );
195 return composerMapRect.
width();
244 mPen = QPen( QColor( 0, 0, 0 ) );
245 mPen.setJoinStyle( Qt::MiterJoin );
246 mPen.setWidthF( 1.0 );
248 mBrush.setColor( QColor( 0, 0, 0 ) );
249 mBrush.setStyle( Qt::SolidPattern );
253 QString defaultFontString = settings.value(
"/Composer/defaultFont" ).toString();
254 if ( !defaultFontString.isEmpty() )
256 mFont.setFamily( defaultFontString );
258 mFont.setPointSizeF( 12.0 );
271 double upperMagnitudeMultiplier = 1.0;
272 double widthInSelectedUnits =
mapWidth();
273 double initialUnitsPerSegment = widthInSelectedUnits / 10.0;
280 upperMagnitudeMultiplier = 1.0;
286 if ( initialUnitsPerSegment > 1000.0 )
288 upperMagnitudeMultiplier = 1000.0;
293 upperMagnitudeMultiplier = 1.0;
300 if ( initialUnitsPerSegment > 5419.95 )
302 upperMagnitudeMultiplier = 5419.95;
307 upperMagnitudeMultiplier = 1.0;
314 upperMagnitudeMultiplier = 1;
320 double segmentWidth = initialUnitsPerSegment / upperMagnitudeMultiplier;
321 int segmentMagnitude = floor( log10( segmentWidth ) );
322 double unitsPerSegment = upperMagnitudeMultiplier * ( pow( 10.0, segmentMagnitude ) );
323 double multiplier = floor(( widthInSelectedUnits / ( unitsPerSegment * 10.0 ) ) / 2.5 ) * 2.5;
325 if ( multiplier > 0 )
327 unitsPerSegment = unitsPerSegment * multiplier;
359 QgsComposerItem::update();
378 posWidthList.clear();
384 posWidthList.push_back( qMakePair( mCurrentXCoord,
mSegmentMillimeters / mNumSegmentsLeft ) );
402 if ( styleName ==
"Single Box" )
406 else if ( styleName ==
"Double Box" )
410 else if ( styleName ==
"Line Ticks Middle" || styleName ==
"Line Ticks Down" || styleName ==
"Line Ticks Up" )
413 if ( styleName ==
"Line Ticks Middle" )
417 else if ( styleName ==
"Line Ticks Down" )
421 else if ( styleName ==
"Line Ticks Up" )
427 else if ( styleName ==
"Numeric" )
477 QDomElement composerScaleBarElem = doc.createElement(
"ComposerScaleBar" );
478 composerScaleBarElem.setAttribute(
"height", QString::number(
mHeight ) );
479 composerScaleBarElem.setAttribute(
"labelBarSpace", QString::number(
mLabelBarSpace ) );
480 composerScaleBarElem.setAttribute(
"boxContentSpace", QString::number(
mBoxContentSpace ) );
481 composerScaleBarElem.setAttribute(
"numSegments",
mNumSegments );
483 composerScaleBarElem.setAttribute(
"numUnitsPerSegment", QString::number(
mNumUnitsPerSegment ) );
484 composerScaleBarElem.setAttribute(
"segmentMillimeters", QString::number(
mSegmentMillimeters ) );
486 composerScaleBarElem.setAttribute(
"font",
mFont.toString() );
487 composerScaleBarElem.setAttribute(
"outlineWidth", QString::number(
mPen.widthF() ) );
488 composerScaleBarElem.setAttribute(
"unitLabel",
mUnitLabeling );
489 composerScaleBarElem.setAttribute(
"units",
mUnits );
494 composerScaleBarElem.setAttribute(
"style",
mStyle->
name() );
504 composerScaleBarElem.setAttribute(
"brushColor",
mBrush.color().name() );
505 composerScaleBarElem.setAttribute(
"penColor",
mPen.color().name() );
506 composerScaleBarElem.setAttribute(
"fontColor",
mFontColor.name() );
509 composerScaleBarElem.setAttribute(
"alignment", QString::number((
int )
mAlignment ) );
511 elem.appendChild( composerScaleBarElem );
512 return _writeXML( composerScaleBarElem, doc );
517 if ( itemElem.isNull() )
522 mHeight = itemElem.attribute(
"height",
"5.0" ).toDouble();
523 mLabelBarSpace = itemElem.attribute(
"labelBarSpace",
"3.0" ).toDouble();
524 mBoxContentSpace = itemElem.attribute(
"boxContentSpace",
"1.0" ).toDouble();
525 mNumSegments = itemElem.attribute(
"numSegments",
"2" ).toInt();
530 mPen.setWidthF( itemElem.attribute(
"outlineWidth",
"1.0" ).toDouble() );
532 QString fontString = itemElem.attribute(
"font",
"" );
533 if ( !fontString.isEmpty() )
535 mFont.fromString( fontString );
540 mBrush.setColor( QColor( itemElem.attribute(
"brushColor",
"#000000" ) ) );
541 mPen.setColor( QColor( itemElem.attribute(
"penColor",
"#000000" ) ) );
542 mFontColor.setNamedColor( itemElem.attribute(
"fontColor",
"#000000" ) );
547 QString styleString = itemElem.attribute(
"style",
"" );
548 setStyle(
tr( styleString.toLocal8Bit().data() ) );
554 int mapId = itemElem.attribute(
"mapId",
"-1" ).toInt();
569 QDomNodeList composerItemList = itemElem.elementsByTagName(
"ComposerItem" );
570 if ( composerItemList.size() > 0 )
572 QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
589 move( -( widthAfter - width ) / 2.0, 0 );
593 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)
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'.
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)
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)
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
double xMaximum() const
Get the x maximum value (right side of rectangle)
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...
bool hasCrsTransformEnabled() const
returns true if projections are enabled for this layer set
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
const QgsMapRenderer * mapRenderer() const
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.
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. ...