init
This commit is contained in:
147
customcontrols/mipaddresseditor.cpp
Normal file
147
customcontrols/mipaddresseditor.cpp
Normal file
@@ -0,0 +1,147 @@
|
||||
#include "mipaddresseditor.h"
|
||||
#include <QStyleOption>
|
||||
#include <QHBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QEvent>
|
||||
#include <QKeyEvent>
|
||||
#include <QPainter>
|
||||
|
||||
|
||||
MIpAddressEditor::MIpAddressEditor(QWidget* parent) :
|
||||
QWidget(parent), focused(false)
|
||||
{
|
||||
setStyleSheet("* { background-color:white; }");
|
||||
QHBoxLayout* lay = new QHBoxLayout();
|
||||
lay->setContentsMargins(1, 1, 1, 1);
|
||||
lay->setSpacing(0);
|
||||
QLabel* label1 = new QLabel(".", this);
|
||||
QLabel* label2 = new QLabel(".", this);
|
||||
QLabel* label3 = new QLabel(".", this);
|
||||
leIp1 = new QLineEdit(this);
|
||||
leIp1->setStyleSheet("QLineEdit { border:none; }");
|
||||
leIp1->setAlignment(Qt::AlignCenter);
|
||||
leIp2 = new QLineEdit(this);
|
||||
leIp2->setStyleSheet("QLineEdit { border:none; }");
|
||||
leIp2->setAlignment(Qt::AlignCenter);
|
||||
leIp3 = new QLineEdit(this);
|
||||
leIp3->setStyleSheet("QLineEdit { border:none; }");
|
||||
leIp3->setAlignment(Qt::AlignCenter);
|
||||
leIp4 = new QLineEdit(this);
|
||||
leIp4->setStyleSheet("QLineEdit { border:none; }");
|
||||
leIp4->setAlignment(Qt::AlignCenter);
|
||||
lay->addWidget(leIp1);
|
||||
lay->addWidget(label1);
|
||||
lay->addWidget(leIp2);
|
||||
lay->addWidget(label2);
|
||||
lay->addWidget(leIp3);
|
||||
lay->addWidget(label3);
|
||||
lay->addWidget(leIp4);
|
||||
setLayout(lay);
|
||||
|
||||
QIntValidator* validator = new QIntValidator(this);
|
||||
validator->setRange(0, 255);
|
||||
leIp1->setValidator(validator);
|
||||
leIp2->setValidator(validator);
|
||||
leIp3->setValidator(validator);
|
||||
leIp4->setValidator(validator);
|
||||
leIp1->installEventFilter(this);
|
||||
leIp2->installEventFilter(this);
|
||||
leIp3->installEventFilter(this);
|
||||
leIp4->installEventFilter(this);
|
||||
connect(leIp1, &QLineEdit::textChanged, this, &MIpAddressEditor::leIpTextChanged);
|
||||
connect(leIp2, &QLineEdit::textChanged, this, &MIpAddressEditor::leIpTextChanged);
|
||||
connect(leIp3, &QLineEdit::textChanged, this, &MIpAddressEditor::leIpTextChanged);
|
||||
connect(leIp4, &QLineEdit::textChanged, this, &MIpAddressEditor::leIpTextChanged);
|
||||
}
|
||||
|
||||
void MIpAddressEditor::setIp(const QString& ip)
|
||||
{
|
||||
QStringList nums = ip.split('.');
|
||||
if (nums.size() == 4)
|
||||
{
|
||||
leIp1->setText(nums[0]);
|
||||
leIp2->setText(nums[1]);
|
||||
leIp3->setText(nums[2]);
|
||||
leIp4->setText(nums[3]);
|
||||
}
|
||||
}
|
||||
|
||||
QString MIpAddressEditor::ip() const
|
||||
{
|
||||
QString ipAddr = QString("%1.%2.%3.%4")
|
||||
.arg(leIp1->text(), leIp2->text(), leIp3->text(), leIp4->text());
|
||||
return ipAddr;
|
||||
}
|
||||
|
||||
bool MIpAddressEditor::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
if (event->type() == QEvent::KeyPress)
|
||||
{
|
||||
const QVarLengthArray<QWidget*, 4> editors = { leIp1, leIp2, leIp3, leIp4 };
|
||||
QKeyEvent* keyEvent = dynamic_cast<QKeyEvent*>(event);
|
||||
QWidget* target = dynamic_cast<QWidget*>(watched);
|
||||
if (keyEvent->key() == Qt::Key_Space)
|
||||
{
|
||||
if (target != editors.back())
|
||||
{
|
||||
editors[editors.indexOf(target) + 1]->setFocus();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (event->type() == QEvent::FocusIn)
|
||||
{
|
||||
focused = true;
|
||||
update();
|
||||
}
|
||||
else if (event->type() == QEvent::FocusOut)
|
||||
{
|
||||
if (!leIp1->hasFocus() && !leIp2->hasFocus() &&
|
||||
!leIp3->hasFocus() && !leIp4->hasFocus())
|
||||
{
|
||||
focused = false;
|
||||
update();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void MIpAddressEditor::leIpTextChanged(const QString& text)
|
||||
{
|
||||
if (text.size() >= 3)
|
||||
{
|
||||
const QVarLengthArray<QWidget*, 4> editors = { leIp1, leIp2, leIp3, leIp4 };
|
||||
QWidget* src = dynamic_cast<QWidget*>(sender());
|
||||
if (src != editors.back())
|
||||
{
|
||||
editors[editors.indexOf(src) + 1]->setFocus();
|
||||
}
|
||||
}
|
||||
|
||||
emit IPChanged(ip());
|
||||
}
|
||||
|
||||
void MIpAddressEditor::paintEvent(QPaintEvent*)
|
||||
{
|
||||
QStyleOption opt;
|
||||
opt.init(this);
|
||||
QPainter painter(this);
|
||||
style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
|
||||
QPalette pale = palette();
|
||||
painter.setBrush(Qt::NoBrush);
|
||||
if (focused)
|
||||
{
|
||||
painter.setPen(QPen(pale.highlight(), 1));
|
||||
painter.drawRect(0, 0, width() - 1, height() - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
painter.setPen(QPen(pale.mid(), 1));
|
||||
painter.drawRect(0, 0, width() - 1, height() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
QSize MIpAddressEditor::sizeHint() const
|
||||
{
|
||||
return QSize(160, 24);
|
||||
}
|
||||
Reference in New Issue
Block a user