Loading ballontranslator/data/config/stylesheet.css +17 −7 Original line number Diff line number Diff line Loading @@ -64,7 +64,7 @@ TransTextEdit { QComboBox { height: 40px; font-size: 20px; padding-left: 16px; padding-left: 12px; border: 1px solid whitesmoke; } Loading @@ -85,9 +85,9 @@ QComboBox::down-arrow { } QFontComboBox#FontFamilyBox { width: 200px; width: 160px; height: 40px; font-size: 24px; font-size: 20px; padding-right: 20px; padding-left: 10px; border-color: #b3b6bf; Loading @@ -105,17 +105,19 @@ QFontComboBox#FontFamilyBox::down-arrow { margin-right: 10px; } QComboBox#FontSizeComboBox { SizeComboBox { padding-left: 8px; padding-right: 5px; font-size: 24px; font-size: 20px; width: 52px; border-color: #b3b6bf; } QComboBox#FontSizeComboBox::hover { SizeComboBox::hover { border: 1px solid rgb(30, 147, 229); } QComboBox#FontSizeComboBox::down-arrow { SizeComboBox::down-arrow { image: url(data/icons/combobox_arrow.svg); width: 42px; height: 16px; Loading @@ -123,6 +125,14 @@ QComboBox#FontSizeComboBox::down-arrow { margin-right: 5px; } QLabel#lineSpacingLabel { image: url(data/icons/fontfmt_linespacing.svg); max-width: 42px; max-height: 42px; min-width: 42px; min-height: 42px; } ColorPicker { max-height: 46px; min-height: 46px; Loading ballontranslator/data/icons/fontfmt_linespacing.svg 0 → 100644 +1 −0 Original line number Diff line number Diff line <?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1613973513102" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1676" width="128" height="128" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><style type="text/css"></style></defs><path d="M298.666667 792.96l55.168-55.125333a42.666667 42.666667 0 0 1 56.32-3.541334l4.010666 3.541334a42.666667 42.666667 0 0 1 3.541334 56.32l-3.541334 4.010666-128 128-1.877333 1.792-2.901333 2.346667-4.693334 3.029333-2.645333 1.322667-3.456 1.450667-3.242667 1.024-6.357333 1.28L256 938.666667l-3.498667-0.170667-5.077333-0.682667-4.736-1.28-4.736-1.877333-4.181333-2.218667-3.157334-2.133333-4.778666-4.138667-128-128a42.666667 42.666667 0 0 1 56.32-63.872l4.010666 3.541334L213.333333 792.96V231.04L158.165333 286.165333l-4.010666 3.541334A42.666667 42.666667 0 0 1 94.293333 229.845333l3.541334-4.010666 128-128 4.778666-4.138667 3.157334-2.133333 4.181333-2.218667 4.693333-1.877333 4.778667-1.28 5.077333-0.682667L256 85.333333l4.992 0.298667 6.357333 1.237333 3.242667 1.024 3.456 1.450667 2.602667 1.28 4.736 3.072 2.901333 2.346667 1.877333 1.792 128 128 3.541334 4.010666a42.666667 42.666667 0 0 1 0 52.309334l-3.541334 4.010666-4.010666 3.541334a42.666667 42.666667 0 0 1-52.309334 0l-4.010666-3.541334L298.666667 231.04v561.92zM896 810.666667a42.666667 42.666667 0 0 1 0 85.333333h-341.333333a42.666667 42.666667 0 0 1 0-85.333333h341.333333z m0-341.333334a42.666667 42.666667 0 0 1 0 85.333334h-341.333333a42.666667 42.666667 0 0 1 0-85.333334h341.333333z m0-341.333333a42.666667 42.666667 0 0 1 0 85.333333h-341.333333a42.666667 42.666667 0 0 1 0-85.333333h341.333333z" p-id="2141" fill="#697187"></path></svg> No newline at end of file ballontranslator/ui/fontformatpanel.py +74 −48 Original line number Diff line number Diff line Loading @@ -97,17 +97,42 @@ def set_textblk_strokecolor(blkitem: TextBlkItem, cursor: QTextCursor, stroke_co @restore_textcursor def set_textblk_family(blkitem: TextBlkItem, cursor: QTextCursor, family: str): format = cursor.blockCharFormat() format.setFontFamily(family) cursor.setBlockCharFormat(format) doc = blkitem.document() lastpos = doc.rootFrame().lastPosition() if cursor.selectionStart() == 0 and \ cursor.selectionEnd() == lastpos: font = doc.defaultFont() font.setFamily(family) doc.setDefaultFont(font) cursor.mergeCharFormat(format) # if cursor.selectionStart() == 0 and \ # cursor.selectionEnd() == lastpos: # font = doc.defaultFont() # font.setFamily(family) # doc.setDefaultFont(font) sel_start = cursor.selectionStart() sel_end = cursor.selectionEnd() block = doc.firstBlock() while block.isValid(): it = block.begin() while not it.atEnd(): fragment = it.fragment() frag_start = fragment.position() frag_end = frag_start + fragment.length() pos2 = min(frag_end, sel_end) pos1 = max(frag_start, sel_start) if pos1 < pos2: cfmt = fragment.charFormat() under_line = cfmt.fontUnderline() cfont = cfmt.font() font = QFont(family, cfont.pointSizeF(), cfont.weight(), cfont.italic()) font.setBold(font.bold()) font.setWordSpacing(cfont.wordSpacing()) font.setLetterSpacing(cfont.letterSpacingType(), cfont.letterSpacing()) cfmt.setFont(font) cfmt.setFontUnderline(under_line) cursor.setPosition(pos1) cursor.setPosition(pos2, QTextCursor.KeepAnchor) cursor.setCharFormat(cfmt) it += 1 block = block.next() @restore_textcursor def set_textblk_linespacing(blkitem: TextBlkItem, cursor: QTextCursor, line_spacing: float): Loading Loading @@ -212,7 +237,7 @@ class FormatGroupBtn(QFrame): def setUnderline(self): self.set_underline.emit(self.underlineBtn.isChecked()) class FontSizeComboBox(QComboBox): class SizeComboBox(QComboBox): enter_pressed = Signal() Loading @@ -220,6 +245,9 @@ class FontSizeComboBox(QComboBox): super().__init__(*args, **kwargs) self.text_changed_by_user = False self.editTextChanged.connect(self.on_text_changed) self.setEditable(True) validator = QDoubleValidator() self.setValidator(validator) def keyPressEvent(self, e: QKeyEvent) -> None: key = e.key() Loading @@ -235,6 +263,12 @@ class FontSizeComboBox(QComboBox): if self.hasFocus(): self.text_changed_by_user = True def value(self) -> float: return float(self.currentText()) def setValue(self, value: float): self.setCurrentText(str(value)) class FontSizeBox(QFrame): apply_fontsize = Signal(float) Loading @@ -246,11 +280,9 @@ class FontSizeBox(QFrame): self.downBtn.setObjectName("FsizeIncrementDown") self.upBtn.clicked.connect(self.onUpBtnClicked) self.downBtn.clicked.connect(self.onDownBtnClicked) self.fcombobox = FontSizeComboBox(self) self.fcombobox = SizeComboBox(self) self.fcombobox.enter_pressed.connect(self.on_fbox_enter_pressed) self.fcombobox.setFixedWidth(200) self.fcombobox.setEditable(True) self.fcombobox.setObjectName("FontSizeComboBox") # self.fcombobox.setObjectName("SizeComboBox") self.fcombobox.addItems([ "5", "5.5", "6.5", "7.5", "8", "9", "10", "10.5", "11", "12", "14", "16", "18", "20", '22', "26", "28", Loading @@ -271,8 +303,8 @@ class FontSizeBox(QFrame): hlayout.addWidget(self.fcombobox) hlayout.setSpacing(3) def getFontSize(self): return float(self.fcombobox.currentText()) def getFontSize(self) -> float: return self.fcombobox.value() def onUpBtnClicked(self): size = self.getFontSize() Loading Loading @@ -327,6 +359,15 @@ class FontFormatPanel(Widget): self.fontsizebox.setToolTip(self.tr("Font Size")) self.fontsizebox.setObjectName("FontSizeBox") self.fontsizebox.apply_fontsize.connect(self.onApplyFontsize) self.fontsizebox.fcombobox.setToolTip("Change font size") self.lineSpacingLabel = QLabel(self) self.lineSpacingLabel.setObjectName("lineSpacingLabel") self.lineSpacingBox = SizeComboBox(self) self.lineSpacingBox.addItems(["1.0", "1.1", "1.2"]) self.lineSpacingBox.setToolTip("Change line spacing") self.lineSpacingBox.enter_pressed.connect(self.onLinespacingChanged) self.lineSpacingBox.currentIndexChanged.connect(self.onLinespacingChanged) self.colorPicker = ColorPicker(self) self.colorPicker.setObjectName("FontColorPicker") Loading @@ -346,23 +387,21 @@ class FontFormatPanel(Widget): self.verticalChecker.setObjectName("FontVerticalChecker") self.verticalChecker.clicked.connect(self.onOrientationChanged) self.fontStrokeLabel = QLabel(self) self.fontStrokeLabel.setObjectName("fontStrokeLabel") self.strokeColorPicker = ColorPicker(self) self.strokeColorPicker.setToolTip(self.tr("Change stroke color")) self.strokeColorPicker.changingColor.connect(self.changingColor) self.strokeColorPicker.colorChanged.connect(self.onStrokeColorChanged) self.strokeColorPicker.setObjectName("StrokeColorPicker") self.strokeWidthSlider = PaintQSlider(self.tr("Stroke width: ") + 'value%', Qt.Orientation.Horizontal) self.strokeWidthSlider.setFixedHeight(50) self.strokeWidthSlider.setRange(0, 100) self.strokeWidthSlider.valueChanged.connect(self.onSrokeWidthChanged) self.strokeWidthSlider.mouse_released.connect(self.onStrokeSliderRealeased) self.lineSpacingSlider = PaintQSlider(self.tr("line spacing: ") + 'value%', Qt.Orientation.Horizontal) self.lineSpacingSlider.setFixedHeight(50) self.lineSpacingSlider.setRange(0, 300) self.lineSpacingSlider.valueChanged.connect(self.onLinespacingChanged) self.lineSpacingSlider.mouse_released.connect(self.onLinespacingSliderReleased) self.strokeWidthBox = SizeComboBox(self) self.strokeWidthBox.addItems(["0.1"]) self.strokeWidthBox.setToolTip("Change stroke width") self.strokeWidthBox.enter_pressed.connect(self.onSrokeWidthChanged) self.strokeWidthBox.currentIndexChanged.connect(self.onSrokeWidthChanged) self.global_fontfmt_str = self.tr("Global Font Format") self.fontfmtLabel = QLabel(self) Loading @@ -378,6 +417,8 @@ class FontFormatPanel(Widget): hl1 = QHBoxLayout() hl1.addWidget(self.familybox) hl1.addWidget(self.fontsizebox) hl1.addWidget(self.lineSpacingLabel) hl1.addWidget(self.lineSpacingBox) hl1.setSpacing(10) hl2 = QHBoxLayout() hl2.setAlignment(Qt.AlignmentFlag.AlignCenter) Loading @@ -389,9 +430,8 @@ class FontFormatPanel(Widget): hl2.setContentsMargins(0, 0, 0, 0) hl3 = QHBoxLayout() hl3.setAlignment(Qt.AlignmentFlag.AlignLeft) hl3.addWidget(self.lineSpacingSlider) hl3.addWidget(self.strokeColorPicker) hl3.addWidget(self.strokeWidthSlider) hl3.addWidget(self.strokeWidthBox) hl3.setContentsMargins(5, 5, 5, 5) hl3.setSpacing(20) Loading Loading @@ -469,14 +509,7 @@ class FontFormatPanel(Widget): self.textblk_item.setVertical(self.active_format.vertical) def onSrokeWidthChanged(self): if len(self.canvas.selectedItems()) > 1 and self.strokeWidthSlider.pressed: return if self.strokeWidthSlider.pressed: self.update_stroke_width(self.strokeWidthSlider.value() / 100) def onStrokeSliderRealeased(self): if len(self.canvas.selectedItems()) > 1: self.update_stroke_width(self.strokeWidthSlider.value() / 100) self.update_stroke_width(self.strokeWidthBox.value()) def update_stroke_width(self, value: float): self.active_format.stroke_width = value Loading @@ -484,14 +517,7 @@ class FontFormatPanel(Widget): set_textblk_strokewidth(self.textblk_item, self.active_format.stroke_width) def onLinespacingChanged(self): if len(self.canvas.selectedItems()) > 1 and self.lineSpacingSlider.pressed: return if self.lineSpacingSlider.pressed: self.update_line_spacing(self.lineSpacingSlider.value() / 100) def onLinespacingSliderReleased(self): if len(self.canvas.selectedItems()) > 1: self.update_line_spacing(self.lineSpacingSlider.value() / 100) self.update_line_spacing(self.lineSpacingBox.value()) def update_line_spacing(self, value: float): self.active_format.line_spacing = value Loading @@ -504,8 +530,8 @@ class FontFormatPanel(Widget): self.familybox.setCurrentText(font_format.family) self.colorPicker.setPickerColor(font_format.frgb) self.strokeColorPicker.setPickerColor(font_format.srgb) self.strokeWidthSlider.setValue(font_format.stroke_width * 100) self.lineSpacingSlider.setValue(font_format.line_spacing * 100) self.strokeWidthBox.setValue(font_format.stroke_width) self.lineSpacingBox.setValue(font_format.line_spacing) self.verticalChecker.setChecked(font_format.vertical) self.formatBtnGroup.boldBtn.setChecked(font_format.bold) self.formatBtnGroup.underlineBtn.setChecked(font_format.underline) Loading ballontranslator/ui/textitem.py +20 −27 Original line number Diff line number Diff line Loading @@ -9,7 +9,7 @@ from qtpy.QtGui import QFont, QTextCursor, QPixmap, QPainterPath, QTextDocument, from dl.textdetector.textblock import TextBlock from utils.imgproc_utils import xywh2xyxypoly, rotate_polygons from .misc import FontFormat, px2pt, pt2px, td_pattern, table_pattern from .textlayout import VerticalTextDocumentLayout, HorizontalTextDocumentLayout from .textlayout import VerticalTextDocumentLayout, HorizontalTextDocumentLayout, SceneTextLayout TEXTRECT_SHOW_COLOR = QColor(30, 147, 229, 170) TEXTRECT_SELECTED_COLOR = QColor(248, 64, 147, 170) Loading Loading @@ -63,14 +63,14 @@ class TextBlkItem(QGraphicsTextItem): self.update() def repaint_background(self): self.repainting = True doc = self.document().clone() doc.setDocumentMargin(self.document().documentMargin()) layout = VerticalTextDocumentLayout(doc) if self.is_vertical else HorizontalTextDocumentLayout(doc) layout.line_spacing = self.line_spacing rect = self.rect() layout.setMaxSize(rect.width(), rect.height(), False) doc.setDocumentLayout(layout) layout.setMaxSize(rect.width(), rect.height()) cursor = QTextCursor(doc) block = doc.firstBlock() Loading Loading @@ -121,20 +121,21 @@ class TextBlkItem(QGraphicsTextItem): if blk.angle != 0: self.setRotation(blk.angle) # blk.vertical = False set_char_fmt = False if not blk.rich_text: if blk.translation: self.setPlainText(blk.translation) set_char_fmt = True else: self.setHtml(blk.rich_text) if set_format: font_fmt = FontFormat() font_fmt.from_textblock(blk) self.set_fontformat(font_fmt, set_char_format=set_char_fmt) if not blk.rich_text: if blk.translation: self.setPlainText(blk.translation) else: self.setHtml(blk.rich_text) def setCenterTransform(self): center = self.boundingRect().center() self.setTransformOriginPoint(center) Loading Loading @@ -252,14 +253,6 @@ class TextBlkItem(QGraphicsTextItem): if self.background_pixmap is not None: self.repaint_background() # cursor = QTextCursor(doc) # format = cursor.charFormat() # cursor.mergeCharFormat(format) # cursor.select(QTextCursor.Document) # cursor.mergeBlockCharFormat(format) # cursor.clearSelection() # # https://stackoverflow.com/questions/37160039/set-default-character-format-in-qtextdocument # self.setTextCursor(cursor) self.doc_size_changed.emit(self.idx) def on_document_enlarged(self): Loading @@ -282,21 +275,22 @@ class TextBlkItem(QGraphicsTextItem): pos.setY(pos.y() + delta_x * np.sin(rad)) self.setPos(pos) def documentLayout(self) -> VerticalTextDocumentLayout: def documentLayout(self) -> SceneTextLayout: return self.document().documentLayout() def setStrokeWidth(self, stroke_width): def setStrokeWidth(self, stroke_width: float): if self.stroke_width == stroke_width: return self.stroke_width = stroke_width sw = stroke_width * pt2px(self.document().defaultFont().pointSizeF()) self.documentLayout().updateDocumentMargin(sw/2) self.documentLayout().reLayout() if stroke_width > 0: self.repaint_background() else: self.background_pixmap = None sw = self.stroke_width * pt2px(self.document().defaultFont().pointSizeF()) self.document().setDocumentMargin(sw/2) self.documentLayout().updateDocumentMargin(sw/2) self.on_document_enlarged() self.update() def setStrokeColor(self, scolor): Loading Loading @@ -534,7 +528,6 @@ class TextBlkItem(QGraphicsTextItem): # https://stackoverflow.com/questions/37160039/set-default-character-format-in-qtextdocument cursor.movePosition(QTextCursor.Start) self.setTextCursor(cursor) self.stroke_width = ffmat.stroke_width self.setStrokeWidth(ffmat.stroke_width) self.setStrokeColor(ffmat.srgb) Loading ballontranslator/ui/textlayout.py +66 −85 File changed.Preview size limit exceeded, changes collapsed. Show changes Loading
ballontranslator/data/config/stylesheet.css +17 −7 Original line number Diff line number Diff line Loading @@ -64,7 +64,7 @@ TransTextEdit { QComboBox { height: 40px; font-size: 20px; padding-left: 16px; padding-left: 12px; border: 1px solid whitesmoke; } Loading @@ -85,9 +85,9 @@ QComboBox::down-arrow { } QFontComboBox#FontFamilyBox { width: 200px; width: 160px; height: 40px; font-size: 24px; font-size: 20px; padding-right: 20px; padding-left: 10px; border-color: #b3b6bf; Loading @@ -105,17 +105,19 @@ QFontComboBox#FontFamilyBox::down-arrow { margin-right: 10px; } QComboBox#FontSizeComboBox { SizeComboBox { padding-left: 8px; padding-right: 5px; font-size: 24px; font-size: 20px; width: 52px; border-color: #b3b6bf; } QComboBox#FontSizeComboBox::hover { SizeComboBox::hover { border: 1px solid rgb(30, 147, 229); } QComboBox#FontSizeComboBox::down-arrow { SizeComboBox::down-arrow { image: url(data/icons/combobox_arrow.svg); width: 42px; height: 16px; Loading @@ -123,6 +125,14 @@ QComboBox#FontSizeComboBox::down-arrow { margin-right: 5px; } QLabel#lineSpacingLabel { image: url(data/icons/fontfmt_linespacing.svg); max-width: 42px; max-height: 42px; min-width: 42px; min-height: 42px; } ColorPicker { max-height: 46px; min-height: 46px; Loading
ballontranslator/data/icons/fontfmt_linespacing.svg 0 → 100644 +1 −0 Original line number Diff line number Diff line <?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1613973513102" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1676" width="128" height="128" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><style type="text/css"></style></defs><path d="M298.666667 792.96l55.168-55.125333a42.666667 42.666667 0 0 1 56.32-3.541334l4.010666 3.541334a42.666667 42.666667 0 0 1 3.541334 56.32l-3.541334 4.010666-128 128-1.877333 1.792-2.901333 2.346667-4.693334 3.029333-2.645333 1.322667-3.456 1.450667-3.242667 1.024-6.357333 1.28L256 938.666667l-3.498667-0.170667-5.077333-0.682667-4.736-1.28-4.736-1.877333-4.181333-2.218667-3.157334-2.133333-4.778666-4.138667-128-128a42.666667 42.666667 0 0 1 56.32-63.872l4.010666 3.541334L213.333333 792.96V231.04L158.165333 286.165333l-4.010666 3.541334A42.666667 42.666667 0 0 1 94.293333 229.845333l3.541334-4.010666 128-128 4.778666-4.138667 3.157334-2.133333 4.181333-2.218667 4.693333-1.877333 4.778667-1.28 5.077333-0.682667L256 85.333333l4.992 0.298667 6.357333 1.237333 3.242667 1.024 3.456 1.450667 2.602667 1.28 4.736 3.072 2.901333 2.346667 1.877333 1.792 128 128 3.541334 4.010666a42.666667 42.666667 0 0 1 0 52.309334l-3.541334 4.010666-4.010666 3.541334a42.666667 42.666667 0 0 1-52.309334 0l-4.010666-3.541334L298.666667 231.04v561.92zM896 810.666667a42.666667 42.666667 0 0 1 0 85.333333h-341.333333a42.666667 42.666667 0 0 1 0-85.333333h341.333333z m0-341.333334a42.666667 42.666667 0 0 1 0 85.333334h-341.333333a42.666667 42.666667 0 0 1 0-85.333334h341.333333z m0-341.333333a42.666667 42.666667 0 0 1 0 85.333333h-341.333333a42.666667 42.666667 0 0 1 0-85.333333h341.333333z" p-id="2141" fill="#697187"></path></svg> No newline at end of file
ballontranslator/ui/fontformatpanel.py +74 −48 Original line number Diff line number Diff line Loading @@ -97,17 +97,42 @@ def set_textblk_strokecolor(blkitem: TextBlkItem, cursor: QTextCursor, stroke_co @restore_textcursor def set_textblk_family(blkitem: TextBlkItem, cursor: QTextCursor, family: str): format = cursor.blockCharFormat() format.setFontFamily(family) cursor.setBlockCharFormat(format) doc = blkitem.document() lastpos = doc.rootFrame().lastPosition() if cursor.selectionStart() == 0 and \ cursor.selectionEnd() == lastpos: font = doc.defaultFont() font.setFamily(family) doc.setDefaultFont(font) cursor.mergeCharFormat(format) # if cursor.selectionStart() == 0 and \ # cursor.selectionEnd() == lastpos: # font = doc.defaultFont() # font.setFamily(family) # doc.setDefaultFont(font) sel_start = cursor.selectionStart() sel_end = cursor.selectionEnd() block = doc.firstBlock() while block.isValid(): it = block.begin() while not it.atEnd(): fragment = it.fragment() frag_start = fragment.position() frag_end = frag_start + fragment.length() pos2 = min(frag_end, sel_end) pos1 = max(frag_start, sel_start) if pos1 < pos2: cfmt = fragment.charFormat() under_line = cfmt.fontUnderline() cfont = cfmt.font() font = QFont(family, cfont.pointSizeF(), cfont.weight(), cfont.italic()) font.setBold(font.bold()) font.setWordSpacing(cfont.wordSpacing()) font.setLetterSpacing(cfont.letterSpacingType(), cfont.letterSpacing()) cfmt.setFont(font) cfmt.setFontUnderline(under_line) cursor.setPosition(pos1) cursor.setPosition(pos2, QTextCursor.KeepAnchor) cursor.setCharFormat(cfmt) it += 1 block = block.next() @restore_textcursor def set_textblk_linespacing(blkitem: TextBlkItem, cursor: QTextCursor, line_spacing: float): Loading Loading @@ -212,7 +237,7 @@ class FormatGroupBtn(QFrame): def setUnderline(self): self.set_underline.emit(self.underlineBtn.isChecked()) class FontSizeComboBox(QComboBox): class SizeComboBox(QComboBox): enter_pressed = Signal() Loading @@ -220,6 +245,9 @@ class FontSizeComboBox(QComboBox): super().__init__(*args, **kwargs) self.text_changed_by_user = False self.editTextChanged.connect(self.on_text_changed) self.setEditable(True) validator = QDoubleValidator() self.setValidator(validator) def keyPressEvent(self, e: QKeyEvent) -> None: key = e.key() Loading @@ -235,6 +263,12 @@ class FontSizeComboBox(QComboBox): if self.hasFocus(): self.text_changed_by_user = True def value(self) -> float: return float(self.currentText()) def setValue(self, value: float): self.setCurrentText(str(value)) class FontSizeBox(QFrame): apply_fontsize = Signal(float) Loading @@ -246,11 +280,9 @@ class FontSizeBox(QFrame): self.downBtn.setObjectName("FsizeIncrementDown") self.upBtn.clicked.connect(self.onUpBtnClicked) self.downBtn.clicked.connect(self.onDownBtnClicked) self.fcombobox = FontSizeComboBox(self) self.fcombobox = SizeComboBox(self) self.fcombobox.enter_pressed.connect(self.on_fbox_enter_pressed) self.fcombobox.setFixedWidth(200) self.fcombobox.setEditable(True) self.fcombobox.setObjectName("FontSizeComboBox") # self.fcombobox.setObjectName("SizeComboBox") self.fcombobox.addItems([ "5", "5.5", "6.5", "7.5", "8", "9", "10", "10.5", "11", "12", "14", "16", "18", "20", '22', "26", "28", Loading @@ -271,8 +303,8 @@ class FontSizeBox(QFrame): hlayout.addWidget(self.fcombobox) hlayout.setSpacing(3) def getFontSize(self): return float(self.fcombobox.currentText()) def getFontSize(self) -> float: return self.fcombobox.value() def onUpBtnClicked(self): size = self.getFontSize() Loading Loading @@ -327,6 +359,15 @@ class FontFormatPanel(Widget): self.fontsizebox.setToolTip(self.tr("Font Size")) self.fontsizebox.setObjectName("FontSizeBox") self.fontsizebox.apply_fontsize.connect(self.onApplyFontsize) self.fontsizebox.fcombobox.setToolTip("Change font size") self.lineSpacingLabel = QLabel(self) self.lineSpacingLabel.setObjectName("lineSpacingLabel") self.lineSpacingBox = SizeComboBox(self) self.lineSpacingBox.addItems(["1.0", "1.1", "1.2"]) self.lineSpacingBox.setToolTip("Change line spacing") self.lineSpacingBox.enter_pressed.connect(self.onLinespacingChanged) self.lineSpacingBox.currentIndexChanged.connect(self.onLinespacingChanged) self.colorPicker = ColorPicker(self) self.colorPicker.setObjectName("FontColorPicker") Loading @@ -346,23 +387,21 @@ class FontFormatPanel(Widget): self.verticalChecker.setObjectName("FontVerticalChecker") self.verticalChecker.clicked.connect(self.onOrientationChanged) self.fontStrokeLabel = QLabel(self) self.fontStrokeLabel.setObjectName("fontStrokeLabel") self.strokeColorPicker = ColorPicker(self) self.strokeColorPicker.setToolTip(self.tr("Change stroke color")) self.strokeColorPicker.changingColor.connect(self.changingColor) self.strokeColorPicker.colorChanged.connect(self.onStrokeColorChanged) self.strokeColorPicker.setObjectName("StrokeColorPicker") self.strokeWidthSlider = PaintQSlider(self.tr("Stroke width: ") + 'value%', Qt.Orientation.Horizontal) self.strokeWidthSlider.setFixedHeight(50) self.strokeWidthSlider.setRange(0, 100) self.strokeWidthSlider.valueChanged.connect(self.onSrokeWidthChanged) self.strokeWidthSlider.mouse_released.connect(self.onStrokeSliderRealeased) self.lineSpacingSlider = PaintQSlider(self.tr("line spacing: ") + 'value%', Qt.Orientation.Horizontal) self.lineSpacingSlider.setFixedHeight(50) self.lineSpacingSlider.setRange(0, 300) self.lineSpacingSlider.valueChanged.connect(self.onLinespacingChanged) self.lineSpacingSlider.mouse_released.connect(self.onLinespacingSliderReleased) self.strokeWidthBox = SizeComboBox(self) self.strokeWidthBox.addItems(["0.1"]) self.strokeWidthBox.setToolTip("Change stroke width") self.strokeWidthBox.enter_pressed.connect(self.onSrokeWidthChanged) self.strokeWidthBox.currentIndexChanged.connect(self.onSrokeWidthChanged) self.global_fontfmt_str = self.tr("Global Font Format") self.fontfmtLabel = QLabel(self) Loading @@ -378,6 +417,8 @@ class FontFormatPanel(Widget): hl1 = QHBoxLayout() hl1.addWidget(self.familybox) hl1.addWidget(self.fontsizebox) hl1.addWidget(self.lineSpacingLabel) hl1.addWidget(self.lineSpacingBox) hl1.setSpacing(10) hl2 = QHBoxLayout() hl2.setAlignment(Qt.AlignmentFlag.AlignCenter) Loading @@ -389,9 +430,8 @@ class FontFormatPanel(Widget): hl2.setContentsMargins(0, 0, 0, 0) hl3 = QHBoxLayout() hl3.setAlignment(Qt.AlignmentFlag.AlignLeft) hl3.addWidget(self.lineSpacingSlider) hl3.addWidget(self.strokeColorPicker) hl3.addWidget(self.strokeWidthSlider) hl3.addWidget(self.strokeWidthBox) hl3.setContentsMargins(5, 5, 5, 5) hl3.setSpacing(20) Loading Loading @@ -469,14 +509,7 @@ class FontFormatPanel(Widget): self.textblk_item.setVertical(self.active_format.vertical) def onSrokeWidthChanged(self): if len(self.canvas.selectedItems()) > 1 and self.strokeWidthSlider.pressed: return if self.strokeWidthSlider.pressed: self.update_stroke_width(self.strokeWidthSlider.value() / 100) def onStrokeSliderRealeased(self): if len(self.canvas.selectedItems()) > 1: self.update_stroke_width(self.strokeWidthSlider.value() / 100) self.update_stroke_width(self.strokeWidthBox.value()) def update_stroke_width(self, value: float): self.active_format.stroke_width = value Loading @@ -484,14 +517,7 @@ class FontFormatPanel(Widget): set_textblk_strokewidth(self.textblk_item, self.active_format.stroke_width) def onLinespacingChanged(self): if len(self.canvas.selectedItems()) > 1 and self.lineSpacingSlider.pressed: return if self.lineSpacingSlider.pressed: self.update_line_spacing(self.lineSpacingSlider.value() / 100) def onLinespacingSliderReleased(self): if len(self.canvas.selectedItems()) > 1: self.update_line_spacing(self.lineSpacingSlider.value() / 100) self.update_line_spacing(self.lineSpacingBox.value()) def update_line_spacing(self, value: float): self.active_format.line_spacing = value Loading @@ -504,8 +530,8 @@ class FontFormatPanel(Widget): self.familybox.setCurrentText(font_format.family) self.colorPicker.setPickerColor(font_format.frgb) self.strokeColorPicker.setPickerColor(font_format.srgb) self.strokeWidthSlider.setValue(font_format.stroke_width * 100) self.lineSpacingSlider.setValue(font_format.line_spacing * 100) self.strokeWidthBox.setValue(font_format.stroke_width) self.lineSpacingBox.setValue(font_format.line_spacing) self.verticalChecker.setChecked(font_format.vertical) self.formatBtnGroup.boldBtn.setChecked(font_format.bold) self.formatBtnGroup.underlineBtn.setChecked(font_format.underline) Loading
ballontranslator/ui/textitem.py +20 −27 Original line number Diff line number Diff line Loading @@ -9,7 +9,7 @@ from qtpy.QtGui import QFont, QTextCursor, QPixmap, QPainterPath, QTextDocument, from dl.textdetector.textblock import TextBlock from utils.imgproc_utils import xywh2xyxypoly, rotate_polygons from .misc import FontFormat, px2pt, pt2px, td_pattern, table_pattern from .textlayout import VerticalTextDocumentLayout, HorizontalTextDocumentLayout from .textlayout import VerticalTextDocumentLayout, HorizontalTextDocumentLayout, SceneTextLayout TEXTRECT_SHOW_COLOR = QColor(30, 147, 229, 170) TEXTRECT_SELECTED_COLOR = QColor(248, 64, 147, 170) Loading Loading @@ -63,14 +63,14 @@ class TextBlkItem(QGraphicsTextItem): self.update() def repaint_background(self): self.repainting = True doc = self.document().clone() doc.setDocumentMargin(self.document().documentMargin()) layout = VerticalTextDocumentLayout(doc) if self.is_vertical else HorizontalTextDocumentLayout(doc) layout.line_spacing = self.line_spacing rect = self.rect() layout.setMaxSize(rect.width(), rect.height(), False) doc.setDocumentLayout(layout) layout.setMaxSize(rect.width(), rect.height()) cursor = QTextCursor(doc) block = doc.firstBlock() Loading Loading @@ -121,20 +121,21 @@ class TextBlkItem(QGraphicsTextItem): if blk.angle != 0: self.setRotation(blk.angle) # blk.vertical = False set_char_fmt = False if not blk.rich_text: if blk.translation: self.setPlainText(blk.translation) set_char_fmt = True else: self.setHtml(blk.rich_text) if set_format: font_fmt = FontFormat() font_fmt.from_textblock(blk) self.set_fontformat(font_fmt, set_char_format=set_char_fmt) if not blk.rich_text: if blk.translation: self.setPlainText(blk.translation) else: self.setHtml(blk.rich_text) def setCenterTransform(self): center = self.boundingRect().center() self.setTransformOriginPoint(center) Loading Loading @@ -252,14 +253,6 @@ class TextBlkItem(QGraphicsTextItem): if self.background_pixmap is not None: self.repaint_background() # cursor = QTextCursor(doc) # format = cursor.charFormat() # cursor.mergeCharFormat(format) # cursor.select(QTextCursor.Document) # cursor.mergeBlockCharFormat(format) # cursor.clearSelection() # # https://stackoverflow.com/questions/37160039/set-default-character-format-in-qtextdocument # self.setTextCursor(cursor) self.doc_size_changed.emit(self.idx) def on_document_enlarged(self): Loading @@ -282,21 +275,22 @@ class TextBlkItem(QGraphicsTextItem): pos.setY(pos.y() + delta_x * np.sin(rad)) self.setPos(pos) def documentLayout(self) -> VerticalTextDocumentLayout: def documentLayout(self) -> SceneTextLayout: return self.document().documentLayout() def setStrokeWidth(self, stroke_width): def setStrokeWidth(self, stroke_width: float): if self.stroke_width == stroke_width: return self.stroke_width = stroke_width sw = stroke_width * pt2px(self.document().defaultFont().pointSizeF()) self.documentLayout().updateDocumentMargin(sw/2) self.documentLayout().reLayout() if stroke_width > 0: self.repaint_background() else: self.background_pixmap = None sw = self.stroke_width * pt2px(self.document().defaultFont().pointSizeF()) self.document().setDocumentMargin(sw/2) self.documentLayout().updateDocumentMargin(sw/2) self.on_document_enlarged() self.update() def setStrokeColor(self, scolor): Loading Loading @@ -534,7 +528,6 @@ class TextBlkItem(QGraphicsTextItem): # https://stackoverflow.com/questions/37160039/set-default-character-format-in-qtextdocument cursor.movePosition(QTextCursor.Start) self.setTextCursor(cursor) self.stroke_width = ffmat.stroke_width self.setStrokeWidth(ffmat.stroke_width) self.setStrokeColor(ffmat.srgb) Loading
ballontranslator/ui/textlayout.py +66 −85 File changed.Preview size limit exceeded, changes collapsed. Show changes