#include "HoverInfo.h" #include "../base/Plot.h" namespace YSG { void HoverInfoRenderState::drawHover(QPainter* painter, AbsAxis* h, AbsAxis* v) { QPoint pos = hoverInfoPos; if(!renderAble) { auto that = dynamic_cast(this); renderAble = dynamic_cast(that->dPtr->qPtr); } QVector lines = renderAble->createHoverString(h, v, pos); QFontMetrics fm(hoverInfoFont); int width = std::numeric_limits::min(); for(QString& s : lines) width = qMax(width, fm.horizontalAdvance(s)); QRect r = QRect(pos.x() + hoverInfoLeft, pos.y() + hoverInfoTop, (int)width + hoverInfoRight + hoverInfoLeft, fm.height() * lines.size() + hoverInfoBottom); painter->fillRect(r, hoverInfoBrush); painter->setPen(hoverInfoPen); painter->setFont(hoverInfoFont); int lineHeight = fm.height(); for (int i = 0; i < lines.size(); ++i) { QRect lineRect(r.left(), r.top() + i * lineHeight, r.width(), lineHeight); painter->drawText(lineRect, Qt::AlignCenter, lines[i]); } } bool HoverInfoRenderState::hoverOK(RenderAble* able) const { auto tmp = dynamic_cast(able); return useHoverInfo && hoverInfoActive && tmp->hoverTest(hoverInfoPos); } QVector HoverInfoRenderAbleInterFace::createHoverString(AbsAxis* h, AbsAxis* v, QPoint pos) { double tick1 = h->pixelToCoord(pos.x(), SRC::Render); double tick2 = v->pixelToCoord(pos.y(), SRC::Render); QString line1 = h->getTickLabel(tick1, SRC::Render) + h->unitText(); QString line2 = v->getTickLabel(tick2, SRC::Render) + v->unitText(); return {line1, line2}; } }