Commit e1d52f0e authored by dmMaze's avatar dmMaze
Browse files

fix qt6 compatibility

parent 5227f1ce
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ from .misc import FontFormat, set_html_color
from .textitem import TextBlkItem, TextBlock
from .canvas import Canvas
from .constants import CONFIG_FONTSIZE_CONTENT, WIDGET_SPACING_CLOSE
from . import constants as C

from utils.logger import logger as LOGGER

@@ -381,7 +382,11 @@ class SizeControlLabel(QLabel):
    def mousePressEvent(self, e: QMouseEvent) -> None:
        if e.button() == Qt.MouseButton.LeftButton:
            self.mouse_pressed = True
            self.cur_pos = e.globalPos().x() if self.direction == 0 else e.globalPos().y()
            if C.FLAG_QT6:
                g_pos = e.globalPosition().toPoint()
            else:
                g_pos = e.globalPos()
            self.cur_pos = g_pos.x() if self.direction == 0 else g_pos.y()
        return super().mousePressEvent(e)

    def mouseReleaseEvent(self, e: QMouseEvent) -> None:
@@ -392,11 +397,15 @@ class SizeControlLabel(QLabel):

    def mouseMoveEvent(self, e: QMouseEvent) -> None:
        if self.mouse_pressed:
            if C.FLAG_QT6:
                g_pos = e.globalPosition().toPoint()
            else:
                g_pos = e.globalPos()
            if self.direction == 0:
                new_pos = e.globalPos().x()
                new_pos = g_pos.x()
                self.size_ctrl_changed.emit(new_pos - self.cur_pos)
            else:
                new_pos = e.globalPos().y()
                new_pos = g_pos.y()
                self.size_ctrl_changed.emit(self.cur_pos - new_pos)
            self.cur_pos = new_pos
        return super().mouseMoveEvent(e)
+3 −2
Original line number Diff line number Diff line
@@ -43,9 +43,8 @@ def pixmap2ndarray(pixmap: Union[QPixmap, QImage], keep_alpha=True):
        return img
    else:
        return np.copy(img[:,:,:3])
        return cv2.cvtColor(img[:, :, 0:3], cv2.COLOR_RGB2BGR)

def ndarray2pixmap(img):
def ndarray2pixmap(img, return_qimg=False):
    if len(img.shape) == 2:
        img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    height, width, channel = img.shape
@@ -56,6 +55,8 @@ def ndarray2pixmap(img):
        img_format = QImage.Format.Format_RGB888
    img = np.ascontiguousarray(img)
    qImg = QImage(img.data, width, height, bytesPerLine, img_format).rgbSwapped()
    if return_qimg:
        return qImg
    return QPixmap(qImg)

class TextBlkEncoder(NumpyEncoder):
+38 −15
Original line number Diff line number Diff line
@@ -3,10 +3,11 @@ from qtpy.QtGui import QTextCharFormat, QTextDocument, QImage, QTransform, QPale

import cv2
import numpy as np
from typing import List, Set, Any
from typing import List
from functools import lru_cache, cached_property

from .misc import pixmap2ndarray, LruIgnoreArgs, pt2px
from .misc import pixmap2ndarray, ndarray2pixmap, LruIgnoreArgs
from . import constants as C

def print_transform(tr: QTransform):
    print(f'[[{tr.m11(), tr.m12(), tr.m13()}]\n [{tr.m21(), tr.m22(), tr.m23()}]\n [{tr.m31(), tr.m32(), tr.m33()}]]')
@@ -192,6 +193,29 @@ class SceneTextLayout(QAbstractTextDocumentLayout):
            return self._max_font_size
        return self.document().defaultFont().pointSizeF()

