mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-01 05:26:45 +08:00
* also consider Qt::Key_Enter (NUMPAD) * signal returnPressedUnemptyLine * use signal returnPressedUnemptyLine instead of returnPressed which avoids empty lines
83 lines
2.5 KiB
C++
83 lines
2.5 KiB
C++
/* Copyright (C) 2017
|
|
* 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 "lineedithistory.h"
|
|
#include <QKeyEvent>
|
|
#include <QMenu>
|
|
|
|
namespace BlackGui
|
|
{
|
|
QString CLineEditHistory::getLastEnteredLine() const
|
|
{
|
|
if (m_history.isEmpty()) return {};
|
|
return m_history.first();
|
|
}
|
|
|
|
QString CLineEditHistory::getLastEnteredLineFormatted() const
|
|
{
|
|
return this->getLastEnteredLine().trimmed().simplified();
|
|
}
|
|
|
|
void CLineEditHistory::clearHistory()
|
|
{
|
|
m_history.clear();
|
|
}
|
|
|
|
void CLineEditHistory::keyPressEvent(QKeyEvent *event)
|
|
{
|
|
const int key = event->key();
|
|
bool nonEmpty = false;
|
|
|
|
if (key == Qt::Key_Up)
|
|
{
|
|
// move back in history
|
|
if (m_history.size() > m_position)
|
|
{
|
|
this->setText(m_history.at(m_position++));
|
|
}
|
|
}
|
|
else if (key == Qt::Key_Down)
|
|
{
|
|
// move forward in history
|
|
if (m_position <= 0) { this->clear(); return; }
|
|
if (m_position == m_history.size()) { --m_position; } // avoid need of 2xKeyDown at end
|
|
if (m_position > 0 && m_history.size() > --m_position)
|
|
{
|
|
this->setText(m_history.at(m_position));
|
|
}
|
|
}
|
|
else if (key == Qt::Key_Return || key == Qt::Key_Enter) // normal and keypad enter
|
|
{
|
|
const QString t = this->text().trimmed();
|
|
if (!t.isEmpty())
|
|
{
|
|
m_history.push_front(t);
|
|
m_position = 0;
|
|
this->clear();
|
|
nonEmpty = true;
|
|
}
|
|
}
|
|
// default handler for event
|
|
QLineEdit::keyPressEvent(event);
|
|
|
|
// signal
|
|
if (nonEmpty) { emit this->returnPressedUnemptyLine(); }
|
|
}
|
|
|
|
void CLineEditHistory::contextMenuEvent(QContextMenuEvent *event)
|
|
{
|
|
if (!event) { return; }
|
|
QMenu *menu = this->createStandardContextMenu();
|
|
menu->addSeparator();
|
|
menu->addAction("Clear history");
|
|
connect(menu->actions().last(), &QAction::triggered, this, &CLineEditHistory::clearHistory);
|
|
menu->exec(event->globalPos());
|
|
delete menu;
|
|
}
|
|
} // ns
|