如何在使用QScintilla setText时撤销操作?

2024-06-17 13:33:18 发布

您现在位置:Python中文网/ 问答频道 /正文

让我先发布一些小助手函数,我将用它们来表达我的问题:

import textwrap
import sys
from pathlib import Path

from PyQt5.Qsci import QsciScintilla
from PyQt5.Qt import *  # noqa


def set_style(sci):
    # Set default font
    sci.font = QFont()
    sci.font.setFamily('Consolas')
    sci.font.setFixedPitch(True)
    sci.font.setPointSize(8)
    sci.font.setBold(True)
    sci.setFont(sci.font)
    sci.setMarginsFont(sci.font)
    sci.setUtf8(True)

    # Set paper
    sci.setPaper(QColor(39, 40, 34))

    # Set margin defaults
    fontmetrics = QFontMetrics(sci.font)
    sci.setMarginsFont(sci.font)
    sci.setMarginWidth(0, fontmetrics.width("000") + 6)
    sci.setMarginLineNumbers(0, True)
    sci.setMarginsForegroundColor(QColor(128, 128, 128))
    sci.setMarginsBackgroundColor(QColor(39, 40, 34))
    sci.setMarginType(1, sci.SymbolMargin)
    sci.setMarginWidth(1, 12)

    # Set indentation defaults
    sci.setIndentationsUseTabs(False)
    sci.setIndentationWidth(4)
    sci.setBackspaceUnindents(True)
    sci.setIndentationGuides(True)
    sci.setFoldMarginColors(QColor(39, 40, 34), QColor(39, 40, 34))

    # Set caret defaults
    sci.setCaretForegroundColor(QColor(247, 247, 241))
    sci.setCaretWidth(2)

    # Set edge defaults
    sci.setEdgeColumn(80)
    sci.setEdgeColor(QColor(221, 221, 221))
    sci.setEdgeMode(sci.EdgeLine)

    # Set folding defaults (http://www.scintilla.org/ScintillaDoc.html#Folding)
    sci.setFolding(QsciScintilla.CircledFoldStyle)

    # Set wrapping
    sci.setWrapMode(sci.WrapNone)

    # Set selection color defaults
    sci.setSelectionBackgroundColor(QColor(61, 61, 52))
    sci.resetSelectionForegroundColor()

    # Set scrollwidth defaults
    sci.SendScintilla(QsciScintilla.SCI_SETSCROLLWIDTHTRACKING, 1)

    # Current line visible with special background color
    sci.setCaretLineBackgroundColor(QColor(255, 255, 224))

    # Set multiselection defaults
    sci.SendScintilla(QsciScintilla.SCI_SETMULTIPLESELECTION, True)
    sci.SendScintilla(QsciScintilla.SCI_SETMULTIPASTE, 1)
    sci.SendScintilla(QsciScintilla.SCI_SETADDITIONALSELECTIONTYPING, True)


def set_state1(sci):
    sci.clear_selections()
    base = "line{} state1"
    view.setText("\n".join([base.format(i) for i in range(10)]))
    for i in range(0, 10, 2):
        region = (len(base) * i, len(base) * (i + 1) - 1)
        if i == 0:
            view.set_selection(region)
        else:
            view.add_selection(region)


def set_state2(sci):
    base = "line{} state2"
    view.setText("\n".join([base.format(i) for i in range(10)]))
    for i in range(1, 10, 2):
        region = (len(base) * i, len(base) * (i + 1) - 1)
        if i == 1:
            view.set_selection(region)
        else:
            view.add_selection(region)


class Editor(QsciScintilla):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        set_style(self)

    def clear_selections(self):
        sci = self
        sci.SendScintilla(sci.SCI_CLEARSELECTIONS)

    def set_selection(self, r):
        sci = self
        sci.SendScintilla(sci.SCI_SETSELECTION, r[1], r[0])

    def add_selection(self, r):
        sci = self
        sci.SendScintilla(sci.SCI_ADDSELECTION, r[1], r[0])

    def sel(self):
        sci = self
        regions = []

        for i in range(sci.SendScintilla(sci.SCI_GETSELECTIONS)):
            regions.append(
                sci.SendScintilla(sci.SCI_GETSELECTIONNSTART, i),
                sci.SendScintilla(sci.SCI_GETSELECTIONNEND, i)
            )

        return sorted(regions)

实际上我有几个问题:

问题1)

if __name__ == '__main__':
    app = QApplication(sys.argv)

    view = Editor()
    set_state1(view)
    view.move(1000, 100)
    view.resize(800, 300)
    view.show()
    app.exec_()

我会得到这个(你可以在下面的快照中看到这个问题):

enter image description here

问题2)

if __name__ == '__main__':
    app = QApplication(sys.argv)

    view = Editor()
    set_state1(view)
    set_state2(view)
    view.move(1000, 100)
    view.resize(800, 300)
    view.show()
    app.exec_()

如何修改代码,以便在按ctrl+z时恢复state1?你知道吗

现在使用ctrl+z时,您将无法获得state1:

enter image description here

主要是因为setText的行为方式:

Replaces all of the current text with text. Note that the undo/redo history is cleared by this function.

我已经尝试了一些发布在undo and redo文档中的函数,但是到目前为止没有成功。你知道吗

例如,我的一次尝试是先选择所有文本,然后使用replaceSelectedText,最后手动将选择从以前的状态还原,结果很糟糕(我不希望编辑器在撤消/重做时滚动出错)。。。基本上,我想得到和升华文本一样的感觉。你知道吗

顺便说一句,这是一个很小的例子,但在实际情况下,我会积累一堆操作,而不是承诺闪烁非常频繁。。。这就是为什么我有兴趣找出如何回滚到以前的状态时,使用不可撤消的setText。。。否则,我希望避免使用闪烁函数,如insertAt、replaceSelectedText或similar。。。因为我使用python字符串内置函数在内部修改缓冲区。你知道吗

编辑:

我很肯定beginUndoAction&endUndoAction不会帮助我回答问题2,但是。。。那SCI_ADDUNDOACTION呢?虽然docs很混乱,但是。。。:/


Tags: selfviewtruebasedeffontdefaultssci
1条回答
网友
1楼 · 发布于 2024-06-17 13:33:18

问题1: 最后添加的选择将自动设置为Main选择。要删除它,请在set_state1函数的末尾添加行sci.SendScintilla(sci.SCI_SETMAINSELECTION, -1)。你知道吗

问题2:

  • 您描述它的方法是存储选择,使用replaceSelectedText,然后使用setCursorPosition/reselecting all selectionssetFirstVisibleLine恢复滚动位置。你知道吗
  • 查看^ {
// Set the given text.
void QsciScintilla::setText(const QString &text)
{
    bool ro = ensureRW();

    SendScintilla(SCI_SETTEXT, ScintillaBytesConstData(textAsBytes(text)));
    SendScintilla(SCI_EMPTYUNDOBUFFER);

    setReadOnly(ro);
}

您可以尝试使用sci.SendScintilla(sci.SCI_SETTEXT, b"some text")设置文本,这不会重置撤消/重做缓冲区。你知道吗

相关问题 更多 >