def line_draw_qt6(painter: QPainter, line: QTextLine, x: float, y: float, selected: bool, selection: QAbstractTextDocumentLayout.Selection = None):
    # some how qt6 line.draw doesn't allow pass FormatRange
    if selected:    
        qimg = QImage(line.naturalTextWidth(), line.height(), QImage.Format.Format_ARGB32)
        qimg.fill(Qt.GlobalColor.transparent)
        p = QPainter(qimg)
        line.draw(p, QPointF(-line.x(), -line.y()), )
        img = pixmap2ndarray(qimg, keep_alpha=True)[..., -1]
        img = ndarray2pixmap(img, return_qimg=True)
        p.drawImage(0, 0, img)
        p.end()
        painter.drawImage(QPointF(line.x() + x, line.y() + y), qimg)
    else:
        line.draw(painter, QPointF(x, y))

def line_draw_qt5(painter: QPainter, line: QTextLine, x: float, y: float, selected: bool, selection: QAbstractTextDocumentLayout.Selection = None):
    o = None
    if selected:
        o = QTextLayout.FormatRange()
        o.start = line.textStart()
        o.length = line.textLength()
        o.format = selection.format
    line.draw(painter, QPointF(x, y), o)

class VerticalTextDocumentLayout(SceneTextLayout):

@@ -206,9 +230,10 @@ class VerticalTextDocumentLayout(SceneTextLayout):
        self.has_selection = False
        self.punc_rect_cache = {} 
        self.punc_align_center = True

        self.draw_shifted = 0

        self.line_draw = line_draw_qt6 if C.FLAG_QT6 else line_draw_qt5

    @property
    def align_right(self):
        return False
@@ -257,6 +282,7 @@ class VerticalTextDocumentLayout(SceneTextLayout):
        cursor_block = None
        context_sel = context.selections
        has_selection = False
        selection = None
        if len(context_sel) > 0:
            has_selection = True
            selection = context_sel[0]
@@ -285,15 +311,12 @@ class VerticalTextDocumentLayout(SceneTextLayout):
                char = blk_text[char_idx]
                cfmt = self.get_char_fontfmt(blk_no, char_idx)
                fm = cfmt.font_metrics
                o = None
                selected = False
                if has_selection:
                    sel_start = selection.cursor.selectionStart() - blpos 
                    sel_end = selection.cursor.selectionEnd() - blpos
                    if char_idx < sel_end and char_idx >= sel_start:
                        o = QTextLayout.FormatRange()
                        o.start = line.textStart()
                        o.length = line.textLength()
                        o.format = selection.format
                        selected = True
                
                natral_shifted = max(line.naturalTextWidth() - cfmt.br.width(), 0)
                if char in PUNSET_VERNEEDROTATE:
@@ -323,18 +346,15 @@ class VerticalTextDocumentLayout(SceneTextLayout):
                    else:
                        yoff = -pun_tbr.top() - fm.ascent() - pun_tbr.height() / 2 - cfmt.br.width() / 2

                    line.draw(painter, QPointF(hight_comp,  yoff), o)
                    self.line_draw(painter, line, hight_comp,  yoff, selected, selection)
                    painter.setTransform(inv_transform, True)

                elif char in PUNSET_PAUSEORSTOP:
                    pun_tbr, pun_br = cfmt.punc_rect(char)
                    act_rect = cfmt.punc_actual_rect(line, char, cache=True)
                    # yoff = 0
                    # yoff = pun_br.top() - pun_tbr.top()
                    yoff = -act_rect[1]
                    xoff = -pun_tbr.left()
                    yoff += self.draw_shifted
                    # xoff -= self.draw_shifted
                    if num_lspaces > 0:
                        if natral_shifted == 0:
                            natral_shifted = num_lspaces * cfmt.space_width
@@ -347,9 +367,12 @@ class VerticalTextDocumentLayout(SceneTextLayout):
                    else:
                        xoff += cfmt.br.width() - pun_tbr.width()
                        xoff -= self.draw_shifted
                    line.draw(painter, QPointF(xoff, yoff), o)
                    self.line_draw(painter, line, xoff, yoff, selected, selection)

                else:
                    line.draw(painter, QPointF(-natral_shifted, -cfmt.tbr.top() - fm.ascent() + natral_shifted), o)
                    yoff = -cfmt.tbr.top() - fm.ascent() + natral_shifted
                    self.line_draw(painter, line, -natral_shifted, yoff, selected, selection)

            block = block.next()
        
        if cursor_block is not None: