Skip to content

Commit

Permalink
feat: add widget for settings
Browse files Browse the repository at this point in the history
  • Loading branch information
lvjonok committed Mar 18, 2024
1 parent a26ca34 commit c45ed5b
Show file tree
Hide file tree
Showing 4 changed files with 847 additions and 59 deletions.
59 changes: 59 additions & 0 deletions src/qt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from PyQt5.QtCore import QEventLoop
from PyQt5.QtWidgets import QProgressDialog, QLineEdit
from PyQt5.QtCore import Qt
from PyQt5 import QtGui


class ClickableLineEdit(QLineEdit):
Expand Down Expand Up @@ -36,6 +37,64 @@ def _internal_done_callback(self, future):
self.finished.emit(data)


class LineEdit(QLineEdit):
colorize_invalid_value = False

def __init__(self, parent=None):
super().__init__(parent)
self.returnPressed.connect(self.value_formatting)
self.textChanged.connect(self.input_validation)
self.textChanged.connect(self.colorize_field)

def setValidator(self, validator, colorize_invalid_value=False):
self.colorize_invalid_value = colorize_invalid_value
super().setValidator(validator)

def focusOutEvent(self, event):
self.value_formatting()
self.colorize_field()
super().focusOutEvent(event)

def fill_empty(self):
if (not self.text()) or (self.text() == "."):
self.setText("0")

def value_formatting(self):
self.fill_empty()
if isinstance(self.validator(), QtGui.QDoubleValidator):
cursor_position = self.cursorPosition()
self.setText(str(float(self.text())))
self.setCursorPosition(cursor_position)

def input_validation(self):
cursor_position = self.cursorPosition()
self.setText(self.text().replace(",", "."))

if (not self.colorize_invalid_value) and self.validator():
value = float(self.text()) if self.text() else 0

max_value = self.validator().top()
min_value = self.validator().bottom()

if value > max_value:
self.setText(str(max_value))
if value < min_value:
self.setText(str(min_value))
self.setCursorPosition(cursor_position)

def colorize_field(self):
default_background_color = "#0e1621"
invalid_value_background_color = "#ff6e00"

if self.colorize_invalid_value:
if self.hasAcceptableInput() or (not self.text()):
self.setStyleSheet(f"background-color: {default_background_color}")
else:
self.setStyleSheet(
f"background-color: {invalid_value_background_color}"
)


def progress_dialog(title, msg, work_fn, parent=None):
"""Show a blocking progress dialog while executing work in background thread"""
progress = QProgressDialog(msg, None, 0, 0, parent=parent)
Expand Down
Loading

0 comments on commit c45ed5b

Please sign in to comment.