Coverage for pyPhotoAlbum/text_edit_dialog.py: 100%

78 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-20 12:55 +0000

1""" 

2Text editing dialog for pyPhotoAlbum 

3""" 

4 

5from PyQt6.QtWidgets import ( 

6 QDialog, 

7 QVBoxLayout, 

8 QHBoxLayout, 

9 QPushButton, 

10 QTextEdit, 

11 QLabel, 

12 QComboBox, 

13 QSpinBox, 

14 QColorDialog, 

15) 

16from PyQt6.QtCore import Qt 

17from PyQt6.QtGui import QFont, QColor 

18 

19 

20class TextEditDialog(QDialog): 

21 """Dialog for editing text box content and properties""" 

22 

23 def __init__(self, text_element, parent=None): 

24 super().__init__(parent) 

25 self.text_element = text_element 

26 self.setWindowTitle("Edit Text") 

27 self.resize(500, 400) 

28 

29 # Create UI 

30 self._init_ui() 

31 

32 # Load current values 

33 self._load_values() 

34 

35 def _init_ui(self): 

36 """Initialize the user interface""" 

37 layout = QVBoxLayout() 

38 

39 # Text editor 

40 text_label = QLabel("Text:") 

41 self.text_edit = QTextEdit() 

42 self.text_edit.setAcceptRichText(False) # Plain text only 

43 layout.addWidget(text_label) 

44 layout.addWidget(self.text_edit) 

45 

46 # Font settings 

47 font_layout = QHBoxLayout() 

48 

49 # Font family 

50 font_layout.addWidget(QLabel("Font:")) 

51 self.font_combo = QComboBox() 

52 self.font_combo.addItems( 

53 ["Arial", "Times New Roman", "Courier New", "Helvetica", "Verdana", "Georgia", "Comic Sans MS"] 

54 ) 

55 font_layout.addWidget(self.font_combo) 

56 

57 # Font size 

58 font_layout.addWidget(QLabel("Size:")) 

59 self.font_size_spin = QSpinBox() 

60 self.font_size_spin.setRange(6, 72) 

61 self.font_size_spin.setValue(12) 

62 font_layout.addWidget(self.font_size_spin) 

63 

64 # Text color 

65 self.color_button = QPushButton("Color") 

66 self.color_button.clicked.connect(self._choose_color) 

67 self.current_color = QColor(0, 0, 0) # Default black 

68 font_layout.addWidget(self.color_button) 

69 

70 font_layout.addStretch() 

71 layout.addLayout(font_layout) 

72 

73 # Alignment 

74 alignment_layout = QHBoxLayout() 

75 alignment_layout.addWidget(QLabel("Alignment:")) 

76 self.alignment_combo = QComboBox() 

77 self.alignment_combo.addItems(["left", "center", "right", "justify"]) 

78 alignment_layout.addWidget(self.alignment_combo) 

79 alignment_layout.addStretch() 

80 layout.addLayout(alignment_layout) 

81 

82 # Buttons 

83 button_layout = QHBoxLayout() 

84 button_layout.addStretch() 

85 

86 cancel_button = QPushButton("Cancel") 

87 cancel_button.clicked.connect(self.reject) 

88 button_layout.addWidget(cancel_button) 

89 

90 ok_button = QPushButton("OK") 

91 ok_button.clicked.connect(self.accept) 

92 ok_button.setDefault(True) 

93 button_layout.addWidget(ok_button) 

94 

95 layout.addLayout(button_layout) 

96 

97 self.setLayout(layout) 

98 

99 def _load_values(self): 

100 """Load current values from text element""" 

101 # Load text content 

102 self.text_edit.setPlainText(self.text_element.text_content) 

103 

104 # Load font settings 

105 font_family = self.text_element.font_settings.get("family", "Arial") 

106 index = self.font_combo.findText(font_family) 

107 if index >= 0: 

108 self.font_combo.setCurrentIndex(index) 

109 

110 font_size = self.text_element.font_settings.get("size", 12) 

111 self.font_size_spin.setValue(int(font_size)) 

112 

113 # Load color 

114 color = self.text_element.font_settings.get("color", (0, 0, 0)) 

115 if all(isinstance(c, int) and c > 1 for c in color): 

116 # Color in 0-255 range 

117 self.current_color = QColor(*color) 

118 else: 

119 # Color in 0-1 range 

120 self.current_color = QColor(int(color[0] * 255), int(color[1] * 255), int(color[2] * 255)) 

121 self._update_color_button() 

122 

123 # Load alignment 

124 alignment = self.text_element.alignment 

125 index = self.alignment_combo.findText(alignment) 

126 if index >= 0: 

127 self.alignment_combo.setCurrentIndex(index) 

128 

129 def _choose_color(self): 

130 """Open color picker dialog""" 

131 color = QColorDialog.getColor(self.current_color, self, "Choose Text Color") 

132 if color.isValid(): 

133 self.current_color = color 

134 self._update_color_button() 

135 

136 def _update_color_button(self): 

137 """Update color button appearance""" 

138 self.color_button.setStyleSheet( 

139 f"background-color: {self.current_color.name()}; " 

140 f"color: {'white' if self.current_color.lightness() < 128 else 'black'};" 

141 ) 

142 

143 def get_values(self): 

144 """Get the edited values""" 

145 return { 

146 "text_content": self.text_edit.toPlainText(), 

147 "font_settings": { 

148 "family": self.font_combo.currentText(), 

149 "size": self.font_size_spin.value(), 

150 "color": (self.current_color.red(), self.current_color.green(), self.current_color.blue()), 

151 }, 

152 "alignment": self.alignment_combo.currentText(), 

153 }