From e58f70be5c5a6a856f6560b666a855e7e0be43f9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Mal=C3=BD?= Date: Thu, 2 Apr 2015 22:48:06 +0200 Subject: [PATCH] Scale all elements in the chart to DPI --- signaldrawer.cpp | 36 ++++++++++++++++++++---------------- signaldrawer.h | 12 ++++++++---- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/signaldrawer.cpp b/signaldrawer.cpp index bc7c957..65b40e8 100644 --- a/signaldrawer.cpp +++ b/signaldrawer.cpp @@ -25,10 +25,11 @@ #include "signaldrawer.h" #include -const int SignalDrawer::AXIS_LABEL_BORDER_OFFSET(2); -const int SignalDrawer::AXIS_LABEL_SCALE_OFFSET(3); +const int SignalDrawer::DEFAULT_AXIS_LABEL_BORDER_OFFSET(2); +const int SignalDrawer::DEFAULT_AXIS_LABEL_SCALE_OFFSET(4); const int SignalDrawer::DEFAULT_BIGTICK_LENGTH(8); const double SignalDrawer::DEFAULT_DPI(96); +const int SignalDrawer::DEFAULT_LINE_WIDTH(1); const int SignalDrawer::DEFAULT_MAXIMAL_AXIS_BIGTICK_STEP(150); const int SignalDrawer::DEFAULT_MINIMAL_AXIS_BIGTICK_STEP(25); const int SignalDrawer::DEFAULT_SCALE_TEXT_VERTICAL_OFFSET(15); @@ -61,8 +62,12 @@ SignalDrawer::SignalDrawer(std::shared_ptr controller, const C m_axisLabelFont = new QFont("arial", scaleToDpiX(LABEL_FONT_SIZE)); m_axisLabelFM = new QFontMetrics(*m_axisLabelFont); + + m_axisLabelBorderOffset = scaleToDpiX(DEFAULT_AXIS_LABEL_BORDER_OFFSET); + m_axisLabelScaleOffset = scaleToDpiX(DEFAULT_AXIS_LABEL_SCALE_OFFSET); m_bigTickLength = scaleToDpiX(DEFAULT_BIGTICK_LENGTH); m_subTickLength = scaleToDpiX(DEFAULT_SUBTICK_LENGTH); + m_lineWidth = scaleToDpiX(DEFAULT_LINE_WIDTH); m_maximalBigtickStep = scaleToDpiX(DEFAULT_MAXIMAL_AXIS_BIGTICK_STEP); m_minimalBigtickStep = scaleToDpiX(DEFAULT_MINIMAL_AXIS_BIGTICK_STEP); m_scaleTextVerticalOffset = scaleToDpiY(DEFAULT_SCALE_TEXT_VERTICAL_OFFSET); @@ -106,8 +111,8 @@ bool SignalDrawer::draw(const double fromX, const double fromY, const double toX } m_xAxisLabelRect = m_axisLabelFM->boundingRect(m_xAxisLabelText); m_yAxisLabelRect = m_axisLabelFM->boundingRect(m_yAxisLabelText); - m_xAxisLabelRect.moveTo(m_width - m_axisLabelFM->width(m_xAxisLabelText) - AXIS_LABEL_BORDER_OFFSET, m_gHeight - AXIS_LABEL_SCALE_OFFSET); - m_yAxisLabelRect.moveTo(m_leftGraphOffset + AXIS_LABEL_SCALE_OFFSET, m_axisLabelFM->height() - m_axisLabelFM->underlinePos()); + m_xAxisLabelRect.moveTo(m_width - m_axisLabelFM->width(m_xAxisLabelText) - m_axisLabelBorderOffset, m_gHeight - m_axisLabelScaleOffset); + m_yAxisLabelRect.moveTo(m_leftGraphOffset + m_axisLabelScaleOffset, m_axisLabelFM->height() - m_axisLabelFM->underlinePos()); } else { m_gWidth = m_width; m_leftGraphOffset = 0; @@ -355,6 +360,7 @@ bool SignalDrawer::drawScale(QPainter* const painter) return false; } + painter->setPen(QPen(Qt::black, m_lineWidth)); painter->setFont(*m_axisLabelFont); /* VALUE scale must be drawn first - left offset of the graph depends on it */ drawValueScale(painter); @@ -483,7 +489,7 @@ bool SignalDrawer::renderGraph(QPainter* const painter) return false; } - painter->setPen(QColor(Qt::blue)); + painter->setPen(QPen(Qt::blue, m_lineWidth)); fromXPix = relToXPix(m_gdData->ddata[0].first); fromYPix = relToYPix(m_gdData->ddata[0].second); @@ -540,8 +546,6 @@ QRegion SignalDrawer::renderPeak(const PeakDrawData &pd, QPixmap* const pixmap) QRegion SignalDrawer::renderPeak(const PeakDrawData& pd, QPainter* const painter) { - QFont font("arial", 10); - QFontMetrics fm(font); QString aucText, timeText; QRegion peakReg; int tTextHeight, tTextWidth, aTextWidth; @@ -585,7 +589,7 @@ QRegion SignalDrawer::renderPeak(const PeakDrawData& pd, QPainter* const painter aucText = m_locale.toString(pd.auc, 'f', 4); timeText = QString("A:") + m_locale.toString(pd.time, 'f', 4); - painter->setPen(Qt::red); + painter->setPen(QPen(Qt::red, m_lineWidth)); painter->drawLine(fromXPix, fromYPix, toXPix, toYPix); if (pd.itype == IntegrationType::BASELINE) { painter->drawLine(fromXPix, fromYPix, fromXPix, relToYPix(pd.blFromY)); @@ -593,19 +597,19 @@ QRegion SignalDrawer::renderPeak(const PeakDrawData& pd, QPainter* const painter } /* Draw AREA and TIME caption */ - painter->setFont(font); + painter->setFont(*m_axisLabelFont); painter->setPen(Qt::black); - tTextWidth = fm.width(timeText); + tTextWidth = m_axisLabelFM->width(timeText); if (peakY - tTextWidth < 2) peakY += tTextWidth - peakY + 2; - tTextHeight = fm.height(); + tTextHeight = m_axisLabelFM->height(); painter->save(); painter->translate(peakX, peakY); painter->rotate(-90); painter->drawText(0, 0, timeText); painter->rotate(+90); painter->restore(); - aTextWidth = fm.width(aucText); + aTextWidth = m_axisLabelFM->width(aucText); painter->drawText(peakX + 5, peakY, aucText); if (peakX - tTextHeight < fromXPix) @@ -697,15 +701,15 @@ void SignalDrawer::restoreRelativeConstraints() } template -T SignalDrawer::scaleToDpiX(const T value) +int SignalDrawer::scaleToDpiX(const T value) { - return static_cast(value) * m_dpiX / DEFAULT_DPI; + return floor((static_cast(value) * m_dpiX / DEFAULT_DPI) + 0.5); } template -T SignalDrawer::scaleToDpiY(const T value) +int SignalDrawer::scaleToDpiY(const T value) { - return static_cast(value) * m_dpiY / DEFAULT_DPI; + return floor((static_cast(value) * m_dpiY / DEFAULT_DPI) + 0.5); } void SignalDrawer::setNewRelativeConstraints(const double fromX, const double fromY, const double toX, const double toY) diff --git a/signaldrawer.h b/signaldrawer.h index 25d7b2b..7831d3f 100644 --- a/signaldrawer.h +++ b/signaldrawer.h @@ -106,10 +106,11 @@ protected: double xPixToRel(const int pix); double yPixToRel(const int pix); - static const int AXIS_LABEL_BORDER_OFFSET; - static const int AXIS_LABEL_SCALE_OFFSET; + static const int DEFAULT_AXIS_LABEL_BORDER_OFFSET; + static const int DEFAULT_AXIS_LABEL_SCALE_OFFSET; static const int DEFAULT_BIGTICK_LENGTH; static const double DEFAULT_DPI; + static const int DEFAULT_LINE_WIDTH; static const int DEFAULT_MAXIMAL_AXIS_BIGTICK_STEP; static const int DEFAULT_MINIMAL_AXIS_BIGTICK_STEP; static const int DEFAULT_SCALE_TEXT_VERTICAL_OFFSET; @@ -123,8 +124,8 @@ private: int calculateMaximalTickStep(const int dimension); double linesIntersection(const double k1, const double q1, const double k2, const double q2); void restoreRelativeConstraints(); - template T scaleToDpiX(const T value); - template T scaleToDpiY(const T value); + template int scaleToDpiX(const T value); + template int scaleToDpiY(const T value); void setNewRelativeConstraints(const double fromX, const double fromY, const double toX, const double toY); double subStep(const double step); @@ -135,8 +136,11 @@ private: int m_leftGraphOffset; bool m_validConstraints; + int m_axisLabelBorderOffset; + int m_axisLabelScaleOffset; int m_bigTickLength; int m_subTickLength; + int m_lineWidth; int m_maximalBigtickStep; int m_minimalBigtickStep; int m_scaleTextVerticalOffset; -- 2.43.5