#include <QDebug>
const QString SignalDrawer::ME_SENDER_STR("SignalDrawer");
+const int SignalDrawer::AXIS_LABEL_BORDER_OFFSET(2);
+const int SignalDrawer::AXIS_LABEL_SCALE_OFFSET(2);
const int SignalDrawer::SCALE_MARGIN_TIME(16);
const int SignalDrawer::SCALE_MARGIN_VALUE(48);
m_relXMax(constraints.toX),
m_relXMin(constraints.fromX),
m_relYMax(constraints.toY),
- m_relYMin(constraints.fromY)
+ m_relYMin(constraints.fromY),
+ m_axisLabelFont(QFont("arial", 10)),
+ m_axisLabelFM(QFontMetrics(m_axisLabelFont)),
+ m_xAxisLabelText("[" + QString::fromStdString(controller->signal()->xunitToString()) + "]"),
+ m_yAxisLabelText("[" + QString::fromStdString(controller->signal()->yunitToString()) + "]")
{
}
bool ret;
setNewRelativeConstraints(fromX, fromY, toX, toY);
+ 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(SCALE_MARGIN_VALUE + AXIS_LABEL_SCALE_OFFSET, m_axisLabelFM.height() - m_axisLabelFM.underlinePos());
fresh = createFreshPixmap();
if (fresh == nullptr)
ret = drawGraph(fresh);
if (!ret) {
Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " cannot draw graph");
- restoreRelativeConstraints();
- delete fresh;
- return ret;
+ goto error_out;
}
}
/* Draw the scale */
if (flags(GraphLayers::SCALE & layers)) {
- ret = renderScale(SignalController::Axis::VALUE, fresh);
- ret = renderScale(SignalController::Axis::TIME, fresh);
+ ret = drawScale(SignalController::Axis::VALUE, fresh);
+ if (!ret) {
+ Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " cannot draw scale for Y axis");
+ goto error_out;
+ }
+ ret = drawScale(SignalController::Axis::TIME, fresh);
+ if (!ret) {
+ Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " cannot draw scale for X axis");
+ goto error_out;
+ }
}
/* Draw integrated peaks */
ret = drawPeaks(fresh);
if (!ret) {
Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " cannot draw integrated peaks");
- restoreRelativeConstraints();
- delete fresh;
- return ret;
+ goto error_out;
}
}
+
+ /* Draw axes labels */
+ ret = drawAxisLabel(SignalController::Axis::TIME, fresh);
+ 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);
+ if (!ret) {
+ Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " cannot draw Y axis label");
+ goto error_out;
+ }
+
renderFresh(fresh);
return true;
+
+error_out:
+ restoreRelativeConstraints();
+ delete fresh;
+ return ret;
}
bool SignalDrawer::setDimensions(const int width, const int height)
/** Protected methods **/
-void SignalDrawer::copyPixmapRegion(const QRect& rect, QPixmap* const source, QPixmap* const target)
+void SignalDrawer::copyPixmapRegion(const QRegion& reg, QPixmap* const source, QPixmap* const target)
{
QPainter p(target);
- p.drawPixmap(rect, *source, rect);
+ for (const QRect& rect : reg.rects())
+ p.drawPixmap(rect, *source, rect);
}
bool SignalDrawer::drawGraph(QPixmap* const target)
return renderGraph(target);
}
+bool SignalDrawer::drawAxisLabel(const SignalController::Axis axis, QPixmap* const target)
+{
+ QPainter p;
+ int xPix, yPix;
+ QString text;
+
+ if (target == nullptr) {
+ Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " null pointer to pixmap");
+ return false;
+ }
+
+ switch (axis) {
+ case SignalController::Axis::TIME:
+ xPix = m_xAxisLabelRect.x();
+ yPix = m_xAxisLabelRect.y();
+ text = m_xAxisLabelText;
+ break;
+ case SignalController::Axis::VALUE:
+ xPix = m_yAxisLabelRect.x();
+ yPix = m_yAxisLabelRect.y();
+ text = m_yAxisLabelText;
+ break;
+ }
+
+ p.begin(target);
+ p.setFont(m_axisLabelFont);
+ p.drawText(xPix, yPix, text);
+ p.end();
+
+ return true;
+}
+
bool SignalDrawer::drawPeaks(QPixmap* const target)
{
std::vector<PeakDrawData> pdData = m_controller->getPeaksDrawData(m_relXMin, m_relYMin, m_relXMax, m_relYMax);
return true;
}
-bool SignalDrawer::renderScale(const SignalController::Axis axis, QPixmap* const target)
+bool SignalDrawer::drawScale(const SignalController::Axis axis, QPixmap* const target)
{
QPainter p;
double absVal;
}
}
-QRect SignalDrawer::erasePeak(const PeakDrawData& pd)
+QRegion SignalDrawer::erasePeak(const PeakDrawData& pd)
{
- QPainter p;
+ //QPainter p;
QFont font("arial", 10);
QFontMetrics fm(font);
QString aucText, timeText;
+ QRegion peakReg;
int tTextHeight, tTextWidth, aTextWidth;
int fromXPix, fromYPix;
int toXPix, toYPix;
else
endXPix = toXPix;
- return QRect(beginXPix, 0, endXPix - beginXPix, m_height);
+ peakReg = QRect(beginXPix, 0, endXPix - beginXPix, m_height);
+ if (peakReg.intersects(m_xAxisLabelRect)) {
+ peakReg += m_xAxisLabelRect;
+ }
+ if (peakReg.intersects(m_yAxisLabelRect)) {
+ peakReg += m_yAxisLabelRect;
+ }
+
+ return peakReg;
}
bool SignalDrawer::renderGraph(QPixmap* const target)
}
-QRect SignalDrawer::renderPeak(const PeakDrawData& pd, QPixmap* const target)
+QRegion SignalDrawer::renderPeak(const PeakDrawData& pd, QPixmap* const target)
{
QPainter p;
QFont font("arial", 10);
QFontMetrics fm(font);
QString aucText, timeText;
+ QRegion peakReg;
int tTextHeight, tTextWidth, aTextWidth;
int fromXPix, fromYPix;
int toXPix, toYPix;
else
endXPix = toXPix;
+ peakReg = QRect(beginXPix, 0, endXPix - beginXPix, m_height);
+ p.end();
- return QRect(beginXPix, 0, endXPix - beginXPix, m_height);
+ if (peakReg.intersects(m_xAxisLabelRect)) {
+ qDebug() << "x inters";
+ drawAxisLabel(SignalController::Axis::TIME, target);
+ peakReg += m_xAxisLabelRect;
+ }
+ if (peakReg.intersects(m_yAxisLabelRect)) {
+ qDebug() << "y inters";
+ drawAxisLabel(SignalController::Axis::VALUE, target);
+ peakReg += m_yAxisLabelRect;
+ }
+
+ return peakReg;
}
/** Private functions **/
}
/** Private methods **/
-
void SignalDrawer::restoreRelativeConstraints()
{
m_relXMin = m_oldRelXMin;
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);
- QRect erasePeak(const PeakDrawData& pd);
+ bool drawScale(const SignalController::Axis axis, QPixmap* const target);
+ QRegion erasePeak(const PeakDrawData& pd);
int dwidth() const { return m_width; }
bool renderGraph(QPixmap* const fresh);
- QRect renderPeak(const PeakDrawData& pd, QPixmap* const target);
- bool renderScale(const SignalController::Axis axis, QPixmap* const target);
+ QRegion renderPeak(const PeakDrawData& pd, QPixmap* const target);
bool setDimensions(const int width, const int height);
protected:
double m_relYMin;
std::shared_ptr<GraphDrawData> m_gdData;
- void copyPixmapRegion(const QRect& region, QPixmap* const source, QPixmap* const target);
+ void copyPixmapRegion(const QRegion& region, QPixmap* const source, QPixmap* const target);
QPixmap* createFreshPixmap() { return new QPixmap(m_width, m_height); }
void renderFresh(QPixmap* const fresh);
void renderTimeScaleText(QPainter*const p, const double rel, const double value);
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 SCALE_MARGIN_TIME;
static const int SCALE_MARGIN_VALUE;
private:
+ QRect axisLabelBoundingRect(const SignalController::Axis axis);
double linesIntersection(const double k1, const double q1, const double k2, const double q2);
+ void restoreRelativeConstraints();
+ void setNewRelativeConstraints(const double fromX, const double fromY, const double toX, const double toY);
int m_height;
int m_width;
double m_oldRelYMax;
double m_oldRelYMin;
- void setNewRelativeConstraints(const double fromX, const double fromY, const double toX, const double toY);
- void restoreRelativeConstraints();
+ const QFont m_axisLabelFont;
+ const QFontMetrics m_axisLabelFM;
+ QRect m_xAxisLabelRect;
+ QRect m_yAxisLabelRect;
+ const QString m_xAxisLabelText;
+ const QString m_yAxisLabelText;
+
static const QString ME_SENDER_STR;