From: Soeren Apel <[email protected]>

This patch partly reverts two patches I submitted before: [1] [2]
Instead, it implements 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.

[1] http://sigrok.org/gitweb/?p=pulseview.git;a=commit;h=3c10012
[2] http://sigrok.org/gitweb/?p=pulseview.git;a=commit;h=0891e69

---
 pv/view/signal.cpp   |   21 ---------------------
 pv/view/signal.h     |    2 --
 pv/view/trace.cpp    |   26 --------------------------
 pv/view/trace.h      |    4 ----
 pv/widgets/popup.cpp |   40 ++++++++++++++++++++++++++++++++++++++++
 pv/widgets/popup.h   |    4 ++++
 6 files changed, 44 insertions(+), 53 deletions(-)

diff --git a/pv/view/signal.cpp b/pv/view/signal.cpp
index 97e0549..51af899 100644
--- a/pv/view/signal.cpp
+++ b/pv/view/signal.cpp
@@ -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();
diff --git a/pv/view/signal.h b/pv/view/signal.h
index 7cf08b2..cce9f46 100644
--- a/pv/view/signal.h
+++ b/pv/view/signal.h
@@ -75,8 +75,6 @@ public:
 
        void delete_pressed();
 
-       bool eventFilter(QObject *obj, QEvent *evt);
-
 private Q_SLOTS:
        void on_disable();
 
diff --git a/pv/view/trace.cpp b/pv/view/trace.cpp
index 250376e..7aa199c 100644
--- a/pv/view/trace.cpp
+++ b/pv/view/trace.cpp
@@ -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;
diff --git a/pv/view/trace.h b/pv/view/trace.h
index 5cab440..7726be2 100644
--- a/pv/view/trace.h
+++ b/pv/view/trace.h
@@ -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);
 
diff --git a/pv/widgets/popup.cpp b/pv/widgets/popup.cpp
index 9bdbd76..141985e 100644
--- a/pv/widgets/popup.cpp
+++ b/pv/widgets/popup.cpp
@@ -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
diff --git a/pv/widgets/popup.h b/pv/widgets/popup.h
index ec12304..2fdb23c 100644
--- a/pv/widgets/popup.h
+++ b/pv/widgets/popup.h
@@ -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;
 
-- 
1.7.8.6


------------------------------------------------------------------------------
Want excitement?
Manually upgrade your production database.
When you want reliability, choose Perforce.
Perforce version control. Predictably reliable.
http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
_______________________________________________
sigrok-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sigrok-devel

Reply via email to