From 0715fb8c638b53ac25590841fcbf3a1da3546b68 Mon Sep 17 00:00:00 2001 From: Soeren Apel Date: Tue, 9 Sep 2014 22:45:40 +0200 Subject: [PATCH] Use a generic approach when adding the "close on enter" hook for popups 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 | 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 97e05494..51af899e 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(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 7cf08b29..cce9f469 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 250376eb..7aa199c6 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(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 5cab4404..7726be25 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 9bdbd764..141985eb 100644 --- a/pv/widgets/popup.cpp +++ b/pv/widgets/popup.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #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(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())) { + + // 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 ec123048..2fdb23c0 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; -- 2.30.2