Guest User

Untitled

a guest
Jun 18th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. import sys
  2. from PyQt5.QtCore import pyqtSignal, QRegularExpression
  3. from PyQt5.QtWidgets import (QApplication, QFrame, QLabel, QLineEdit,
  4. QHBoxLayout, QVBoxLayout, QWidget, QPushButton)
  5.  
  6. class FormGroup(QWidget):
  7. groupValidation = pyqtSignal(bool)
  8. def __init__(self, orientation='vertical'):
  9. super().__init__()
  10.  
  11. self.form_controls = []
  12. self.main_layout = QVBoxLayout() if orientation == 'vertical' else QHBoxLayout()
  13. self.setLayout(self.main_layout)
  14.  
  15. def addControl(self, control):
  16. if not isinstance(self, FormControl):
  17. self.form_controls.append(control)
  18. self.main_layout.addWidget(control)
  19. control.inputValidation.connect(self.validateGroup)
  20. else:
  21. raise ValueError('could not add object of type {}'.format(type(control)))
  22.  
  23.  
  24. def validateGroup(self, _):
  25. for control in self.form_controls:
  26. if not control.valid:
  27. self.groupValidation.emit(False)
  28. return
  29.  
  30. self.groupValidation.emit(True)
  31.  
  32.  
  33. class FormControl(QWidget):
  34. inputValidation = pyqtSignal(bool)
  35. def __init__(self, title=''):
  36. super().__init__()
  37.  
  38. control_layout = QVBoxLayout()
  39.  
  40. self.form_label = QLabel()
  41. self.form_control = QLineEdit()
  42. self.validation_label = QLabel()
  43. self.validation_label.setStyleSheet('color: red')
  44.  
  45. control_layout.addWidget(self.form_label)
  46. control_layout.addWidget(self.form_control)
  47. control_layout.addWidget(self.validation_label)
  48. control_layout.addStretch(1)
  49.  
  50. self.setLayout(control_layout)
  51.  
  52. self.title = title
  53. self.required = False
  54. self.required_error_message = '{} is required.'
  55.  
  56. self.email = False
  57. self.email_error_message = 'The value should be an email'
  58. self.email_regex = QRegularExpression("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b",
  59. QRegularExpression.CaseInsensitiveOption)
  60.  
  61. self.valid = False
  62.  
  63. self.form_control.textChanged.connect(self.validate)
  64.  
  65. @property
  66. def title(self):
  67. return self._title
  68.  
  69. @title.setter
  70. def title(self, title):
  71. self._title = title
  72. self.form_label.setText('{}:'.format(self._title))
  73.  
  74. def validate(self, input_text):
  75. text = input_text.strip()
  76. if self.required and not text:
  77. self.onInvalid(self.required_error_message.format(self.title))
  78. return
  79. if self.email and not self.email_regex.match(text).hasMatch():
  80. self.onInvalid(self.email_error_message)
  81. return
  82.  
  83. self.onValid()
  84.  
  85. def onValid(self):
  86. self.form_control.setStyleSheet('')
  87. self.validation_label.setText('')
  88. self.valid = True
  89. self.inputValidation.emit(True)
  90.  
  91. def onInvalid(self, error_message):
  92. self.form_control.setStyleSheet('border: 1px solid red;')
  93. self.validation_label.setText(error_message)
  94. self.valid = False
  95. self.inputValidation.emit(False)
  96.  
  97. class Window(QWidget):
  98. def __init__(self):
  99. super().__init__()
  100. self.setWindowTitle('Validation Demo')
  101. self.setMinimumSize(400, 300)
  102.  
  103. layout = QVBoxLayout()
  104.  
  105. name = FormControl('Name')
  106. name.required = True
  107.  
  108. email = FormControl('Email')
  109. email.required = True
  110. email.email = True
  111.  
  112. self.button = QPushButton('Submit')
  113. self.button.setDisabled(True)
  114.  
  115. self.group = FormGroup()
  116. self.group.groupValidation.connect(self.formValidation)
  117. self.group.addControl(name)
  118. self.group.addControl(email)
  119.  
  120. button_layout = QHBoxLayout()
  121. button_layout.addStretch(1)
  122. button_layout.addWidget(self.button)
  123.  
  124. layout.addWidget(self.group)
  125. layout.addLayout(button_layout)
  126. layout.addStretch(1)
  127. self.setLayout(layout)
  128.  
  129. def formValidation(self, is_valid):
  130. if is_valid:
  131. self.button.setEnabled(True)
  132. else:
  133. self.button.setDisabled(True)
  134.  
  135. if __name__ == '__main__':
  136. app = QApplication(sys.argv)
  137.  
  138. w = Window()
  139. w.show()
  140.  
  141. sys.exit(app.exec_())
Add Comment
Please, Sign In to add comment