confidence_bar.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """
  2. Confidence Bar Widget
  3. Horizontal progress bar for displaying confidence scores.
  4. """
  5. from PyQt5.QtWidgets import QWidget, QHBoxLayout, QLabel, QProgressBar
  6. from PyQt5.QtCore import Qt
  7. from PyQt5.QtGui import QFont
  8. class ConfidenceBar(QWidget):
  9. """
  10. Horizontal confidence bar with label and percentage.
  11. Args:
  12. label: Label text (e.g., "Ripe")
  13. color: Bar color hex code
  14. parent: Parent widget
  15. """
  16. def __init__(self, label: str, color: str = "#27ae60", parent=None):
  17. super().__init__(parent)
  18. self.color = color
  19. self.init_ui(label)
  20. def init_ui(self, label: str):
  21. """Initialize the confidence bar UI."""
  22. layout = QHBoxLayout(self)
  23. layout.setContentsMargins(0, 5, 0, 5)
  24. layout.setSpacing(10)
  25. # Label
  26. self.label = QLabel(label + ":")
  27. self.label.setFont(QFont("Arial", 9))
  28. self.label.setMinimumWidth(70)
  29. layout.addWidget(self.label)
  30. # Progress bar
  31. self.progress_bar = QProgressBar()
  32. self.progress_bar.setFixedHeight(12)
  33. self.progress_bar.setMaximum(100)
  34. self.progress_bar.setValue(0)
  35. self.progress_bar.setTextVisible(False)
  36. self.progress_bar.setStyleSheet(f"""
  37. QProgressBar {{
  38. background-color: #ecf0f1;
  39. border: 1px solid #bdc3c7;
  40. border-radius: 2px;
  41. }}
  42. QProgressBar::chunk {{
  43. background-color: {self.color};
  44. border-radius: 2px;
  45. }}
  46. """)
  47. layout.addWidget(self.progress_bar, 1)
  48. # Percentage label
  49. self.percent_label = QLabel("0.0%")
  50. self.percent_label.setFont(QFont("Arial", 9))
  51. self.percent_label.setMinimumWidth(50)
  52. self.percent_label.setStyleSheet("color: #2c3e50;")
  53. layout.addWidget(self.percent_label)
  54. def set_value(self, value: float, is_primary: bool = False):
  55. """
  56. Set the confidence value.
  57. Args:
  58. value: Confidence value (0-100)
  59. is_primary: Whether this is the primary/selected classification
  60. """
  61. self.progress_bar.setValue(int(value))
  62. self.percent_label.setText(f"{value:.1f}%")
  63. # Bold text for primary classification
  64. if is_primary:
  65. self.label.setStyleSheet("font-weight: bold; color: #2c3e50;")
  66. self.percent_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
  67. else:
  68. self.label.setStyleSheet("color: #2c3e50;")
  69. self.percent_label.setStyleSheet("color: #2c3e50;")