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();
391 if ( !defaultFontString.
isEmpty() )
408 double upperMagnitudeMultiplier = 1.0;
409 double widthInSelectedUnits =
mapWidth();
410 double initialUnitsPerSegment = widthInSelectedUnits / 10.0;
417 upperMagnitudeMultiplier = 1.0;
423 if ( initialUnitsPerSegment > 1000.0 )
425 upperMagnitudeMultiplier = 1000.0;
430 upperMagnitudeMultiplier = 1.0;
437 if ( initialUnitsPerSegment > 5419.95 )
439 upperMagnitudeMultiplier = 5419.95;
444 upperMagnitudeMultiplier = 1.0;
451 upperMagnitudeMultiplier = 1;
457 double segmentWidth = initialUnitsPerSegment / upperMagnitudeMultiplier;
458 int segmentMagnitude = floor( log10( segmentWidth ) );
459 double unitsPerSegment = upperMagnitudeMultiplier * ( qPow( 10.0, segmentMagnitude ) );
460 double multiplier = floor(( widthInSelectedUnits / ( unitsPerSegment * 10.0 ) ) / 2.5 ) * 2.5;
462 if ( multiplier > 0 )
464 unitsPerSegment = unitsPerSegment * multiplier;
560 posWidthList.clear();
567 posWidthList.push_back( qMakePair( mCurrentXCoord, leftSegmentSize ) );
568 mCurrentXCoord += leftSegmentSize;
585 if ( styleName ==
"Single Box" )
589 else if ( styleName ==
"Double Box" )
593 else if ( styleName ==
"Line Ticks Middle" || styleName ==
"Line Ticks Down" || styleName ==
"Line Ticks Up" )
596 if ( styleName ==
"Line Ticks Middle" )
600 else if ( styleName ==
"Line Ticks Down" )
604 else if ( styleName ==
"Line Ticks Up" )
610 else if ( styleName ==
"Numeric" )
709 composerScaleBarElem.
appendChild( fillColor2Elem );
718 composerScaleBarElem.
appendChild( strokeColorElem );
732 return _writeXML( composerScaleBarElem, doc );
767 if ( fillColorList.
size() > 0 )
770 bool redOk, greenOk, blueOk, alphaOk;
771 int fillRed, fillGreen, fillBlue, fillAlpha;
778 if ( redOk && greenOk && blueOk && alphaOk )
790 if ( fillColor2List.
size() > 0 )
793 bool redOk, greenOk, blueOk, alphaOk;
794 int fillRed, fillGreen, fillBlue, fillAlpha;
801 if ( redOk && greenOk && blueOk && alphaOk )
813 if ( strokeColorList.
size() > 0 )
816 bool redOk, greenOk, blueOk, alphaOk;
817 int strokeRed, strokeGreen, strokeBlue, strokeAlpha;
824 if ( redOk && greenOk && blueOk && alphaOk )
836 if ( textColorList.
size() > 0 )
839 bool redOk, greenOk, blueOk, alphaOk;
840 int textRed, textGreen, textBlue, textAlpha;
847 if ( redOk && greenOk && blueOk && alphaOk )
883 if ( composerItemList.
size() > 0 )
902 move( -( widthAfter - width ) / 2.0, 0 );
906 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
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)
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)
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.
SegmentSizeMode
Modes for setting size 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)
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.
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
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)
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
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
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...
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.
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 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)