From 320bae9a2d3a16bfde20a050bf079703c44e7fd7 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sat, 13 Apr 2019 03:44:51 +0200 Subject: [PATCH] Ref T599, event filter for uc characters --- src/blackgui/eventfilter.cpp | 45 ++++++++++++++++++++++++++++++++++++ src/blackgui/eventfilter.h | 32 +++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/blackgui/eventfilter.cpp create mode 100644 src/blackgui/eventfilter.h diff --git a/src/blackgui/eventfilter.cpp b/src/blackgui/eventfilter.cpp new file mode 100644 index 000000000..52d9b2384 --- /dev/null +++ b/src/blackgui/eventfilter.cpp @@ -0,0 +1,45 @@ +/* Copyright (C) 2019 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated, + * or distributed except according to the terms contained in the LICENSE file. + */ + +#include "eventfilter.h" +#include "blackmisc/stringutils.h" + +#include +#include + +namespace BlackGui +{ + bool CUpperCaseEventFilter::eventFilter(QObject *object, QEvent *event) + { + if (event->type() == QEvent::KeyPress) + { + if (QKeyEvent *e = dynamic_cast(event)) + { + // If QKeyEvent::text() returns an empty QString then let normal + // processing proceed as it may be a control (e.g. cursor movement) + // key. Otherwise convert the text to upper case and insert it at + // the current cursor position. + if (e->text().length() == 1) + { + QPlainTextEdit *pte = qobject_cast(object); + if (!pte) { return false; } + + const QChar c = e->text().front(); + if (!c.isLower()) { return false; } + if (!c.isLetter()) { return false; } + + pte->insertPlainText(e->text().toUpper()); + + // return true to prevent further processing + return true; + } + } + } + return false; + } +} // namespace diff --git a/src/blackgui/eventfilter.h b/src/blackgui/eventfilter.h new file mode 100644 index 000000000..35367a66f --- /dev/null +++ b/src/blackgui/eventfilter.h @@ -0,0 +1,32 @@ +/* Copyright (C) 2019 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated, + * or distributed except according to the terms contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKGUI_EVENTFILTER_H +#define BLACKGUI_EVENTFILTER_H + +#include + +namespace BlackGui +{ + //! Uppercase key press + class CUpperCaseEventFilter : public QObject + { + Q_OBJECT + + public: + using QObject::QObject; + + protected: + //! Filter + bool eventFilter(QObject *object, QEvent *event); + }; +} // namespace + +#endif // guard