Browse Source

fix(ui): Correct PyQt6 imports and handle optional matplotlib dependency

- Move QAction to QtGui (i18n.py)
- Move QAbstractTableModel to QtCore (log_viewer.py)
- Wrap StatisticsFigureCanvas in MATPLOTLIB_AVAILABLE check (stats_panel.py)
- Minor formatting cleanup (test_i18n.py)

These fixes ensure proper PyQt6 compatibility and graceful handling of optional dependencies.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
d8dfun 2 days ago
parent
commit
883d6daf88
4 changed files with 42 additions and 40 deletions
  1. 1 1
      src/ui/i18n.py
  2. 2 1
      src/ui/log_viewer.py
  3. 38 36
      src/ui/stats_panel.py
  4. 1 2
      tests/ui/test_i18n.py

+ 1 - 1
src/ui/i18n.py

@@ -10,7 +10,7 @@ from pathlib import Path
 from typing import Dict, Optional, Callable
 from dataclasses import dataclass
 
-from PyQt6.QtWidgets import QWidget, QApplication, QMenu, QAction
+from PyQt6.QtWidgets import QWidget, QApplication, QMenu
 from PyQt6.QtCore import QTranslator, QLocale, QSettings, QObject, pyqtSignal
 from PyQt6.QtGui import QAction
 

+ 2 - 1
src/ui/log_viewer.py

@@ -33,7 +33,8 @@ from PyQt6.QtWidgets import (
     QApplication,
 )
 from PyQt6.QtCore import Qt, pyqtSignal, QAbstractListModel, QModelIndex, QSettings, QTimer
-from PyQt6.QtGui import QFont, QPalette, QColor, QAbstractTableModel
+from PyQt6.QtGui import QFont, QPalette, QColor
+from PyQt6.QtCore import QAbstractTableModel
 
 
 class LogLevel(Enum):

+ 38 - 36
src/ui/stats_panel.py

@@ -12,6 +12,7 @@ from dataclasses import dataclass
 from enum import Enum
 
 from PyQt6.QtWidgets import (
+    QDialog,
     QWidget,
     QVBoxLayout,
     QHBoxLayout,
@@ -110,42 +111,43 @@ class StatisticsData:
     average_wpm: float
 
 
-class StatisticsFigureCanvas(FigureCanvas):
-    """
-    Matplotlib canvas for embedding charts in PyQt.
-
-    Provides a clean interface for rendering various chart types.
-    """
-
-    def __init__(self, parent: Optional[QWidget] = None, width: int = 5, height: int = 4, dpi: int = 100) -> None:
-        """Initialize the figure canvas."""
-        if Figure is None:
-            raise RuntimeError("Matplotlib is not installed")
-
-        self.fig = Figure(figsize=(width, height), dpi=dpi)
-        self.axes = self.fig.add_subplot(111)
-        super().__init__(self.fig)
-        self.setParent(parent)
-
-        # Styling
-        self.fig.patch.set_facecolor("#ffffff")
-        self.fig.subplots_adjust(left=0.1, right=0.95, top=0.95, bottom=0.1)
-
-    def clear(self) -> None:
-        """Clear the current chart."""
-        self.axes.clear()
-        self.draw()
-
-    def set_chinese_font(self) -> None:
-        """Configure matplotlib to use Chinese-compatible fonts."""
-        try:
-            import matplotlib as mpl
-            mpl.rcParams["font.sans-serif"] = ["SimHei", "Microsoft YaHei", "Arial Unicode MS"]
-            mpl.rcParams["axes.unicode_minus"] = False
-        except Exception:
-            pass
-
-
+if MATPLOTLIB_AVAILABLE:
+    class StatisticsFigureCanvas(FigureCanvas):
+        """
+        Matplotlib canvas for embedding charts in PyQt.
+    
+        Provides a clean interface for rendering various chart types.
+        """
+    
+        def __init__(self, parent: Optional[QWidget] = None, width: int = 5, height: int = 4, dpi: int = 100) -> None:
+            """Initialize the figure canvas."""
+            if Figure is None:
+                raise RuntimeError("Matplotlib is not installed")
+    
+            self.fig = Figure(figsize=(width, height), dpi=dpi)
+            self.axes = self.fig.add_subplot(111)
+            super().__init__(self.fig)
+            self.setParent(parent)
+    
+            # Styling
+            self.fig.patch.set_facecolor("#ffffff")
+            self.fig.subplots_adjust(left=0.1, right=0.95, top=0.95, bottom=0.1)
+    
+        def clear(self) -> None:
+            """Clear the current chart."""
+            self.axes.clear()
+            self.draw()
+    
+        def set_chinese_font(self) -> None:
+            """Configure matplotlib to use Chinese-compatible fonts."""
+            try:
+                import matplotlib as mpl
+                mpl.rcParams["font.sans-serif"] = ["SimHei", "Microsoft YaHei", "Arial Unicode MS"]
+                mpl.rcParams["axes.unicode_minus"] = False
+            except Exception:
+                pass
+    
+    
 class StatisticsPanel(QWidget):
     """
     Statistics panel widget (Story 7.23).

+ 1 - 2
tests/ui/test_i18n.py

@@ -34,8 +34,7 @@ def clean_manager():
 
 class TestSupportedLanguage:
     """Tests for SupportedLanguage enum."""
-
-    def test_language_values(self):<arg_value>(self):
+    def test_language_values(self):
         """Test SupportedLanguage enum values."""
         assert SupportedLanguage.CHINESE_SIMPLIFIED.code == "zh_CN"
         assert SupportedLanguage.ENGLISH.code == "en_US"