From 9e79d986e4d3e398887581061d315408ce52c99e Mon Sep 17 00:00:00 2001 From: Roland W-H Date: Sun, 27 Apr 2025 11:52:14 +0100 Subject: [PATCH] add additional input validation --- src/main_window.py | 4 ++++ src/platform_edit.py | 10 ++++++++-- src/platform_list.py | 25 +++++++++++++++++-------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/main_window.py b/src/main_window.py index d35fc3e..72c8806 100644 --- a/src/main_window.py +++ b/src/main_window.py @@ -78,6 +78,7 @@ class SIPPCompare(QMainWindow): fund_trades_num = int(self.fund_trades_combo.currentText()) share_trades_num = int(self.share_trades_combo.currentText()) shares_value = (1 - (slider_val / 100)) * value_num + index = 0 for platform in self.platform_list_win.plat_list: if not platform.enabled: @@ -88,6 +89,8 @@ class SIPPCompare(QMainWindow): share_plat_fees = 0.0 share_deal_fees = 0.0 plat_name = platform.plat_name + if plat_name is None or plat_name == "": + plat_name = f"Unnamed [ID: {index}]" if platform.fund_deal_fee is not None: fund_deal_fees = fund_trades_num * platform.fund_deal_fee @@ -119,6 +122,7 @@ class SIPPCompare(QMainWindow): share_deal_fees = platform.share_deal_fee * share_trades_num self.results.append([fund_plat_fees, fund_deal_fees, share_plat_fees, share_deal_fees, plat_name]) + index += 1 # Save details entered by user for next session self.db.write_user_details(value_num, slider_val, share_trades_num, fund_trades_num) diff --git a/src/platform_edit.py b/src/platform_edit.py index 97960f9..4ff38b6 100644 --- a/src/platform_edit.py +++ b/src/platform_edit.py @@ -1,7 +1,7 @@ from PyQt6 import uic from PyQt6.QtCore import QRegularExpression, QRect from PyQt6.QtGui import QRegularExpressionValidator, QFont, QIcon -from PyQt6.QtWidgets import QWidget, QLabel +from PyQt6.QtWidgets import QLabel, QDialog import resource_finder from db_handler import DBHandler @@ -9,7 +9,7 @@ from data_struct import Platform from widgets.fastedit_spinbox import FastEditQDoubleSpinBox -class PlatformEdit(QWidget): +class PlatformEdit(QDialog): def __init__(self, plat: Platform): super().__init__() # Import Qt Designer UI XML file @@ -178,6 +178,8 @@ class PlatformEdit(QWidget): else: self.plat.share_deal_reduce_amount = None + self.accept() + # This method does multiple things in order to validate the user's inputs: # 1) Check all required fields have a non-zero value # 2) If an optional checkbox is toggled: toggle editing of the corresponding field @@ -347,3 +349,7 @@ class PlatformEdit(QWidget): self.check_valid() self.update_tier_labels() + + def closeEvent(self, event): + event.ignore() + self.reject() diff --git a/src/platform_list.py b/src/platform_list.py index 77574e1..d37ea22 100644 --- a/src/platform_list.py +++ b/src/platform_list.py @@ -63,7 +63,7 @@ class PlatformList(QWidget): # Initialise class variables self.db = db self.plat_edit_win = None - self.plat_list_dialog = PlatformRename() + self.plat_list_dialog = None self.del_plat_dialog = RemoveConfirm() self.plat_list = [] self.plat_name_list = [] @@ -95,6 +95,7 @@ class PlatformList(QWidget): self.platListWidget.addItem(item) def add_platform(self): + self.plat_list_dialog = PlatformRename() name_dialog_res = self.plat_list_dialog.exec() if name_dialog_res == QDialog.DialogCode.Accepted: name = self.plat_list_dialog.new_name @@ -110,13 +111,19 @@ class PlatformList(QWidget): index, [[0], [0]], name_param, True, 0, 0, None, 0, None, None) ) self.plat_edit_win = PlatformEdit(self.plat_list[index]) - self.plat_edit_win.show() + plat_edit_res = self.plat_edit_win.exec() + if plat_edit_res == QDialog.DialogCode.Rejected: + self.plat_list.pop() + self.platListWidget.takeItem(self.platListWidget.count() - 1) def get_enabled_state(self): index = self.platListWidget.currentRow() - is_enabled = self.plat_list[index].enabled - if is_enabled: - self.plat_enabled_check.setChecked(True) + if len(self.plat_list) > 0: + is_enabled = self.plat_list[index].enabled + if is_enabled: + self.plat_enabled_check.setChecked(True) + else: + self.plat_enabled_check.setChecked(False) else: self.plat_enabled_check.setChecked(False) @@ -127,8 +134,9 @@ class PlatformList(QWidget): def edit_platform(self): index = self.platListWidget.currentRow() - self.plat_edit_win = PlatformEdit(self.plat_list[index]) - self.plat_edit_win.show() + if len(self.plat_list) > 0: + self.plat_edit_win = PlatformEdit(self.plat_list[index]) + self.plat_edit_win.exec() def save_platforms(self): self.db.write_platforms(self.plat_list) @@ -138,7 +146,8 @@ class PlatformList(QWidget): def toggle_platform_state(self): index = self.platListWidget.currentRow() state = self.plat_enabled_check.isChecked() - self.plat_list[index].enabled = state + if len(self.plat_list) > 0 and index >= 0: + self.plat_list[index].enabled = state def remove_platform(self): index = self.platListWidget.currentRow()