]> sigrok.org Git - pulseview.git/blobdiff - pv/widgets/popup.cpp
Use a generic approach when adding the "close on enter" hook for popups
[pulseview.git] / pv / widgets / popup.cpp
index 73ac1d84bd81eb5d268bd48c82492c6d84afc935..141985eb556a2e1c87ca2e558f90acd9203d7330 100644 (file)
 #include <assert.h>
 
 #include <QtGui>
+#include <QApplication>
+#include <QDesktopWidget>
+#include <QLineEdit>
 
 #include "popup.h"
 
-using namespace std;
+using std::max;
+using std::min;
 
 namespace pv {
 namespace widgets {
@@ -64,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