* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include <QApplication>
#include <QMenu>
#include <QMouseEvent>
namespace view {
MarginWidget::MarginWidget(View &parent) :
- ViewWidget(parent),
- dragging_(false)
+ ViewWidget(parent)
{
setAttribute(Qt::WA_NoSystemBackground, true);
- setFocusPolicy(Qt::ClickFocus);
- setMouseTracking(true);
+}
+
+void MarginWidget::item_clicked(const shared_ptr<ViewItem> &item)
+{
+ if (item && item->enabled())
+ show_popup(item);
}
void MarginWidget::show_popup(const shared_ptr<ViewItem> &item)
p->show();
}
-void MarginWidget::mouse_left_press_event(QMouseEvent *event)
-{
- (void)event;
-
- const bool ctrl_pressed =
- QApplication::keyboardModifiers() & Qt::ControlModifier;
-
- // Clear selection if control is not pressed and this item is unselected
- if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
- !ctrl_pressed)
- clear_selection();
-
- // Set the signal selection state if the item has been clicked
- if (mouse_down_item_) {
- if (ctrl_pressed)
- mouse_down_item_->select(!mouse_down_item_->selected());
- else
- mouse_down_item_->select(true);
- }
-
- // Save the offsets of any signals which will be dragged
- const auto items = this->items();
- for (auto &i : items)
- if (i->selected())
- i->drag();
-
- selection_changed();
- update();
-}
-
-void MarginWidget::mouse_left_release_event(QMouseEvent *event)
-{
- assert(event);
-
- auto items = this->items();
- const bool ctrl_pressed =
- QApplication::keyboardModifiers() & Qt::ControlModifier;
-
- // Unselect everything if control is not pressed
- const shared_ptr<ViewItem> mouse_over =
- get_mouse_over_item(event->pos());
-
- for (auto &i : items)
- i->drag_release();
-
- if (dragging_)
- view_.restack_all_row_items();
- else
- {
- if (!ctrl_pressed) {
- for (shared_ptr<ViewItem> i : items)
- if (mouse_down_item_ != i)
- i->select(false);
-
- if (mouse_down_item_)
- show_popup(mouse_down_item_);
- }
- }
-
- dragging_ = false;
-}
-
-void MarginWidget::mousePressEvent(QMouseEvent *event)
-{
- assert(event);
-
- mouse_down_point_ = event->pos();
- mouse_down_item_ = get_mouse_over_item(event->pos());
-
- if (event->button() & Qt::LeftButton)
- mouse_left_press_event(event);
-}
-
-void MarginWidget::mouseReleaseEvent(QMouseEvent *event)
-{
- assert(event);
- if (event->button() & Qt::LeftButton)
- mouse_left_release_event(event);
-
- mouse_down_item_ = nullptr;
-}
-
-void MarginWidget::mouseMoveEvent(QMouseEvent *event)
-{
- assert(event);
- mouse_point_ = event->pos();
-
- if (!(event->buttons() & Qt::LeftButton))
- return;
-
- if ((event->pos() - mouse_down_point_).manhattanLength() <
- QApplication::startDragDistance())
- return;
-
- if (!accept_drag())
- return;
-
- // Do the drag
- dragging_ = true;
- drag_items(event->pos() - mouse_down_point_);
-
- update();
-}
-
-void MarginWidget::leaveEvent(QEvent*)
-{
- mouse_point_ = QPoint(-1, -1);
- update();
-}
-
void MarginWidget::contextMenuEvent(QContextMenuEvent *event)
{
const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_);
}
}
-void MarginWidget::clear_selection()
-{
- const auto items = this->items();
- for (auto &i : items)
- i->select(false);
- update();
-}
-
} // namespace view
} // namespace pv
protected:
/**
- * Gets the items in the margin widget.
+ * Indicates the event an a view item has been clicked.
+ * @param item the view item that has been clicked.
*/
- virtual std::vector< std::shared_ptr<pv::view::ViewItem> > items() = 0;
-
- /**
- * Gets the first view item which has a label that contains @c pt .
- * @param pt the point to search with.
- * @return the view item that has been found, or and empty
- * @c shared_ptr if no item was found.
- */
- virtual std::shared_ptr<pv::view::ViewItem> get_mouse_over_item(
- const QPoint &pt) = 0;
+ virtual void item_clicked(
+ const std::shared_ptr<pv::view::ViewItem> &item);
/**
* Shows the popup of a the specified @c ViewItem .
*/
void show_popup(const std::shared_ptr<ViewItem> &item);
- /**
- * Handles left mouse button press events.
- * @param event the mouse event that triggered this handler.
- */
- void mouse_left_press_event(QMouseEvent *event);
-
- /**
- * Handles left mouse button release events.
- * @param event the mouse event that triggered this handler.
- */
- void mouse_left_release_event(QMouseEvent *event);
-
protected:
- void mousePressEvent(QMouseEvent * event);
- void mouseReleaseEvent(QMouseEvent *event);
- void mouseMoveEvent(QMouseEvent *event);
-
- void leaveEvent(QEvent *event);
-
virtual void contextMenuEvent(QContextMenuEvent *event);
virtual void keyPressEvent(QKeyEvent *e);
-
-public Q_SLOTS:
- void clear_selection();
-
-Q_SIGNALS:
- void selection_changed();
-
-protected:
- QPoint mouse_point_;
- QPoint mouse_down_point_;
- std::shared_ptr<ViewItem> mouse_down_item_;
- bool dragging_;
};
} // namespace view
#include <QMouseEvent>
using std::abs;
+using std::back_inserter;
+using std::copy;
using std::max;
using std::min;
using std::none_of;
setBackgroundRole(QPalette::Base);
}
+shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
+{
+ const vector< shared_ptr<ViewItem> > items(this->items());
+ for (auto i = items.rbegin(); i != items.rend(); i++)
+ if ((*i)->enabled() &&
+ (*i)->hit_box_rect(rect()).contains(pt))
+ return *i;
+ return nullptr;
+}
+
+vector< shared_ptr<ViewItem> > Viewport::items()
+{
+ vector< shared_ptr<ViewItem> > items(view_.begin(), view_.end());
+ const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
+ copy(time_items.begin(), time_items.end(), back_inserter(items));
+ return items;
+}
+
void Viewport::paintEvent(QPaintEvent*)
{
vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
public:
explicit Viewport(View &parent);
-protected:
+private:
+ /**
+ * Gets the first view item which has a hit-box that contains @c pt .
+ * @param pt the point to search with.
+ * @return the view item that has been found, or and empty
+ * @c shared_ptr if no item was found.
+ */
+ std::shared_ptr<pv::view::ViewItem> get_mouse_over_item(
+ const QPoint &pt);
+
+ /**
+ * Gets the items in the view widget.
+ */
+ std::vector< std::shared_ptr<pv::view::ViewItem> > items();
+
void paintEvent(QPaintEvent *event);
-private:
bool event(QEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include <algorithm>
+#include <QApplication>
+#include <QMouseEvent>
#include "rowitem.hpp"
-#include "timeitem.hpp"
#include "view.hpp"
#include "viewwidget.hpp"
ViewWidget::ViewWidget(View &parent) :
QWidget(&parent),
- view_(parent)
+ view_(parent),
+ dragging_(false)
{
+ setFocusPolicy(Qt::ClickFocus);
+ setMouseTracking(true);
+}
+
+void ViewWidget::clear_selection()
+{
+ const auto items = this->items();
+ for (auto &i : items)
+ i->select(false);
+ update();
+}
+
+void ViewWidget::item_clicked(const shared_ptr<ViewItem> &item)
+{
+ (void)item;
}
bool ViewWidget::accept_drag() const
i->drag_by(delta);
}
+void ViewWidget::mouse_left_press_event(QMouseEvent *event)
+{
+ (void)event;
+
+ const bool ctrl_pressed =
+ QApplication::keyboardModifiers() & Qt::ControlModifier;
+
+ // Clear selection if control is not pressed and this item is unselected
+ if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
+ !ctrl_pressed)
+ clear_selection();
+
+ // Set the signal selection state if the item has been clicked
+ if (mouse_down_item_) {
+ if (ctrl_pressed)
+ mouse_down_item_->select(!mouse_down_item_->selected());
+ else
+ mouse_down_item_->select(true);
+ }
+
+ // Save the offsets of any signals which will be dragged
+ const auto items = this->items();
+ for (auto &i : items)
+ if (i->selected())
+ i->drag();
+
+ selection_changed();
+ update();
+}
+
+void ViewWidget::mouse_left_release_event(QMouseEvent *event)
+{
+ assert(event);
+
+ auto items = this->items();
+ const bool ctrl_pressed =
+ QApplication::keyboardModifiers() & Qt::ControlModifier;
+
+ // Unselect everything if control is not pressed
+ const shared_ptr<ViewItem> mouse_over =
+ get_mouse_over_item(event->pos());
+
+ for (auto &i : items)
+ i->drag_release();
+
+ if (dragging_)
+ view_.restack_all_row_items();
+ else
+ {
+ if (!ctrl_pressed) {
+ for (shared_ptr<ViewItem> i : items)
+ if (mouse_down_item_ != i)
+ i->select(false);
+
+ if (mouse_down_item_)
+ item_clicked(mouse_down_item_);
+ }
+ }
+
+ dragging_ = false;
+}
+
+void ViewWidget::mousePressEvent(QMouseEvent *event)
+{
+ assert(event);
+
+ mouse_down_point_ = event->pos();
+ mouse_down_item_ = get_mouse_over_item(event->pos());
+
+ if (event->button() & Qt::LeftButton)
+ mouse_left_press_event(event);
+}
+
+void ViewWidget::mouseReleaseEvent(QMouseEvent *event)
+{
+ assert(event);
+ if (event->button() & Qt::LeftButton)
+ mouse_left_release_event(event);
+
+ mouse_down_item_ = nullptr;
+}
+
+void ViewWidget::mouseMoveEvent(QMouseEvent *event)
+{
+ assert(event);
+ mouse_point_ = event->pos();
+
+ if (!(event->buttons() & Qt::LeftButton))
+ return;
+
+ if ((event->pos() - mouse_down_point_).manhattanLength() <
+ QApplication::startDragDistance())
+ return;
+
+ if (!accept_drag())
+ return;
+
+ // Do the drag
+ dragging_ = true;
+ drag_items(event->pos() - mouse_down_point_);
+
+ update();
+}
+
+void ViewWidget::leaveEvent(QEvent*)
+{
+ mouse_point_ = QPoint(-1, -1);
+ update();
+}
+
} // namespace view
} // namespace pv
#ifndef PULSEVIEW_PV_VIEWWIDGET_H
#define PULSEVIEW_PV_VIEWWIDGET_H
+#include <memory>
+
#include <QWidget>
namespace pv {
namespace view {
class View;
+class ViewItem;
class ViewWidget : public QWidget
{
protected:
ViewWidget(View &parent);
+ /**
+ * Indicates the event an a view item has been clicked.
+ * @param item the view item that has been clicked.
+ * @remarks the default implementation does nothing.
+ */
+ virtual void item_clicked(
+ const std::shared_ptr<pv::view::ViewItem> &item);
+
/**
* Returns true if the selection of row items allows dragging.
* @return Returns true if the drag is acceptable.
*/
void drag_items(const QPoint &delta);
+ /**
+ * Gets the items in the view widget.
+ */
+ virtual std::vector< std::shared_ptr<pv::view::ViewItem> > items() = 0;
+
+ /**
+ * Gets the first view item which has a hit-box that contains @c pt .
+ * @param pt the point to search with.
+ * @return the view item that has been found, or and empty
+ * @c shared_ptr if no item was found.
+ */
+ virtual std::shared_ptr<pv::view::ViewItem> get_mouse_over_item(
+ const QPoint &pt) = 0;
+
+ /**
+ * Handles left mouse button press events.
+ * @param event the mouse event that triggered this handler.
+ */
+ void mouse_left_press_event(QMouseEvent *event);
+
+ /**
+ * Handles left mouse button release events.
+ * @param event the mouse event that triggered this handler.
+ */
+ void mouse_left_release_event(QMouseEvent *event);
+
+protected:
+ void mousePressEvent(QMouseEvent * event);
+ void mouseReleaseEvent(QMouseEvent *event);
+ void mouseMoveEvent(QMouseEvent *event);
+
+ void leaveEvent(QEvent *event);
+
+public Q_SLOTS:
+ void clear_selection();
+
+Q_SIGNALS:
+ void selection_changed();
+
protected:
pv::view::View &view_;
+ QPoint mouse_point_;
+ QPoint mouse_down_point_;
+ std::shared_ptr<ViewItem> mouse_down_item_;
+ bool dragging_;
};
} // namespace view