]> sigrok.org Git - pulseview.git/commitdiff
Cursor: Moved paint_label into TimeMarker
authorJoel Holdsworth <redacted>
Sun, 30 Nov 2014 13:04:12 +0000 (13:04 +0000)
committerUwe Hermann <redacted>
Wed, 10 Dec 2014 17:06:15 +0000 (18:06 +0100)
pv/view/cursor.cpp
pv/view/cursor.hpp
pv/view/cursorpair.cpp
pv/view/timemarker.cpp
pv/view/timemarker.hpp

index 071967998eac0047d894bca3e8edf712af736878..d5b0a641f075a5b9c5d7f4ddd0cb511102aee08a 100644 (file)
@@ -23,6 +23,7 @@
 #include "view.hpp"
 #include "pv/util.hpp"
 
+#include <QApplication>
 #include <QBrush>
 #include <QPainter>
 #include <QPointF>
 #include <cassert>
 #include <cstdio>
 
-#include <extdef.h>
-
 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<Cursor> 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> Cursor::get_other_cursor() const
 {
        const CursorPair &cursors = view_.cursors();
index 5df0ca2c3e4b34e176717a1175c0f6febdcec560..9f0adeddbd8eb0bdd20e16ded2fd16d6036533cc 100644 (file)
@@ -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<Cursor> get_other_cursor() const;
-
-private:
-       QSizeF text_size_;
 };
 
 } // namespace view
index 8aba0abd1274a7fc778d89a70d8df06fe67347a3..12b7bc341160b90f391b2c8d3df3056e3696a67f 100644 (file)
@@ -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));
        }
index 6571bea95c0eac9e6c98c3fca0f27bb9511a5c79..9d6e7fde6b126e547af4184b27f1542ed6faaf2e 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#include <algorithm>
+
+#include <extdef.h>
+
 #include "timemarker.hpp"
 
 #include "view.hpp"
 #include <QFormLayout>
 #include <QPainter>
 
+#include <pv/util.hpp>
 #include <pv/widgets/popup.hpp>
 
+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;
index 1c0703a43adf2fc2a409fcd3a1a72692591cdebd..21de26258f4b4153ce4e970236ded0e2511360e7 100644 (file)
@@ -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);