def convert_click(self): """ Called when the user clicks the convert button Takes the given input and outputs all the possible conversions """ # If no input don't attempt to convert if self.input.text() == "" or self.input.text().isspace(): return if self.container is not None: self.container.deleteLater() self.container.update() self.v_layout.deleteLater() self.v_layout.update() self.scroll_area.deleteLater() self.scroll_area.update() # Delete all the current results for result in self.results: result.deleteLater() result.update() # Clear the results list self.results.clear() # Create the converter converter = \ glfcn(self.options.current)\ .get_unit_from_symbol(self.current_option_symbol)\ .cls(self.input.text()) self.container = QWidget(self) self.v_layout = QVBoxLayout() # Get all the results and append them to the results list and crate a visible label for each result for i, (key, value) in enumerate(converter.all().items()): value = round(value, 6) if type(value) != str else value label = QLabel(f"{value} <strong>{key}</strong>", self) label.setProperty("class", "result" if i != len(converter.all().items()) - 1 else "result no-border") label.setGeometry(0, 230 + (i * 70), 300, 70) label.setFixedSize(300, 70) self.v_layout.addWidget(label, alignment=Qt.AlignLeft) # Append the results self.results.append(label) self.container.setLayout(self.v_layout) self.container.setFixedWidth(300) self.scroll_area = QScrollArea(self) self.scroll_area.setWidget(self.container) self.scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.scroll_area.setGeometry(0, 230, 300, 270) self.scroll_area.show() self.container.show()
def next_button_click(self, event): """ Called when the next option button is clicked. Changes the conversion to the next option :param event: Event type """ self.options.change_next_to_current() self.combo.clear() self.combo.addItems(glfcn(self.options.current)) self.update_navigation()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # noinspection PyArgumentList # noinspection PyCallByClass QFontDatabase.addApplicationFont("fonts/krub/Krub-Regular.ttf") # Set window properties self.setWindowTitle("Converter") self.setWindowIcon(QIcon("images/app/logo.png")) self.setFixedSize(300, 500) self.setObjectName("main") self.setStyleSheet(open("main.css").read()) # Navigation buttons/labels placeholders self.previous: QLabel = None self.current: QLabel = None self.next: QLabel = None # Animation property for the navigation buttons/labels self.animation: QPropertyAnimation = None # List of available conversions self.options: helpers.ListHelper = helpers.ListHelper(CONVERSIONS) self.current_option_symbol: str = glfcn(self.options.current)[0].symbol # Create the navigation self.create_navigation() # Main conversion UI placeholders self.input: QLineEdit = None self.convert: QPushButton = None self.clear: QPushButton = None self.combo: QComboBox = None self.results: List[QLabel] = [] self.container: QWidget = None self.v_layout: QVBoxLayout = None self.scroll_area: QScrollArea = None # Create the main UI self.create_ui() # Show all self.show()
def create_ui(self): """ Creates the main UI for the conversions """ self.input = QLineEdit(self) self.input.setGeometry(0, 90, 250, 70) self.combo = QComboBox(self) self.combo.currentIndexChanged.connect(self.change_option_symbol) self.combo.setObjectName("combo") self.combo.addItems(glfcn(self.options.current)) self.combo.setGeometry(230, 90, 70, 70) self.convert = QPushButton("Convert", self) self.convert.clicked.connect(self.convert_click) self.convert.setObjectName("convert") self.convert.setGeometry(0, 160, 150, 70) self.clear = QPushButton("Clear", self) self.clear.clicked.connect(self.clear_click) self.clear.setObjectName("clear") self.clear.setGeometry(150, 160, 150, 70)
def change_option_symbol(self, i): """ Called when the user changed the unit of conversion for the given conversion type :param i: Current index """ self.current_option_symbol = glfcn(self.options.current)[i].symbol