song_text_widget.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:songscreen 作者: maccesch 项目源码 文件源码
def _add_line(self, scene, line_index, left, text_str, font, color, offset=0, align_right=False):
        y = self._line_height * line_index + offset + self.h * 0.1

        if line_index == 1:
            self._first_lyrics_line_y = y

        metrics = QFontMetricsF(font)
        text_width = metrics.width(text_str)
        max_text_width = (self.w - left - left)
        overflow = text_width - max_text_width

        if overflow <= 0:
            text = scene.addText(text_str, font)
            if align_right:
                text.setPos(self.w - left - text_width, y)
            else:
                text.setPos(left, y)
            text.setDefaultTextColor(color)
        else:
            scale_factor = max_text_width / text_width
            if scale_factor >= 0.9:
                text = scene.addText(text_str, font)
                text.setPos(left, y)
                text.setDefaultTextColor(color)
                text.setTransform(QTransform().scale(scale_factor, 1.0))
            else:
                self._extra_lines_after.append(line_index)

                idx = len(text_str) // 2
                while idx < len(text_str) and not text_str[idx].isspace():
                    idx += 1

                line_index = self._add_line(scene, line_index, left, text_str[:idx], font, color, offset)
                line_index += 1
                line_index = self._add_line(scene, line_index, left, "\t" + text_str[idx:], font, color,
                                            offset - self._line_height * 0.1)

        return line_index
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号