From 819e2e95555b2b3c2190f24a3cfa82250d1f34e5 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 20 Dec 2014 09:26:30 +0000 Subject: [PATCH] Ruler: Recombined with CursorHeader --- CMakeLists.txt | 2 - pv/view/cursorheader.cpp | 178 --------------------------------------- pv/view/cursorheader.hpp | 77 ----------------- pv/view/header.cpp | 5 ++ pv/view/header.hpp | 7 ++ pv/view/marginwidget.hpp | 7 ++ pv/view/ruler.cpp | 156 +++++++++++++++++++++++++++++++--- pv/view/ruler.hpp | 35 ++++++++ pv/view/view.cpp | 29 +++---- pv/view/view.hpp | 1 - pv/view/viewitem.hpp | 2 +- test/CMakeLists.txt | 2 - 12 files changed, 210 insertions(+), 291 deletions(-) delete mode 100644 pv/view/cursorheader.cpp delete mode 100644 pv/view/cursorheader.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index af6ee357..546b99dc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -168,7 +168,6 @@ set(pulseview_SOURCES pv/toolbars/samplingbar.cpp pv/view/analogsignal.cpp pv/view/cursor.cpp - pv/view/cursorheader.cpp pv/view/cursorpair.cpp pv/view/flag.cpp pv/view/header.cpp @@ -214,7 +213,6 @@ set(pulseview_HEADERS pv/prop/binding/deviceoptions.hpp pv/toolbars/samplingbar.hpp pv/view/cursor.hpp - pv/view/cursorheader.hpp pv/view/flag.hpp pv/view/header.hpp pv/view/logicsignal.hpp diff --git a/pv/view/cursorheader.cpp b/pv/view/cursorheader.cpp deleted file mode 100644 index efc9205e..00000000 --- a/pv/view/cursorheader.cpp +++ /dev/null @@ -1,178 +0,0 @@ -/* - * This file is part of the PulseView project. - * - * Copyright (C) 2012 Joel Holdsworth - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include "cursorheader.hpp" - -#include "ruler.hpp" -#include "view.hpp" - -#include -#include -#include - -#include - -using std::shared_ptr; -using std::vector; - -namespace pv { -namespace view { - -const int CursorHeader::Padding = 20; -const int CursorHeader::BaselineOffset = 5; - -int CursorHeader::calculateTextHeight() -{ - QFontMetrics fm(font()); - return fm.boundingRect(0, 0, INT_MAX, INT_MAX, - Qt::AlignLeft | Qt::AlignTop, "8").height(); -} - -CursorHeader::CursorHeader(View &parent) : - MarginWidget(parent), - textHeight_(calculateTextHeight()) -{ -} - -QSize CursorHeader::sizeHint() const -{ - return QSize(0, textHeight_ + Padding + BaselineOffset); -} - -void CursorHeader::clear_selection() -{ - const vector< shared_ptr > items(view_.time_items()); - for (auto &i : items) - i->select(false); - update(); -} - -void CursorHeader::paintEvent(QPaintEvent*) -{ - QPainter p(this); - p.setRenderHint(QPainter::Antialiasing); - - // The cursor labels are not drawn with the arrows exactly on the - // bottom line of the widget, because then the selection shadow - // would be clipped away. - const QRect r = rect().adjusted(0, 0, 0, -BaselineOffset); - - // Draw the items - const vector< shared_ptr > items(view_.time_items()); - for (auto &m : items) - m->paint_label(p, r); -} - -void CursorHeader::mouseMoveEvent(QMouseEvent *e) -{ - mouse_point_ = e->pos(); - - if (!(e->buttons() & Qt::LeftButton)) - return; - - if ((e->pos() - mouse_down_point_).manhattanLength() < - QApplication::startDragDistance()) - return; - - // Do the drag - dragging_ = true; - - const int delta = e->pos().x() - mouse_down_point_.x(); - const vector< shared_ptr > items(view_.time_items()); - for (auto &i : items) - if (i->dragging()) - i->set_time(view_.offset() + - (i->drag_point().x() + delta - 0.5) * - view_.scale()); -} - -void CursorHeader::mousePressEvent(QMouseEvent *e) -{ - if (e->buttons() & Qt::LeftButton) { - mouse_down_point_ = e->pos(); - - mouse_down_item_.reset(); - - clear_selection(); - - const vector< shared_ptr > items(view_.time_items()); - for (auto i = items.rbegin(); i != items.rend(); i++) - if ((*i)->label_rect(rect()).contains(e->pos())) { - mouse_down_item_ = (*i); - break; - } - - if (mouse_down_item_) { - mouse_down_item_->select(); - mouse_down_item_->drag(); - } - - selection_changed(); - } -} - -void CursorHeader::mouseReleaseEvent(QMouseEvent *) -{ - using pv::widgets::Popup; - - if (!dragging_ && mouse_down_item_) { - Popup *const p = mouse_down_item_->create_popup(&view_); - if (p) { - const QPoint arrpos(mouse_down_item_->get_x(), - height() - BaselineOffset); - p->set_position(mapToGlobal(arrpos), Popup::Bottom); - p->show(); - } - } - - dragging_ = false; - mouse_down_item_.reset(); - - const vector< shared_ptr > items(view_.time_items()); - for (auto &i : items) - i->drag_release(); -} - -void CursorHeader::leaveEvent(QEvent*) -{ - mouse_point_ = QPoint(-1, -1); - update(); -} - -void CursorHeader::mouseDoubleClickEvent(QMouseEvent *e) -{ - view_.add_flag(view_.offset() + ((double)e->x() + 0.5) * view_.scale()); -} - -void CursorHeader::keyPressEvent(QKeyEvent *e) -{ - assert(e); - - if (e->key() == Qt::Key_Delete) - { - const vector< shared_ptr > items(view_.time_items()); - for (auto &i : items) - if (i->selected()) - i->delete_pressed(); - } -} - -} // namespace view -} // namespace pv diff --git a/pv/view/cursorheader.hpp b/pv/view/cursorheader.hpp deleted file mode 100644 index ff732856..00000000 --- a/pv/view/cursorheader.hpp +++ /dev/null @@ -1,77 +0,0 @@ -/* - * This file is part of the PulseView project. - * - * Copyright (C) 2012 Joel Holdsworth - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef PULSEVIEW_PV_VIEW_CURSORHEADER_H -#define PULSEVIEW_PV_VIEW_CURSORHEADER_H - -#include - -#include "marginwidget.hpp" - -namespace pv { -namespace view { - -class TimeItem; - -/** - * Widget to hold the labels over the cursors. - */ -class CursorHeader : public MarginWidget -{ - Q_OBJECT - - static const int Padding; - - /** - * The vertical offset, relative to the bottom line of the widget, - * where the arrows of the cursor labels end. - */ - static const int BaselineOffset; - -public: - CursorHeader(View &parent); - - QSize sizeHint() const; - - void clear_selection(); - -private: - void paintEvent(QPaintEvent *event); - - void mouseMoveEvent(QMouseEvent *e); - void mousePressEvent(QMouseEvent *e); - void mouseReleaseEvent(QMouseEvent *); - void leaveEvent(QEvent*); - - void mouseDoubleClickEvent(QMouseEvent *e); - - void keyPressEvent(QKeyEvent *e); - -private: - int calculateTextHeight(); - - std::shared_ptr mouse_down_item_; - const int textHeight_; -}; - -} // namespace view -} // namespace pv - -#endif // PULSEVIEW_PV_VIEW_CURSORHEADER_H diff --git a/pv/view/header.cpp b/pv/view/header.cpp index 4b02c194..82c7dd05 100644 --- a/pv/view/header.cpp +++ b/pv/view/header.cpp @@ -75,6 +75,11 @@ QSize Header::sizeHint() const return QSize(max_rect.width() + Padding + BaselineOffset, 0); } +QSize Header::extended_size_hint() const +{ + return sizeHint() + QSize(ViewItem::HighlightRadius, 0); +} + shared_ptr Header::get_mouse_over_row_item(const QPoint &pt) { const QRect r(0, 0, width() - BaselineOffset, height()); diff --git a/pv/view/header.hpp b/pv/view/header.hpp index ae39bc84..c5c8ae7a 100644 --- a/pv/view/header.hpp +++ b/pv/view/header.hpp @@ -45,6 +45,13 @@ public: QSize sizeHint() const; + /** + * The extended area that the header widget would like to be sized to. + * @remarks This area is the area specified by sizeHint, extended by + * the area to overlap the viewport. + */ + QSize extended_size_hint() const; + /** * The horizontal offset, relative to the left edge of the widget, * where the arrows of the trace labels end. diff --git a/pv/view/marginwidget.hpp b/pv/view/marginwidget.hpp index b4f0eb72..6e98e4b0 100644 --- a/pv/view/marginwidget.hpp +++ b/pv/view/marginwidget.hpp @@ -36,6 +36,13 @@ class MarginWidget : public QWidget public: MarginWidget(pv::view::View &parent); + /** + * The extended area that the margin widget would like to be sized to. + * @remarks This area is the area specified by sizeHint, extended by + * the area to overlap the viewport. + */ + virtual QSize extended_size_hint() const = 0; + public Q_SLOTS: virtual void clear_selection(); diff --git a/pv/view/ruler.cpp b/pv/view/ruler.cpp index ee521f0e..7018c212 100644 --- a/pv/view/ruler.cpp +++ b/pv/view/ruler.cpp @@ -18,15 +18,23 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "ruler.hpp" +#include +#include +#include +#include + +#include "ruler.hpp" #include "view.hpp" -#include "pv/util.hpp" -#include +#include +#include using namespace Qt; +using std::shared_ptr; +using std::vector; + namespace pv { namespace view { @@ -35,8 +43,12 @@ const int Ruler::MinorTickSubdivision = 4; const int Ruler::HoverArrowSize = 5; +const int Ruler::Padding = 20; +const int Ruler::BaselineOffset = 5; + Ruler::Ruler(View &parent) : - MarginWidget(parent) + MarginWidget(parent), + text_height_(calculate_text_height()) { setMouseTracking(true); @@ -44,11 +56,25 @@ Ruler::Ruler(View &parent) : this, SLOT(hover_point_changed())); } +void Ruler::clear_selection() +{ + const vector< shared_ptr > items(view_.time_items()); + for (auto &i : items) + i->select(false); + update(); +} + QSize Ruler::sizeHint() const { return QSize(0, RulerHeight); } +QSize Ruler::extended_size_hint() const +{ + return QSize(0, RulerHeight + + (text_height_ + Padding + BaselineOffset) / 2); +} + void Ruler::paintEvent(QPaintEvent*) { const int ValueMargin = 3; @@ -59,9 +85,6 @@ void Ruler::paintEvent(QPaintEvent*) const double tick_period = view_.tick_period(); const unsigned int prefix = view_.tick_prefix(); - const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX, - AlignLeft | AlignTop, "8").height(); - // Draw the tick marks p.setPen(palette().color(foregroundRole())); @@ -75,8 +98,8 @@ void Ruler::paintEvent(QPaintEvent*) int division = (int)round(first_minor_division - first_major_division * MinorTickSubdivision) - 1; - const int major_tick_y1 = text_height + ValueMargin * 2; - const int tick_y2 = height(); + const int major_tick_y1 = text_height_ + ValueMargin * 2; + const int tick_y2 = RulerHeight; const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2; double x; @@ -88,7 +111,7 @@ void Ruler::paintEvent(QPaintEvent*) if (division % MinorTickSubdivision == 0) { // Draw a major tick - p.drawText(x, ValueMargin, 0, text_height, + p.drawText(x, ValueMargin, 0, text_height_, AlignCenter | AlignTop | TextDontClip, pv::util::format_time(t, prefix)); p.drawLine(QPointF(x, major_tick_y1), @@ -107,6 +130,110 @@ void Ruler::paintEvent(QPaintEvent*) // Draw the hover mark draw_hover_mark(p); + + // The cursor labels are not drawn with the arrows exactly on the + // bottom line of the widget, because then the selection shadow + // would be clipped away. + const QRect r = rect().adjusted(0, 0, 0, -BaselineOffset); + + // Draw the items + const vector< shared_ptr > items(view_.time_items()); + for (auto &i : items) + i->paint_label(p, r); +} + +void Ruler::mouseMoveEvent(QMouseEvent *e) +{ + mouse_point_ = e->pos(); + + if (!(e->buttons() & Qt::LeftButton)) + return; + + if ((e->pos() - mouse_down_point_).manhattanLength() < + QApplication::startDragDistance()) + return; + + // Do the drag + dragging_ = true; + + const int delta = e->pos().x() - mouse_down_point_.x(); + const vector< shared_ptr > items(view_.time_items()); + for (auto &i : items) + if (i->dragging()) + i->set_time(view_.offset() + + (i->drag_point().x() + delta - 0.5) * + view_.scale()); +} + +void Ruler::mousePressEvent(QMouseEvent *e) +{ + if (e->buttons() & Qt::LeftButton) { + mouse_down_point_ = e->pos(); + + mouse_down_item_.reset(); + + clear_selection(); + + const vector< shared_ptr > items(view_.time_items()); + for (auto i = items.rbegin(); i != items.rend(); i++) + if ((*i)->label_rect(rect()).contains(e->pos())) { + mouse_down_item_ = (*i); + break; + } + + if (mouse_down_item_) { + mouse_down_item_->select(); + mouse_down_item_->drag(); + } + + selection_changed(); + } +} + +void Ruler::mouseReleaseEvent(QMouseEvent *) +{ + using pv::widgets::Popup; + + if (!dragging_ && mouse_down_item_) { + Popup *const p = mouse_down_item_->create_popup(&view_); + if (p) { + const QPoint arrpos(mouse_down_item_->get_x(), + height() - BaselineOffset); + p->set_position(mapToGlobal(arrpos), Popup::Bottom); + p->show(); + } + } + + dragging_ = false; + mouse_down_item_.reset(); + + const vector< shared_ptr > items(view_.time_items()); + for (auto &i : items) + i->drag_release(); +} + +void Ruler::leaveEvent(QEvent*) +{ + mouse_point_ = QPoint(-1, -1); + update(); +} + +void Ruler::mouseDoubleClickEvent(QMouseEvent *e) +{ + view_.add_flag(view_.offset() + ((double)e->x() + 0.5) * view_.scale()); +} + +void Ruler::keyPressEvent(QKeyEvent *e) +{ + assert(e); + + if (e->key() == Qt::Key_Delete) + { + const vector< shared_ptr > items(view_.time_items()); + for (auto &i : items) + if (i->selected()) + i->delete_pressed(); + } } void Ruler::draw_hover_mark(QPainter &p) @@ -119,7 +246,7 @@ void Ruler::draw_hover_mark(QPainter &p) p.setPen(QPen(Qt::NoPen)); p.setBrush(QBrush(palette().color(foregroundRole()))); - const int b = height() - 1; + const int b = RulerHeight; const QPointF points[] = { QPointF(x, b), QPointF(x - HoverArrowSize, b - HoverArrowSize), @@ -128,6 +255,13 @@ void Ruler::draw_hover_mark(QPainter &p) p.drawPolygon(points, countof(points)); } +int Ruler::calculate_text_height() +{ + QFontMetrics fm(font()); + return fm.boundingRect(0, 0, INT_MAX, INT_MAX, + Qt::AlignLeft | Qt::AlignTop, "8").height(); +} + void Ruler::hover_point_changed() { update(); diff --git a/pv/view/ruler.hpp b/pv/view/ruler.hpp index 073c86c9..e47c424a 100644 --- a/pv/view/ruler.hpp +++ b/pv/view/ruler.hpp @@ -28,6 +28,8 @@ namespace pv { namespace view { +class TimeItem; + class Ruler : public MarginWidget { Q_OBJECT @@ -38,21 +40,54 @@ private: static const int HoverArrowSize; + static const int Padding; + + /** + * The vertical offset, relative to the bottom line of the widget, + * where the arrows of the cursor labels end. + */ + static const int BaselineOffset; + public: Ruler(View &parent); +public: + void clear_selection(); + public: QSize sizeHint() const; + /** + * The extended area that the header widget would like to be sized to. + * @remarks This area is the area specified by sizeHint, extended by + * the area to overlap the viewport. + */ + QSize extended_size_hint() const; + private: void paintEvent(QPaintEvent *event); + void mouseMoveEvent(QMouseEvent *e); + void mousePressEvent(QMouseEvent *e); + void mouseReleaseEvent(QMouseEvent *); + void leaveEvent(QEvent*); + + void mouseDoubleClickEvent(QMouseEvent *e); + + void keyPressEvent(QKeyEvent *e); + private: /** * Draw a hover arrow under the cursor position. */ void draw_hover_mark(QPainter &p); + int calculate_text_height(); + +private: + std::shared_ptr mouse_down_item_; + const int text_height_; + private Q_SLOTS: void hover_point_changed(); }; diff --git a/pv/view/view.cpp b/pv/view/view.cpp index e4352257..9cc1487e 100644 --- a/pv/view/view.cpp +++ b/pv/view/view.cpp @@ -38,7 +38,6 @@ #include -#include "cursorheader.hpp" #include "decodetrace.hpp" #include "header.hpp" #include "logicsignal.hpp" @@ -94,7 +93,6 @@ View::View(Session &session, QWidget *parent) : session_(session), viewport_(new Viewport(*this)), ruler_(new Ruler(*this)), - cursorheader_(new CursorHeader(*this)), header_(new Header(*this)), scale_(1e-6), offset_(0), @@ -125,13 +123,13 @@ View::View(Session &session, QWidget *parent) : this, SLOT(on_signals_moved())); connect(header_, SIGNAL(selection_changed()), - cursorheader_, SLOT(clear_selection())); - connect(cursorheader_, SIGNAL(selection_changed()), + ruler_, SLOT(clear_selection())); + connect(ruler_, SIGNAL(selection_changed()), header_, SLOT(clear_selection())); connect(header_, SIGNAL(selection_changed()), this, SIGNAL(selection_changed())); - connect(cursorheader_, SIGNAL(selection_changed()), + connect(ruler_, SIGNAL(selection_changed()), this, SIGNAL(selection_changed())); connect(this, SIGNAL(hover_point_changed()), @@ -145,7 +143,6 @@ View::View(Session &session, QWidget *parent) : viewport_->installEventFilter(this); ruler_->installEventFilter(this); - cursorheader_->installEventFilter(this); header_->installEventFilter(this); // Trigger the initial event manually. The default device has signals @@ -153,7 +150,7 @@ View::View(Session &session, QWidget *parent) : signals_changed(); // make sure the transparent widgets are on the top - cursorheader_->raise(); + ruler_->raise(); header_->raise(); // Update the zoom state @@ -294,7 +291,6 @@ void View::set_scale_offset(double scale, double offset) update_scroll(); ruler_->update(); - cursorheader_->update(); viewport_->update(); scale_offset_changed(); } @@ -347,7 +343,7 @@ bool View::cursors_shown() const void View::show_cursors(bool show) { show_cursors_ = show; - cursorheader_->update(); + ruler_->update(); viewport_->update(); } @@ -356,7 +352,7 @@ void View::centre_cursors() const double time_width = scale_ * viewport_->width(); cursors_->first()->set_time(offset_ + time_width * 0.4); cursors_->second()->set_time(offset_ + time_width * 0.6); - cursorheader_->update(); + ruler_->update(); viewport_->update(); } @@ -518,13 +514,9 @@ void View::update_layout() header_->sizeHint().width() - pv::view::Header::BaselineOffset, ruler_->sizeHint().height(), 0, 0); ruler_->setGeometry(viewport_->x(), 0, - viewport_->width(), viewport_->y()); - cursorheader_->setGeometry( - viewport_->x(), - ruler_->sizeHint().height() - cursorheader_->sizeHint().height() / 2, - viewport_->width(), cursorheader_->sizeHint().height()); + viewport_->width(), ruler_->extended_size_hint().height()); header_->setGeometry(0, viewport_->y(), - header_->sizeHint().width(), viewport_->height()); + header_->extended_size_hint().width(), viewport_->height()); update_scroll(); } @@ -588,7 +580,7 @@ bool View::eventFilter(QObject *object, QEvent *event) const QMouseEvent *const mouse_event = (QMouseEvent*)event; if (object == viewport_) hover_point_ = mouse_event->pos(); - else if (object == ruler_ || object == cursorheader_) + else if (object == ruler_) hover_point_ = QPoint(mouse_event->x(), 0); else if (object == header_) hover_point_ = QPoint(0, mouse_event->y()); @@ -640,7 +632,7 @@ void View::row_item_appearance_changed(bool label, bool content) void View::time_item_appearance_changed(bool label, bool content) { if (label) - cursorheader_->update(); + ruler_->update(); if (content) viewport_->update(); } @@ -668,7 +660,6 @@ void View::h_scroll_value_changed(int value) } ruler_->update(); - cursorheader_->update(); viewport_->update(); } diff --git a/pv/view/view.hpp b/pv/view/view.hpp index 6b7841f9..344e252e 100644 --- a/pv/view/view.hpp +++ b/pv/view/view.hpp @@ -272,7 +272,6 @@ private: Viewport *viewport_; Ruler *ruler_; - CursorHeader *cursorheader_; Header *header_; /// The view time scale in seconds per pixel. diff --git a/pv/view/viewitem.hpp b/pv/view/viewitem.hpp index 4c3df154..12e1f44f 100644 --- a/pv/view/viewitem.hpp +++ b/pv/view/viewitem.hpp @@ -43,7 +43,7 @@ class ViewItem : public QObject { Q_OBJECT -private: +public: static const int HighlightRadius; public: diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 494f8dda..446c868b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -40,7 +40,6 @@ set(pulseview_TEST_SOURCES ${PROJECT_SOURCE_DIR}/pv/popups/channels.cpp ${PROJECT_SOURCE_DIR}/pv/view/analogsignal.cpp ${PROJECT_SOURCE_DIR}/pv/view/cursor.cpp - ${PROJECT_SOURCE_DIR}/pv/view/cursorheader.cpp ${PROJECT_SOURCE_DIR}/pv/view/cursorpair.cpp ${PROJECT_SOURCE_DIR}/pv/view/flag.cpp ${PROJECT_SOURCE_DIR}/pv/view/header.cpp @@ -84,7 +83,6 @@ set(pulseview_TEST_HEADERS ${PROJECT_SOURCE_DIR}/pv/prop/string.hpp ${PROJECT_SOURCE_DIR}/pv/prop/binding/deviceoptions.hpp ${PROJECT_SOURCE_DIR}/pv/view/cursor.hpp - ${PROJECT_SOURCE_DIR}/pv/view/cursorheader.hpp ${PROJECT_SOURCE_DIR}/pv/view/flag.hpp ${PROJECT_SOURCE_DIR}/pv/view/header.hpp ${PROJECT_SOURCE_DIR}/pv/view/logicsignal.hpp -- 2.30.2