Python 使用PySide和QTextEdit的半透明高光(版本2)
我正在尝试制作一个更完整的使用列表的示例: (1) 聚合R、G、B、A和突出显示计数(五个列表) (2) 平均R、G、B、A(四个列表) 平均值列表中的值被指定为背景色。聚合列表用于通过添加和减去高光颜色来计算平均值。每个列表的元素数与文本小部件中的字符数相同 我使用pandas数据框将高光存储在内存中,因为最终可能会有大量高光以各种方式处理,以及其他数据。将鼠标放置在角色上可打印其位置、高光数量和RGBA值。除此之外,有时会出现一个问题——就像画笔从一个高光位置移动到另一个高光位置时被拖了一秒钟。查看文本“PySide.QtCore”末尾“e”上的绿松石色。我认为问题在于如何使用光标的设置和移动位置——但我不确定。添加每个突出显示后是否需要重置光标的位置?我没有正确选择一个字符吗Python 使用PySide和QTextEdit的半透明高光(版本2),python,qt,pyside,qtextedit,Python,Qt,Pyside,Qtextedit,我正在尝试制作一个更完整的使用列表的示例: (1) 聚合R、G、B、A和突出显示计数(五个列表) (2) 平均R、G、B、A(四个列表) 平均值列表中的值被指定为背景色。聚合列表用于通过添加和减去高光颜色来计算平均值。每个列表的元素数与文本小部件中的字符数相同 我使用pandas数据框将高光存储在内存中,因为最终可能会有大量高光以各种方式处理,以及其他数据。将鼠标放置在角色上可打印其位置、高光数量和RGBA值。除此之外,有时会出现一个问题——就像画笔从一个高光位置移动到另一个高光位置时被拖了一秒
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_())
导入系统
作为pd进口熊猫
导入sqlite3
从PySide.QtCore导入*
从PySide.QtGui导入*
def connect_数据库(db_文件):
尝试:
conn=sqlite3.connect(db_文件)
返回控制
除错误e外:
打印(e)
一无所获
def创建数据(连接):#数据库连接
c=connection.cursor()#数据库游标
c、 执行(“创建表节(id整数主键、开始整数、结束整数、r整数、g整数、b整数、a整数)”)
c、 执行(“插入节值(1,0,20100200100100)”)
c、 执行(“插入节值(2,15,20200100100,50)”)
c、 执行(“插入节值(3,18,30100100100)”)
c、 执行(“插入节值(4,50,60100200150)”)
db.commit()
返回c.lastrowid
类QTextEdit2(QTextEdit):
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
self.setMouseTracking(True)
self.cursor=self.textCursor()
self.length=len(self.toPlainText())
self.bg_red=[0表示范围内的n(self.length)]#存储所有高光的聚合值(不是平均值)
self.bg_绿色=[0表示范围内的n(self.length)]
self.bg_blue=[0表示范围内的n(self.length)]
self.bg_alpha=[0表示范围内的n(self.length)]
self.bg_计数=[0表示范围内的n(self.length)]#高光数。如果该值为0,则不显示
#存储的r、g、b仅显示为白色。在这个例子中
#只写亮点。其他的都留下来了
#违约
self.display_red=[0表示范围内的n(self.length)]#设置为要显示的值(高光的平均值)
self.display_green=[0表示范围内的n(self.length)]
self.display_blue=[0表示范围内的n(self.length)]
self.display_alpha=[0表示范围内的n(self.length)]
self.sections=self.load_sections()
self.color_节()
def mouseMoveEvent(自身、事件):
点=QPoint()
x=event.x()
y=event.y()
点setX(x)
赛蒂角(y)
n=self.cursorForPosition(点).position()
打印(“%d:段计数:%d RGBA:%d%d%d”%(n,self.bg\u计数[n],self.display\u红色[n],self.display\u绿色[n],self.display\u蓝色[n],self.display\u alpha[n]))
super().mouseMoveEvent(事件)
def加载_段(自):
c=sqlite3.connect(“qda\u test\u 01.sqlite”)
df=pd.read\u sql\u查询(“从节中选择*”,c)
返回df
def混合_颜色(自身、开始、结束、r、g、b、a):
对于范围内的n(开始、结束):
self.bg_red[n]=self.bg_red[n]+r
self.bg_绿色[n]=self.bg_绿色[n]+g
self.bg_蓝[n]=self.bg_蓝[n]+b
self.bg_alpha[n]=self.bg_alpha[n]+a
self.bg_计数[n]=self.bg_计数[n]+1
self.display\u red[n]=self.bg\u red[n]/self.bg\u 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]
如果self.display_red[n]>255:#以防万一RGBA数据很奇怪。。。
自显示红色[n]=255
如果自身显示为绿色[n]>255:
自我显示_绿色[n]=255
如果自身显示为蓝色[n]>255:
自显示\u蓝色[n]=255
如果self.display_alpha[n]>255:
自我显示α[n]=255
如果自身显示为红色[n]<0:
自显示\u红色[n]=0
如果自身显示为绿色[n]<0:
自我显示_绿色[n]=0
如果自身显示为蓝色[n]<0:
自显示\u蓝色[n]=0
如果自我显示α[n]<0:
自我显示α[n]=0
打印(“位置:%d |部分:r:%d g:%g b:%d a:%d |显示:r:%d g:%g b:%d a:%d”%(n,self.bg|红色[n],self.bg|绿色[n],self.bg|蓝色[n],self.bg|阿尔法[n],self.DISPLAY|红色[n],self.DISPLAY|绿色[n],self.bg|蓝色[n]))
颜色=QColor(自显示\红色[n]、自显示\绿色[n]、自显示\蓝色[n])
color.setAlpha(self.display_alpha[n])
cursor=self.textCursor()
光标设置位置(n)
cursor.movePosition(QTextCursor.NextCharacter,QTextCursor.KeepAnchor,1)
charfmt=cursor.charFormat()
charfmt.挫折地面(颜色)
self.setCurrentCharFormat(charfmt)
self.setTextCursor(游标)
def颜色_部分(自身):
对于范围内的n(self.sections.id.count()):
>>> 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'
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)