Ref T591, avoid duplicated text message lines

* also consider Qt::Key_Enter (NUMPAD)
* signal returnPressedUnemptyLine
* use signal returnPressedUnemptyLine instead of returnPressed which avoids empty lines
This commit is contained in:
Klaus Basan
2019-04-04 23:25:16 +02:00
committed by Mat Sutcliffe
parent 3bb5efcb29
commit dc6e18b5a2
4 changed files with 26 additions and 12 deletions

View File

@@ -49,7 +49,7 @@ namespace BlackGui
connect(sGui->getIContextNetwork(), &IContextNetwork::connectedServerChanged, this, &CCommandInput::onConnectedServerChanged, Qt::QueuedConnection); connect(sGui->getIContextNetwork(), &IContextNetwork::connectedServerChanged, this, &CCommandInput::onConnectedServerChanged, Qt::QueuedConnection);
} }
} }
connect(this, &CCommandInput::returnPressed, this, &CCommandInput::validateCommand); connect(this, &CCommandInput::returnPressedUnemptyLine, this, &CCommandInput::validateCommand, Qt::QueuedConnection);
} }
void CCommandInput::showToolTip(bool show) void CCommandInput::showToolTip(bool show)

View File

@@ -69,14 +69,14 @@ namespace BlackGui
ui->comp_AtcStations->setWithIcons(false); ui->comp_AtcStations->setWithIcons(false);
// lep_textMessages is the own line edit // lep_textMessages is the own line edit
bool c = connect(ui->lep_textMessages, &CLineEditHistory::returnPressed, this, &CTextMessageComponent::textMessageEntered); bool c = connect(ui->lep_textMessages, &CLineEditHistory::returnPressedUnemptyLine, this, &CTextMessageComponent::textMessageEntered, Qt::QueuedConnection);
Q_ASSERT_X(c, Q_FUNC_INFO, "Missing connect"); Q_ASSERT_X(c, Q_FUNC_INFO, "Missing connect");
c = connect(ui->gb_Settings, &QGroupBox::toggled, this, &CTextMessageComponent::onSettingsChecked); c = connect(ui->gb_Settings, &QGroupBox::toggled, this, &CTextMessageComponent::onSettingsChecked, Qt::QueuedConnection);
Q_ASSERT_X(c, Q_FUNC_INFO, "Missing connect"); Q_ASSERT_X(c, Q_FUNC_INFO, "Missing connect");
c = connect(ui->gb_MessageTo, &QGroupBox::toggled, this, &CTextMessageComponent::onMessageToChecked); c = connect(ui->gb_MessageTo, &QGroupBox::toggled, this, &CTextMessageComponent::onMessageToChecked, Qt::QueuedConnection);
Q_ASSERT_X(c, Q_FUNC_INFO, "Missing connect"); Q_ASSERT_X(c, Q_FUNC_INFO, "Missing connect");
c = connect(ui->comp_AtcStations, &CAtcButtonComponent::requestAtcStation, this, &CTextMessageComponent::onAtcButtonClicked); c = connect(ui->comp_AtcStations, &CAtcButtonComponent::requestAtcStation, this, &CTextMessageComponent::onAtcButtonClicked, Qt::QueuedConnection);
Q_ASSERT_X(c, Q_FUNC_INFO, "Missing connect"); Q_ASSERT_X(c, Q_FUNC_INFO, "Missing connect");
// style sheet // style sheet

View File

@@ -30,15 +30,18 @@ namespace BlackGui
void CLineEditHistory::keyPressEvent(QKeyEvent *event) void CLineEditHistory::keyPressEvent(QKeyEvent *event)
{ {
if (event->key() == Qt::Key_Up) const int key = event->key();
bool nonEmpty = false;
if (key == Qt::Key_Up)
{ {
// move back in history // move back in history
if (m_history.size() > m_position) if (m_history.size() > m_position)
{ {
setText(m_history.at(m_position++)); this->setText(m_history.at(m_position++));
} }
} }
else if (event->key() == Qt::Key_Down) else if (key == Qt::Key_Down)
{ {
// move forward in history // move forward in history
if (m_position <= 0) { this->clear(); return; } if (m_position <= 0) { this->clear(); return; }
@@ -48,17 +51,22 @@ namespace BlackGui
this->setText(m_history.at(m_position)); this->setText(m_history.at(m_position));
} }
} }
else if (event->key() == Qt::Key_Return) else if (key == Qt::Key_Return || key == Qt::Key_Enter) // normal and keypad enter
{ {
if (!text().isEmpty()) const QString t = this->text().trimmed();
if (!t.isEmpty())
{ {
m_history.push_front(text()); m_history.push_front(t);
m_position = 0; m_position = 0;
this->clear(); this->clear();
nonEmpty = true;
} }
} }
// default handler for event // default handler for event
QLineEdit::keyPressEvent(event); QLineEdit::keyPressEvent(event);
// signal
if (nonEmpty) { emit this->returnPressedUnemptyLine(); }
} }
void CLineEditHistory::contextMenuEvent(QContextMenuEvent *event) void CLineEditHistory::contextMenuEvent(QContextMenuEvent *event)

View File

@@ -19,6 +19,7 @@ namespace BlackGui
{ {
/*! /*!
* Line edit with history * Line edit with history
* \details lines are trimmed and empty lines are ignored
*/ */
class BLACKGUI_EXPORT CLineEditHistory : public QLineEdit class BLACKGUI_EXPORT CLineEditHistory : public QLineEdit
{ {
@@ -37,6 +38,11 @@ namespace BlackGui
//! Clear the history //! Clear the history
void clearHistory(); void clearHistory();
signals:
//! Return has been pressed, line is NOT empty (spaces are trimmed)
//! \details returnPressed alsofires on empty lines, but those are not in the history
void returnPressedUnemptyLine();
protected: protected:
//! \copydoc QLineEdit::keyPressEvent //! \copydoc QLineEdit::keyPressEvent
virtual void keyPressEvent(QKeyEvent *event) override; virtual void keyPressEvent(QKeyEvent *event) override;