119 lines
2.2 KiB
C++
Executable File
119 lines
2.2 KiB
C++
Executable File
#include "lts_date_time_edit_p.h"
|
|
|
|
#include <QApplication>
|
|
#include <QRegularExpression>
|
|
#include <QValidator>
|
|
#include <QKeyEvent>
|
|
|
|
|
|
namespace LtsWidgets
|
|
{
|
|
|
|
|
|
|
|
|
|
void LineEdit::clear()
|
|
{
|
|
setStyleSheet(QString());
|
|
QLineEdit::clear();
|
|
}
|
|
|
|
bool LineEdit::isEmpty() const
|
|
{
|
|
return text().isEmpty();
|
|
}
|
|
|
|
void LineEdit::keyPressEvent(QKeyEvent *event)
|
|
{
|
|
const bool wasEmpty = isEmpty();
|
|
QLineEdit::keyPressEvent(event);
|
|
int keyCode = event->key();
|
|
if (keyCode == Qt::Key_Backspace && wasEmpty)
|
|
{
|
|
emit focusOutBackspace();
|
|
focusPreviousChild();
|
|
event->accept();
|
|
}
|
|
}
|
|
|
|
void LineEdit::contextMenuEvent(QContextMenuEvent *e)
|
|
{
|
|
Q_UNUSED(e);
|
|
|
|
}
|
|
|
|
void LineEdit::focusOutEvent(QFocusEvent *event)
|
|
{
|
|
QLineEdit::focusOutEvent(event);
|
|
emit focusOut();
|
|
}
|
|
|
|
|
|
|
|
|
|
DigitsLineEdit::DigitsLineEdit(QWidget *parent)
|
|
: LineEdit(parent)
|
|
{
|
|
setAlignment(Qt::AlignRight);
|
|
QRegularExpression rx(QLatin1String("[-]{0,1}[0-9 ]*"));
|
|
setValidator(new QRegularExpressionValidator(rx, this));
|
|
}
|
|
|
|
void DigitsLineEdit::setExactNumDigits(int num)
|
|
{
|
|
m_exactNumDigits = num;
|
|
|
|
QRegularExpression rx(QString::asprintf("^[0-9 ]{%d}$", num));
|
|
setValidator(new QRegularExpressionValidator(rx, this));
|
|
}
|
|
|
|
void DigitsLineEdit::setValue(int val)
|
|
{
|
|
QString str;
|
|
|
|
if (m_exactNumDigits == -1)
|
|
str.setNum(val);
|
|
else
|
|
str = QStringLiteral("%1").arg(val, m_exactNumDigits, 10, QLatin1Char('0'));
|
|
|
|
setText(str);
|
|
}
|
|
|
|
int DigitsLineEdit::getValue() const
|
|
{
|
|
if (text().isEmpty())
|
|
return 0;
|
|
return text().remove(QLatin1Char(' ')).toInt();
|
|
}
|
|
|
|
void DigitsLineEdit::setCursorPosition(int curPos)
|
|
{
|
|
|
|
curPos = curPos < 0 ? 1 : curPos;
|
|
QLineEdit::setCursorPosition(curPos);
|
|
}
|
|
|
|
void DigitsLineEdit::wheelEvent(QWheelEvent *event)
|
|
{
|
|
LineEdit::wheelEvent(event);
|
|
const QString txt = text();
|
|
if (m_exactNumDigits > 0 && txt.length() != m_exactNumDigits)
|
|
return;
|
|
event->accept();
|
|
int cursorPos = cursorPosition();
|
|
emit wheelEventReceived(event->delta() > 0, cursorPos);
|
|
}
|
|
|
|
void DigitsLineEdit::keyPressEvent(QKeyEvent *event)
|
|
{
|
|
LineEdit::keyPressEvent(event);
|
|
if (event->key() == Qt::Key_Up || event->key() == Qt::Key_Down)
|
|
{
|
|
const bool bUp = event->key() == Qt::Key_Up;
|
|
const int cursorPos = cursorPosition();
|
|
emit wheelEventReceived(bUp, cursorPos);
|
|
}
|
|
}
|
|
|
|
}
|