Skip to content

Instantly share code, notes, and snippets.

@loathingKernel
Last active October 8, 2021 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loathingKernel/fd2180ee4f178c37a4e73d22c7030d41 to your computer and use it in GitHub Desktop.
Save loathingKernel/fd2180ee4f178c37a4e73d22c7030d41 to your computer and use it in GitHub Desktop.
class IndicatorLineEdit(QWidget):
def __init__(self,
text: str = "",
edit_func: Callable[[str], bool] = None,
save_func: Callable[[str], None] = None,
horiz_policy: QSizePolicy = QSizePolicy.Expanding,
parent=None):
super(IndicatorLineEdit, self).__init__(parent=parent)
self.setObjectName("IndicatorTextEdit")
self.layout = QHBoxLayout(self)
self.layout.setObjectName("layout")
self.layout.setContentsMargins(0, 0, 0, 0)
self.line_edit = QLineEdit(self)
self.line_edit.setObjectName("line_edit")
self.line_edit.setSizePolicy(horiz_policy, QSizePolicy.Fixed)
self.layout.addWidget(self.line_edit)
if edit_func is not None:
self.indicator_label = QLabel()
self.indicator_label.setPixmap(icon("ei.info-circle", color="gray").pixmap(16, 16))
self.indicator_label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.layout.addWidget(self.indicator_label)
_translate = QCoreApplication.translate
self.line_edit.setPlaceholderText(_translate("PathEdit", "Default"))
if text:
self.line_edit.setText(text)
self.edit_func = edit_func
self.save_func = save_func
self.line_edit.textChanged.connect(self.__edit)
if self.edit_func is None:
self.line_edit.textChanged.connect(self.__save)
def text(self):
return self.line_edit.text()
def setText(self, text: str):
self.line_edit.setText(text)
def setPlaceholderText(self, text: str):
self.line_edit.setPlaceholderText(text)
def __indicator(self, res):
color = "green" if res else "red"
self.indicator_label.setPixmap(icon("ei.info-circle", color=color).pixmap(16, 16))
def __edit(self, text):
if self.edit_func is not None:
res = self.edit_func(text)
self.__indicator(res)
if res:
self.__save(text)
def __save(self, text):
if self.save_func is not None:
self.save_func(text)
class PathEdit(IndicatorLineEdit):
def __init__(self,
text: str = "",
file_type: QFileDialog.FileType = QFileDialog.AnyFile,
type_filter: str = "",
name_filter: str = "",
edit_func: Callable[[str], bool] = None,
save_func: Callable[[str], None] = None,
horiz_policy: QSizePolicy = QSizePolicy.Expanding,
parent=None):
super(PathEdit, self).__init__(text=text,
edit_func=edit_func,
save_func=save_func,
horiz_policy=horiz_policy,
parent=parent)
self.setObjectName("PathEdit")
self.path_select = QToolButton(self)
self.path_select.setObjectName("path_select")
self.layout.addWidget(self.path_select)
_translate = QCoreApplication.translate
self.path_select.setText(_translate("PathEdit", "Browse..."))
self.type_filter = type_filter
self.name_filter = name_filter
self.file_type = file_type
self.path_select.clicked.connect(self.__set_path)
def __set_path(self):
dlg_path = self.line_edit.text()
if not dlg_path:
dlg_path = os.path.expanduser("~/")
dlg = QFileDialog(self, self.tr("Choose path"), dlg_path)
dlg.setFileMode(self.file_type)
if self.type_filter:
dlg.setFilter([self.type_filter])
if self.name_filter:
dlg.setNameFilter(self.name_filter)
if dlg.exec_():
names = dlg.selectedFiles()
self.line_edit.setText(names[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment