diff options
author | Kiyoshi Aman <adelie@aerdan.vulpine.house> | 2020-02-10 08:57:09 -0600 |
---|---|---|
committer | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2020-02-10 08:57:09 -0600 |
commit | 89934d31b65f070b1954ae2f6201841bfd90d3e2 (patch) | |
tree | 5019930dad6c28b4183a0aec74fbda6e3f408125 /ui/qt5/subnetbox.cc | |
parent | 423c4fd6e25cc9306b84abceb8f79e8c1a469a7b (diff) | |
download | horizon-89934d31b65f070b1954ae2f6201841bfd90d3e2.tar.gz horizon-89934d31b65f070b1954ae2f6201841bfd90d3e2.tar.bz2 horizon-89934d31b65f070b1954ae2f6201841bfd90d3e2.tar.xz horizon-89934d31b65f070b1954ae2f6201841bfd90d3e2.zip |
Qt UI: Add subnet input widget
Diffstat (limited to 'ui/qt5/subnetbox.cc')
-rw-r--r-- | ui/qt5/subnetbox.cc | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/ui/qt5/subnetbox.cc b/ui/qt5/subnetbox.cc new file mode 100644 index 0000000..54f1732 --- /dev/null +++ b/ui/qt5/subnetbox.cc @@ -0,0 +1,65 @@ +/* + * subnetbox.cc - Implementation of a widget for inputting an IPv4 subnet mask + * horizon-qt5, the Qt 5 user interface for + * Project Horizon + * + * Copyright (c) 2020 Adélie Linux and contributors. All rights reserved. + * This code is licensed under the AGPL 3.0 license, as noted in the + * LICENSE-code file in the root directory of this repository. + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +#include "subnetbox.hh" +#include "util/net.hh" + +SubnetBox::SubnetBox(QWidget *parent) : QWidget(parent) { + SubnetBox::layout = new QHBoxLayout(this); + SubnetBox::subnet = new QLineEdit(this); + SubnetBox::cidr = new QSpinBox(this); + + this->setLayout(layout); + + layout->addWidget(subnet); + layout->addWidget(cidr); + layout->setMargin(0); + + subnet->setInputMask("000.000.000.000;_"); + cidr->setRange(1, 32); + + connect(subnet, &QLineEdit::textEdited, this, &SubnetBox::subnetEdited); + connect(cidr, QOverload<int>::of(&QSpinBox::valueChanged), + this, &SubnetBox::cidrEdited); +} + +QString SubnetBox::subnetMask(void) const { + return subnet->text(); +} + +int SubnetBox::subnetCIDR(void) const { + return cidr->value(); +} + +void SubnetBox::subnetEdited(const QString &text) { + int value = subnet_mask_to_cidr(text.toUtf8()); + if(value > 0) { + cidr->setValue(value); + } +} + +void SubnetBox::cidrEdited(int value) { + int bytes = value / 8, bits = value % 8, lastfilled = 0; + QString data; + char temp[4]; + + for(int i = 0; i < bytes; i++) { + data.append("255."); + } + for(int i = bits; i > 0; i--) { + lastfilled |= (1 << (8 - i)); + } + snprintf(temp, 4, "%d", lastfilled); + data.append(temp); + + subnet->setText(data); +} |