X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;ds=inline;f=pv%2Fview%2Fheader.cpp;h=8849ee3fed2beb4b7fe8e9ba1fcfd445412c41df;hb=4e4d72b22a81d1f2d8daee1093ac45264a0a1e2d;hp=5e978a70252565cf47f4a3bb95712707a813842d;hpb=68b21a71797051fb48ed272bc2a6b4893bdbf517;p=pulseview.git
diff --git a/pv/view/header.cpp b/pv/view/header.cpp
index 5e978a7..8849ee3 100644
--- a/pv/view/header.cpp
+++ b/pv/view/header.cpp
@@ -14,98 +14,88 @@
* 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
+ * along with this program; if not, see .
*/
-#include "header.h"
-#include "view.h"
+#include "header.hpp"
+#include "view.hpp"
-#include "signal.h"
-#include "../sigsession.h"
+#include "signal.hpp"
+#include "tracegroup.hpp"
#include
#include
+#include
+
#include
#include
#include
#include
#include
-#include
+#include
+#include
+using boost::make_filter_iterator;
+using std::dynamic_pointer_cast;
using std::max;
using std::make_pair;
+using std::min;
using std::pair;
using std::shared_ptr;
using std::stable_sort;
using std::vector;
namespace pv {
-namespace view {
+namespace views {
+namespace TraceView {
const int Header::Padding = 12;
const int Header::BaselineOffset = 5;
-Header::Header(View &parent) :
- MarginWidget(parent),
- _dragging(false)
+static bool item_selected(shared_ptr r)
{
- setFocusPolicy(Qt::ClickFocus);
- setMouseTracking(true);
-
- connect(&_view.session(), SIGNAL(signals_changed()),
- this, SLOT(on_signals_changed()));
-
- connect(&_view, SIGNAL(signals_moved()),
- this, SLOT(on_signals_moved()));
+ return r->selected();
+}
- // Trigger the initial event manually. The default device has signals
- // which were created before this object came into being
- on_signals_changed();
+Header::Header(View &parent) :
+ MarginWidget(parent)
+{
}
QSize Header::sizeHint() const
{
- int max_width = 0;
-
- const vector< shared_ptr > row_items(_view.child_items());
- for (shared_ptr r : row_items) {
- assert(r);
-
- if (r->enabled()) {
- max_width = max(max_width, (int)r->label_rect(0).width());
- }
- }
-
- return QSize(max_width + Padding + BaselineOffset, 0);
+ QRectF max_rect(-Padding, 0, Padding, 0);
+ const vector> items(
+ view_.list_by_type());
+ for (auto &i : items)
+ if (i->enabled())
+ max_rect = max_rect.united(i->label_rect(QRect()));
+ return QSize(max_rect.width() + Padding + BaselineOffset, 0);
}
-shared_ptr Header::get_mouse_over_row_item(const QPoint &pt)
+QSize Header::extended_size_hint() const
{
- const int w = width() - BaselineOffset;
- const vector< shared_ptr > row_items(_view.child_items());
-
- for (const shared_ptr r : row_items)
- {
- assert(r);
- if (r->enabled() && r->label_rect(w).contains(pt))
- return r;
- }
-
- return shared_ptr();
+ return sizeHint() + QSize(ViewItem::HighlightRadius, 0);
}
-void Header::clear_selection()
+vector< shared_ptr > Header::items()
{
- const vector< shared_ptr > row_items(_view.child_items());
- for (const shared_ptr r : row_items) {
- assert(r);
- r->select(false);
- }
+ const vector> items(
+ view_.list_by_type());
+ return vector< shared_ptr >(items.begin(), items.end());
+}
- update();
+shared_ptr Header::get_mouse_over_item(const QPoint &pt)
+{
+ const QRect r(0, 0, width() - BaselineOffset, height());
+ const vector> items(
+ view_.list_by_type());
+ for (auto i = items.rbegin(); i != items.rend(); i++)
+ if ((*i)->enabled() && (*i)->label_rect(r).contains(pt))
+ return *i;
+ return shared_ptr();
}
void Header::paintEvent(QPaintEvent*)
@@ -113,203 +103,121 @@ void Header::paintEvent(QPaintEvent*)
// The trace labels are not drawn with the arrows exactly on the
// left edge of the widget, because then the selection shadow
// would be clipped away.
- const int w = width() - BaselineOffset;
+ const QRect rect(0, 0, width() - BaselineOffset, height());
+
+ vector< shared_ptr > items(
+ view_.list_by_type());
- vector< shared_ptr > row_items(_view.child_items());
- stable_sort(row_items.begin(), row_items.end(),
+ stable_sort(items.begin(), items.end(),
[](const shared_ptr &a, const shared_ptr &b) {
- return a->v_offset() < b->v_offset(); });
+ return a->point(QRect()).y() < b->point(QRect()).y(); });
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
- const bool dragging = !_drag_row_items.empty();
- for (const shared_ptr r : row_items)
- {
+ for (const shared_ptr r : items) {
assert(r);
- const bool highlight = !dragging &&
- r->label_rect(w).contains(_mouse_point);
- r->paint_label(painter, w, highlight);
+ const bool highlight = !item_dragging_ &&
+ r->label_rect(rect).contains(mouse_point_);
+ r->paint_label(painter, rect, highlight);
}
painter.end();
}
-void Header::mousePressEvent(QMouseEvent *event)
-{
- assert(event);
-
- const vector< shared_ptr > row_items(_view.child_items());
-
- if (event->button() & Qt::LeftButton) {
- _mouse_down_point = event->pos();
-
- // Save the offsets of any signals which will be dragged
- for (const shared_ptr r : row_items)
- if (r->selected())
- _drag_row_items.push_back(
- make_pair(r, r->v_offset()));
- }
-
- // Select the signal if it has been clicked
- const shared_ptr mouse_over_row_item =
- get_mouse_over_row_item(event->pos());
- if (mouse_over_row_item) {
- if (mouse_over_row_item->selected())
- mouse_over_row_item->select(false);
- else {
- mouse_over_row_item->select(true);
-
- if (~QApplication::keyboardModifiers() &
- Qt::ControlModifier)
- _drag_row_items.clear();
-
- // Add the signal to the drag list
- if (event->button() & Qt::LeftButton)
- _drag_row_items.push_back(
- make_pair(mouse_over_row_item,
- mouse_over_row_item->v_offset()));
- }
- }
-
- if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
- // Unselect all other signals because the Ctrl is not
- // pressed
- for (const shared_ptr r : row_items)
- if (r != mouse_over_row_item)
- r->select(false);
- }
-
- selection_changed();
- update();
-}
-
-void Header::mouseReleaseEvent(QMouseEvent *event)
-{
- using pv::widgets::Popup;
-
- assert(event);
- if (event->button() == Qt::LeftButton) {
- if (_dragging)
- _view.normalize_layout();
- else
- {
- const shared_ptr mouse_over_row_item =
- get_mouse_over_row_item(event->pos());
- if (mouse_over_row_item) {
- const int w = width() - BaselineOffset;
- Popup *const p =
- mouse_over_row_item->create_popup(&_view);
- p->set_position(mapToGlobal(QPoint(w,
- mouse_over_row_item->get_y())),
- Popup::Right);
- p->show();
- }
- }
-
- _dragging = false;
- _drag_row_items.clear();
- }
-}
-
-void Header::mouseMoveEvent(QMouseEvent *event)
+void Header::contextMenuEvent(QContextMenuEvent *event)
{
- assert(event);
- _mouse_point = event->pos();
-
- if (!(event->buttons() & Qt::LeftButton))
+ const shared_ptr r = get_mouse_over_item(mouse_point_);
+ if (!r)
return;
- if ((event->pos() - _mouse_down_point).manhattanLength() <
- QApplication::startDragDistance())
- return;
+ QMenu *menu = r->create_context_menu(this);
+ if (!menu)
+ menu = new QMenu(this);
- // Move the signals if we are dragging
- if (!_drag_row_items.empty())
+ const vector< shared_ptr > items(
+ view_.list_by_type());
+ if (std::count_if(items.begin(), items.end(), item_selected) > 1)
{
- _dragging = true;
-
- const int delta = event->pos().y() - _mouse_down_point.y();
-
- for (auto i = _drag_row_items.begin();
- i != _drag_row_items.end(); i++) {
- const std::shared_ptr row_item((*i).first);
- if (row_item) {
- const int y = (*i).second + delta;
- const int y_snap =
- ((y + View::SignalSnapGridSize / 2) /
- View::SignalSnapGridSize) *
- View::SignalSnapGridSize;
- row_item->set_v_offset(y_snap);
-
- // Ensure the trace is selected
- row_item->select();
- }
-
- }
-
- signals_moved();
+ menu->addSeparator();
+
+ QAction *const group = new QAction(tr("Group"), this);
+ QList shortcuts;
+ shortcuts.append(QKeySequence(Qt::ControlModifier | Qt::Key_G));
+ group->setShortcuts(shortcuts);
+ connect(group, SIGNAL(triggered()), this, SLOT(on_group()));
+ menu->addAction(group);
}
- update();
+ menu->exec(event->globalPos());
}
-void Header::leaveEvent(QEvent*)
+void Header::keyPressEvent(QKeyEvent *event)
{
- _mouse_point = QPoint(-1, -1);
- update();
-}
-
-void Header::contextMenuEvent(QContextMenuEvent *event)
-{
- const shared_ptr r = get_mouse_over_row_item(_mouse_point);
-
- if (r)
- r->create_context_menu(this)->exec(event->globalPos());
-}
+ assert(event);
-void Header::keyPressEvent(QKeyEvent *e)
-{
- assert(e);
+ MarginWidget::keyPressEvent(event);
- switch (e->key())
- {
- case Qt::Key_Delete:
- {
- const vector< shared_ptr > row_items(_view.child_items());
- for (const shared_ptr r : row_items)
- if (r->selected())
- r->delete_pressed();
- break;
- }
- }
+ if (event->key() == Qt::Key_G && event->modifiers() == Qt::ControlModifier)
+ on_group();
+ else if (event->key() == Qt::Key_U && event->modifiers() == Qt::ControlModifier)
+ on_ungroup();
}
-void Header::on_signals_changed()
+void Header::on_group()
{
- const vector< shared_ptr > row_items(_view.child_items());
- for (shared_ptr r : row_items) {
- assert(r);
- connect(r.get(), SIGNAL(visibility_changed()),
- this, SLOT(on_trace_changed()));
- connect(r.get(), SIGNAL(text_changed()),
- this, SLOT(on_trace_changed()));
- connect(r.get(), SIGNAL(colour_changed()),
- this, SLOT(update()));
+ const vector< shared_ptr > items(
+ view_.list_by_type());
+ vector< shared_ptr > selected_items(
+ make_filter_iterator(item_selected, items.begin(), items.end()),
+ make_filter_iterator(item_selected, items.end(), items.end()));
+ stable_sort(selected_items.begin(), selected_items.end(),
+ [](const shared_ptr &a, const shared_ptr &b) {
+ return a->visual_v_offset() < b->visual_v_offset(); });
+
+ shared_ptr group(new TraceGroup());
+ shared_ptr mouse_down_item(
+ std::dynamic_pointer_cast(mouse_down_item_));
+ shared_ptr focus_item(
+ mouse_down_item ? mouse_down_item : selected_items.front());
+
+ assert(focus_item);
+ assert(focus_item->owner());
+ focus_item->owner()->add_child_item(group);
+
+ // Set the group v_offset here before reparenting
+ group->force_to_v_offset(focus_item->layout_v_offset() +
+ focus_item->v_extents().first);
+
+ for (size_t i = 0; i < selected_items.size(); i++) {
+ const shared_ptr &r = selected_items[i];
+ assert(r->owner());
+ r->owner()->remove_child_item(r);
+ group->add_child_item(r);
+
+ // Put the items at 1-pixel offsets, so that restack will
+ // stack them in the right order
+ r->set_layout_v_offset(i);
}
}
-void Header::on_signals_moved()
+void Header::on_ungroup()
{
- update();
-}
-
-void Header::on_trace_changed()
-{
- update();
- geometry_updated();
+ bool restart;
+ do {
+ restart = false;
+ const vector< shared_ptr > groups(
+ view_.list_by_type());
+ for (const shared_ptr tg : groups)
+ if (tg->selected()) {
+ tg->ungroup();
+ restart = true;
+ break;
+ }
+ } while (restart);
}
-} // namespace view
+} // namespace TraceView
+} // namespace views
} // namespace pv