Make decoder popups close when pressing enter
[pulseview.git] / pv / view / trace.cpp
index ae8ee901a27316738434c6f8e64a29376a77496f..250376ebb8828d931300a8a1a9ba5fc96ca4b749 100644 (file)
@@ -25,6 +25,7 @@
 
 #include <QApplication>
 #include <QFormLayout>
+#include <QKeyEvent>
 #include <QLineEdit>
 
 #include "trace.h"
@@ -32,6 +33,7 @@
 #include "view.h"
 
 #include <pv/widgets/colourbutton.h>
+#include <pv/widgets/popup.h>
 
 namespace pv {
 namespace view {
@@ -39,8 +41,7 @@ namespace view {
 const QPen Trace::AxisPen(QColor(128, 128, 128, 64));
 const int Trace::LabelHitPadding = 2;
 
-Trace::Trace(pv::SigSession &session, QString name) :
-       _session(session),
+Trace::Trace(QString name) :
        _name(name),
        _v_offset(0),
        _popup(NULL),
@@ -165,7 +166,7 @@ bool Trace::pt_in_label_rect(int left, int right, const QPoint &point)
        (void)left;
 
        const QRectF label = get_label_rect(right);
-       return QRectF(
+       return enabled() && QRectF(
                QPointF(label.left() - LabelHitPadding,
                        label.top() - LabelHitPadding),
                QPointF(right, label.bottom() + LabelHitPadding)
@@ -218,6 +219,24 @@ QRectF Trace::get_label_rect(int right)
                label_size.width(), label_size.height());
 }
 
+bool Trace::eventFilter(QObject *obj, QEvent *evt)
+{
+       QKeyEvent *keyEvent;
+
+       (void)obj;
+
+       if (evt->type() == QEvent::KeyPress) {
+               keyEvent = static_cast<QKeyEvent*>(evt);
+               if (keyEvent->key() == Qt::Key_Enter ||
+                   keyEvent->key() == Qt::Key_Return) {
+                       close_popup();
+                       return true;
+               }
+       }
+
+       return false;
+}
+
 QColor Trace::get_text_colour() const
 {
        return (_colour.lightness() > 64) ? Qt::black : Qt::white;
@@ -263,13 +282,23 @@ void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
 {
        QLineEdit *const name_edit = new QLineEdit(parent);
        name_edit->setText(_name);
+       name_edit->selectAll();
+       name_edit->setFocus();
        connect(name_edit, SIGNAL(textChanged(const QString&)),
                this, SLOT(on_text_changed(const QString&)));
        form->addRow(tr("Name"), name_edit);
 
+       // We want to close the popup when the Enter key was pressed.
+       name_edit->installEventFilter(this);
+
        add_colour_option(parent, form);
 }
 
+void Trace::close_popup()
+{
+       _popup->close();
+}
+
 void Trace::on_popup_closed()
 {
        _popup = NULL;