quality_history_panel.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. """
  2. Quality History Panel
  3. Panel for displaying recent quality test results in a table format.
  4. """
  5. from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QLabel,
  6. QPushButton, QSizePolicy, QTableWidget,
  7. QTableWidgetItem, QHeaderView, QScrollArea)
  8. from PyQt5.QtCore import Qt
  9. from PyQt5.QtGui import QFont, QColor
  10. from ui.widgets.panel_header import PanelHeader
  11. class QualityHistoryPanel(QWidget):
  12. """
  13. Panel for displaying recent quality test history.
  14. Shows table with test results and export functionality.
  15. """
  16. def __init__(self, parent=None):
  17. super().__init__(parent)
  18. self.test_history = []
  19. self.init_ui()
  20. def init_ui(self):
  21. """Initialize the panel UI."""
  22. # Set size policy
  23. self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
  24. layout = QVBoxLayout(self)
  25. layout.setContentsMargins(0, 0, 0, 0)
  26. layout.setSpacing(0)
  27. # Main panel container with card styling
  28. self.setStyleSheet("""
  29. QWidget {
  30. background-color: white;
  31. border: 1px solid #ddd;
  32. }
  33. """)
  34. # Header using the PanelHeader widget
  35. header = PanelHeader(
  36. title="Recent Quality Tests",
  37. color="#34495e" # Dark gray for history
  38. )
  39. layout.addWidget(header)
  40. # Content area
  41. content = QWidget()
  42. content.setStyleSheet("""
  43. background-color: #2c3e50;
  44. border: 1px solid #34495e;
  45. border-top: none;
  46. """)
  47. content_layout = QVBoxLayout(content)
  48. content_layout.setContentsMargins(10, 10, 10, 10)
  49. content_layout.setSpacing(10)
  50. # Create and populate the history table
  51. self._create_history_table(content_layout)
  52. # Export button
  53. self._create_export_button(content_layout)
  54. layout.addWidget(content, 1)
  55. def _create_history_table(self, parent_layout):
  56. """Create the history table widget."""
  57. # Table widget
  58. self.history_table = QTableWidget()
  59. self.history_table.setColumnCount(5)
  60. self.history_table.setHorizontalHeaderLabels(["Time", "ID", "Grade", "Score", "Defects"])
  61. # Configure table appearance
  62. self.history_table.setAlternatingRowColors(True)
  63. self.history_table.setSelectionBehavior(QTableWidget.SelectRows)
  64. self.history_table.setSelectionMode(QTableWidget.SingleSelection)
  65. self.history_table.setFocusPolicy(Qt.NoFocus)
  66. # Set column widths
  67. self.history_table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Fixed)
  68. self.history_table.horizontalHeader().setSectionResizeMode(1, QHeaderView.Fixed)
  69. self.history_table.horizontalHeader().setSectionResizeMode(2, QHeaderView.Fixed)
  70. self.history_table.horizontalHeader().setSectionResizeMode(3, QHeaderView.Fixed)
  71. self.history_table.horizontalHeader().setSectionResizeMode(4, QHeaderView.Fixed)
  72. self.history_table.setColumnWidth(0, 80) # Time
  73. self.history_table.setColumnWidth(1, 70) # ID
  74. self.history_table.setColumnWidth(2, 50) # Grade
  75. self.history_table.setColumnWidth(3, 70) # Score
  76. self.history_table.setColumnWidth(4, 70) # Defects
  77. # Hide vertical header (row numbers)
  78. self.history_table.verticalHeader().setVisible(False)
  79. # Style the table
  80. self.history_table.setStyleSheet("""
  81. QTableWidget {
  82. background-color: #34495e;
  83. border: 1px solid #4a5f7a;
  84. border-radius: 5px;
  85. gridline-color: #4a5f7a;
  86. selection-background-color: #3498db;
  87. }
  88. QHeaderView::section {
  89. background-color: #2c3e50;
  90. color: #ecf0f1;
  91. padding: 8px;
  92. border: none;
  93. font-weight: bold;
  94. font-size: 10px;
  95. }
  96. QTableWidget::item {
  97. padding: 5px;
  98. color: #ecf0f1;
  99. border: none;
  100. }
  101. QTableWidget::item:selected {
  102. background-color: #3498db;
  103. color: white;
  104. }
  105. """)
  106. # Populate with sample data
  107. self._populate_sample_data()
  108. parent_layout.addWidget(self.history_table)
  109. def _populate_sample_data(self):
  110. """Populate table with sample history data."""
  111. # Sample test history data
  112. sample_data = [
  113. ("14:32:15", "Q-0032", "B", "78.5%", "2", "#f39c12"),
  114. ("14:28:10", "Q-0031", "A", "94.2%", "0", "#27ae60"),
  115. ("14:24:55", "Q-0030", "C", "52.8%", "5", "#e74c3c"),
  116. ("14:20:33", "Q-0029", "A", "91.7%", "1", "#27ae60"),
  117. ("14:16:12", "Q-0028", "B", "76.3%", "3", "#f39c12"),
  118. ("14:12:45", "Q-0027", "A", "89.4%", "0", "#27ae60"),
  119. ("14:08:22", "Q-0026", "B", "81.1%", "2", "#f39c12"),
  120. ("14:04:18", "Q-0025", "C", "45.9%", "7", "#e74c3c")
  121. ]
  122. self.history_table.setRowCount(len(sample_data))
  123. for row, (time, test_id, grade, score, defects, color) in enumerate(sample_data):
  124. # Time column
  125. time_item = QTableWidgetItem(time)
  126. time_item.setTextAlignment(Qt.AlignCenter)
  127. self.history_table.setItem(row, 0, time_item)
  128. # ID column
  129. id_item = QTableWidgetItem(test_id)
  130. id_item.setTextAlignment(Qt.AlignCenter)
  131. self.history_table.setItem(row, 1, id_item)
  132. # Grade column (color-coded widget)
  133. grade_item = QTableWidgetItem(grade)
  134. grade_item.setTextAlignment(Qt.AlignCenter)
  135. grade_item.setBackground(QColor(color))
  136. grade_item.setForeground(QColor("white"))
  137. self.history_table.setItem(row, 2, grade_item)
  138. # Score column
  139. score_item = QTableWidgetItem(score)
  140. score_item.setTextAlignment(Qt.AlignCenter)
  141. self.history_table.setItem(row, 3, score_item)
  142. # Defects column
  143. defects_item = QTableWidgetItem(defects)
  144. defects_item.setTextAlignment(Qt.AlignCenter)
  145. defects_item.setForeground(QColor(color))
  146. self.history_table.setItem(row, 4, defects_item)
  147. self.test_history = sample_data
  148. def _create_export_button(self, parent_layout):
  149. """Create the export results button."""
  150. self.export_btn = QPushButton("EXPORT RESULTS")
  151. self.export_btn.setFixedHeight(30)
  152. self.export_btn.setFont(QFont("Arial", 10, QFont.Bold))
  153. self.export_btn.setStyleSheet("""
  154. QPushButton {
  155. background-color: #9b59b6;
  156. color: white;
  157. border: 1px solid #8e44ad;
  158. border-radius: 3px;
  159. font-size: 10px;
  160. font-weight: bold;
  161. padding: 5px 15px;
  162. }
  163. QPushButton:hover {
  164. background-color: #8e44ad;
  165. border-color: #7d3c98;
  166. }
  167. QPushButton:pressed {
  168. background-color: #7d3c98;
  169. padding-top: 7px;
  170. }
  171. QPushButton:disabled {
  172. background-color: #7f8c8d;
  173. border-color: #95a5a6;
  174. color: #bdc3c7;
  175. }
  176. """)
  177. # For now, just show placeholder functionality
  178. self.export_btn.setToolTip("Export functionality - Coming soon")
  179. self.export_btn.clicked.connect(self._export_results)
  180. parent_layout.addWidget(self.export_btn)
  181. def _export_results(self):
  182. """Handle export button click (placeholder)."""
  183. # Placeholder for future export functionality
  184. print("Export functionality - Coming soon!")
  185. def add_test_result(self, time, test_id, grade, score, defects):
  186. """Add a new test result to the history."""
  187. # Add to beginning of list (most recent first)
  188. color = self._get_grade_color(grade)
  189. new_result = (time, test_id, grade, score, defects, color)
  190. self.test_history.insert(0, new_result)
  191. # Keep only last 50 results
  192. if len(self.test_history) > 50:
  193. self.test_history = self.test_history[:50]
  194. # Refresh table
  195. self._refresh_table()
  196. def _get_grade_color(self, grade):
  197. """Get color code for grade."""
  198. grade_colors = {
  199. 'A': '#27ae60',
  200. 'B': '#f39c12',
  201. 'C': '#e74c3c'
  202. }
  203. return grade_colors.get(grade, '#95a5a6')
  204. def _refresh_table(self):
  205. """Refresh the table display."""
  206. self.history_table.setRowCount(len(self.test_history))
  207. for row, (time, test_id, grade, score, defects, color) in enumerate(self.test_history):
  208. # Time column
  209. time_item = QTableWidgetItem(time)
  210. time_item.setTextAlignment(Qt.AlignCenter)
  211. self.history_table.setItem(row, 0, time_item)
  212. # ID column
  213. id_item = QTableWidgetItem(test_id)
  214. id_item.setTextAlignment(Qt.AlignCenter)
  215. self.history_table.setItem(row, 1, id_item)
  216. # Grade column (color-coded)
  217. grade_item = QTableWidgetItem(grade)
  218. grade_item.setTextAlignment(Qt.AlignCenter)
  219. grade_item.setBackground(QColor(color))
  220. grade_item.setForeground(QColor("white"))
  221. self.history_table.setItem(row, 2, grade_item)
  222. # Score column
  223. score_item = QTableWidgetItem(score)
  224. score_item.setTextAlignment(Qt.AlignCenter)
  225. self.history_table.setItem(row, 3, score_item)
  226. # Defects column
  227. defects_item = QTableWidgetItem(defects)
  228. defects_item.setTextAlignment(Qt.AlignCenter)
  229. defects_item.setForeground(QColor(color))
  230. self.history_table.setItem(row, 4, defects_item)
  231. def clear_history(self):
  232. """Clear all test history."""
  233. self.test_history = []
  234. self.history_table.setRowCount(0)
  235. def get_recent_tests(self, count=10):
  236. """Get the most recent test results."""
  237. return self.test_history[:count]