itom  4.1.0
menuOnlyForEnter.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet Stuttgart, Germany
6 
7  This file is part of itom.
8 
9  itom is free software; you can redistribute it and/or modify it
10  under the terms of the GNU Library General Public Licence as published by
11  the Free Software Foundation; either version 2 of the Licence, or (at
12  your option) any later version.
13 
14  itom is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17  General Public Licence for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with itom. If not, see <http://www.gnu.org/licenses/>.
21 
22  This class is a port of the Python class TabSwitcherWidget
23  of the Spyder IDE (https://github.com/spyder-ide),
24  licensed under the MIT License and developed by the Spyder Project
25  Contributors.
26 *********************************************************************** */
27 
28 #pragma once
29 
30 #include <qmenu.h>
31 #include <qevent.h>
32 #include <qapplication.h>
33 
34 namespace ito
35 {
36 
37 /*
38 The class executes the selected action when "enter key" is input.
39 If a input of keyboard is not the "enter key", the menu is closed and
40 the input is inserted to code editor.
41 */
42 class MenuOnlyForEnter : public QMenu
43 {
44 public:
45  explicit MenuOnlyForEnter(QWidget *parent = nullptr) :
46  QMenu(parent),
47  m_pEditor(parent)
48  {
49  }
50 
51 protected:
53  void keyPressEvent(QKeyEvent *e)
54  {
55  if (e->key() != Qt::Key_Enter &&
56  e->key() != Qt::Key_Return)
57  {
58  e->accept();
59 
60  if (m_pEditor)
61  {
62  QKeyEvent ev2(e->type(),
63  e->key(),
64  e->modifiers(),
65  e->text(),
66  e->isAutoRepeat(),
67  e->count());
68  QApplication::sendEvent(m_pEditor, &ev2);
69  }
70 
71  close();
72  }
73  else
74  {
75  QMenu::keyPressEvent(e);
76  }
77  }
78 
79 private:
80  QWidget *m_pEditor;
81 };
82 
83 } //end namespace ito
Definition: apiFunctionsGraph.cpp:39
Definition: menuOnlyForEnter.h:42
void keyPressEvent(QKeyEvent *e)
< close the instance if key is not enter key.
Definition: menuOnlyForEnter.h:53