def re_translate_ui(self, lang): self.ml = ManageLng(lang) self.inputbox.setTitle( self.ml.get_tr_text("tab_num_conv_inputbox_gbox_name")) self.input_number_label.setText( self.ml.get_tr_text("tab_num_conv_inputbox_in_number_lab")) self.bin_button.setText( self.ml.get_tr_text("tab_num_conv_inputbox_bin_chkbox")) self.dec_button.setText( self.ml.get_tr_text("tab_num_conv_inputbox_dec_chkbox")) self.hex_button.setText( self.ml.get_tr_text("tab_num_conv_inputbox_hex_chkbox")) self.convert_button.setText( self.ml.get_tr_text("tab_num_conv_inputbox_conv_btn")) self.outputbox.setTitle( self.ml.get_tr_text("tab_num_conv_outputbox_gbox_name")) self.bin_label.setText( self.ml.get_tr_text("tab_num_conv_outputbox_bin_lab")) self.dec_label.setText( self.ml.get_tr_text("tab_num_conv_outputbox_dec_lab")) self.hex_label.setText( self.ml.get_tr_text("tab_num_conv_outputbox_hex_lab")) self.bin_output_copy_button.setText( self.ml.get_tr_text("tab_num_conv_outputbox_copy_btn")) self.dec_output_copy_button.setText( self.ml.get_tr_text("tab_num_conv_outputbox_copy_btn")) self.hex_output_copy_button.setText( self.ml.get_tr_text("tab_num_conv_outputbox_copy_btn")) self.ip_address_number_conversion_box.setTitle( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_gbox_name")) self.input_label.setText( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_in_lab")) self.dec_to_bin_button.setText( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_dectobin")) self.bin_to_dec_button.setText( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_bintodec")) self.output_label.setText( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_out_lab")) self.output_textfield_copy_button.setText( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_copy_btn")) self.convert_button_2.setText( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_convert_btn")) if self.bin_output.text(): self.bin_output.setText( get_bin_format(self.bin_format_base, self.ml.get_tr_text("byte_format_str"), self.bin_format_base_byte))
def re_translate_ui(self, lang): self.ml = ManageLng(lang) self.starting_network_address_label.setText(self.ml.get_tr_text("tab_vlsm_starting_net")) self.endpoint_numbers_per_network_label.setText(self.ml.get_tr_text("tab_vlsm_endpoint_nums")) self.starting_network_prefix_label.setText(self.ml.get_tr_text("tab_vlsm_starting_net_prefix")) self.calculation_button.setText(self.ml.get_tr_text("tab_vlsm_calc_btn")) self.table_column_names = [self.ml.get_tr_text("table_column_network_add"), self.ml.get_tr_text("table_column_ip_range"), self.ml.get_tr_text("table_column_broadcast_add"), self.ml.get_tr_text("table_column_subnet_mask"), self.ml.get_tr_text("table_column_prefix"), self.ml.get_tr_text("table_column_addressable_host")] self.table.setHorizontalHeaderLabels(self.table_column_names)
def re_translate_ui(self, lang): self.ml = ManageLng(lang) self.starting_network_address_label.setText(self.ml.get_tr_text("tab_ipsubnet_starting_net")) self.number_of_subnets_label.setText(self.ml.get_tr_text("tab_ipsubnet_required_subnet_num")) self.calculation_button.setText(self.ml.get_tr_text("tab_ipsubnet_calc_btn")) self.table_column_names = [self.ml.get_tr_text("table_column_network_add"), self.ml.get_tr_text("table_column_ip_range"), self.ml.get_tr_text("table_column_broadcast_add"), self.ml.get_tr_text("table_column_subnet_mask"), self.ml.get_tr_text("table_column_prefix"), self.ml.get_tr_text("table_column_addressable_host")] self.table.setHorizontalHeaderLabels(self.table_column_names) self.cancel_btn.setText(self.ml.get_tr_text("tab_ipsubnet_cancel_btn"))
def __init__(self): super(NumberConversion, self).__init__() # App attributes self.bin_format_base = None self.bin_format_base_byte = None # Use language settings self.ml = ManageLng() main_layout = QGridLayout() self.setLayout(main_layout) # Input self.inputbox = QGroupBox( self.ml.get_tr_text("tab_num_conv_inputbox_gbox_name")) self.inputbox.setMaximumWidth(400) main_layout.addWidget(self.inputbox, 0, 0) inputbox_layout = QGridLayout() inputbox_layout.setHorizontalSpacing(25) inputbox_layout.setVerticalSpacing(35) inputbox_layout.setAlignment(Qt.AlignCenter) self.inputbox.setLayout(inputbox_layout) self.input_number_label = QLabel( self.ml.get_tr_text("tab_num_conv_inputbox_in_number_lab")) self.input_number_textfield = QLineEdit() self.input_number_textfield.setPlaceholderText("192") self.input_number_textfield.setAlignment(Qt.AlignCenter) self.input_number_textfield.returnPressed.connect(self.convert_action) inputbox_layout.addWidget(self.input_number_label, 0, 0, alignment=Qt.AlignCenter) inputbox_layout.addWidget(self.input_number_textfield, 0, 1) button_layout = QVBoxLayout() self.bin_button = QRadioButton( self.ml.get_tr_text("tab_num_conv_inputbox_bin_chkbox")) self.bin_button.clicked.connect( lambda: self.input_number_textfield.setPlaceholderText("11100010")) self.dec_button = QRadioButton( self.ml.get_tr_text("tab_num_conv_inputbox_dec_chkbox")) self.dec_button.clicked.connect( lambda: self.input_number_textfield.setPlaceholderText("192")) self.hex_button = QRadioButton( self.ml.get_tr_text("tab_num_conv_inputbox_hex_chkbox")) self.hex_button.clicked.connect( lambda: self.input_number_textfield.setPlaceholderText("FF")) self.dec_button.setChecked(True) button_layout.addWidget(self.bin_button) button_layout.addWidget(self.dec_button) button_layout.addWidget(self.hex_button) inputbox_layout.addLayout(button_layout, 0, 3, 1, 2) self.convert_button = QPushButton( self.ml.get_tr_text("tab_num_conv_inputbox_conv_btn")) self.convert_button.clicked.connect(self.convert_action) self.convert_button.setIcon(QIcon("static/images/exchange.png")) inputbox_layout.addWidget(self.convert_button, 1, 1, alignment=Qt.AlignCenter) # Output self.outputbox = QGroupBox( self.ml.get_tr_text("tab_num_conv_outputbox_gbox_name")) main_layout.addWidget(self.outputbox, 0, 1) outputbox_layout = QGridLayout() outputbox_layout.setHorizontalSpacing(25) self.outputbox.setLayout(outputbox_layout) self.bin_label = QLabel( self.ml.get_tr_text("tab_num_conv_outputbox_bin_lab")) self.bin_label.setAlignment(Qt.AlignCenter) self.dec_label = QLabel( self.ml.get_tr_text("tab_num_conv_outputbox_dec_lab")) self.dec_label.setAlignment(Qt.AlignCenter) self.hex_label = QLabel( self.ml.get_tr_text("tab_num_conv_outputbox_hex_lab")) self.hex_label.setAlignment(Qt.AlignCenter) self.bin_output = QLineEdit() self.bin_output.setReadOnly(True) self.bin_output.setAlignment(Qt.AlignCenter) self.dec_output = QLineEdit() self.dec_output.setReadOnly(True) self.dec_output.setAlignment(Qt.AlignCenter) self.hex_output = QLineEdit() self.hex_output.setReadOnly(True) self.hex_output.setAlignment(Qt.AlignCenter) self.bin_output_copy_button = QPushButton( self.ml.get_tr_text("tab_num_conv_outputbox_copy_btn")) self.bin_output_copy_button.setIcon( QIcon("static/images/copy_clipboard.png")) self.bin_output_copy_button.clicked.connect( lambda: copy_action(self.bin_output.text())) self.dec_output_copy_button = QPushButton( self.ml.get_tr_text("tab_num_conv_outputbox_copy_btn")) self.dec_output_copy_button.setIcon( QIcon("static/images/copy_clipboard.png")) self.dec_output_copy_button.clicked.connect( lambda: copy_action(self.dec_output.text())) self.hex_output_copy_button = QPushButton( self.ml.get_tr_text("tab_num_conv_outputbox_copy_btn")) self.hex_output_copy_button.setIcon( QIcon("static/images/copy_clipboard.png")) self.hex_output_copy_button.clicked.connect( lambda: copy_action(self.hex_output.text())) outputbox_layout.addWidget(self.bin_label, 0, 0) outputbox_layout.addWidget(self.bin_output, 0, 1) outputbox_layout.addWidget(self.bin_output_copy_button, 0, 2) outputbox_layout.addWidget(self.dec_label, 1, 0) outputbox_layout.addWidget(self.dec_output, 1, 1) outputbox_layout.addWidget(self.dec_output_copy_button, 1, 2) outputbox_layout.addWidget(self.hex_label, 2, 0) outputbox_layout.addWidget(self.hex_output, 2, 1) outputbox_layout.addWidget(self.hex_output_copy_button, 2, 2) # IP address/mask number conversion self.ip_address_number_conversion_box = QGroupBox( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_gbox_name")) main_layout.addWidget(self.ip_address_number_conversion_box, 1, 0, 1, 2) ip_address_number_conversion_layout = QGridLayout() ip_address_number_conversion_layout.setAlignment(Qt.AlignCenter) ip_address_number_conversion_layout.setHorizontalSpacing(25) ip_address_number_conversion_layout.setVerticalSpacing(24) self.ip_address_number_conversion_box.setLayout( ip_address_number_conversion_layout) self.input_label = QLabel( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_in_lab")) self.input_label.setAlignment(Qt.AlignCenter) self.input_label.setMaximumWidth(150) self.input_textfield = QLineEdit() self.input_textfield.setPlaceholderText("192.168.1.1") self.input_textfield.setAlignment(Qt.AlignLeft) self.input_textfield.setMaximumWidth(300) self.input_textfield.setAlignment(Qt.AlignCenter) self.input_textfield.returnPressed.connect(self.convert_action_2) ip_address_number_conversion_layout.addWidget(self.input_label, 0, 0) ip_address_number_conversion_layout.addWidget(self.input_textfield, 0, 1) button_layout_2 = QVBoxLayout() self.dec_to_bin_button = QRadioButton( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_dectobin")) self.dec_to_bin_button.clicked.connect( lambda: self.input_textfield.setPlaceholderText("192.168.1.1")) self.dec_to_bin_button.setMaximumWidth(150) self.bin_to_dec_button = QRadioButton( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_bintodec")) self.bin_to_dec_button.clicked.connect( lambda: self.input_textfield.setPlaceholderText( "11000000.10101000.00000001.00000001")) self.bin_to_dec_button.setMaximumWidth(150) self.dec_to_bin_button.setChecked(True) button_layout_2.addWidget(self.dec_to_bin_button) button_layout_2.addWidget(self.bin_to_dec_button) ip_address_number_conversion_layout.addLayout(button_layout_2, 0, 2) self.output_label = QLabel( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_out_lab")) self.output_label.setAlignment(Qt.AlignCenter) self.output_textfield = QLineEdit() self.output_textfield.setMaximumWidth(300) self.output_textfield.setReadOnly(True) self.output_textfield.setAlignment(Qt.AlignCenter) ip_address_number_conversion_layout.addWidget(self.output_label, 1, 0) ip_address_number_conversion_layout.addWidget(self.output_textfield, 1, 1) self.output_textfield_copy_button = QPushButton( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_copy_btn")) self.output_textfield_copy_button.setIcon( QIcon("static/images/copy_clipboard.png")) self.output_textfield_copy_button.clicked.connect( lambda: copy_action(self.output_textfield.text())) ip_address_number_conversion_layout.addWidget( self.output_textfield_copy_button, 1, 2, alignment=Qt.AlignLeft) self.convert_button_2 = QPushButton( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_convert_btn")) self.convert_button_2.clicked.connect(self.convert_action_2) self.convert_button_2.setIcon(QIcon("static/images/exchange.png")) ip_address_number_conversion_layout.addWidget( self.convert_button_2, 2, 0, 1, 3, alignment=Qt.AlignHCenter)
class NumberConversion(QWidget): def __init__(self): super(NumberConversion, self).__init__() # App attributes self.bin_format_base = None self.bin_format_base_byte = None # Use language settings self.ml = ManageLng() main_layout = QGridLayout() self.setLayout(main_layout) # Input self.inputbox = QGroupBox( self.ml.get_tr_text("tab_num_conv_inputbox_gbox_name")) self.inputbox.setMaximumWidth(400) main_layout.addWidget(self.inputbox, 0, 0) inputbox_layout = QGridLayout() inputbox_layout.setHorizontalSpacing(25) inputbox_layout.setVerticalSpacing(35) inputbox_layout.setAlignment(Qt.AlignCenter) self.inputbox.setLayout(inputbox_layout) self.input_number_label = QLabel( self.ml.get_tr_text("tab_num_conv_inputbox_in_number_lab")) self.input_number_textfield = QLineEdit() self.input_number_textfield.setPlaceholderText("192") self.input_number_textfield.setAlignment(Qt.AlignCenter) self.input_number_textfield.returnPressed.connect(self.convert_action) inputbox_layout.addWidget(self.input_number_label, 0, 0, alignment=Qt.AlignCenter) inputbox_layout.addWidget(self.input_number_textfield, 0, 1) button_layout = QVBoxLayout() self.bin_button = QRadioButton( self.ml.get_tr_text("tab_num_conv_inputbox_bin_chkbox")) self.bin_button.clicked.connect( lambda: self.input_number_textfield.setPlaceholderText("11100010")) self.dec_button = QRadioButton( self.ml.get_tr_text("tab_num_conv_inputbox_dec_chkbox")) self.dec_button.clicked.connect( lambda: self.input_number_textfield.setPlaceholderText("192")) self.hex_button = QRadioButton( self.ml.get_tr_text("tab_num_conv_inputbox_hex_chkbox")) self.hex_button.clicked.connect( lambda: self.input_number_textfield.setPlaceholderText("FF")) self.dec_button.setChecked(True) button_layout.addWidget(self.bin_button) button_layout.addWidget(self.dec_button) button_layout.addWidget(self.hex_button) inputbox_layout.addLayout(button_layout, 0, 3, 1, 2) self.convert_button = QPushButton( self.ml.get_tr_text("tab_num_conv_inputbox_conv_btn")) self.convert_button.clicked.connect(self.convert_action) self.convert_button.setIcon(QIcon("static/images/exchange.png")) inputbox_layout.addWidget(self.convert_button, 1, 1, alignment=Qt.AlignCenter) # Output self.outputbox = QGroupBox( self.ml.get_tr_text("tab_num_conv_outputbox_gbox_name")) main_layout.addWidget(self.outputbox, 0, 1) outputbox_layout = QGridLayout() outputbox_layout.setHorizontalSpacing(25) self.outputbox.setLayout(outputbox_layout) self.bin_label = QLabel( self.ml.get_tr_text("tab_num_conv_outputbox_bin_lab")) self.bin_label.setAlignment(Qt.AlignCenter) self.dec_label = QLabel( self.ml.get_tr_text("tab_num_conv_outputbox_dec_lab")) self.dec_label.setAlignment(Qt.AlignCenter) self.hex_label = QLabel( self.ml.get_tr_text("tab_num_conv_outputbox_hex_lab")) self.hex_label.setAlignment(Qt.AlignCenter) self.bin_output = QLineEdit() self.bin_output.setReadOnly(True) self.bin_output.setAlignment(Qt.AlignCenter) self.dec_output = QLineEdit() self.dec_output.setReadOnly(True) self.dec_output.setAlignment(Qt.AlignCenter) self.hex_output = QLineEdit() self.hex_output.setReadOnly(True) self.hex_output.setAlignment(Qt.AlignCenter) self.bin_output_copy_button = QPushButton( self.ml.get_tr_text("tab_num_conv_outputbox_copy_btn")) self.bin_output_copy_button.setIcon( QIcon("static/images/copy_clipboard.png")) self.bin_output_copy_button.clicked.connect( lambda: copy_action(self.bin_output.text())) self.dec_output_copy_button = QPushButton( self.ml.get_tr_text("tab_num_conv_outputbox_copy_btn")) self.dec_output_copy_button.setIcon( QIcon("static/images/copy_clipboard.png")) self.dec_output_copy_button.clicked.connect( lambda: copy_action(self.dec_output.text())) self.hex_output_copy_button = QPushButton( self.ml.get_tr_text("tab_num_conv_outputbox_copy_btn")) self.hex_output_copy_button.setIcon( QIcon("static/images/copy_clipboard.png")) self.hex_output_copy_button.clicked.connect( lambda: copy_action(self.hex_output.text())) outputbox_layout.addWidget(self.bin_label, 0, 0) outputbox_layout.addWidget(self.bin_output, 0, 1) outputbox_layout.addWidget(self.bin_output_copy_button, 0, 2) outputbox_layout.addWidget(self.dec_label, 1, 0) outputbox_layout.addWidget(self.dec_output, 1, 1) outputbox_layout.addWidget(self.dec_output_copy_button, 1, 2) outputbox_layout.addWidget(self.hex_label, 2, 0) outputbox_layout.addWidget(self.hex_output, 2, 1) outputbox_layout.addWidget(self.hex_output_copy_button, 2, 2) # IP address/mask number conversion self.ip_address_number_conversion_box = QGroupBox( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_gbox_name")) main_layout.addWidget(self.ip_address_number_conversion_box, 1, 0, 1, 2) ip_address_number_conversion_layout = QGridLayout() ip_address_number_conversion_layout.setAlignment(Qt.AlignCenter) ip_address_number_conversion_layout.setHorizontalSpacing(25) ip_address_number_conversion_layout.setVerticalSpacing(24) self.ip_address_number_conversion_box.setLayout( ip_address_number_conversion_layout) self.input_label = QLabel( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_in_lab")) self.input_label.setAlignment(Qt.AlignCenter) self.input_label.setMaximumWidth(150) self.input_textfield = QLineEdit() self.input_textfield.setPlaceholderText("192.168.1.1") self.input_textfield.setAlignment(Qt.AlignLeft) self.input_textfield.setMaximumWidth(300) self.input_textfield.setAlignment(Qt.AlignCenter) self.input_textfield.returnPressed.connect(self.convert_action_2) ip_address_number_conversion_layout.addWidget(self.input_label, 0, 0) ip_address_number_conversion_layout.addWidget(self.input_textfield, 0, 1) button_layout_2 = QVBoxLayout() self.dec_to_bin_button = QRadioButton( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_dectobin")) self.dec_to_bin_button.clicked.connect( lambda: self.input_textfield.setPlaceholderText("192.168.1.1")) self.dec_to_bin_button.setMaximumWidth(150) self.bin_to_dec_button = QRadioButton( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_bintodec")) self.bin_to_dec_button.clicked.connect( lambda: self.input_textfield.setPlaceholderText( "11000000.10101000.00000001.00000001")) self.bin_to_dec_button.setMaximumWidth(150) self.dec_to_bin_button.setChecked(True) button_layout_2.addWidget(self.dec_to_bin_button) button_layout_2.addWidget(self.bin_to_dec_button) ip_address_number_conversion_layout.addLayout(button_layout_2, 0, 2) self.output_label = QLabel( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_out_lab")) self.output_label.setAlignment(Qt.AlignCenter) self.output_textfield = QLineEdit() self.output_textfield.setMaximumWidth(300) self.output_textfield.setReadOnly(True) self.output_textfield.setAlignment(Qt.AlignCenter) ip_address_number_conversion_layout.addWidget(self.output_label, 1, 0) ip_address_number_conversion_layout.addWidget(self.output_textfield, 1, 1) self.output_textfield_copy_button = QPushButton( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_copy_btn")) self.output_textfield_copy_button.setIcon( QIcon("static/images/copy_clipboard.png")) self.output_textfield_copy_button.clicked.connect( lambda: copy_action(self.output_textfield.text())) ip_address_number_conversion_layout.addWidget( self.output_textfield_copy_button, 1, 2, alignment=Qt.AlignLeft) self.convert_button_2 = QPushButton( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_convert_btn")) self.convert_button_2.clicked.connect(self.convert_action_2) self.convert_button_2.setIcon(QIcon("static/images/exchange.png")) ip_address_number_conversion_layout.addWidget( self.convert_button_2, 2, 0, 1, 3, alignment=Qt.AlignHCenter) def convert_action(self): if is_empty(self.input_number_textfield.text()): PopupWindow("warning", self.ml.get_tr_text("tab_num_conv_warning01"), self.input_number_textfield) else: if self.bin_button.isChecked(): self.source_bin(self.input_number_textfield.text()) elif self.dec_button.isChecked(): self.source_dec(self.input_number_textfield.text()) else: self.source_hex(self.input_number_textfield.text()) def source_bin(self, bin_number): bin_number_corrected = get_corrected_number(bin_number) bin_number_corrected_byte = bin_number_corrected.rjust(8, "0") if not is_correct_binary(bin_number_corrected): PopupWindow("warning", self.ml.get_tr_text("tab_num_conv_warning02"), self.input_number_textfield) else: if 0 <= int(bin_number_corrected, 2) <= 255: if bin_number_corrected != bin_number_corrected_byte: self.bin_format_base = bin_number_corrected self.bin_format_base_byte = bin_number_corrected_byte bin_format = get_bin_format( self.bin_format_base, self.ml.get_tr_text("byte_format_str"), self.bin_format_base_byte) else: bin_format = self.bin_format_base else: bin_format = bin_number_corrected dec_format = str(int(bin_number_corrected, 2)) hex_format = hex(int(bin_number_corrected, 2)).replace("0x", "").upper() self.bin_output.setText(bin_format) self.dec_output.setText(dec_format) self.hex_output.setText(hex_format) def source_dec(self, dec_number): dec_number_corrected = get_corrected_number(dec_number) if not is_correct_decimal(dec_number_corrected): PopupWindow("warning", self.ml.get_tr_text("tab_num_conv_warning03"), self.input_number_textfield) else: if 0 <= int(dec_number_corrected) <= 255: self.bin_format_base = bin(int(dec_number_corrected)).replace( "0b", "") self.bin_format_base_byte = self.bin_format_base.rjust(8, "0") if self.bin_format_base != self.bin_format_base_byte: bin_format = get_bin_format( self.bin_format_base, self.ml.get_tr_text("byte_format_str"), self.bin_format_base_byte) else: bin_format = self.bin_format_base else: bin_format = bin(int(dec_number_corrected)).replace("0b", "") dec_format = dec_number_corrected hex_format = hex(int(dec_number_corrected)).replace("0x", "").upper() self.bin_output.setText(bin_format) self.dec_output.setText(dec_format) self.hex_output.setText(hex_format) def source_hex(self, hex_number): hex_number_corrected = get_corrected_number(hex_number).upper() if not is_correct_hexadecimal(hex_number_corrected): PopupWindow("warning", self.ml.get_tr_text("tab_num_conv_warning04"), self.input_number_textfield) else: if 0 <= int(hex_number_corrected, 16) <= 255: self.bin_format_base = bin(int(hex_number_corrected, 16)).replace("0b", "") self.bin_format_base_byte = self.bin_format_base.rjust(8, "0") if self.bin_format_base != self.bin_format_base_byte: bin_format = get_bin_format( self.bin_format_base, self.ml.get_tr_text("byte_format_str"), self.bin_format_base_byte) else: bin_format = self.bin_format_base else: bin_format = bin(int(hex_number_corrected, 16)).replace("0b", "") dec_format = str(int(hex_number_corrected, 16)) hex_format = hex_number_corrected self.bin_output.setText(bin_format) self.dec_output.setText(dec_format) self.hex_output.setText(hex_format) def convert_action_2(self): if is_empty(self.input_textfield.text()): PopupWindow("warning", self.ml.get_tr_text("tab_num_conv_warning05"), self.input_textfield) elif self.dec_to_bin_button.isChecked(): if is_correct_any_ip_dec(self.input_textfield.text()): self.output_textfield.setText( dec_to_bin(self.input_textfield.text())) else: PopupWindow("warning", self.ml.get_tr_text("tab_num_conv_warning06"), self.input_textfield) else: if is_correct_any_ip_bin(self.input_textfield.text()): self.output_textfield.setText( bin_to_dec(self.input_textfield.text())) else: PopupWindow("warning", self.ml.get_tr_text("tab_num_conv_warning07"), self.input_textfield) def re_translate_ui(self, lang): self.ml = ManageLng(lang) self.inputbox.setTitle( self.ml.get_tr_text("tab_num_conv_inputbox_gbox_name")) self.input_number_label.setText( self.ml.get_tr_text("tab_num_conv_inputbox_in_number_lab")) self.bin_button.setText( self.ml.get_tr_text("tab_num_conv_inputbox_bin_chkbox")) self.dec_button.setText( self.ml.get_tr_text("tab_num_conv_inputbox_dec_chkbox")) self.hex_button.setText( self.ml.get_tr_text("tab_num_conv_inputbox_hex_chkbox")) self.convert_button.setText( self.ml.get_tr_text("tab_num_conv_inputbox_conv_btn")) self.outputbox.setTitle( self.ml.get_tr_text("tab_num_conv_outputbox_gbox_name")) self.bin_label.setText( self.ml.get_tr_text("tab_num_conv_outputbox_bin_lab")) self.dec_label.setText( self.ml.get_tr_text("tab_num_conv_outputbox_dec_lab")) self.hex_label.setText( self.ml.get_tr_text("tab_num_conv_outputbox_hex_lab")) self.bin_output_copy_button.setText( self.ml.get_tr_text("tab_num_conv_outputbox_copy_btn")) self.dec_output_copy_button.setText( self.ml.get_tr_text("tab_num_conv_outputbox_copy_btn")) self.hex_output_copy_button.setText( self.ml.get_tr_text("tab_num_conv_outputbox_copy_btn")) self.ip_address_number_conversion_box.setTitle( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_gbox_name")) self.input_label.setText( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_in_lab")) self.dec_to_bin_button.setText( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_dectobin")) self.bin_to_dec_button.setText( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_bintodec")) self.output_label.setText( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_out_lab")) self.output_textfield_copy_button.setText( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_copy_btn")) self.convert_button_2.setText( self.ml.get_tr_text("tab_num_conv_ip_mask_conv_convert_btn")) if self.bin_output.text(): self.bin_output.setText( get_bin_format(self.bin_format_base, self.ml.get_tr_text("byte_format_str"), self.bin_format_base_byte))
def __init__(self): super(IpSubnetCalculation, self).__init__() # Use language settings self.ml = ManageLng() # App attributes self.network_ip = None self.needed_networks = None self.subnet_bits = None self.default_networkbits = None self.calculation_worker = None main_layout = QVBoxLayout() main_layout.setSpacing(10) main_layout.setAlignment(Qt.AlignTop) self.setLayout(main_layout) top_bar = QGridLayout() # Left, top, right and bottom margins top_bar.setContentsMargins(40, 15, 40, 15) top_bar.setHorizontalSpacing(40) main_layout.addLayout(top_bar) self.starting_network_address_label = QLabel(self.ml.get_tr_text("tab_ipsubnet_starting_net")) self.number_of_subnets_label = QLabel(self.ml.get_tr_text("tab_ipsubnet_required_subnet_num")) self.starting_network_address_input = QLineEdit() self.starting_network_address_input.returnPressed.connect(self.calculation_action) self.number_of_subnets_input = QLineEdit() self.number_of_subnets_input.returnPressed.connect(self.calculation_action) top_bar.addWidget(self.starting_network_address_label, 0, 0) top_bar.addWidget(self.starting_network_address_input, 0, 1) top_bar.addWidget(self.number_of_subnets_label, 1, 0) top_bar.addWidget(self.number_of_subnets_input, 1, 1) self.calculation_button = QPushButton(self.ml.get_tr_text("tab_ipsubnet_calc_btn")) self.calculation_button.clicked.connect(self.calculation_action) self.calculation_button.setIcon(QIcon("static/images/get_info.png")) main_layout.addWidget(self.calculation_button, alignment=Qt.AlignCenter) self.table = QTableWidget() self.table.itemDoubleClicked.connect(copy_text_action) self.table.setColumnCount(6) self.table_column_names = [self.ml.get_tr_text("table_column_network_add"), self.ml.get_tr_text("table_column_ip_range"), self.ml.get_tr_text("table_column_broadcast_add"), self.ml.get_tr_text("table_column_subnet_mask"), self.ml.get_tr_text("table_column_prefix"), self.ml.get_tr_text("table_column_addressable_host")] self.table.setHorizontalHeaderLabels(self.table_column_names) # Automatic resizing of the columns to the content self.table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch) self.table.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch) self.table.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch) # Set table text align of vertical header self.table.verticalHeader().setDefaultAlignment(Qt.AlignCenter) # Fixed height of table rows self.table.verticalHeader().setSectionResizeMode(QHeaderView.Fixed) main_layout.addWidget(self.table) # Bottom bar bottom_bar = QHBoxLayout() # Create progressbar self.progressbar = QProgressBar() self.progressbar.setVisible(False) # Create cancel button self.cancel_btn = QPushButton(self.ml.get_tr_text("tab_ipsubnet_cancel_btn")) self.cancel_btn.setVisible(False) self.cancel_btn.clicked.connect(self.terminate_calculation) bottom_bar.addWidget(self.progressbar) bottom_bar.addWidget(self.cancel_btn) main_layout.addLayout(bottom_bar)
class IpSubnetCalculation(QWidget): def __init__(self): super(IpSubnetCalculation, self).__init__() # Use language settings self.ml = ManageLng() # App attributes self.network_ip = None self.needed_networks = None self.subnet_bits = None self.default_networkbits = None self.calculation_worker = None main_layout = QVBoxLayout() main_layout.setSpacing(10) main_layout.setAlignment(Qt.AlignTop) self.setLayout(main_layout) top_bar = QGridLayout() # Left, top, right and bottom margins top_bar.setContentsMargins(40, 15, 40, 15) top_bar.setHorizontalSpacing(40) main_layout.addLayout(top_bar) self.starting_network_address_label = QLabel(self.ml.get_tr_text("tab_ipsubnet_starting_net")) self.number_of_subnets_label = QLabel(self.ml.get_tr_text("tab_ipsubnet_required_subnet_num")) self.starting_network_address_input = QLineEdit() self.starting_network_address_input.returnPressed.connect(self.calculation_action) self.number_of_subnets_input = QLineEdit() self.number_of_subnets_input.returnPressed.connect(self.calculation_action) top_bar.addWidget(self.starting_network_address_label, 0, 0) top_bar.addWidget(self.starting_network_address_input, 0, 1) top_bar.addWidget(self.number_of_subnets_label, 1, 0) top_bar.addWidget(self.number_of_subnets_input, 1, 1) self.calculation_button = QPushButton(self.ml.get_tr_text("tab_ipsubnet_calc_btn")) self.calculation_button.clicked.connect(self.calculation_action) self.calculation_button.setIcon(QIcon("static/images/get_info.png")) main_layout.addWidget(self.calculation_button, alignment=Qt.AlignCenter) self.table = QTableWidget() self.table.itemDoubleClicked.connect(copy_text_action) self.table.setColumnCount(6) self.table_column_names = [self.ml.get_tr_text("table_column_network_add"), self.ml.get_tr_text("table_column_ip_range"), self.ml.get_tr_text("table_column_broadcast_add"), self.ml.get_tr_text("table_column_subnet_mask"), self.ml.get_tr_text("table_column_prefix"), self.ml.get_tr_text("table_column_addressable_host")] self.table.setHorizontalHeaderLabels(self.table_column_names) # Automatic resizing of the columns to the content self.table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch) self.table.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch) self.table.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch) # Set table text align of vertical header self.table.verticalHeader().setDefaultAlignment(Qt.AlignCenter) # Fixed height of table rows self.table.verticalHeader().setSectionResizeMode(QHeaderView.Fixed) main_layout.addWidget(self.table) # Bottom bar bottom_bar = QHBoxLayout() # Create progressbar self.progressbar = QProgressBar() self.progressbar.setVisible(False) # Create cancel button self.cancel_btn = QPushButton(self.ml.get_tr_text("tab_ipsubnet_cancel_btn")) self.cancel_btn.setVisible(False) self.cancel_btn.clicked.connect(self.terminate_calculation) bottom_bar.addWidget(self.progressbar) bottom_bar.addWidget(self.cancel_btn) main_layout.addLayout(bottom_bar) def check_input(self): # If the starting network address is empty if is_empty(self.starting_network_address_input.text()): PopupWindow("warning", self.ml.get_tr_text("tab_ipsubnet_warning01"), self.starting_network_address_input) return False else: # If the starting network address is incorrect if not is_correct_network_address(self.starting_network_address_input.text()): PopupWindow("warning", self.ml.get_tr_text("tab_ipsubnet_warning02"), self.starting_network_address_input) return False # If number of subnets is empty if is_empty(self.number_of_subnets_input.text()): PopupWindow("warning", self.ml.get_tr_text("tab_ipsubnet_warning03"), self.number_of_subnets_input) return False else: # If number of subnets is incorrect if not is_correct_number_of_subnets(self.number_of_subnets_input.text()): PopupWindow("warning", self.ml.get_tr_text("tab_ipsubnet_warning04"), self.number_of_subnets_input) return False self.storing_input_data() # If max addressable host least two and have least one needed network if self.get_max_addressable_hosts() >= 2 and int(self.needed_networks) >= 1: return True else: # Unreal subnet number PopupWindow("warning", self.ml.get_tr_text("tab_ipsubnet_warning05"), self.number_of_subnets_input) return False def storing_input_data(self): self.network_ip = self.starting_network_address_input.text() self.needed_networks = self.number_of_subnets_input.text() self.subnet_bits = int(log2(int(power_bit_length(int(self.needed_networks))))) def get_max_addressable_hosts(self): self.default_networkbits = 32 - get_32bit_format(self.get_starting_network_default_mask()).count("0") return pow(2, ((self.default_networkbits + self.subnet_bits) * "1").ljust(32, "0").count("0")) - 2 def get_starting_network_default_mask(self): first_octet = int(self.network_ip.split(".")[0]) if 1 <= first_octet < 128: return "255.0.0.0" elif 128 <= first_octet < 192: return "255.255.0.0" elif 192 <= first_octet < 224: return "255.255.255.0" def calculation_action(self): if self.check_input(): self.table.setRowCount(0) self.progressbar.setValue(0) self.progressbar.setMaximum(int(self.number_of_subnets_input.text())) self.calculation_worker = CalculationWorker(self) self.calculation_worker.setTerminationEnabled(True) self.calculation_worker.subnet_calculated.connect(self.subnet_calculated) self.calculation_worker.calculation_finished.connect(self.calculation_finished) self.progressbar.setVisible(True) self.cancel_btn.setVisible(True) self.calculation_worker.start() def subnet_calculated(self, row, subnet): self.table.insertRow(row) column = 0 for subnet_item in subnet.items(): self.table.setItem(row, column, TableItem(str(subnet_item[1]))) column += 1 self.progressbar.setValue(row + 1) def calculation_finished(self): self.progressbar.setVisible(False) self.cancel_btn.setVisible(False) self.progressbar.setValue(0) def terminate_calculation(self): if self.calculation_worker.isRunning(): self.calculation_worker.terminate() self.calculation_worker.wait() self.progressbar.setVisible(False) self.cancel_btn.setVisible(False) self.progressbar.setValue(0) def re_translate_ui(self, lang): self.ml = ManageLng(lang) self.starting_network_address_label.setText(self.ml.get_tr_text("tab_ipsubnet_starting_net")) self.number_of_subnets_label.setText(self.ml.get_tr_text("tab_ipsubnet_required_subnet_num")) self.calculation_button.setText(self.ml.get_tr_text("tab_ipsubnet_calc_btn")) self.table_column_names = [self.ml.get_tr_text("table_column_network_add"), self.ml.get_tr_text("table_column_ip_range"), self.ml.get_tr_text("table_column_broadcast_add"), self.ml.get_tr_text("table_column_subnet_mask"), self.ml.get_tr_text("table_column_prefix"), self.ml.get_tr_text("table_column_addressable_host")] self.table.setHorizontalHeaderLabels(self.table_column_names) self.cancel_btn.setText(self.ml.get_tr_text("tab_ipsubnet_cancel_btn"))
def re_translate_ui(self, lang): self.ml = ManageLng(lang) if self.ip_address_textfield.text(): ip = self.ip_address_textfield.text().split("/")[0] prefix = self.ip_address_textfield.text().split("/")[1] else: ip = None prefix = None self.prefix_mask_box.setTitle( self.ml.get_tr_text("tab_ip_information_prefix_mask_conv")) self.prefix_input.setPlaceholderText( self.ml.get_tr_text("tab_ip_information_prefix_ptext")) self.mask_input.setPlaceholderText( self.ml.get_tr_text("tab_ip_information_mask_ptext")) self.convert_btn.setText( self.ml.get_tr_text("tab_ip_information_conv_btn")) self.ip_information_box.setTitle( self.ml.get_tr_text("tab_ip_information_ipinfo_gbox")) self.ip_address_label.setText( self.ml.get_tr_text("tab_ip_information_ipadd_lab")) self.get_info_btn.setText( self.ml.get_tr_text("tab_ip_information_getinfo_btn")) self.ip_class_label.setText( self.ml.get_tr_text("tab_ip_information_ipclass_lab")) self.ip_class_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.ip_type_label.setText( self.ml.get_tr_text("tab_ip_information_iptype_lab")) self.ip_type_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.network_address_label.setText( self.ml.get_tr_text("tab_ip_information_netadd_lab")) self.network_address_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.subnet_mask_label.setText( self.ml.get_tr_text("tab_ip_information_mask_lab")) self.subnet_mask_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.first_addressable_ip_label.setText( self.ml.get_tr_text("tab_ip_information_firstip_lab")) self.first_addressable_ip_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.last_addressable_ip_label.setText( self.ml.get_tr_text("tab_ip_information_lastip_lab")) self.last_addressable_ip_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.broadcast_address_label.setText( self.ml.get_tr_text("tab_ip_information_bcastip_lab")) self.broadcast_address_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.next_network_address_label.setText( self.ml.get_tr_text("tab_ip_information_nextnetip_lab")) self.next_network_address_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.max_endpoint_label.setText( self.ml.get_tr_text("tab_ip_information_maxend_lab")) self.max_endpoint_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) if self.ip_class_textfield.text() and self.ip_type_textfield.text(): self.ip_class_textfield.setText( self.ml.get_tr_text(get_ip_class(ip))) self.ip_type_textfield.setText( self.ml.get_tr_text(get_ip_type(ip, prefix)))
def __init__(self): super(IpInformation, self).__init__() # Use language settings self.ml = ManageLng() main_layout = QGridLayout() self.setLayout(main_layout) # Prefix-mask-conversion group box self.prefix_mask_box = QGroupBox( self.ml.get_tr_text("tab_ip_information_prefix_mask_conv")) self.prefix_mask_box.setMaximumHeight(90) main_layout.addWidget(self.prefix_mask_box, 0, 0) prefix_mask_box_layout = QGridLayout() prefix_mask_box_layout.setContentsMargins(200, 20, 200, 20) prefix_mask_box_layout.setHorizontalSpacing(30) self.prefix_mask_box.setLayout(prefix_mask_box_layout) self.prefix_input = QLineEdit() self.prefix_input.setMaxLength(3) self.prefix_input.setAlignment(Qt.AlignCenter) self.prefix_input.setPlaceholderText( self.ml.get_tr_text("tab_ip_information_prefix_ptext")) prefix_mask_box_layout.addWidget(self.prefix_input, 0, 0) self.mask_input = QLineEdit() self.mask_input.setMaxLength(15) self.mask_input.setAlignment(Qt.AlignCenter) self.mask_input.setPlaceholderText( self.ml.get_tr_text("tab_ip_information_mask_ptext")) prefix_mask_box_layout.addWidget(self.mask_input, 0, 1) self.convert_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_conv_btn")) self.convert_btn.setIcon(QIcon("static/images/exchange.png")) self.convert_btn.clicked.connect(self.convert_action) self.prefix_input.returnPressed.connect(self.convert_action) self.mask_input.returnPressed.connect(self.convert_action) prefix_mask_box_layout.addWidget(self.convert_btn, 0, 2, alignment=Qt.AlignLeft) # IP information group box self.ip_information_box = QGroupBox( self.ml.get_tr_text("tab_ip_information_ipinfo_gbox")) main_layout.addWidget(self.ip_information_box, 1, 0) ip_information_box_layout = QGridLayout() ip_information_box_layout.setContentsMargins(80, 80, 80, 80) ip_information_box_layout.setHorizontalSpacing(30) ip_information_box_layout.setVerticalSpacing(15) self.ip_information_box.setLayout(ip_information_box_layout) self.ip_address_label = QLabel( self.ml.get_tr_text("tab_ip_information_ipadd_lab")) ip_information_box_layout.addWidget(self.ip_address_label, 0, 0, alignment=Qt.AlignCenter) self.ip_address_textfield = QLineEdit() self.ip_address_textfield.setPlaceholderText("192.168.1.100/24") self.ip_address_textfield.setAlignment(Qt.AlignCenter) self.ip_address_textfield.setMaxLength(18) ip_information_box_layout.addWidget(self.ip_address_textfield, 0, 1) self.get_info_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_getinfo_btn")) self.get_info_btn.setIcon(QIcon("static/images/get_info.png")) self.get_info_btn.clicked.connect(self.get_info_action) self.ip_address_textfield.returnPressed.connect(self.get_info_action) ip_information_box_layout.addWidget(self.get_info_btn, 0, 2) self.ip_class_label = QLabel( self.ml.get_tr_text("tab_ip_information_ipclass_lab")) ip_information_box_layout.addWidget(self.ip_class_label, 1, 0, alignment=Qt.AlignCenter) self.ip_class_textfield = QLineEdit() self.ip_class_textfield.setReadOnly(True) self.ip_class_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget(self.ip_class_textfield, 1, 1) self.ip_class_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.ip_class_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.ip_class_copy_btn.clicked.connect( lambda: copy_action(self.ip_class_textfield.text())) ip_information_box_layout.addWidget(self.ip_class_copy_btn, 1, 2) self.ip_type_label = QLabel( self.ml.get_tr_text("tab_ip_information_iptype_lab")) ip_information_box_layout.addWidget(self.ip_type_label, 2, 0, alignment=Qt.AlignCenter) self.ip_type_textfield = QLineEdit() self.ip_type_textfield.setReadOnly(True) self.ip_type_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget(self.ip_type_textfield, 2, 1) self.ip_type_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.ip_type_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.ip_type_copy_btn.clicked.connect( lambda: copy_action(self.ip_type_textfield.text())) ip_information_box_layout.addWidget(self.ip_type_copy_btn, 2, 2) self.network_address_label = QLabel( self.ml.get_tr_text("tab_ip_information_netadd_lab")) ip_information_box_layout.addWidget(self.network_address_label, 3, 0, alignment=Qt.AlignCenter) self.network_address_textfield = QLineEdit() self.network_address_textfield.setReadOnly(True) self.network_address_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget(self.network_address_textfield, 3, 1) self.network_address_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.network_address_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.network_address_copy_btn.clicked.connect( lambda: copy_action(self.network_address_textfield.text())) ip_information_box_layout.addWidget(self.network_address_copy_btn, 3, 2) self.subnet_mask_label = QLabel( self.ml.get_tr_text("tab_ip_information_mask_lab")) ip_information_box_layout.addWidget(self.subnet_mask_label, 4, 0, alignment=Qt.AlignCenter) self.subnet_mask_textfield = QLineEdit() self.subnet_mask_textfield.setReadOnly(True) self.subnet_mask_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget(self.subnet_mask_textfield, 4, 1) self.subnet_mask_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.subnet_mask_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.subnet_mask_copy_btn.clicked.connect( lambda: copy_action(self.subnet_mask_textfield.text())) ip_information_box_layout.addWidget(self.subnet_mask_copy_btn, 4, 2) self.first_addressable_ip_label = QLabel( self.ml.get_tr_text("tab_ip_information_firstip_lab")) ip_information_box_layout.addWidget(self.first_addressable_ip_label, 5, 0, alignment=Qt.AlignCenter) self.first_addressable_ip_textfield = QLineEdit() self.first_addressable_ip_textfield.setReadOnly(True) self.first_addressable_ip_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget( self.first_addressable_ip_textfield, 5, 1) self.first_addressable_ip_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.first_addressable_ip_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.first_addressable_ip_copy_btn.clicked.connect( lambda: copy_action(self.first_addressable_ip_textfield.text())) ip_information_box_layout.addWidget(self.first_addressable_ip_copy_btn, 5, 2) self.last_addressable_ip_label = QLabel( self.ml.get_tr_text("tab_ip_information_lastip_lab")) ip_information_box_layout.addWidget(self.last_addressable_ip_label, 6, 0, alignment=Qt.AlignCenter) self.last_addressable_ip_textfield = QLineEdit() self.last_addressable_ip_textfield.setReadOnly(True) self.last_addressable_ip_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget(self.last_addressable_ip_textfield, 6, 1) self.last_addressable_ip_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.last_addressable_ip_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.last_addressable_ip_copy_btn.clicked.connect( lambda: copy_action(self.last_addressable_ip_textfield.text())) ip_information_box_layout.addWidget(self.last_addressable_ip_copy_btn, 6, 2) self.broadcast_address_label = QLabel( self.ml.get_tr_text("tab_ip_information_bcastip_lab")) ip_information_box_layout.addWidget(self.broadcast_address_label, 7, 0, alignment=Qt.AlignCenter) self.broadcast_address_textfield = QLineEdit() self.broadcast_address_textfield.setReadOnly(True) self.broadcast_address_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget(self.broadcast_address_textfield, 7, 1) self.broadcast_address_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.broadcast_address_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.broadcast_address_copy_btn.clicked.connect( lambda: copy_action(self.broadcast_address_textfield.text())) ip_information_box_layout.addWidget(self.broadcast_address_copy_btn, 7, 2) self.next_network_address_label = QLabel( self.ml.get_tr_text("tab_ip_information_nextnetip_lab")) ip_information_box_layout.addWidget(self.next_network_address_label, 8, 0, alignment=Qt.AlignCenter) self.next_network_address_textfield = QLineEdit() self.next_network_address_textfield.setReadOnly(True) self.next_network_address_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget( self.next_network_address_textfield, 8, 1) self.next_network_address_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.next_network_address_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.next_network_address_copy_btn.clicked.connect( lambda: copy_action(self.next_network_address_textfield.text())) ip_information_box_layout.addWidget(self.next_network_address_copy_btn, 8, 2) self.max_endpoint_label = QLabel( self.ml.get_tr_text("tab_ip_information_maxend_lab")) ip_information_box_layout.addWidget(self.max_endpoint_label, 9, 0, alignment=Qt.AlignCenter) self.max_endpoint_textfield = QLineEdit() self.max_endpoint_textfield.setReadOnly(True) self.max_endpoint_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget(self.max_endpoint_textfield, 9, 1) self.max_endpoint_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.max_endpoint_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.max_endpoint_copy_btn.clicked.connect( lambda: copy_action(self.max_endpoint_textfield.text())) ip_information_box_layout.addWidget(self.max_endpoint_copy_btn, 9, 2)
class IpInformation(QWidget): def __init__(self): super(IpInformation, self).__init__() # Use language settings self.ml = ManageLng() main_layout = QGridLayout() self.setLayout(main_layout) # Prefix-mask-conversion group box self.prefix_mask_box = QGroupBox( self.ml.get_tr_text("tab_ip_information_prefix_mask_conv")) self.prefix_mask_box.setMaximumHeight(90) main_layout.addWidget(self.prefix_mask_box, 0, 0) prefix_mask_box_layout = QGridLayout() prefix_mask_box_layout.setContentsMargins(200, 20, 200, 20) prefix_mask_box_layout.setHorizontalSpacing(30) self.prefix_mask_box.setLayout(prefix_mask_box_layout) self.prefix_input = QLineEdit() self.prefix_input.setMaxLength(3) self.prefix_input.setAlignment(Qt.AlignCenter) self.prefix_input.setPlaceholderText( self.ml.get_tr_text("tab_ip_information_prefix_ptext")) prefix_mask_box_layout.addWidget(self.prefix_input, 0, 0) self.mask_input = QLineEdit() self.mask_input.setMaxLength(15) self.mask_input.setAlignment(Qt.AlignCenter) self.mask_input.setPlaceholderText( self.ml.get_tr_text("tab_ip_information_mask_ptext")) prefix_mask_box_layout.addWidget(self.mask_input, 0, 1) self.convert_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_conv_btn")) self.convert_btn.setIcon(QIcon("static/images/exchange.png")) self.convert_btn.clicked.connect(self.convert_action) self.prefix_input.returnPressed.connect(self.convert_action) self.mask_input.returnPressed.connect(self.convert_action) prefix_mask_box_layout.addWidget(self.convert_btn, 0, 2, alignment=Qt.AlignLeft) # IP information group box self.ip_information_box = QGroupBox( self.ml.get_tr_text("tab_ip_information_ipinfo_gbox")) main_layout.addWidget(self.ip_information_box, 1, 0) ip_information_box_layout = QGridLayout() ip_information_box_layout.setContentsMargins(80, 80, 80, 80) ip_information_box_layout.setHorizontalSpacing(30) ip_information_box_layout.setVerticalSpacing(15) self.ip_information_box.setLayout(ip_information_box_layout) self.ip_address_label = QLabel( self.ml.get_tr_text("tab_ip_information_ipadd_lab")) ip_information_box_layout.addWidget(self.ip_address_label, 0, 0, alignment=Qt.AlignCenter) self.ip_address_textfield = QLineEdit() self.ip_address_textfield.setPlaceholderText("192.168.1.100/24") self.ip_address_textfield.setAlignment(Qt.AlignCenter) self.ip_address_textfield.setMaxLength(18) ip_information_box_layout.addWidget(self.ip_address_textfield, 0, 1) self.get_info_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_getinfo_btn")) self.get_info_btn.setIcon(QIcon("static/images/get_info.png")) self.get_info_btn.clicked.connect(self.get_info_action) self.ip_address_textfield.returnPressed.connect(self.get_info_action) ip_information_box_layout.addWidget(self.get_info_btn, 0, 2) self.ip_class_label = QLabel( self.ml.get_tr_text("tab_ip_information_ipclass_lab")) ip_information_box_layout.addWidget(self.ip_class_label, 1, 0, alignment=Qt.AlignCenter) self.ip_class_textfield = QLineEdit() self.ip_class_textfield.setReadOnly(True) self.ip_class_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget(self.ip_class_textfield, 1, 1) self.ip_class_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.ip_class_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.ip_class_copy_btn.clicked.connect( lambda: copy_action(self.ip_class_textfield.text())) ip_information_box_layout.addWidget(self.ip_class_copy_btn, 1, 2) self.ip_type_label = QLabel( self.ml.get_tr_text("tab_ip_information_iptype_lab")) ip_information_box_layout.addWidget(self.ip_type_label, 2, 0, alignment=Qt.AlignCenter) self.ip_type_textfield = QLineEdit() self.ip_type_textfield.setReadOnly(True) self.ip_type_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget(self.ip_type_textfield, 2, 1) self.ip_type_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.ip_type_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.ip_type_copy_btn.clicked.connect( lambda: copy_action(self.ip_type_textfield.text())) ip_information_box_layout.addWidget(self.ip_type_copy_btn, 2, 2) self.network_address_label = QLabel( self.ml.get_tr_text("tab_ip_information_netadd_lab")) ip_information_box_layout.addWidget(self.network_address_label, 3, 0, alignment=Qt.AlignCenter) self.network_address_textfield = QLineEdit() self.network_address_textfield.setReadOnly(True) self.network_address_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget(self.network_address_textfield, 3, 1) self.network_address_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.network_address_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.network_address_copy_btn.clicked.connect( lambda: copy_action(self.network_address_textfield.text())) ip_information_box_layout.addWidget(self.network_address_copy_btn, 3, 2) self.subnet_mask_label = QLabel( self.ml.get_tr_text("tab_ip_information_mask_lab")) ip_information_box_layout.addWidget(self.subnet_mask_label, 4, 0, alignment=Qt.AlignCenter) self.subnet_mask_textfield = QLineEdit() self.subnet_mask_textfield.setReadOnly(True) self.subnet_mask_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget(self.subnet_mask_textfield, 4, 1) self.subnet_mask_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.subnet_mask_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.subnet_mask_copy_btn.clicked.connect( lambda: copy_action(self.subnet_mask_textfield.text())) ip_information_box_layout.addWidget(self.subnet_mask_copy_btn, 4, 2) self.first_addressable_ip_label = QLabel( self.ml.get_tr_text("tab_ip_information_firstip_lab")) ip_information_box_layout.addWidget(self.first_addressable_ip_label, 5, 0, alignment=Qt.AlignCenter) self.first_addressable_ip_textfield = QLineEdit() self.first_addressable_ip_textfield.setReadOnly(True) self.first_addressable_ip_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget( self.first_addressable_ip_textfield, 5, 1) self.first_addressable_ip_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.first_addressable_ip_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.first_addressable_ip_copy_btn.clicked.connect( lambda: copy_action(self.first_addressable_ip_textfield.text())) ip_information_box_layout.addWidget(self.first_addressable_ip_copy_btn, 5, 2) self.last_addressable_ip_label = QLabel( self.ml.get_tr_text("tab_ip_information_lastip_lab")) ip_information_box_layout.addWidget(self.last_addressable_ip_label, 6, 0, alignment=Qt.AlignCenter) self.last_addressable_ip_textfield = QLineEdit() self.last_addressable_ip_textfield.setReadOnly(True) self.last_addressable_ip_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget(self.last_addressable_ip_textfield, 6, 1) self.last_addressable_ip_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.last_addressable_ip_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.last_addressable_ip_copy_btn.clicked.connect( lambda: copy_action(self.last_addressable_ip_textfield.text())) ip_information_box_layout.addWidget(self.last_addressable_ip_copy_btn, 6, 2) self.broadcast_address_label = QLabel( self.ml.get_tr_text("tab_ip_information_bcastip_lab")) ip_information_box_layout.addWidget(self.broadcast_address_label, 7, 0, alignment=Qt.AlignCenter) self.broadcast_address_textfield = QLineEdit() self.broadcast_address_textfield.setReadOnly(True) self.broadcast_address_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget(self.broadcast_address_textfield, 7, 1) self.broadcast_address_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.broadcast_address_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.broadcast_address_copy_btn.clicked.connect( lambda: copy_action(self.broadcast_address_textfield.text())) ip_information_box_layout.addWidget(self.broadcast_address_copy_btn, 7, 2) self.next_network_address_label = QLabel( self.ml.get_tr_text("tab_ip_information_nextnetip_lab")) ip_information_box_layout.addWidget(self.next_network_address_label, 8, 0, alignment=Qt.AlignCenter) self.next_network_address_textfield = QLineEdit() self.next_network_address_textfield.setReadOnly(True) self.next_network_address_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget( self.next_network_address_textfield, 8, 1) self.next_network_address_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.next_network_address_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.next_network_address_copy_btn.clicked.connect( lambda: copy_action(self.next_network_address_textfield.text())) ip_information_box_layout.addWidget(self.next_network_address_copy_btn, 8, 2) self.max_endpoint_label = QLabel( self.ml.get_tr_text("tab_ip_information_maxend_lab")) ip_information_box_layout.addWidget(self.max_endpoint_label, 9, 0, alignment=Qt.AlignCenter) self.max_endpoint_textfield = QLineEdit() self.max_endpoint_textfield.setReadOnly(True) self.max_endpoint_textfield.setAlignment(Qt.AlignCenter) ip_information_box_layout.addWidget(self.max_endpoint_textfield, 9, 1) self.max_endpoint_copy_btn = QPushButton( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.max_endpoint_copy_btn.setIcon( QIcon("static/images/copy_clipboard.png")) self.max_endpoint_copy_btn.clicked.connect( lambda: copy_action(self.max_endpoint_textfield.text())) ip_information_box_layout.addWidget(self.max_endpoint_copy_btn, 9, 2) def convert_action(self): if is_empty(self.prefix_input.text()) and \ is_empty(self.mask_input.text()): PopupWindow("warning", self.ml.get_tr_text("tab_ip_information_warning01"), self.prefix_input) elif not is_empty(self.prefix_input.text()) and \ not is_empty(self.mask_input.text()): if is_correct_prefix(self.prefix_input.text()): prefix_corrected = self.prefix_input.text().replace( "/", "").replace("\\", "") self.mask_input.setText(get_mask_from_prefix(prefix_corrected)) else: PopupWindow( "warning", self.ml.get_tr_text("tab_ip_information_warning02"), self.prefix_input) else: if self.prefix_input.text(): if is_correct_prefix(self.prefix_input.text()): prefix_corrected = self.prefix_input.text().replace( "/", "").replace("\\", "") self.mask_input.setText( get_mask_from_prefix(prefix_corrected)) else: PopupWindow( "warning", self.ml.get_tr_text("tab_ip_information_warning02"), self.prefix_input) else: if is_correct_mask(self.mask_input.text()): self.prefix_input.setText( f"/{get_prefix_from_mask(self.mask_input.text())}") else: PopupWindow( "warning", self.ml.get_tr_text("tab_ip_information_warning03"), self.mask_input) def get_info_action(self): if is_empty(self.ip_address_textfield.text()): PopupWindow("warning", self.ml.get_tr_text("tab_ip_information_warning04"), self.ip_address_textfield) elif is_correct_ip_with_prefix(self.ip_address_textfield.text()): ip = self.ip_address_textfield.text().split("/")[0] ip_first_octet = int(str(ip).split(".")[0]) prefix = self.ip_address_textfield.text().split("/")[1] if ip_first_octet == 127: PopupWindow( "warning", self.ml.get_tr_text("tab_ip_information_warning05"), self.ip_address_textfield) elif 224 <= ip_first_octet <= 239: PopupWindow( "warning", self.ml.get_tr_text("tab_ip_information_warning06"), self.ip_address_textfield) elif 240 <= ip_first_octet <= 254: PopupWindow( "warning", self.ml.get_tr_text("tab_ip_information_warning07"), self.ip_address_textfield) elif ip_first_octet == 255: PopupWindow( "warning", self.ml.get_tr_text("tab_ip_information_warning08"), self.ip_address_textfield) else: self.ip_class_textfield.setText( self.ml.get_tr_text(get_ip_class(ip))) self.ip_type_textfield.setText( self.ml.get_tr_text(get_ip_type(ip, prefix))) self.network_address_textfield.setText( get_network_address(ip, prefix)) self.subnet_mask_textfield.setText( get_mask_from_prefix(prefix)) self.first_addressable_ip_textfield.setText( get_first_addressable_ip(ip, prefix)) self.last_addressable_ip_textfield.setText( get_last_addressable_ip(ip, prefix)) self.broadcast_address_textfield.setText( get_broadcast_ip(ip, prefix)) self.next_network_address_textfield.setText( get_next_network_ip(ip, prefix)) self.max_endpoint_textfield.setText( get_max_endpoint_formatted(prefix)) else: PopupWindow("warning", self.ml.get_tr_text("tab_ip_information_warning09"), self.ip_address_textfield) def re_translate_ui(self, lang): self.ml = ManageLng(lang) if self.ip_address_textfield.text(): ip = self.ip_address_textfield.text().split("/")[0] prefix = self.ip_address_textfield.text().split("/")[1] else: ip = None prefix = None self.prefix_mask_box.setTitle( self.ml.get_tr_text("tab_ip_information_prefix_mask_conv")) self.prefix_input.setPlaceholderText( self.ml.get_tr_text("tab_ip_information_prefix_ptext")) self.mask_input.setPlaceholderText( self.ml.get_tr_text("tab_ip_information_mask_ptext")) self.convert_btn.setText( self.ml.get_tr_text("tab_ip_information_conv_btn")) self.ip_information_box.setTitle( self.ml.get_tr_text("tab_ip_information_ipinfo_gbox")) self.ip_address_label.setText( self.ml.get_tr_text("tab_ip_information_ipadd_lab")) self.get_info_btn.setText( self.ml.get_tr_text("tab_ip_information_getinfo_btn")) self.ip_class_label.setText( self.ml.get_tr_text("tab_ip_information_ipclass_lab")) self.ip_class_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.ip_type_label.setText( self.ml.get_tr_text("tab_ip_information_iptype_lab")) self.ip_type_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.network_address_label.setText( self.ml.get_tr_text("tab_ip_information_netadd_lab")) self.network_address_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.subnet_mask_label.setText( self.ml.get_tr_text("tab_ip_information_mask_lab")) self.subnet_mask_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.first_addressable_ip_label.setText( self.ml.get_tr_text("tab_ip_information_firstip_lab")) self.first_addressable_ip_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.last_addressable_ip_label.setText( self.ml.get_tr_text("tab_ip_information_lastip_lab")) self.last_addressable_ip_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.broadcast_address_label.setText( self.ml.get_tr_text("tab_ip_information_bcastip_lab")) self.broadcast_address_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.next_network_address_label.setText( self.ml.get_tr_text("tab_ip_information_nextnetip_lab")) self.next_network_address_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) self.max_endpoint_label.setText( self.ml.get_tr_text("tab_ip_information_maxend_lab")) self.max_endpoint_copy_btn.setText( self.ml.get_tr_text("tab_ip_information_copy_btn")) if self.ip_class_textfield.text() and self.ip_type_textfield.text(): self.ip_class_textfield.setText( self.ml.get_tr_text(get_ip_class(ip))) self.ip_type_textfield.setText( self.ml.get_tr_text(get_ip_type(ip, prefix)))
def __init__(self): super(VlsmCalculation, self).__init__() # Use language settings self.ml = ManageLng() # App attributes self.network_ip = None self.prefix = None self.network_hosts = None self.length_of_subnets = [] self.subnets = [] main_layout = QVBoxLayout() main_layout.setSpacing(10) main_layout.setAlignment(Qt.AlignTop) self.setLayout(main_layout) top_bar = QGridLayout() # Left, top, right and bottom margins top_bar.setContentsMargins(40, 15, 40, 15) top_bar.setHorizontalSpacing(40) main_layout.addLayout(top_bar) self.starting_network_address_label = QLabel(self.ml.get_tr_text("tab_vlsm_starting_net")) self.endpoint_numbers_per_network_label = QLabel(self.ml.get_tr_text("tab_vlsm_endpoint_nums")) self.starting_network_prefix_label = QLabel(self.ml.get_tr_text("tab_vlsm_starting_net_prefix")) self.starting_network_address_input = QLineEdit() self.starting_network_address_input.returnPressed.connect(self.calculation_action) self.starting_network_prefix_input = QLineEdit() self.starting_network_prefix_input.returnPressed.connect(self.calculation_action) self.endpoint_numbers_per_network_input = QLineEdit() self.endpoint_numbers_per_network_input.returnPressed.connect(self.calculation_action) top_bar.addWidget(self.starting_network_address_label, 0, 0) top_bar.addWidget(self.starting_network_address_input, 0, 1) top_bar.addWidget(self.starting_network_prefix_label, 1, 0) top_bar.addWidget(self.starting_network_prefix_input, 1, 1) top_bar.addWidget(self.endpoint_numbers_per_network_label, 2, 0) top_bar.addWidget(self.endpoint_numbers_per_network_input, 2, 1) self.calculation_button = QPushButton(self.ml.get_tr_text("tab_vlsm_calc_btn")) self.calculation_button.setIcon(QIcon("static/images/get_info.png")) self.calculation_button.clicked.connect(self.calculation_action) main_layout.addWidget(self.calculation_button, alignment=Qt.AlignCenter) self.table = QTableWidget() self.table.setColumnCount(6) self.table.itemDoubleClicked.connect(copy_text_action) # Set table header labels self.table_column_names = [self.ml.get_tr_text("table_column_network_add"), self.ml.get_tr_text("table_column_ip_range"), self.ml.get_tr_text("table_column_broadcast_add"), self.ml.get_tr_text("table_column_subnet_mask"), self.ml.get_tr_text("table_column_prefix"), self.ml.get_tr_text("table_column_addressable_host")] self.table.setHorizontalHeaderLabels(self.table_column_names) # Automatic resizing of the columns to the content self.table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch) self.table.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch) self.table.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch) # Fixed height of table rows self.table.verticalHeader().setSectionResizeMode(QHeaderView.Fixed) # Set table text align of vertical header self.table.verticalHeader().setDefaultAlignment(Qt.AlignCenter) main_layout.addWidget(self.table)
class VlsmCalculation(QWidget): def __init__(self): super(VlsmCalculation, self).__init__() # Use language settings self.ml = ManageLng() # App attributes self.network_ip = None self.prefix = None self.network_hosts = None self.length_of_subnets = [] self.subnets = [] main_layout = QVBoxLayout() main_layout.setSpacing(10) main_layout.setAlignment(Qt.AlignTop) self.setLayout(main_layout) top_bar = QGridLayout() # Left, top, right and bottom margins top_bar.setContentsMargins(40, 15, 40, 15) top_bar.setHorizontalSpacing(40) main_layout.addLayout(top_bar) self.starting_network_address_label = QLabel(self.ml.get_tr_text("tab_vlsm_starting_net")) self.endpoint_numbers_per_network_label = QLabel(self.ml.get_tr_text("tab_vlsm_endpoint_nums")) self.starting_network_prefix_label = QLabel(self.ml.get_tr_text("tab_vlsm_starting_net_prefix")) self.starting_network_address_input = QLineEdit() self.starting_network_address_input.returnPressed.connect(self.calculation_action) self.starting_network_prefix_input = QLineEdit() self.starting_network_prefix_input.returnPressed.connect(self.calculation_action) self.endpoint_numbers_per_network_input = QLineEdit() self.endpoint_numbers_per_network_input.returnPressed.connect(self.calculation_action) top_bar.addWidget(self.starting_network_address_label, 0, 0) top_bar.addWidget(self.starting_network_address_input, 0, 1) top_bar.addWidget(self.starting_network_prefix_label, 1, 0) top_bar.addWidget(self.starting_network_prefix_input, 1, 1) top_bar.addWidget(self.endpoint_numbers_per_network_label, 2, 0) top_bar.addWidget(self.endpoint_numbers_per_network_input, 2, 1) self.calculation_button = QPushButton(self.ml.get_tr_text("tab_vlsm_calc_btn")) self.calculation_button.setIcon(QIcon("static/images/get_info.png")) self.calculation_button.clicked.connect(self.calculation_action) main_layout.addWidget(self.calculation_button, alignment=Qt.AlignCenter) self.table = QTableWidget() self.table.setColumnCount(6) self.table.itemDoubleClicked.connect(copy_text_action) # Set table header labels self.table_column_names = [self.ml.get_tr_text("table_column_network_add"), self.ml.get_tr_text("table_column_ip_range"), self.ml.get_tr_text("table_column_broadcast_add"), self.ml.get_tr_text("table_column_subnet_mask"), self.ml.get_tr_text("table_column_prefix"), self.ml.get_tr_text("table_column_addressable_host")] self.table.setHorizontalHeaderLabels(self.table_column_names) # Automatic resizing of the columns to the content self.table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch) self.table.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch) self.table.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch) # Fixed height of table rows self.table.verticalHeader().setSectionResizeMode(QHeaderView.Fixed) # Set table text align of vertical header self.table.verticalHeader().setDefaultAlignment(Qt.AlignCenter) main_layout.addWidget(self.table) def check_input(self): # If the starting network address is empty if is_empty(self.starting_network_address_input.text()): PopupWindow("warning", self.ml.get_tr_text("tab_vlsm_warning01"), self.starting_network_address_input) return False else: # If the starting network address is incorrect if not is_correct_network_address(self.starting_network_address_input.text()): PopupWindow("warning", self.ml.get_tr_text("tab_vlsm_warning02"), self.starting_network_address_input) return False # If endpoint numbers are empty if is_empty(self.endpoint_numbers_per_network_input.text()): PopupWindow("warning", self.ml.get_tr_text("tab_vlsm_warning03"), self.endpoint_numbers_per_network_input) return False else: # If endpoint numbers are incorrect if not is_correct_endpoint_numbers_per_network(self.endpoint_numbers_per_network_input.text()): PopupWindow("warning", self.ml.get_tr_text("tab_vlsm_warning04"), self.endpoint_numbers_per_network_input) return False # If prefix is incorrect self.prefix = self.starting_network_prefix_input.text().replace("/", "").replace("\\", "") if not is_correct_prefix(self.prefix): PopupWindow("warning", self.ml.get_tr_text("tab_vlsm_warning05"), self.starting_network_prefix_input) return False return True def inject_data_to_table(self): row = 0 for subnet in self.subnets: self.table.insertRow(row) column = 0 for i in subnet.items(): value = str(i[1]) self.table.setItem(row, column, TableItem(value)) column += 1 row += 1 def storing_input_data(self): self.network_ip = self.starting_network_address_input.text() self.network_hosts = self.endpoint_numbers_per_network_input.text() def calculation_action(self): if self.check_input(): # Stores user-specified data self.storing_input_data() # Resets table before injects data self.table.setRowCount(0) self.processing() self.inject_data_to_table() # Clears lists to next calculation self.length_of_subnets.clear() self.subnets.clear() def inject_data_to_dict(self): for network in self.length_of_subnets: hostbits = int(log2(network)) prefix = 32 - hostbits mask = get_ip_from_32bit_format(("0" * hostbits).rjust(32, "1")) self.subnets.append({self.table_column_names[0]: self.network_ip, self.table_column_names[1]: f"{get_first_addressable_ip(self.network_ip)} - " f"{get_last_addressable_ip(self.network_ip, mask)}", self.table_column_names[2]: get_broadcast_ip(self.network_ip, mask), self.table_column_names[3]: mask, self.table_column_names[4]: f"/{prefix}", self.table_column_names[5]: pow(2, hostbits) - 2}) self.network_ip = get_next_network_ip(self.network_ip, mask) def processing(self): # User-specified hosts are converted to the nearest 2 powers for hosts in self.network_hosts.split(","): if int(hosts) > 0: hosts = int(hosts) + 2 self.length_of_subnets.append(power_bit_length(int(hosts))) # The largest host network will be pre-sorting self.length_of_subnets.sort(reverse=True) sum_all_hosts = sum(self.length_of_subnets) if is_empty(self.prefix): first_octet = int(self.network_ip.split(".")[0]) # Determining what could be the default mask based on network address if 1 <= first_octet < 128: if sum_all_hosts <= pow(2, 24): self.inject_data_to_dict() else: PopupWindow("warning", self.ml.get_tr_text("tab_vlsm_warning06"), self.endpoint_numbers_per_network_input) elif 128 <= first_octet < 192: if sum_all_hosts <= pow(2, 16): self.inject_data_to_dict() else: PopupWindow("warning", self.ml.get_tr_text("tab_vlsm_warning07"), self.endpoint_numbers_per_network_input) elif 192 <= first_octet < 224: if sum_all_hosts <= pow(2, 8): self.inject_data_to_dict() else: PopupWindow("warning", self.ml.get_tr_text("tab_vlsm_warning08"), self.endpoint_numbers_per_network_input) else: if sum_all_hosts <= pow(2, 32 - int(self.prefix)): self.inject_data_to_dict() else: s1 = self.ml.get_tr_text("tab_vlsm_warning09a") s2 = self.ml.get_tr_text("tab_vlsm_warning09b") PopupWindow("warning", f"{s1} /{self.prefix} {s2}", self.endpoint_numbers_per_network_input) def re_translate_ui(self, lang): self.ml = ManageLng(lang) self.starting_network_address_label.setText(self.ml.get_tr_text("tab_vlsm_starting_net")) self.endpoint_numbers_per_network_label.setText(self.ml.get_tr_text("tab_vlsm_endpoint_nums")) self.starting_network_prefix_label.setText(self.ml.get_tr_text("tab_vlsm_starting_net_prefix")) self.calculation_button.setText(self.ml.get_tr_text("tab_vlsm_calc_btn")) self.table_column_names = [self.ml.get_tr_text("table_column_network_add"), self.ml.get_tr_text("table_column_ip_range"), self.ml.get_tr_text("table_column_broadcast_add"), self.ml.get_tr_text("table_column_subnet_mask"), self.ml.get_tr_text("table_column_prefix"), self.ml.get_tr_text("table_column_addressable_host")] self.table.setHorizontalHeaderLabels(self.table_column_names)