我正在尝试使用以下列表的semi-transparent highlights更完整的示例:
(1)汇总R,G,B,A和突出显示计数(五个列表)
(2)平均R,G,B,A(四个列表)
平均列表中的值被指定为背景颜色。聚合列表用于通过添加和减去高亮颜色来计算平均值。每个列表都包含与文本小部件中的字符一样多的元素。
我使用pandas数据帧将亮点存储在内存中,因为它们可能会以各种方式处理大量的内容以及最终的其他数据。将鼠标放在字符上会打印出其位置,高亮显示数量和RGBA值。除此之外,有时会弹出一个问题 - 就好像从一个高亮显示位置移动到另一个高亮显示位置时,画笔会被拖动一秒钟。看看" e"的绿松石色。在文本的末尾" PySide.QtCore"。我认为问题在于我如何使用光标的设置和移动位置 - 但我不确定。添加每个高光后,是否需要重置光标位置?我没有正确选择单个字符吗?
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_())
答案 0 :(得分:1)
如果您使用了我在previous answer中提供的代码,则当前示例将正常运行。事实上,您的更改引入了许多一对一的错误。
要进行调试,您应首先检查以确切地突出显示哪些文本块。获取文本的前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'
如果将其与实际输出中的高光进行比较,您将看到没有一个部分正确匹配(例如,查看&#34; S&#34;在每个&#34; PySide&#34 ;)
要使其正常工作,您必须在开头使用文本光标一次,使用它进行所有必要的更改,然后重新设置一次强>最后:
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)
这类似于更新数据库:您使用游标来安排一系列更改,然后在最后将它们作为单个操作提交。