33 #include <QDomDocument> 34 #include <QDomElement> 35 #include <QFontMetricsF> 42 , mComposerMap( nullptr )
43 , mNumUnitsPerSegment( 0 )
44 , mSegmentSizeMode( SegmentSizeFixed )
47 , mFontColor(
QColor( 0, 0, 0 ) )
49 , mSegmentMillimeters( 0.0 )
52 , mLineJoinStyle(
Qt::MiterJoin )
53 , mLineCapStyle(
Qt::SquareCap )
66 Q_UNUSED( itemStyle );
231 double s = qPow( 10.0, floor( log10( a ) ) ) / d;
232 return ceil( a / s ) * s;
238 double s = qPow( 10.0, floor( log10( a ) ) ) / d;
239 return floor( a / s ) * s;
275 while ( lowerNiceUnitsPerSeg > maxUnitsPerSeg && upperNiceUnitsPerSeg < minUnitsPerSeg )
283 mNumUnitsPerSegment = upperNiceUnitsPerSeg < minUnitsPerSeg ? lowerNiceUnitsPerSeg : upperNiceUnitsPerSeg;
300 return composerMapRect.
width();
402 if ( !defaultFontString.
isEmpty() )
419 double upperMagnitudeMultiplier = 1.0;
420 double widthInSelectedUnits =
mapWidth();
421 double initialUnitsPerSegment = widthInSelectedUnits / 10.0;
428 upperMagnitudeMultiplier = 1.0;
434 if ( initialUnitsPerSegment > 1000.0 )
436 upperMagnitudeMultiplier = 1000.0;
441 upperMagnitudeMultiplier = 1.0;
448 if ( initialUnitsPerSegment > 5419.95 )
450 upperMagnitudeMultiplier = 5419.95;
455 upperMagnitudeMultiplier = 1.0;
462 upperMagnitudeMultiplier = 1;
468 double segmentWidth = initialUnitsPerSegment / upperMagnitudeMultiplier;
469 int segmentMagnitude = floor( log10( segmentWidth ) );
470 double unitsPerSegment = upperMagnitudeMultiplier * ( qPow( 10.0, segmentMagnitude ) );
471 double multiplier = floor(( widthInSelectedUnits / ( unitsPerSegment * 10.0 ) ) / 2.5 ) * 2.5;
473 if ( multiplier > 0 )
475 unitsPerSegment = unitsPerSegment * multiplier;
571 posWidthList.clear();
578 posWidthList.push_back( qMakePair( mCurrentXCoord, leftSegmentSize ) );
579 mCurrentXCoord += leftSegmentSize;
596 if ( styleName ==
"Single Box" )
600 else if ( styleName ==
"Double Box" )
604 else if ( styleName ==
"Line Ticks Middle" || styleName ==
"Line Ticks Down" || styleName ==
"Line Ticks Up" )
607 if ( styleName ==
"Line Ticks Middle" )
611 else if ( styleName ==
"Line Ticks Down" )
615 else if ( styleName ==
"Line Ticks Up" )
621 else if ( styleName ==
"Numeric" )
720 composerScaleBarElem.
appendChild( fillColor2Elem );
729 composerScaleBarElem.
appendChild( strokeColorElem );
743 return _writeXML( composerScaleBarElem, doc );
778 if ( !fillColorList.
isEmpty() )
781 bool redOk, greenOk, blueOk, alphaOk;
782 int fillRed, fillGreen, fillBlue, fillAlpha;
789 if ( redOk && greenOk && blueOk && alphaOk )
801 if ( !fillColor2List.
isEmpty() )
804 bool redOk, greenOk, blueOk, alphaOk;
805 int fillRed, fillGreen, fillBlue, fillAlpha;
812 if ( redOk && greenOk && blueOk && alphaOk )
824 if ( !strokeColorList.
isEmpty() )
827 bool redOk, greenOk, blueOk, alphaOk;
828 int strokeRed, strokeGreen, strokeBlue, strokeAlpha;
835 if ( redOk && greenOk && blueOk && alphaOk )
847 if ( !textColorList.
isEmpty() )
850 bool redOk, greenOk, blueOk, alphaOk;
851 int textRed, textGreen, textBlue, textAlpha;
858 if ( redOk && greenOk && blueOk && alphaOk )
894 if ( !composerItemList.
isEmpty() )
913 move( -( widthAfter - width ) / 2.0, 0 );
917 move( -( widthAfter - width ), 0 );
Double box with alternating colors.
ScaleBarUnits units() const
QDomNodeList elementsByTagName(const QString &tagname) const
A rectangle specified with double values.
double mLabelBarSpace
Space between bar and Text labels.
const QgsComposerMap * getComposerMapById(const int id) const
Returns the composer map with specified id.
virtual QRectF calculateBoxSize() const
double mHeight
Height of bars/lines.
double mNumUnitsPerSegment
Size of a segment (in map units)
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
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
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.
QRectF evalItemRect(const QRectF &newRect, const bool resizeOnly=false, const QgsExpressionContext *context=nullptr)
Evaluates an item's bounding rect to consider data defined position and size of item and reference po...
bool hasCrsTransformEnabled() const
returns true if projections are enabled for this layer set
void setNumSegmentsLeft(int nSegmentsLeft)
double mMinBarWidth
Minimum allowed bar width, when mSegmentSizeMode is FitWidth.
int id() const
Get identification number.
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()
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
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.
QString firstLabelString() const
Returns string of first label (important for drawing, labeling, size calculation. ...
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.
double width() const
Width of the rectangle.
int toInt(bool *ok, int base) const
static QString encodePenJoinStyle(Qt::PenJoinStyle style)
void setWidthF(qreal width)
double prevNiceNumber(double a, double d=1)
static Qt::PenCapStyle decodePenCapStyle(const QString &str)
QgsComposerScaleBar(QgsComposition *composition)
double measureLine(const QList< QgsPoint > &points) const
Measures the length of a line with multiple segments.
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.
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.
Graphics scene for map printing.
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
Object representing map window.
static double fromUnitToUnitFactor(QGis::UnitType fromUnit, QGis::UnitType toUnit)
Returns the conversion factor between the specified distance units.
QByteArray toLocal8Bit() const
void setComposerMap(const QgsComposerMap *map)
void refreshSegmentMillimeters()
Calculates with of a segment in mm and stores it in mSegmentMillimeters.
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
void invalidateCurrentMap()
Sets mCompositionMap to 0 if the map is deleted.
void setBoxContentSpace(double space)
double xMaximum() const
Get the x maximum value (right side of rectangle)
void setPointSizeF(qreal pointSize)
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
const QgsComposerMap * composerMap() const
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.
void setSegmentSizeMode(SegmentSizeMode mode)
Sets the size mode for scale bar segments.
SegmentSizeMode mSegmentSizeMode
Either fixed (i.e.
double mapWidth() const
Returns diagonal of composer map in selected units (map units / meters / feet / nautical miles) ...
void setWidth(qreal width)
virtual void drawBackground(QPainter *p)
Draw background.
QString style() const
Returns style name.
static QgsProject * instance()
Returns the QgsProject singleton 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.
UnitType
Map units that qgis supports.
double xMinimum() const
Get the x minimum value (left side of rectangle)
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)
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)
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...
QDomElement createElement(const QString &tagName)
long srsid() const
Returns the SrsId, if available.
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...
const QgsRectangle * currentMapExtent() const
Returns a pointer to the current map extent, which is either the original user specified extent or th...
const QgsCoordinateReferenceSystem & destinationCrs() const
returns CRS of destination coordinate reference system
void destroyed(QObject *obj)
void setEllipsoidalMode(bool flag)
Sets whether coordinates must be projected to ellipsoid before measuring.
void setTickPosition(TickPosition p)
QDomNode at(int index) const
static QString encodePenCapStyle(Qt::PenCapStyle style)