-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_format_palette.py
More file actions
224 lines (197 loc) · 9.18 KB
/
text_format_palette.py
File metadata and controls
224 lines (197 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
""" Widgets for displaying interfaces into formatting text: Bold, size and color """
from PySide6.QtWidgets import QPushButton, QToolBar, QLabel, QLineEdit, QColorDialog, QComboBox, QWidget, QHBoxLayout
from PySide6.QtGui import QIcon, QKeySequence, QColor, QFontDatabase, QFont, QValidator, QAction
from PySide6.QtCore import Signal, QObject, Qt, QSize
class FormatSignaller(QObject):
bolded = Signal()
italicized = Signal()
underlined = Signal()
struckthrough = Signal()
justified = Signal(Qt.Alignment)
sized = Signal(float)
# below emitted when a text box is selected with a different active font
active_font_changed = Signal(QFont)
foreground_colored = Signal(QColor)
background_colored = Signal(QColor)
code_formatted = Signal()
family_formatted = Signal(str)
bulleted = Signal()
numbered = Signal()
checkbox_inserted = Signal()
G_FORMAT_SIGNALLER = FormatSignaller()
class TextFormatPalette(QToolBar):
""" The main container for text formatting options """
def __init__(self, parent):
super().__init__(parent)
self.setStyleSheet("""
QToolBar {
spacing: 5px;
}
QToolBar::separator {
}
""")
# Check here for icon names
# https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html#names
self._bold_action = QIcon.fromTheme("format-text-bold")
self.addAction(self._bold_action, "bold").setShortcut(QKeySequence.Bold)
self._ital_action = QIcon.fromTheme("format-text-italic")
self.addAction(self._ital_action, "italic").setShortcut(QKeySequence.Italic)
self._under_action = QIcon.fromTheme("format-text-underline")
self.addAction(self._under_action, "underline").setShortcut(QKeySequence.Underline)
self._strk_action = QIcon.fromTheme("format-text-strikethrough")
self.addAction(self._strk_action, "strikethrough")
self.addSeparator()
self._justl_action = QIcon.fromTheme("format-justify-left")
self.addAction(self._justl_action, "left")
self._justc_action = QIcon.fromTheme("format-justify-center")
self.addAction(self._justc_action, "center")
self._justr_action = QIcon.fromTheme("format-justify-right")
self.addAction(self._justr_action, "right")
self.addSeparator()
self._bullet_action = QIcon.fromTheme("format-list-unordered")
self.addAction(self._bullet_action, "bullet")
self._number_action = QIcon.fromTheme("format-list-ordered")
self.addAction(self._number_action, "numbered")
self.addSeparator()
# the following convert regular text items into special text items
self._code_action = QIcon.fromTheme("format-text-code")
self.addAction(self._code_action, "source code")
self._check_action = QIcon.fromTheme("checkbox")
self.addAction(self._check_action, "check")
self.addSeparator()
self.current_fg_color = QColor.fromRgb(0x000000)
self.current_bg_color = QColor.fromRgb(0xffffff)
self.foregnd_button = FontColorButton(QIcon.fromTheme("format-text-color"), "set text color")
self.foregnd_button.set_color_display(self.current_fg_color)
self.addWidget(self.foregnd_button)
self.backgnd_button = FontColorButton(QIcon.fromTheme("color-fill"), "set text highlight color")
self.backgnd_button.set_color_display(self.current_bg_color)
self.foregnd_select_button = QPushButton(QIcon.fromTheme("color-management"), "")
self.foregnd_select_button.setToolTip("Select a new text foreground color")
self.addWidget(self.foregnd_select_button)
self.foregnd_select_button.setMaximumWidth(30)
self.backgnd_select_button = QPushButton(QIcon.fromTheme("color-management"), "")
self.backgnd_select_button.setToolTip("Select a new text highlight color")
self.addWidget(self.backgnd_button)
self.backgnd_select_button.setMaximumWidth(30)
self.addWidget(self.backgnd_select_button)
self.addSeparator()
# Configure font family and size input
self.family_label = QLabel("Font")
self.family_label.setMaximumWidth(40)
self.addWidget(self.family_label)
self.family_menu = QComboBox()
self.family_menu.setMaximumWidth(180)
self._fonts = {} # dict for fast lookups
for i, each in enumerate(QFontDatabase().families()):
font = self.family_menu.font()
font.setFamily(each)
font.setPointSize(12)
self.family_menu.addItem(each)
self._fonts[each] = i
self.family_menu.setItemData(i, font, Qt.FontRole)
self.addWidget(self.family_menu)
self.size_label = QLabel("Size")
self.size_label.setMaximumWidth(40)
self.addWidget(self.size_label)
self.size_input = QLineEdit("12")
self.size_input.setValidator(FontSizeValidator())
self.size_input.setMaximumWidth(40)
self.addWidget(self.size_input)
# connect signals
self.actionTriggered.connect(self._dispatch_event)
self.foregnd_button.pressed.connect(lambda: G_FORMAT_SIGNALLER.foreground_colored.emit(self.current_fg_color))
self.backgnd_button.pressed.connect(lambda: G_FORMAT_SIGNALLER.background_colored.emit(self.current_bg_color))
self.foregnd_select_button.pressed.connect(self._select_fg_color)
self.backgnd_select_button.pressed.connect(self._select_bg_color)
G_FORMAT_SIGNALLER.active_font_changed.connect(self._feedback_font_size)
self.size_input.returnPressed.connect(self._update_font_size)
self.family_menu.currentTextChanged[str].connect(lambda x: G_FORMAT_SIGNALLER.family_formatted.emit(x))
def _dispatch_event(self, action: QAction):
action = action.text()
if action == "bold":
G_FORMAT_SIGNALLER.bolded.emit()
elif action == "italic":
G_FORMAT_SIGNALLER.italicized.emit()
elif action == "underline":
G_FORMAT_SIGNALLER.underlined.emit()
elif action == "strikethrough":
G_FORMAT_SIGNALLER.struckthrough.emit()
elif action == "right":
self._justify_right()
elif action == "left":
self._justify_left()
elif action == "center":
self._justify_center()
elif action == "source code":
G_FORMAT_SIGNALLER.code_formatted.emit()
elif action == "bullet":
G_FORMAT_SIGNALLER.bulleted.emit()
elif action == "numbered":
G_FORMAT_SIGNALLER.numbered.emit()
elif action == "check":
G_FORMAT_SIGNALLER.checkbox_inserted.emit()
@staticmethod
def _justify_center():
G_FORMAT_SIGNALLER.justified.emit(Qt.AlignCenter)
@staticmethod
def _justify_right():
G_FORMAT_SIGNALLER.justified.emit(Qt.AlignRight)
@staticmethod
def _justify_left():
G_FORMAT_SIGNALLER.justified.emit(Qt.AlignLeft)
def _update_font_size(self):
value = float(self.size_input.text())
G_FORMAT_SIGNALLER.sized.emit(value)
def _feedback_font_size(self, font: QFont):
size = font.pointSize()
self.size_input.setText(str(size))
family = font.family()
if family in self._fonts:
self.family_menu.setCurrentIndex(self._fonts[family])
def _select_fg_color(self):
color = QColorDialog.getColor()
self.current_fg_color = color
self.foregnd_button.set_color_display(color)
def _select_bg_color(self):
color = QColorDialog.getColor()
self.current_bg_color = color
self.backgnd_button.set_color_display(color)
class FontSizeValidator(QValidator):
def validate(self, contents: str, index: int):
if contents.isnumeric():
return self.Acceptable
parts = contents.split(".")
if len(parts) == 2:
if parts[0].isnumeric() and parts[1].isnumeric():
return self.Acceptable
if parts[0] == "":
return self.Invalid
if parts[0].isnumeric() and parts[1] == "":
return self.Acceptable
return self.Invalid
class FontColorButton(QPushButton):
""" Displays the currently selected color and provides an icon for indicating what is being colored """
def __init__(self, icon: QIcon, s: str):
super().__init__()
self._lo = QHBoxLayout()
self._filler = QToolBar()
self._filler.addAction(icon, s)
self._filler.setMaximumHeight(25)
self._filler.setMaximumWidth(25)
self._filler.setStyleSheet("""
QWidget {
border: 0px
}
""")
self._filler.actionTriggered.connect(lambda _: self.pressed.emit())
self._lo.addWidget(self._filler)
self._lo.setSpacing(0)
self.setMaximumWidth(40)
self._color_display = QLabel("")
self._color_display.setMinimumWidth(8)
self._lo.addWidget(self._color_display)
self.setLayout(self._lo)
self._lo.setContentsMargins(3, 3, 3, 3)
def set_color_display(self, color: QColor):
self._color_display.setStyleSheet("border-radius: 2px; background-color: {};".format(color.name()))