From: Michal MalĂ˝ Date: Fri, 13 Mar 2015 00:55:45 +0000 (+0100) Subject: Toss around only one QPainter instance instead of recreating it whenever X-Git-Tag: 0.4b~3 X-Git-Url: https://gitweb.devoid-pointer.net/?a=commitdiff_plain;h=7440f9959bf50680c88c1faf3f76f9a82b56f3f5;p=anyanka.git Toss around only one QPainter instance instead of recreating it whenever it is needed. --- diff --git a/signaldrawer.cpp b/signaldrawer.cpp index dbadd70..edc61aa 100644 --- a/signaldrawer.cpp +++ b/signaldrawer.cpp @@ -59,6 +59,7 @@ Constraints SignalDrawer::currentConstraints() bool SignalDrawer::draw(const double fromX, const double fromY, const double toX, const double toY, const GraphLayers layers) { QPixmap* fresh; + QPainter painter; bool ret; setNewRelativeConstraints(fromX, fromY, toX, toY); @@ -74,9 +75,10 @@ bool SignalDrawer::draw(const double fromX, const double fromY, const double toX goto error_out; } + painter.begin(fresh); /* Draw the scale */ if (flags(GraphLayers::SCALE & layers)) { - ret = drawScale(fresh); + ret = drawScale(&painter); if (!ret) { Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " cannot draw scales"); goto error_out; @@ -93,7 +95,7 @@ bool SignalDrawer::draw(const double fromX, const double fromY, const double toX /* Draw the signal itself */ if (flags(GraphLayers::GRAPH & layers)) { - ret = drawGraph(fresh); + ret = drawGraph(&painter); if (!ret) { Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " cannot draw graph"); goto error_out; @@ -102,7 +104,7 @@ bool SignalDrawer::draw(const double fromX, const double fromY, const double toX /* Draw integrated peaks */ if (flags(GraphLayers::INTEGRATION & layers)) { - ret = drawPeaks(fresh); + ret = drawPeaks(&painter); if (!ret) { Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " cannot draw integrated peaks"); goto error_out; @@ -110,16 +112,17 @@ bool SignalDrawer::draw(const double fromX, const double fromY, const double toX } /* Draw axes labels */ - ret = drawAxisLabel(SignalController::Axis::TIME, fresh); + ret = drawAxisLabel(SignalController::Axis::TIME, &painter); if (!ret) { Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " cannot draw X axis label"); goto error_out; } - ret = drawAxisLabel(SignalController::Axis::VALUE, fresh); + ret = drawAxisLabel(SignalController::Axis::VALUE, &painter); if (!ret) { Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " cannot draw Y axis label"); goto error_out; } + painter.end(); renderFresh(fresh); @@ -173,7 +176,7 @@ void SignalDrawer::copyPixmapRegion(const QRegion& reg, QPixmap* const source, Q p.drawPixmap(rect, *source, rect); } -bool SignalDrawer::drawGraph(QPixmap* const target) +bool SignalDrawer::drawGraph(QPainter* const painter) { std::shared_ptr gdData = m_controller->getGraphDrawData(m_relXMin, m_relXMax); if (gdData->ddataLen == 0) @@ -181,17 +184,16 @@ bool SignalDrawer::drawGraph(QPixmap* const target) m_gdData = gdData; - return renderGraph(target); + return renderGraph(painter); } -bool SignalDrawer::drawAxisLabel(const SignalController::Axis axis, QPixmap* const target) +bool SignalDrawer::drawAxisLabel(const SignalController::Axis axis, QPainter* const painter) { - QPainter p; int xPix, yPix; QString text; - if (target == nullptr) { - Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " null pointer to pixmap"); + if (painter == nullptr) { + Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " null pointer to QPainter"); return false; } @@ -208,22 +210,21 @@ bool SignalDrawer::drawAxisLabel(const SignalController::Axis axis, QPixmap* con break; } - p.begin(target); - p.setFont(m_axisLabelFont); - p.drawText(xPix, yPix, text); - p.end(); + painter->setPen(QColor(Qt::black)); + painter->setFont(m_axisLabelFont); + painter->drawText(xPix, yPix, text); return true; } -bool SignalDrawer::drawPeaks(QPixmap* const target) +bool SignalDrawer::drawPeaks(QPainter* const painter) { std::vector pdData = m_controller->getPeaksDrawData(m_relXMin, m_relYMin, m_relXMax, m_relYMax); if (pdData.size() < 1) return true; for (const PeakDrawData& pd : pdData) { - QRegion reg = renderPeak(pd, target); + QRegion reg = renderPeak(pd, painter); if (reg.isEmpty()) return false; } @@ -321,21 +322,17 @@ void SignalDrawer::drawValueScale(QPainter* const p) p->drawLine(m_leftGraphOffset, 0, m_leftGraphOffset, m_gHeight); } -bool SignalDrawer::drawScale(QPixmap* const target) +bool SignalDrawer::drawScale(QPainter* const painter) { - QPainter p; - - if (target == nullptr) { - Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " null pointer to pixmap"); + if (painter == nullptr) { + Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " null pointer to QPainter"); return false; } - p.begin(target); - p.setFont(m_axisLabelFont); + painter->setFont(m_axisLabelFont); /* VALUE scale must be drawn first - left offset of the graph depends on it */ - drawValueScale(&p); - drawTimeScale(&p); - p.end(); + drawValueScale(painter); + drawTimeScale(painter); return true; } @@ -447,9 +444,8 @@ QRegion SignalDrawer::erasePeak(const PeakDrawData& pd) return peakReg; } -bool SignalDrawer::renderGraph(QPixmap* const target) +bool SignalDrawer::renderGraph(QPainter* const painter) { - QPainter p; int fromXPix, fromYPix; if (m_width < m_leftGraphOffset || m_height < SCALE_MARGIN_TIME) { @@ -461,8 +457,7 @@ bool SignalDrawer::renderGraph(QPixmap* const target) return false; } - p.begin(target); - p.setPen(QColor(Qt::blue)); + painter->setPen(QColor(Qt::blue)); fromXPix = relToXPix(m_gdData->ddata[0].first); fromYPix = relToYPix(m_gdData->ddata[0].second); @@ -473,7 +468,7 @@ bool SignalDrawer::renderGraph(QPixmap* const target) if (fromYPix >= m_gHeight && toYPix >= m_gHeight) { /* Draw nothing */ } else if (toXPix == fromXPix) - p.drawLine(fromXPix, (fromYPix >= m_gHeight) ? m_gHeight : fromYPix, toXPix, (toYPix >= m_gHeight) ? m_gHeight : toYPix); + painter->drawLine(fromXPix, (fromYPix >= m_gHeight) ? m_gHeight : fromYPix, toXPix, (toYPix >= m_gHeight) ? m_gHeight : toYPix); else if (toYPix >= m_gHeight) { double k = static_cast(toYPix - fromYPix) / static_cast(toXPix - fromXPix); double cToYPix = m_gHeight; @@ -482,7 +477,7 @@ bool SignalDrawer::renderGraph(QPixmap* const target) cToXPix = fromXPix; else cToXPix = floor(cToXPix + 0.5); - p.drawLine(fromXPix, fromYPix, cToXPix, m_gHeight); + painter->drawLine(fromXPix, fromYPix, cToXPix, m_gHeight); } else if (fromYPix >= m_gHeight) { double k = static_cast(toYPix - fromYPix) / static_cast(toXPix - fromXPix); double cFromYPix = m_gHeight; @@ -491,16 +486,14 @@ bool SignalDrawer::renderGraph(QPixmap* const target) cFromXPix = fromXPix; else cFromXPix = floor(cFromXPix + 0.5); - p.drawLine(cFromXPix, m_gHeight, toXPix, toYPix); + painter->drawLine(cFromXPix, m_gHeight, toXPix, toYPix); } else - p.drawLine(fromXPix, fromYPix, toXPix, toYPix); + painter->drawLine(fromXPix, fromYPix, toXPix, toYPix); fromXPix = toXPix; fromYPix = toYPix; } - p.end(); - return true; } @@ -513,10 +506,14 @@ void SignalDrawer::renderFresh(QPixmap* const fresh) m_background = new QPixmap(*m_pixmap); } +QRegion SignalDrawer::renderPeak(const PeakDrawData &pd, QPixmap* const pixmap) +{ + QPainter painter(pixmap); + return renderPeak(pd, &painter); +} -QRegion SignalDrawer::renderPeak(const PeakDrawData& pd, QPixmap* const target) +QRegion SignalDrawer::renderPeak(const PeakDrawData& pd, QPainter* const painter) { - QPainter p; QFont font("arial", 10); QFontMetrics fm(font); QString aucText, timeText; @@ -527,7 +524,7 @@ QRegion SignalDrawer::renderPeak(const PeakDrawData& pd, QPixmap* const target) int peakX, peakY; int beginXPix, endXPix; - if (target == nullptr) { + if (painter == nullptr) { Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " null pointer to pixmap"); return QRect(); } @@ -562,29 +559,28 @@ QRegion SignalDrawer::renderPeak(const PeakDrawData& pd, QPixmap* const target) aucText = m_locale.toString(pd.auc, 'f', 4); timeText = QString("A:") + m_locale.toString(pd.time, 'f', 4); - p.begin(target); - p.setPen(Qt::red); - p.drawLine(fromXPix, fromYPix, toXPix, toYPix); + painter->setPen(Qt::red); + painter->drawLine(fromXPix, fromYPix, toXPix, toYPix); if (pd.itype == IntegrationType::BASELINE) { - p.drawLine(fromXPix, fromYPix, fromXPix, relToYPix(pd.blFromY)); - p.drawLine(toXPix, toYPix, toXPix, relToYPix(pd.blToY)); + painter->drawLine(fromXPix, fromYPix, fromXPix, relToYPix(pd.blFromY)); + painter->drawLine(toXPix, toYPix, toXPix, relToYPix(pd.blToY)); } /* Draw AREA and TIME caption */ - p.setFont(font); - p.setPen(Qt::black); + painter->setFont(font); + painter->setPen(Qt::black); tTextWidth = fm.width(timeText); if (peakY - tTextWidth < 2) peakY += tTextWidth - peakY + 2; tTextHeight = fm.height(); - p.save(); - p.translate(peakX, peakY); - p.rotate(-90); - p.drawText(0, 0, timeText); - p.rotate(+90); - p.restore(); + painter->save(); + painter->translate(peakX, peakY); + painter->rotate(-90); + painter->drawText(0, 0, timeText); + painter->rotate(+90); + painter->restore(); aTextWidth = fm.width(aucText); - p.drawText(peakX + 5, peakY, aucText); + painter->drawText(peakX + 5, peakY, aucText); if (peakX - tTextHeight < fromXPix) beginXPix = (peakX - tTextHeight > 0) ? peakX - tTextHeight : 0; @@ -596,16 +592,15 @@ QRegion SignalDrawer::renderPeak(const PeakDrawData& pd, QPixmap* const target) endXPix = toXPix; peakReg = QRect(beginXPix, 0, endXPix - beginXPix + 1, m_height); - p.end(); if (peakReg.intersects(m_xAxisLabelRect)) { //qDebug() << "x inters"; - drawAxisLabel(SignalController::Axis::TIME, target); + drawAxisLabel(SignalController::Axis::TIME, painter); peakReg += m_xAxisLabelRect; } if (peakReg.intersects(m_yAxisLabelRect)) { //qDebug() << "y inters"; - drawAxisLabel(SignalController::Axis::VALUE, target); + drawAxisLabel(SignalController::Axis::VALUE, painter); peakReg += m_yAxisLabelRect; } diff --git a/signaldrawer.h b/signaldrawer.h index ad73713..39b98e6 100644 --- a/signaldrawer.h +++ b/signaldrawer.h @@ -60,14 +60,15 @@ public: Constraints currentConstraints(); int dheight() const { return m_height; } bool draw(const double fromX, const double fromY, const double toX, const double toY, const GraphLayers layers = GraphLayers::ALL); - bool drawAxisLabel(const SignalController::Axis axis, QPixmap* const target); - bool drawGraph(QPixmap* const target); - bool drawPeaks(QPixmap* const target); - bool drawScale(QPixmap* const target); + bool drawAxisLabel(const SignalController::Axis axis, QPainter* const target); + bool drawGraph(QPainter* const painter); + bool drawPeaks(QPainter* const painter); + bool drawScale(QPainter* const painter); QRegion erasePeak(const PeakDrawData& pd); int dwidth() const { return m_width; } - bool renderGraph(QPixmap* const fresh); - QRegion renderPeak(const PeakDrawData& pd, QPixmap* const target); + bool renderGraph(QPainter* const p); + QRegion renderPeak(const PeakDrawData& pd, QPixmap* const pixmap); + QRegion renderPeak(const PeakDrawData& pd, QPainter* const painter); bool setDimensions(const int width, const int height); protected: