32 #include <QDomDocument>
33 #include <QDomElement>
34 #include <QFontMetricsF>
42 , mNumUnitsPerSegment( 0 )
43 , mSegmentSizeMode( SegmentSizeFixed )
46 , mFontColor(
QColor( 0, 0, 0 ) )
48 , mSegmentMillimeters( 0.0 )
51 , mLineJoinStyle(
Qt::MiterJoin )
52 , mLineCapStyle(
Qt::SquareCap )
65 Q_UNUSED( itemStyle );
230 double s = qPow( 10.0, floor( log10( a ) ) ) / d;
231 return ceil( a / s ) * s;
237 double s = qPow( 10.0, floor( log10( a ) ) ) / d;
238 return floor( a / s ) * s;
274 while ( lowerNiceUnitsPerSeg > maxUnitsPerSeg && upperNiceUnitsPerSeg < minUnitsPerSeg )
282 mNumUnitsPerSegment = upperNiceUnitsPerSeg < minUnitsPerSeg ? lowerNiceUnitsPerSeg : upperNiceUnitsPerSeg;
299 return composerMapRect.
width();
401 if ( !defaultFontString.
isEmpty() )
418 double upperMagnitudeMultiplier = 1.0;
419 double widthInSelectedUnits =
mapWidth();
420 double initialUnitsPerSegment = widthInSelectedUnits / 10.0;
427 upperMagnitudeMultiplier = 1.0;
433 if ( initialUnitsPerSegment > 1000.0 )
435 upperMagnitudeMultiplier = 1000.0;
440 upperMagnitudeMultiplier = 1.0;
447 if ( initialUnitsPerSegment > 5419.95 )
449 upperMagnitudeMultiplier = 5419.95;
454 upperMagnitudeMultiplier = 1.0;
461 upperMagnitudeMultiplier = 1;
467 double segmentWidth = initialUnitsPerSegment / upperMagnitudeMultiplier;
468 int segmentMagnitude = floor( log10( segmentWidth ) );
469 double unitsPerSegment = upperMagnitudeMultiplier * ( qPow( 10.0, segmentMagnitude ) );
470 double multiplier = floor(( widthInSelectedUnits / ( unitsPerSegment * 10.0 ) ) / 2.5 ) * 2.5;
472 if ( multiplier > 0 )
474 unitsPerSegment = unitsPerSegment * multiplier;
570 posWidthList.clear();
577 posWidthList.push_back( qMakePair( mCurrentXCoord, leftSegmentSize ) );
578 mCurrentXCoord += leftSegmentSize;
595 if ( styleName ==
"Single Box" )
599 else if ( styleName ==
"Double Box" )
603 else if ( styleName ==
"Line Ticks Middle" || styleName ==
"Line Ticks Down" || styleName ==
"Line Ticks Up" )
606 if ( styleName ==
"Line Ticks Middle" )
610 else if ( styleName ==
"Line Ticks Down" )
614 else if ( styleName ==
"Line Ticks Up" )
620 else if ( styleName ==
"Numeric" )
719 composerScaleBarElem.
appendChild( fillColor2Elem );
728 composerScaleBarElem.
appendChild( strokeColorElem );
742 return _writeXML( composerScaleBarElem, doc );
777 if ( fillColorList.
size() > 0 )
780 bool redOk, greenOk, blueOk, alphaOk;
781 int fillRed, fillGreen, fillBlue, fillAlpha;
788 if ( redOk && greenOk && blueOk && alphaOk )
800 if ( fillColor2List.
size() > 0 )
803 bool redOk, greenOk, blueOk, alphaOk;
804 int fillRed, fillGreen, fillBlue, fillAlpha;
811 if ( redOk && greenOk && blueOk && alphaOk )
823 if ( strokeColorList.
size() > 0 )
826 bool redOk, greenOk, blueOk, alphaOk;
827 int strokeRed, strokeGreen, strokeBlue, strokeAlpha;
834 if ( redOk && greenOk && blueOk && alphaOk )
846 if ( textColorList.
size() > 0 )
849 bool redOk, greenOk, blueOk, alphaOk;
850 int textRed, textGreen, textBlue, textAlpha;
857 if ( redOk && greenOk && blueOk && alphaOk )
893 if ( composerItemList.
size() > 0 )
912 move( -( widthAfter - width ) / 2.0, 0 );
916 move( -( widthAfter - width ), 0 );
Double box with alternating colors.
QDomNodeList elementsByTagName(const QString &tagname) const
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.
QDomNode appendChild(const QDomNode &newChild)
void setFont(const QFont &font)
A scale bar style that draws text in the form of '1:XXXXX'.
QString attribute(const QString &name, const QString &defValue) const
long srsid() const
Get the SrsId - if possible.
void itemChanged()
Emitted when the item changes.
Qt::PenJoinStyle mLineJoinStyle
UnitType
Map units that qgis supports.
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)
double mMinBarWidth
Minimum allowed bar width, when mSegmentSizeMode is FitWidth.
bool hasCrsTransformEnabled() const
returns true if projections are enabled for this layer set
ScaleBarUnits units() const
void setJoinStyle(Qt::PenJoinStyle style)
static Qt::PenJoinStyle decodePenJoinStyle(const QString &str)
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()
void setNamedColor(const QString &name)
double toDouble(bool *ok) const
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
void setStyle(Qt::BrushStyle style)
QString tr(const char *sourceText, const char *disambiguation, int n)
void setMinBarWidth(double minWidth)
Sets the minimum size (in millimeters) for scale bar segments.
void update(const QRectF &rect)
A scale bar that draws segments using short ticks.
void setNumMapUnitsPerScaleBarUnit(double d)
void setHeight(qreal height)
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.
const QColor & color() const
QDomElement toElement() const
void setMaxBarWidth(double maxWidth)
Sets the maximum size (in millimeters) for scale bar segments.
void setCapStyle(Qt::PenCapStyle style)
double mSegmentMillimeters
Width of a segment (in mm)
QString number(int n, int base)
bool fromString(const QString &descrip)
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)
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
Qt::PenCapStyle mLineCapStyle
void setStyle(const QString &styleName)
Sets style by name.
void setAttribute(const QString &name, const QString &value)
virtual void draw(QPainter *p, double xOffset=0) const =0
Draws the style.
const QgsCoordinateReferenceSystem & destinationCrs() const
returns CRS of destination coordinate reference system
int toInt(bool *ok, int base) const
QRectF evalItemRect(const QRectF &newRect, const bool resizeOnly=false, const QgsExpressionContext *context=0)
Evaluates an item's bounding rect to consider data defined position and size of item and reference po...
static QString encodePenJoinStyle(Qt::PenJoinStyle style)
double measureLine(const QList< QgsPoint > &points) const
measures line
void setWidthF(qreal width)
double prevNiceNumber(double a, double d=1)
static Qt::PenCapStyle decodePenCapStyle(const QString &str)
QgsComposerScaleBar(QgsComposition *composition)
static bool setFromXmlChildNode(QFont &font, const QDomElement &element, const QString &childNode)
Sets the properties of a font to match the properties stored in an XML child node.
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.
SegmentSizeMode
Modes for setting size for scale bar segments.
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
void setColor(const QColor &color)
void moveTopLeft(const QPointF &position)
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.
QByteArray toLocal8Bit() const
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)
void setPointSizeF(qreal pointSize)
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.
QVariant value(const QString &key, const QVariant &defaultValue) const
double mMaxBarWidth
Maximum allowed bar width, when mSegmentSizeMode is FitWidth.
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
void setSegmentSizeMode(SegmentSizeMode mode)
Sets the size mode for scale bar segments.
SegmentSizeMode mSegmentSizeMode
Either fixed (i.e.
void setWidth(qreal width)
virtual void drawBackground(QPainter *p)
Draw background.
static QgsProject * instance()
access to canonical QgsProject instance
void setFamily(const QString &family)
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 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.
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 nextNiceNumber(double a, double d=1)
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.
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
void setNumUnitsPerSegment(double units)
QDomElement createElement(const QString &tagName)
const QgsComposerMap * getComposerMapById(const int id) const
Returns the composer map with specified id.
double width() const
Width of the rectangle.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void move(double dx, double dy)
Moves item in canvas coordinates.
void setColor(const QColor &color)
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 destroyed(QObject *obj)
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. ...
QDomNode at(int index) const
static QString encodePenCapStyle(Qt::PenCapStyle style)