From: Joel Holdsworth Date: Sun, 30 Nov 2014 13:04:12 +0000 (+0000) Subject: Cursor: Moved paint_label into TimeMarker X-Git-Tag: pulseview-0.3.0~390 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=4fabd61a2676246265c12d614cde496bf0a79075;p=pulseview.git Cursor: Moved paint_label into TimeMarker --- diff --git a/pv/view/cursor.cpp b/pv/view/cursor.cpp index 07196799..d5b0a641 100644 --- a/pv/view/cursor.cpp +++ b/pv/view/cursor.cpp @@ -23,6 +23,7 @@ #include "view.hpp" #include "pv/util.hpp" +#include #include #include #include @@ -32,24 +33,15 @@ #include #include -#include - using std::shared_ptr; namespace pv { namespace view { -const QColor Cursor::LineColour(32, 74, 135); const QColor Cursor::FillColour(52, 101, 164); -const QColor Cursor::HighlightColour(83, 130, 186); -const QColor Cursor::TextColour(Qt::white); - -const int Cursor::Offset = 1; - -const int Cursor::ArrowSize = 4; Cursor::Cursor(View &view, double time) : - TimeMarker(view, LineColour, time) + TimeMarker(view, FillColour, time) { } @@ -60,11 +52,15 @@ QRectF Cursor::get_label_rect(const QRect &rect) const const float x = (time_ - view_.offset()) / view_.scale(); + QFontMetrics m(QApplication::font()); + QSize text_size = m.boundingRect( + pv::util::format_time(time_, view_.tick_prefix(), 2)).size(); + const QSizeF label_size( - text_size_.width() + View::LabelPadding.width() * 2, - text_size_.height() + View::LabelPadding.height() * 2); + text_size.width() + View::LabelPadding.width() * 2, + text_size.height() + View::LabelPadding.height() * 2); const float top = rect.height() - label_size.height() - - Cursor::Offset - Cursor::ArrowSize - 0.5f; + TimeMarker::Offset - TimeMarker::ArrowSize - 0.5f; const float height = label_size.height(); if (time_ > other->time()) @@ -74,82 +70,6 @@ QRectF Cursor::get_label_rect(const QRect &rect) const label_size.width(), height); } -void Cursor::paint_label(QPainter &p, const QRect &rect) -{ - const shared_ptr other(get_other_cursor()); - assert(other); - - const unsigned int prefix = view_.tick_prefix(); - - compute_text_size(p, prefix); - const QRectF r(get_label_rect(rect)); - - const QPointF left_points[] = { - r.topLeft(), - r.topRight(), - r.bottomRight(), - QPointF(r.left() + ArrowSize, r.bottom()), - QPointF(r.left(), rect.bottom()), - }; - - const QPointF right_points[] = { - r.topRight(), - r.topLeft(), - r.bottomLeft(), - QPointF(r.right() - ArrowSize, r.bottom()), - QPointF(r.right(), rect.bottom()), - }; - - const QPointF left_highlight_points[] = { - QPointF(r.left() + 1, r.top() + 1), - QPointF(r.right() - 1, r.top() + 1), - QPointF(r.right() - 1, r.bottom() - 1), - QPointF(r.left() + ArrowSize - 1, r.bottom() - 1), - QPointF(r.left() + 1, rect.bottom() - 1), - }; - - const QPointF right_highlight_points[] = { - QPointF(r.right() - 1, r.top() + 1), - QPointF(r.left() + 1, r.top() + 1), - QPointF(r.left() + 1, r.bottom() - 1), - QPointF(r.right() - ArrowSize + 1, r.bottom() - 1), - QPointF(r.right() - 1, rect.bottom() - 1), - }; - - const QPointF *const points = (time_ > other->time()) ? - left_points : right_points; - const QPointF *const highlight_points = (time_ > other->time()) ? - left_highlight_points : right_highlight_points; - - if (selected()) { - p.setPen(highlight_pen()); - p.setBrush(Qt::transparent); - p.drawPolygon(points, countof(left_points)); - } - - p.setPen(Qt::transparent); - p.setBrush(FillColour); - p.drawPolygon(points, countof(left_points)); - - p.setPen(HighlightColour); - p.setBrush(Qt::transparent); - p.drawPolygon(highlight_points, countof(left_highlight_points)); - - p.setPen(LineColour); - p.setBrush(Qt::transparent); - p.drawPolygon(points, countof(left_points)); - - p.setPen(TextColour); - p.drawText(r, Qt::AlignCenter | Qt::AlignVCenter, - pv::util::format_time(time_, prefix, 2)); -} - -void Cursor::compute_text_size(QPainter &p, unsigned int prefix) -{ - text_size_ = p.boundingRect(QRectF(), 0, - pv::util::format_time(time_, prefix, 2)).size(); -} - shared_ptr Cursor::get_other_cursor() const { const CursorPair &cursors = view_.cursors(); diff --git a/pv/view/cursor.hpp b/pv/view/cursor.hpp index 5df0ca2c..9f0adedd 100644 --- a/pv/view/cursor.hpp +++ b/pv/view/cursor.hpp @@ -37,14 +37,7 @@ class Cursor : public TimeMarker Q_OBJECT public: - static const QColor LineColour; static const QColor FillColour; - static const QColor HighlightColour; - static const QColor TextColour; - - static const int Offset; - - static const int ArrowSize; public: /** @@ -62,20 +55,8 @@ public: */ QRectF get_label_rect(const QRect &rect) const; - /** - * Paints the cursor's label to the ruler. - * @param p The painter to draw with. - * @param rect The rectangle of the ruler client area. - */ - void paint_label(QPainter &p, const QRect &rect); - private: - void compute_text_size(QPainter &p, unsigned int prefix); - std::shared_ptr get_other_cursor() const; - -private: - QSizeF text_size_; }; } // namespace view diff --git a/pv/view/cursorpair.cpp b/pv/view/cursorpair.cpp index 8aba0abd..12b7bc34 100644 --- a/pv/view/cursorpair.cpp +++ b/pv/view/cursorpair.cpp @@ -70,7 +70,7 @@ QRectF CursorPair::get_label_rect(const QRect &rect) const (float)rect.width() + height); return QRectF(left, rect.height() - label_size.height() - - Cursor::ArrowSize - Cursor::Offset - 0.5f, + TimeMarker::ArrowSize - TimeMarker::Offset - 0.5f, right - left, height); } @@ -92,14 +92,15 @@ void CursorPair::draw_markers(QPainter &p, const QRect &rect) const int highlight_radius = delta_rect.height() / 2 - 2; p.setBrush(Cursor::FillColour); - p.setPen(Cursor::LineColour); + p.setPen(Cursor::FillColour.darker()); p.drawRoundedRect(delta_rect, radius, radius); delta_rect.adjust(1, 1, -1, -1); - p.setPen(Cursor::HighlightColour); + p.setPen(Cursor::FillColour.lighter()); p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius); - p.setPen(Cursor::TextColour); + p.setPen(SelectableItem::select_text_colour( + Cursor::FillColour)); p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter, pv::util::format_time(second_->time() - first_->time(), prefix, 2)); } diff --git a/pv/view/timemarker.cpp b/pv/view/timemarker.cpp index 6571bea9..9d6e7fde 100644 --- a/pv/view/timemarker.cpp +++ b/pv/view/timemarker.cpp @@ -18,6 +18,10 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#include + +#include + #include "timemarker.hpp" #include "view.hpp" @@ -25,11 +29,18 @@ #include #include +#include #include +using std::max; +using std::min; + namespace pv { namespace view { +const int TimeMarker::ArrowSize = 4; +const int TimeMarker::Offset = 1; + TimeMarker::TimeMarker(View &view, const QColor &colour, double time) : view_(view), colour_(colour), @@ -71,10 +82,59 @@ void TimeMarker::set_time(double time) void TimeMarker::paint(QPainter &p, const QRect &rect) { const float x = get_x(); - p.setPen(colour_); + p.setPen(colour_.darker()); p.drawLine(QPointF(x, rect.top()), QPointF(x, rect.bottom())); } +void TimeMarker::paint_label(QPainter &p, const QRect &rect) +{ + const qreal x = (time_ - view_.offset()) / view_.scale(); + const QRectF r(get_label_rect(rect)); + + const QPointF points[] = { + r.topLeft(), + r.bottomLeft(), + QPointF(max(r.left(), x - ArrowSize), r.bottom()), + QPointF(x, rect.bottom()), + QPointF(min(r.right(), x + ArrowSize), r.bottom()), + r.bottomRight(), + r.topRight() + }; + + const QPointF highlight_points[] = { + QPointF(r.left() + 1, r.top() + 1), + QPointF(r.left() + 1, r.bottom() - 1), + QPointF(max(r.left() + 1, x - ArrowSize), r.bottom() - 1), + QPointF(min(max(r.left() + 1, x), r.right() - 1), + rect.bottom() - 1), + QPointF(min(r.right() - 1, x + ArrowSize), r.bottom() - 1), + QPointF(r.right() - 1, r.bottom() - 1), + QPointF(r.right() - 1, r.top() + 1), + }; + + if (selected()) { + p.setPen(highlight_pen()); + p.setBrush(Qt::transparent); + p.drawPolygon(points, countof(points)); + } + + p.setPen(Qt::transparent); + p.setBrush(colour_); + p.drawPolygon(points, countof(points)); + + p.setPen(colour_.lighter()); + p.setBrush(Qt::transparent); + p.drawPolygon(highlight_points, countof(highlight_points)); + + p.setPen(colour_.darker()); + p.setBrush(Qt::transparent); + p.drawPolygon(points, countof(points)); + + p.setPen(select_text_colour(colour_)); + p.drawText(r, Qt::AlignCenter | Qt::AlignVCenter, + pv::util::format_time(time_, view_.tick_prefix(), 2)); +} + pv::widgets::Popup* TimeMarker::create_popup(QWidget *parent) { using pv::widgets::Popup; diff --git a/pv/view/timemarker.hpp b/pv/view/timemarker.hpp index 1c0703a4..21de2625 100644 --- a/pv/view/timemarker.hpp +++ b/pv/view/timemarker.hpp @@ -41,6 +41,10 @@ class TimeMarker : public SelectableItem { Q_OBJECT +public: + static const int ArrowSize; + static const int Offset; + protected: /** * Constructor. @@ -87,7 +91,7 @@ public: * @param p The painter to draw with. * @param rect The rectangle of the ruler client area. */ - virtual void paint_label(QPainter &p, const QRect &rect) = 0; + void paint_label(QPainter &p, const QRect &rect); pv::widgets::Popup* create_popup(QWidget *parent);