Commit 708cc0ca authored by dmMaze's avatar dmMaze
Browse files

fix textblock boundingRect

parent d683dc09
Loading
Loading
Loading
Loading
+1 −11
Original line number Diff line number Diff line
@@ -807,20 +807,10 @@ class SceneTextManager(QObject):
        if scale != 1 and not fmt.alignment == 0:
            xywh = (np.array(xywh, np.float64) * scale).astype(np.int32).tolist()

        if fmt.alignment == 0 or fmt.alignment == 2:
            x_shift = (scale - 1) * xywh[2] // 2 + xywh[0] * scale
            y_shift = (scale - 1) * xywh[3] // 2 + xywh[1] * scale
            xywh[0] = int(abs_centroid[0] * scale) + x_shift
            xywh[1] = int(abs_centroid[1] * scale)  + y_shift
        if fmt.alignment == 2:
            ex_w, ex_h = xywh[2] - old_br[2], xywh[3] - old_br[3]
            if ex_w > 0:
                xywh[0] -= ex_w

        if restore_charfmts:
            char_fmts = blkitem.get_char_fmts()        
        
        blkitem.setRect(xywh, repaint=False)
        blkitem.set_size(xywh[2], xywh[3], set_layout_maxsize=True)
        blkitem.setPlainText(new_text)
        if len(self.pairwidget_list) > blkitem.idx:
            self.pairwidget_list[blkitem.idx].e_trans.setPlainText(new_text)
+35 −35
Original line number Diff line number Diff line
@@ -300,9 +300,8 @@ class TextBlkItem(QGraphicsTextItem):
    def boundingRect(self) -> QRectF:
        br = super().boundingRect()
        if self._display_rect is not None:
            size = self.documentSize()
            br.setHeight(max(self._display_rect.height(), size.height()))
            br.setWidth(max(self._display_rect.width(), size.width()))
            br.setHeight(self._display_rect.height())
            br.setWidth(self._display_rect.width())
        return br

    def padding(self) -> float:
@@ -464,8 +463,8 @@ class TextBlkItem(QGraphicsTextItem):
        self.old_undo_steps = self.document().availableUndoSteps()

    def on_document_enlarged(self):
        rect = self.rect()
        self.set_size(rect.width(), rect.height())
        size = self.documentSize()
        self.set_size(size.width(), size.height())

    def get_scale(self) -> float:
        tl = self.topLevelItem()
@@ -926,13 +925,16 @@ class TextBlkItem(QGraphicsTextItem):
            return
        br = self.absBoundingRect(qrect=True)
        br_w, br_h = br.width(), br.height()
        if br_w > mw or br_h > mh:

        if self.is_vertical:
            if cond_on_alignment:
                mh = br.height()
        else:
            if cond_on_alignment:
                mw = br.width()

        if br_w > mw or br_h > mh:

            P = self.padding() * 2
            mw += P
            mh += P
@@ -947,18 +949,20 @@ class TextBlkItem(QGraphicsTextItem):
        '''
        rotation invariant
        '''

        if set_layout_maxsize:
            self.layout.setMaxSize(w, h)

        old_w = self._display_rect.width()
        old_h = self._display_rect.height()
        otr = self.sceneBoundingRect().topLeft()

        oc = self.sceneBoundingRect().center()
        self._display_rect.setWidth(w)
        self._display_rect.setHeight(h)
        if set_layout_maxsize:
            self.layout.setMaxSize(w, h)
        self.setCenterTransform()
        pos = self.pos() + otr - self.sceneBoundingRect().topLeft()
        dw, dh = w - old_w, h - old_h
        pos_shift = oc - self.sceneBoundingRect().center()
        
        align_tl = align_tr = align_c = False
        align_c = align_tl = align_tr = False
        if self.is_vertical:
            align_tr = True
        else:
@@ -970,23 +974,19 @@ class TextBlkItem(QGraphicsTextItem):
            else:
                align_c = True

        if align_tr:
            if self.rotation() == 0:
                pos.setX(pos.x() - dw)
            else:
                rad = np.deg2rad(self.rotation())
                pos.setX(pos.x() - dw * np.cos(rad))
                pos.setY(pos.y() - dw * np.sin(rad))
        elif align_c:
            dh /= 2
            if self.rotation() == 0:
                pos.setY(pos.y() - dh)
        if align_c:
            pass
        else:
                rad = np.deg2rad(self.rotation())
                pos.setX(pos.x() - dh * np.sin(rad))
                pos.setY(pos.y() - dh * np.cos(rad))
                
        self.setPos(pos)
            dw, dh = (w - old_w) / 2, (h - old_h) / 2
            if align_tr:
                dw = -dw
            rad = -np.deg2rad(self.rotation())
            c, s = np.cos(rad), np.sin(rad)
            dx = c * dw + s * dh
            dy = -s * dw + c * dh
            pos_shift = pos_shift + QPointF(dx, dy)

        self.setPos(self.pos() + pos_shift)
        if self.blk is not None and set_blk_size:
            self.blk._bounding_rect = self.absBoundingRect()