Files
qad/libs/widgets/qipedit.cpp
2022-12-14 14:14:33 +03:00

95 lines
2.6 KiB
C++

#include "qipedit.h"
#include "qalgorithms.h"
#include <QBoxLayout>
#include <QIntValidator>
#include <QLabel>
#include <QLineEdit>
QIPEdit::QIPEdit(QWidget * parent, const QString & ip): QWidget(parent) {
layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(1);
QIntValidator * validator = new QIntValidator(0, 255, this);
for (int i = 0; i < 4; i++) {
edits.push_back(new QLineEdit(this));
edits.back()->setAlignment(Qt::AlignHCenter);
edits.back()->setMaxLength(3);
edits.back()->setValidator(validator);
edits.back()->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
layout->addWidget(edits.back());
if (i < 3) {
dots.push_back(new QLabel(this));
dots.back()->setText(".");
dots.back()->adjustSize();
layout->addWidget(dots.back());
}
}
connect(edits[0], &QLineEdit::returnPressed, this, &QIPEdit::returnPressed0);
connect(edits[1], &QLineEdit::returnPressed, this, &QIPEdit::returnPressed1);
connect(edits[2], &QLineEdit::returnPressed, this, &QIPEdit::returnPressed2);
connect(edits[3], &QLineEdit::returnPressed, this, &QIPEdit::returnPressed3);
connect(edits[0], &QLineEdit::textChanged, this, &QIPEdit::textChanged0);
connect(edits[1], &QLineEdit::textChanged, this, &QIPEdit::textChanged1);
connect(edits[2], &QLineEdit::textChanged, this, &QIPEdit::textChanged2);
connect(edits[3], &QLineEdit::textChanged, this, &QIPEdit::textChanged3);
setLayout(layout);
setIP(ip);
cind = 0;
}
void QIPEdit::setIP(const QString & text) {
QString s, str = text;
s = str.left(str.indexOf('.'));
edits[0]->setText(s == "" ? "0" : s);
str = str.right(str.length() - s.length() - 1);
s = str.left(str.indexOf('.'));
edits[1]->setText(s == "" ? "0" : s);
str = str.right(str.length() - s.length() - 1);
s = str.left(str.indexOf('.'));
edits[2]->setText(s == "" ? "0" : s);
str = str.right(str.length() - s.length() - 1);
edits[3]->setText(str == "" ? "0" : str);
}
QString QIPEdit::IP() {
QString s;
if (edits[0]->text() == "")
s = "0.";
else
s = edits[0]->text() + ".";
if (edits[1]->text() == "")
s += "0.";
else
s += edits[1]->text() + ".";
if (edits[2]->text() == "")
s += "0.";
else
s += edits[2]->text() + ".";
if (edits[3]->text() == "")
s += "0";
else
s += edits[3]->text();
return s;
}
void QIPEdit::returnPress(int index) {
if (index < 3) {
edits[index + 1]->setFocus();
edits[index + 1]->setSelection(0, 3);
}
}
inline void QIPEdit::textChange(int index, const QString & text) {
if (text.length() == 3 && isVisible()) {
returnPress(index);
}
emit valueChanged(IP());
}