]> sigrok.org Git - pulseview.git/commitdiff
Use a generic approach when adding the "close on enter" hook for popups
authorSoeren Apel <redacted>
Tue, 9 Sep 2014 20:45:40 +0000 (22:45 +0200)
committerUwe Hermann <redacted>
Mon, 15 Sep 2014 19:24:15 +0000 (21:24 +0200)
Implement a generic approach to the "close on enter" feature
in popups by installing the key event handler on the first editable
widget it finds in the popup.

pv/view/signal.cpp
pv/view/signal.h
pv/view/trace.cpp
pv/view/trace.h
pv/widgets/popup.cpp
pv/widgets/popup.h

index 97e05494cca05eac922a47a6bb0c1dd878472d93..51af899ee4d814d8703e92c33ebfe135142cd6ad 100644 (file)
@@ -118,9 +118,6 @@ void Signal::populate_popup_form(QWidget *parent, QFormLayout *form)
        connect(_name_widget, SIGNAL(editTextChanged(const QString&)),
                this, SLOT(on_text_changed(const QString&)));
 
-       // We want to close the popup when the Enter key was pressed.
-       _name_widget->installEventFilter(this);
-
        form->addRow(tr("Name"), _name_widget);
 
        add_colour_option(parent, form);
@@ -140,24 +137,6 @@ QMenu* Signal::create_context_menu(QWidget *parent)
        return menu;
 }
 
-bool Signal::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;
-}
-
 void Signal::delete_pressed()
 {
        on_disable();
index 7cf08b2913503c1e3d89c5f0aa78d3226ff8b96e..cce9f4694e237da6fd7abd8a99b8f2c3f566fc68 100644 (file)
@@ -75,8 +75,6 @@ public:
 
        void delete_pressed();
 
-       bool eventFilter(QObject *obj, QEvent *evt);
-
 private Q_SLOTS:
        void on_disable();
 
index 250376ebb8828d931300a8a1a9ba5fc96ca4b749..7aa199c6ec1ca89acd3cbff98c247f755ae5fe95 100644 (file)
@@ -219,24 +219,6 @@ 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;
@@ -288,17 +270,9 @@ void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
                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;
index 5cab44040051ba3bc2bb71a0c181ee24f27714c5..7726be255f05db2e0f50307881ffc4763bdfe93a 100644 (file)
@@ -148,8 +148,6 @@ public:
         */
        QRectF get_label_rect(int right);
 
-       bool eventFilter(QObject *obj, QEvent *evt);
-
 protected:
 
        /**
@@ -175,8 +173,6 @@ protected:
 
        virtual void populate_popup_form(QWidget *parent, QFormLayout *form);
 
-       void close_popup();
-
 private Q_SLOTS:
        void on_text_changed(const QString &text);
 
index 9bdbd76412e5e99cd355c9888b102c9386116692..141985eb556a2e1c87ca2e558f90acd9203d7330 100644 (file)
@@ -25,6 +25,7 @@
 #include <QtGui>
 #include <QApplication>
 #include <QDesktopWidget>
+#include <QLineEdit>
 
 #include "popup.h"
 
@@ -67,6 +68,45 @@ void Popup::set_position(const QPoint point, Position pos)
 
 }
 
+bool Popup::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) {
+                       this->close();
+                       return true;
+               }
+       }
+
+       return false;
+}
+
+void Popup::show()
+{
+       QLineEdit* le;
+
+       QWidget::show();
+
+       // We want to close the popup when the Enter key was
+       // pressed and the first editable widget had focus.
+       if ((le = this->findChild<QLineEdit*>())) {
+
+               // For combo boxes we need to hook into the parent of
+               // the line edit (i.e. the QComboBox). For edit boxes
+               // we hook into the widget directly.
+               if (le->parent()->metaObject()->className() ==
+                               this->metaObject()->className())
+                       le->installEventFilter(this);
+               else
+                       le->parent()->installEventFilter(this);
+       }
+}
+
 bool Popup::space_for_arrow() const
 {
        // Check if there is room for the arrow
index ec123048074ec9074d18b436e3667d9347de9ac3..2fdb23c0b3b0bbd16da05162ce95dfb3a1ddac96 100644 (file)
@@ -52,6 +52,10 @@ public:
 
        void set_position(const QPoint point, Position pos);
 
+       bool eventFilter(QObject *obj, QEvent *evt);
+
+       void show();
+
 private:
        bool space_for_arrow() const;