]> sigrok.org Git - pulseview.git/blobdiff - pv/view/tracegroup.cpp
RowItem: Pass rect into label_rect
[pulseview.git] / pv / view / tracegroup.cpp
index af756d49be6bea0a4877433223773d0b3bbc84c2..13ba025f9c1235ed01576fe736812e963c73ad79 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#include <extdef.h>
 #include <assert.h>
 
 #include <algorithm>
 
 #include <QMenu>
+#include <QPainter>
 
-#include "tracegroup.h"
+#include "tracegroup.hpp"
 
 using std::pair;
 using std::shared_ptr;
@@ -35,10 +37,12 @@ namespace view {
 
 const int TraceGroup::Padding = 8;
 const int TraceGroup::Width = 12;
+const int TraceGroup::LineThickness = 5;
+const QColor TraceGroup::LineColour(QColor(0x55, 0x57, 0x53));
 
 TraceGroup::~TraceGroup()
 {
-       _owner = nullptr;
+       owner_ = nullptr;
        clear_child_items();
 }
 
@@ -48,28 +52,28 @@ bool TraceGroup::enabled() const
                [](const shared_ptr<RowItem> &r) { return r->enabled(); });
 }
 
-pv::SigSession& TraceGroup::session()
+pv::Session& TraceGroup::session()
 {
-       assert(_owner);
-       return _owner->session();
+       assert(owner_);
+       return owner_->session();
 }
 
-const pv::SigSession& TraceGroup::session() const
+const pv::Session& TraceGroup::session() const
 {
-       assert(_owner);
-       return _owner->session();
+       assert(owner_);
+       return owner_->session();
 }
 
 pv::view::View* TraceGroup::view()
 {
-       assert(_owner);
-       return _owner->view();
+       assert(owner_);
+       return owner_->view();
 }
 
 const pv::view::View* TraceGroup::view() const
 {
-       assert(_owner);
-       return _owner->view();
+       assert(owner_);
+       return owner_->view();
 }
 
 pair<int, int> TraceGroup::v_extents() const
@@ -77,22 +81,46 @@ pair<int, int> TraceGroup::v_extents() const
        return RowItemOwner::v_extents();
 }
 
-void TraceGroup::paint_label(QPainter &p, int right, bool hover)
+void TraceGroup::paint_label(QPainter &p, const QRect &rect, bool hover)
 {
-       (void)p;
-       (void)right;
-       (void)hover;
+       const QRectF r = label_rect(rect).adjusted(
+               LineThickness / 2, LineThickness / 2,
+               -LineThickness / 2, -LineThickness / 2);
+
+       // Paint the label
+       const QPointF points[] = {
+               r.topRight(),
+               r.topLeft(),
+               r.bottomLeft(),
+               r.bottomRight()
+       };
+
+       if (selected()) {
+               const QPen pen(highlight_pen());
+               p.setPen(QPen(pen.brush(), pen.width() + LineThickness,
+                       Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin));
+               p.setBrush(Qt::transparent);
+               p.drawPolyline(points, countof(points));
+       }
+
+       p.setPen(QPen(QBrush(LineColour.darker()), LineThickness,
+               Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin));
+       p.drawPolyline(points, countof(points));
+       p.setPen(QPen(QBrush(hover ? LineColour.lighter() : LineColour),
+               LineThickness - 2, Qt::SolidLine, Qt::SquareCap,
+               Qt::RoundJoin));
+       p.drawPolyline(points, countof(points));
 }
 
-QRectF TraceGroup::label_rect(int right) const
+QRectF TraceGroup::label_rect(const QRectF &rect) const
 {
-       QRectF rect;
+       QRectF child_rect;
        for (const shared_ptr<RowItem> r : child_items())
-               if (r)
-                       rect = rect.united(r->label_rect(right));
+               if (r && r->enabled())
+                       child_rect = child_rect.united(r->label_rect(rect));
 
-       return QRectF(rect.x() - Width - Padding, rect.y(),
-               Width, rect.height());
+       return QRectF(child_rect.x() - Width - Padding, child_rect.y(),
+               Width, child_rect.height());
 }
 
 bool TraceGroup::pt_in_label_rect(int left, int right, const QPoint &point)
@@ -122,30 +150,78 @@ pv::widgets::Popup* TraceGroup::create_popup(QWidget *parent)
        return NULL;
 }
 
-int TraceGroup::owner_v_offset() const
+int TraceGroup::owner_visual_v_offset() const
 {
-       return v_offset() + _owner->owner_v_offset();
+       return owner_ ? visual_v_offset() + owner_->owner_visual_v_offset() : 0;
 }
 
-void TraceGroup::update_viewport()
+void TraceGroup::restack_items()
 {
-       if (_owner)
-               _owner->update_viewport();
+       vector< shared_ptr<RowItem> > items(
+               child_items().begin(), child_items().end());
+
+       // Sort by the centre line of the extents
+       stable_sort(items.begin(), items.end(),
+               [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
+                       const auto aext = a->v_extents();
+                       const auto bext = b->v_extents();
+                        return a->layout_v_offset() +
+                                       (aext.first + aext.second) / 2 <
+                               b->layout_v_offset() +
+                                       (bext.first + bext.second) / 2;
+               });
+
+       int total_offset = 0;
+       for (shared_ptr<RowItem> r : items) {
+               const pair<int, int> extents = r->v_extents();
+               if (extents.first == 0 && extents.second == 0)
+                       continue;
+
+               // We position disabled traces, so that they are close to the
+               // animation target positon should they be re-enabled
+               if (r->enabled())
+                       total_offset += -extents.first;
+
+               if (!r->dragging())
+                       r->set_layout_v_offset(total_offset);
+
+               if (r->enabled())
+                       total_offset += extents.second;
+       }
 }
 
-void TraceGroup::on_ungroup()
+unsigned int TraceGroup::depth() const
+{
+       return owner_ ? owner_->depth() + 1 : 0;
+}
+
+void TraceGroup::ungroup()
 {
        const vector< shared_ptr<RowItem> > items(
                child_items().begin(), child_items().end());
        clear_child_items();
 
-       for (shared_ptr<RowItem> r : items) {
-               _owner->add_child_item(r);
-               r->set_v_offset(r->v_offset() + v_offset());
-       }
+       for (shared_ptr<RowItem> r : items)
+               owner_->add_child_item(r);
 
-       _owner->remove_child_item(shared_from_this());
-       appearance_changed();
+       owner_->remove_child_item(shared_from_this());
+}
+
+void TraceGroup::on_ungroup()
+{
+       ungroup();
+}
+
+void TraceGroup::appearance_changed(bool label, bool content)
+{
+       if (owner_)
+               owner_->appearance_changed(label, content);
+}
+
+void TraceGroup::extents_changed(bool horz, bool vert)
+{
+       if (owner_)
+               owner_->extents_changed(horz, vert);
 }
 
 } // namespace view