[QGIS Commit] r8806 -
branches/advanced_printing_branch/src/app/composer
svn_qgis at osgeo.org
svn_qgis at osgeo.org
Thu Jul 17 10:49:20 EDT 2008
Author: mhugent
Date: 2008-07-17 10:49:20 -0400 (Thu, 17 Jul 2008)
New Revision: 8806
Modified:
branches/advanced_printing_branch/src/app/composer/qgscomposerscalebar.cpp
branches/advanced_printing_branch/src/app/composer/qgscomposerscalebar.h
branches/advanced_printing_branch/src/app/composer/qgsdoubleboxscalebarstyle.cpp
branches/advanced_printing_branch/src/app/composer/qgsdoubleboxscalebarstyle.h
branches/advanced_printing_branch/src/app/composer/qgsnumericscalebarstyle.cpp
branches/advanced_printing_branch/src/app/composer/qgsnumericscalebarstyle.h
branches/advanced_printing_branch/src/app/composer/qgsscalebarstyle.cpp
branches/advanced_printing_branch/src/app/composer/qgsscalebarstyle.h
branches/advanced_printing_branch/src/app/composer/qgssingleboxscalebarstyle.cpp
branches/advanced_printing_branch/src/app/composer/qgssingleboxscalebarstyle.h
branches/advanced_printing_branch/src/app/composer/qgsticksscalebarstyle.cpp
branches/advanced_printing_branch/src/app/composer/qgsticksscalebarstyle.h
Log:
Draw scale bar labels centered
Modified: branches/advanced_printing_branch/src/app/composer/qgscomposerscalebar.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgscomposerscalebar.cpp 2008-07-17 11:33:52 UTC (rev 8805)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposerscalebar.cpp 2008-07-17 14:49:20 UTC (rev 8806)
@@ -43,7 +43,11 @@
return;
}
- mStyle->draw(painter);
+ //calculate half of first label width as labels are drawn centered
+ QFontMetricsF fontMetrics(mFont);
+ QString firstLabel = firstLabelString();
+
+ mStyle->draw(painter, fontMetrics.width(firstLabel) / 2);
//draw frame and selection boxes if necessary
drawFrame(painter);
@@ -225,4 +229,16 @@
}
}
+QString QgsComposerScaleBar::firstLabelString() const
+{
+ if(mNumSegmentsLeft > 0)
+ {
+ return QString::number(mNumUnitsPerSegment / mNumMapUnitsPerScaleBarUnit);
+ }
+ else
+ {
+ return "0";
+ }
+}
+
Modified: branches/advanced_printing_branch/src/app/composer/qgscomposerscalebar.h
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgscomposerscalebar.h 2008-07-17 11:33:52 UTC (rev 8805)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposerscalebar.h 2008-07-17 14:49:20 UTC (rev 8806)
@@ -95,6 +95,9 @@
/**Adjusts box size and calls QgsComposerItem::update()*/
void update();
+ /**Returns string of first label (important for drawing, labeling, size calculation*/
+ QString firstLabelString() const;
+
public slots:
void updateSegmentSize();
/**Sets mCompositionMap to 0 if the map is deleted*/
Modified: branches/advanced_printing_branch/src/app/composer/qgsdoubleboxscalebarstyle.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgsdoubleboxscalebarstyle.cpp 2008-07-17 11:33:52 UTC (rev 8805)
+++ branches/advanced_printing_branch/src/app/composer/qgsdoubleboxscalebarstyle.cpp 2008-07-17 14:49:20 UTC (rev 8806)
@@ -39,7 +39,7 @@
return "Double Box";
}
-void QgsDoubleBoxScaleBarStyle::draw(QPainter* p) const
+void QgsDoubleBoxScaleBarStyle::draw(QPainter* p, double xOffset) const
{
if(!mScaleBar)
{
@@ -71,7 +71,7 @@
p->setBrush(QColor(255, 255, 255));
}
- QRectF segmentRectTop(segmentIt->first, barTopPosition, segmentIt->second, segmentHeight);
+ QRectF segmentRectTop(segmentIt->first + xOffset, barTopPosition, segmentIt->second, segmentHeight);
p->drawRect(segmentRectTop);
//draw bottom half
@@ -84,7 +84,7 @@
p->setBrush(mScaleBar->brush());
}
- QRectF segmentRectBottom(segmentIt->first, barTopPosition + segmentHeight, segmentIt->second, segmentHeight);
+ QRectF segmentRectBottom(segmentIt->first + xOffset, barTopPosition + segmentHeight, segmentIt->second, segmentHeight);
p->drawRect(segmentRectBottom);
useColor = !useColor;
}
Modified: branches/advanced_printing_branch/src/app/composer/qgsdoubleboxscalebarstyle.h
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgsdoubleboxscalebarstyle.h 2008-07-17 11:33:52 UTC (rev 8805)
+++ branches/advanced_printing_branch/src/app/composer/qgsdoubleboxscalebarstyle.h 2008-07-17 14:49:20 UTC (rev 8806)
@@ -28,7 +28,7 @@
QString name() const;
- void draw(QPainter* p) const;
+ void draw(QPainter* p, double xOffset = 0) const;
private:
QgsDoubleBoxScaleBarStyle(); //forbidden
Modified: branches/advanced_printing_branch/src/app/composer/qgsnumericscalebarstyle.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgsnumericscalebarstyle.cpp 2008-07-17 11:33:52 UTC (rev 8805)
+++ branches/advanced_printing_branch/src/app/composer/qgsnumericscalebarstyle.cpp 2008-07-17 14:49:20 UTC (rev 8806)
@@ -40,7 +40,7 @@
return "Numeric";
}
-void QgsNumericScaleBarStyle::draw(QPainter* p) const
+void QgsNumericScaleBarStyle::draw(QPainter* p, double xOffset) const
{
if(!p || !mScaleBar)
{
Modified: branches/advanced_printing_branch/src/app/composer/qgsnumericscalebarstyle.h
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgsnumericscalebarstyle.h 2008-07-17 11:33:52 UTC (rev 8805)
+++ branches/advanced_printing_branch/src/app/composer/qgsnumericscalebarstyle.h 2008-07-17 14:49:20 UTC (rev 8806)
@@ -28,7 +28,7 @@
QString name() const;
- void draw(QPainter* p) const;
+ void draw(QPainter* p, double xOffset = 0) const;
//calculation of box size is different compared to segment based scale bars
QRectF calculateBoxSize() const;
Modified: branches/advanced_printing_branch/src/app/composer/qgsscalebarstyle.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgsscalebarstyle.cpp 2008-07-17 11:33:52 UTC (rev 8805)
+++ branches/advanced_printing_branch/src/app/composer/qgsscalebarstyle.cpp 2008-07-17 14:49:20 UTC (rev 8806)
@@ -44,6 +44,10 @@
p->save();
p->setFont(mScaleBar->font());
+
+ QFontMetricsF fontMetrics(mScaleBar->font());
+ QString firstLabel = mScaleBar->firstLabelString();
+ double xOffset = fontMetrics.width(firstLabel) / 2;
//double mCurrentXCoord = mScaleBar->pen().widthF() + mScaleBar->boxContentSpace();
QList<QPair<double, double> > segmentInfo;
@@ -53,6 +57,7 @@
int nSegmentsLeft = mScaleBar->numSegmentsLeft();
int segmentCounter = 0;
+ QString currentNumericLabel;
QList<QPair<double, double> >::const_iterator segmentIt = segmentInfo.constBegin();
for(; segmentIt != segmentInfo.constEnd(); ++segmentIt)
@@ -60,21 +65,25 @@
if(segmentCounter == 0 && nSegmentsLeft > 0)
{
//label first left segment
- p->drawText(QPointF(segmentIt->first, mScaleBar->fontHeight() + mScaleBar->boxContentSpace()), QString::number(mScaleBar->numUnitsPerSegment() / mScaleBar->numMapUnitsPerScaleBarUnit()));
+ currentNumericLabel = firstLabel;
+ }
+ else if(segmentCounter >= nSegmentsLeft)
+ {
+ currentNumericLabel = QString::number(currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit());
}
-
+ p->drawText(QPointF(segmentIt->first - fontMetrics.width(currentNumericLabel) / 2 + xOffset, mScaleBar->fontHeight() + mScaleBar->boxContentSpace()), currentNumericLabel);
+
if(segmentCounter >= nSegmentsLeft)
{
- p->drawText(QPointF(segmentIt->first, mScaleBar->fontHeight() + mScaleBar->boxContentSpace()), QString::number(currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit()));
- currentLabelNumber += mScaleBar->numUnitsPerSegment();
+ ++segmentCounter;
}
- ++segmentCounter;
}
//also draw the last label
if(!segmentInfo.isEmpty())
{
- p->drawText(QPointF(segmentInfo.last().first + mScaleBar->segmentMM(), mScaleBar->fontHeight() + mScaleBar->boxContentSpace()), QString::number(currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit()) + " " + mScaleBar->unitLabeling());
+ currentNumericLabel = QString::number(currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit());
+ p->drawText(QPointF(segmentInfo.last().first + mScaleBar->segmentMM() - fontMetrics.width(currentNumericLabel) / 2 + xOffset, mScaleBar->fontHeight() + mScaleBar->boxContentSpace()), currentNumericLabel + " " + mScaleBar->unitLabeling());
}
p->restore();
@@ -87,13 +96,20 @@
return QRectF();
}
- //consider width of largest label
+
+ QFontMetricsF fontMetrics(mScaleBar->font());
+
+ //consider centered first label
+ double firstLabelLeft = fontMetrics.width(mScaleBar->firstLabelString()) / 2;
+
+ //consider last number and label
+
double largestLabelNumber = mScaleBar->numSegments() * mScaleBar->numUnitsPerSegment() / mScaleBar->numMapUnitsPerScaleBarUnit();
+ QString largestNumberLabel = QString::number(largestLabelNumber);
QString largestLabel = QString::number(largestLabelNumber) + " " + mScaleBar->unitLabeling();
- double largestLabelWidth = QFontMetricsF(mScaleBar->font()).width(largestLabel);
+ double largestLabelWidth = fontMetrics.width(largestLabel) - fontMetrics.width(largestNumberLabel) / 2;
- //add all the segment length
- double totalBarLength = 0;
+ double totalBarLength;
QList< QPair<double, double> > segmentList;
mScaleBar->segmentPositions(segmentList);
@@ -104,7 +120,7 @@
totalBarLength += segmentIt->second;
}
- double width = totalBarLength + 2 * mScaleBar->pen().widthF() + largestLabelWidth + 2 * mScaleBar->boxContentSpace();
+ double width = firstLabelLeft + totalBarLength + 2 * mScaleBar->pen().widthF() + largestLabelWidth + 2 * mScaleBar->boxContentSpace();
double height = mScaleBar->height() + mScaleBar->labelBarSpace() + 2 * mScaleBar->boxContentSpace() + mScaleBar->fontHeight();
return QRectF(mScaleBar->transform().dx(), mScaleBar->transform().dy(), width, height);
Modified: branches/advanced_printing_branch/src/app/composer/qgsscalebarstyle.h
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgsscalebarstyle.h 2008-07-17 11:33:52 UTC (rev 8805)
+++ branches/advanced_printing_branch/src/app/composer/qgsscalebarstyle.h 2008-07-17 14:49:20 UTC (rev 8806)
@@ -31,7 +31,9 @@
QgsScaleBarStyle(const QgsComposerScaleBar* bar);
virtual ~QgsScaleBarStyle();
- virtual void draw(QPainter* p) const = 0; //to do by every subclass
+ /**Draws the style
+ @param xOffset offset to account for centered labeling*/
+ virtual void draw(QPainter* p, double xOffset = 0) const = 0; //to do by every subclass
virtual void drawLabels(QPainter* p) const; //default implementation provided
virtual QRectF calculateBoxSize() const; //default implementation provided
virtual QString name() const = 0; //return name of the style
Modified: branches/advanced_printing_branch/src/app/composer/qgssingleboxscalebarstyle.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgssingleboxscalebarstyle.cpp 2008-07-17 11:33:52 UTC (rev 8805)
+++ branches/advanced_printing_branch/src/app/composer/qgssingleboxscalebarstyle.cpp 2008-07-17 14:49:20 UTC (rev 8806)
@@ -34,7 +34,7 @@
//nothing to do...
}
-void QgsSingleBoxScaleBarStyle::draw(QPainter* p) const
+void QgsSingleBoxScaleBarStyle::draw(QPainter* p, double xOffset) const
{
if(!mScaleBar)
{
@@ -62,7 +62,7 @@
p->setBrush(QColor(255, 255, 255));
}
- QRectF segmentRect(segmentIt->first, barTopPosition, segmentIt->second, mScaleBar->height());
+ QRectF segmentRect(segmentIt->first + xOffset, barTopPosition, segmentIt->second, mScaleBar->height());
p->drawRect(segmentRect);
useColor = !useColor;
}
Modified: branches/advanced_printing_branch/src/app/composer/qgssingleboxscalebarstyle.h
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgssingleboxscalebarstyle.h 2008-07-17 11:33:52 UTC (rev 8805)
+++ branches/advanced_printing_branch/src/app/composer/qgssingleboxscalebarstyle.h 2008-07-17 14:49:20 UTC (rev 8806)
@@ -29,7 +29,7 @@
QString name() const;
- void draw(QPainter* p) const;
+ void draw(QPainter* p, double xOffset = 0) const;
private:
QgsSingleBoxScaleBarStyle(); //forbidden
Modified: branches/advanced_printing_branch/src/app/composer/qgsticksscalebarstyle.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgsticksscalebarstyle.cpp 2008-07-17 11:33:52 UTC (rev 8805)
+++ branches/advanced_printing_branch/src/app/composer/qgsticksscalebarstyle.cpp 2008-07-17 14:49:20 UTC (rev 8806)
@@ -38,7 +38,7 @@
return "Line Ticks Middle";
}
-void QgsTicksScaleBarStyle::draw(QPainter* p) const
+void QgsTicksScaleBarStyle::draw(QPainter* p, double xOffset) const
{
if(!mScaleBar)
{
@@ -57,17 +57,17 @@
QList<QPair<double, double> >::const_iterator segmentIt = segmentInfo.constBegin();
for(; segmentIt != segmentInfo.constEnd(); ++segmentIt)
{
- p->drawLine(segmentIt->first, barTopPosition, segmentIt->first, barTopPosition + mScaleBar->height());
+ p->drawLine(segmentIt->first + xOffset, barTopPosition, segmentIt->first + xOffset, barTopPosition + mScaleBar->height());
switch(mTickPosition)
{
case DOWN:
- p->drawLine(segmentIt->first, barTopPosition, segmentIt->first + mScaleBar->segmentMM(), barTopPosition);
+ p->drawLine(xOffset + segmentIt->first, barTopPosition, xOffset + segmentIt->first + mScaleBar->segmentMM(), barTopPosition);
break;
case MIDDLE:
- p->drawLine(segmentIt->first, middlePosition, segmentIt->first + mScaleBar->segmentMM(), middlePosition);
+ p->drawLine(xOffset + segmentIt->first, middlePosition, xOffset + segmentIt->first + mScaleBar->segmentMM(), middlePosition);
break;
case UP:
- p->drawLine(segmentIt->first, bottomPosition, segmentIt->first + mScaleBar->segmentMM(), bottomPosition);
+ p->drawLine(xOffset + segmentIt->first, bottomPosition, xOffset + segmentIt->first + mScaleBar->segmentMM(), bottomPosition);
break;
}
}
@@ -76,7 +76,7 @@
if(!segmentInfo.isEmpty())
{
double lastTickPositionX = segmentInfo.last().first + mScaleBar->segmentMM();
- p->drawLine(lastTickPositionX, barTopPosition, lastTickPositionX, barTopPosition + mScaleBar->height());
+ p->drawLine(lastTickPositionX + xOffset, barTopPosition, lastTickPositionX + xOffset, barTopPosition + mScaleBar->height());
}
p->restore();
Modified: branches/advanced_printing_branch/src/app/composer/qgsticksscalebarstyle.h
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgsticksscalebarstyle.h 2008-07-17 11:33:52 UTC (rev 8805)
+++ branches/advanced_printing_branch/src/app/composer/qgsticksscalebarstyle.h 2008-07-17 14:49:20 UTC (rev 8806)
@@ -34,7 +34,7 @@
QString name() const;
- void draw(QPainter* p) const;
+ void draw(QPainter* p, double xOffset = 0) const;
void setTickPosition(TickPosition p){mTickPosition = p;}
More information about the QGIS-commit
mailing list