gpt4 book ai didi

python - 使用 PySide 和 QTextEdit(版本 2)的半透明突出显示

转载 作者:太空宇宙 更新时间:2023-11-03 14:38:46 26 4
gpt4 key购买 nike

我正在尝试制作一个更完整的示例 semi-transparent highlights使用列表:

(1) 聚合 R、G、B、A 和高光计数(五个列表)

(2)平均R、G、B、A(四个列表)

平均值列表中的值被指定为背景颜色。聚合列表用于通过添加和减去突出显示颜色来计算平均值。每个列表的元素数量与文本小部件中的字符数量相同。

我使用 pandas 数据框将突出显示存储在内存中,因为最终可能有大量的突出显示需要以各种方式处理以及其他数据。将鼠标放在某个字符上会打印出其位置、高光数量和 RGBA 值。不过,有时会出现一个问题——就好像画笔从一个高光位置移动到另一个位置时被拖动一秒钟。查看文本“PySide.QtCore”末尾的“e”上的青绿色。我认为问题在于我如何使用光标的设置和移动位置 - 但我不确定。添加每个突出显示后是否需要重置光标的位置?我是否没有正确选择单个字符?

import sys
import pandas as pd
import sqlite3
from PySide.QtCore import *
from PySide.QtGui import *

def connect_database(db_file):
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
return None

def create_data(connection): # database connection
c = connection.cursor() # database cursor
c.execute("CREATE TABLE sections(id INTEGER PRIMARY KEY, start INTEGER, end INTEGER, r INTEGER, g INTEGER, b INTEGER, a INTEGER)")
c.execute("INSERT INTO sections VALUES(1,0,20,100,200,100,100)")
c.execute("INSERT INTO sections VALUES(2,15,20,200,100,100,50)")
c.execute("INSERT INTO sections VALUES(3,18,30,100,100,200,100)")
c.execute("INSERT INTO sections VALUES(4,50,60,100,200,200,150)")
db.commit()
return c.lastrowid


class QTextEdit2(QTextEdit):

def __init__(self, parent=None):
super().__init__(parent)
self.setMouseTracking(True)
self.cursor = self.textCursor()

self.length = len(self.toPlainText())
self.bg_red = [0 for n in range(self.length)] # stores aggregate values of all highlights (not averages)
self.bg_green = [0 for n in range(self.length)]
self.bg_blue = [0 for n in range(self.length)]
self.bg_alpha = [0 for n in range(self.length)]
self.bg_count = [0 for n in range(self.length)] # number of highlights. if this is 0 then don't display
# stored r,g,b just display white. in this example
# only highlights are written. everything else stays
# default

self.display_red = [0 for n in range(self.length)] # set to the value to display (average of highlights)
self.display_green = [0 for n in range(self.length)]
self.display_blue = [0 for n in range(self.length)]
self.display_alpha = [0 for n in range(self.length)]

self.sections = self.load_sections()
self.color_sections()

def mouseMoveEvent(self, event):
point = QPoint()
x = event.x()
y = event.y()
point.setX(x)
point.setY(y)
n = self.cursorForPosition(point).position()
print("%d: Section Count: %d RGBA: %d %d %d %d" % (n, self.bg_count[n],self.display_red[n], self.display_green[n],self.display_blue[n], self.display_alpha[n]))
super().mouseMoveEvent(event)


def load_sections(self):
c = sqlite3.connect("qda_test_01.sqlite")
df = pd.read_sql_query("SELECT * FROM sections", c)
return df


def blend_colors(self, start, end, r, g, b, a):
for n in range(start,end):
self.bg_red[n] = self.bg_red[n]+r
self.bg_green[n] = self.bg_green[n]+g
self.bg_blue[n] = self.bg_blue[n]+b
self.bg_alpha[n] = self.bg_alpha[n]+a
self.bg_count[n] = self.bg_count[n]+1
self.display_red[n] = self.bg_red[n]/self.bg_count[n]
self.display_green[n] = self.bg_green[n] / self.bg_count[n]
self.display_blue[n] = self.bg_blue[n] / self.bg_count[n]
self.display_alpha[n] = self.bg_alpha[n] / self.bg_count[n]
if self.display_red[n] > 255: # just in case RGBA data is weird...
self.display_red[n] = 255
if self.display_green[n] > 255:
self.display_green[n] = 255
if self.display_blue[n] > 255:
self.display_blue[n] = 255
if self.display_alpha[n] > 255:
self.display_alpha[n] = 255
if self.display_red[n] < 0:
self.display_red[n] = 0
if self.display_green[n] < 0:
self.display_green[n] = 0
if self.display_blue[n] < 0:
self.display_blue[n] = 0
if self.display_alpha[n] < 0:
self.display_alpha[n] = 0

print("LOCATION: %d | SECTION: r:%d g:%g b:%d a:%d | DISPLAY: r:%d g:%g b:%d a:%d" % (n,self.bg_red[n],self.bg_green[n],self.bg_blue[n],self.bg_alpha[n],self.display_red[n],self.display_green[n],self.display_blue[n],self.display_alpha[n]))

color = QColor(self.display_red[n], self.display_green[n], self.display_blue[n])
color.setAlpha(self.display_alpha[n])
cursor = self.textCursor()
cursor.setPosition(n)
cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor, 1)
charfmt = cursor.charFormat()
charfmt.setBackground(color)
self.setCurrentCharFormat(charfmt)
self.setTextCursor(cursor)


def color_sections(self):
for n in range(self.sections.id.count()):
print("-----SECTION:%d-----" % (n))
section = self.sections.iloc[n]
self.blend_colors(section.start, section.end, section.r, section.g, section.b, section.a)



if __name__ == '__main__':

# Create database and sections to highlight
fn='qda_test_01.sqlite'
db=connect_database(fn)
id=create_data(db)
db.close()

app = QApplication(sys.argv)
window = QTextEdit2(
"In addition, the PySide.QtCore.QPoint class provides the PySide.QtCore.QPoint.manhattanLength() function which gives an inexpensive approximation of the length of the PySide.QtCore.QPoint object interpreted as a vector. Finally, PySide.QtCore.QPoint objects can be streamed as well as compared.")
window.show()
sys.exit(app.exec_())

最佳答案

如果您使用了我在previous answer中给您的代码,当前示例将正常工作。事实上,您的更改引入了许多相差一的错误。

要调试此问题,您应该首先检查以确定应突出显示哪些文本 block 。从数据库中获取文本的前 65 个字符和开始/结束值,得出:

>>> t = "In addition, the PySide.QtCore.QPoint class provides the PySide."
>>> t[0:20]
'In addition, the PyS'
>>> t[15:20]
'e PyS'
>>> t[18:30]
'ySide.QtCore'
>>> t[50:60]
'es the PyS'

如果将此与实际输出中的突出显示进行比较,您会发现没有任何部分正确匹配(例如,查看每个“PySide”中的“S”)。

要使其正常工作,您必须在开始时一次获取文本光标,使用它进行所有必要的更改,然后重新设置它一次强>最后:

cursor = self.textCursor()
for n in range(start, end):
...
cursor.setPosition(n)
cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor)
charfmt = cursor.charFormat()
charfmt.setBackground(color)
cursor.setCharFormat(charfmt)
cursor.clearSelection()
self.setTextCursor(cursor)

这类似于更新数据库:您使用游标来计划一系列更改,然后最后将它们作为单个操作提交。

关于python - 使用 PySide 和 QTextEdit(版本 2)的半透明突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46716741/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com