From fb204a82172932293069473e94d5b91c94f1482d Mon Sep 17 00:00:00 2001 From: Reinder Feenstra Date: Mon, 5 Jul 2021 23:55:26 +0200 Subject: [PATCH] Improved unit property edit handling while it has focus --- .../src/widget/lineeditwithfocusoutsignal.hpp | 49 +++++++++++++++++++ client/src/widget/unitpropertyedit.cpp | 11 ++++- client/src/widget/unitpropertyedit.hpp | 4 +- 3 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 client/src/widget/lineeditwithfocusoutsignal.hpp diff --git a/client/src/widget/lineeditwithfocusoutsignal.hpp b/client/src/widget/lineeditwithfocusoutsignal.hpp new file mode 100644 index 00000000..d17632dd --- /dev/null +++ b/client/src/widget/lineeditwithfocusoutsignal.hpp @@ -0,0 +1,49 @@ +/** + * client/src/widget/lineeditwithfocusoutsignal.hpp + * + * This file is part of the traintastic source code. + * + * Copyright (C) 2021 Reinder Feenstra + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef TRAINTASTIC_CLIENT_WIDGET_LINEEDITWITHFOCUSOUTSIGNAL_HPP +#define TRAINTASTIC_CLIENT_WIDGET_LINEEDITWITHFOCUSOUTSIGNAL_HPP + +#include + +class LineEditWithFocusOutSignal : public QLineEdit +{ + Q_OBJECT + + protected: + void focusOutEvent(QFocusEvent* event) final + { + QLineEdit::focusOutEvent(event); + emit focusOut(); + } + + public: + explicit LineEditWithFocusOutSignal(const QString& text, QWidget* parent = nullptr) : + QLineEdit(text, parent) + { + } + + signals: + void focusOut(); +}; + +#endif diff --git a/client/src/widget/unitpropertyedit.cpp b/client/src/widget/unitpropertyedit.cpp index 5e992e8e..3fb2ca9b 100644 --- a/client/src/widget/unitpropertyedit.cpp +++ b/client/src/widget/unitpropertyedit.cpp @@ -24,13 +24,14 @@ #include #include #include +#include "lineeditwithfocusoutsignal.hpp" #include "../network/unitproperty.hpp" #include "../utils/enum.hpp" UnitPropertyEdit::UnitPropertyEdit(UnitProperty& property, QWidget *parent) : QWidget(parent), m_property{property}, - m_valueLineEdit{new QLineEdit(m_property.toString(), this)}, + m_valueLineEdit{new LineEditWithFocusOutSignal(m_property.toString(), this)}, m_unitComboBox{new QComboBox(this)} { setEnabled(m_property.getAttributeBool(AttributeName::Enabled, true)); @@ -55,7 +56,8 @@ UnitPropertyEdit::UnitPropertyEdit(UnitProperty& property, QWidget *parent) : connect(&m_property, &UnitProperty::valueChanged, this, [this]() { - m_valueLineEdit->setText(m_property.toString()); + if(!m_valueLineEdit->hasFocus()) + m_valueLineEdit->setText(m_property.toString()); const qint64 unit = m_property.unitValue(); for(int i = 0; i < m_unitComboBox->count(); i++) @@ -72,6 +74,11 @@ UnitPropertyEdit::UnitPropertyEdit(UnitProperty& property, QWidget *parent) : m_valueLineEdit->setReadOnly(!m_property.isWritable()); l->addWidget(m_valueLineEdit, 1); connect(m_valueLineEdit, &QLineEdit::textEdited, &m_property, QOverload::of(&AbstractProperty::setValueString)); + connect(m_valueLineEdit, &LineEditWithFocusOutSignal::focusOut, this, + [this]() + { + m_valueLineEdit->setText(m_property.toString()); + }); for(qint64 value : enumValues(m_property.unitName())) { diff --git a/client/src/widget/unitpropertyedit.hpp b/client/src/widget/unitpropertyedit.hpp index e7476b69..a7d2278e 100644 --- a/client/src/widget/unitpropertyedit.hpp +++ b/client/src/widget/unitpropertyedit.hpp @@ -26,8 +26,8 @@ #include class UnitProperty; -class QLineEdit; class QComboBox; +class LineEditWithFocusOutSignal; class UnitPropertyEdit : public QWidget { @@ -35,7 +35,7 @@ class UnitPropertyEdit : public QWidget protected: UnitProperty& m_property; - QLineEdit* m_valueLineEdit; + LineEditWithFocusOutSignal* m_valueLineEdit; QComboBox* m_unitComboBox; public: