半透明亮點我試圖使semi-transparent highlights使用列表爲一個更完整的示例:使用PySide和的QTextEdit(版本2)
(1)聚合的R,G,B,A,並突出顯示計數(五個列表)
(2)平均的R,G,B,A(四個列表)
在平均列表中的值獲得分配爲背景色。聚合列表用於通過添加和減去高亮顏色來計算平均值。每個列表都有與文本小部件中的字符一樣多的元素。
我正在使用一個熊貓數據框在內存中存儲高亮,因爲可能有很多它們以各種方式以及其他數據最終處理。將鼠標放在字符上可以打印出它的位置,亮點數量和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_())
這是什麼應用?突出顯示的目的究竟是什麼?沒有任何上下文,你的代碼很難理解。 – ekhumoro
我的代碼也很難理解,因爲我是Python的新手,在編程時會生鏽。每個突出顯示代表一個或多個研究人員分配的類別。研究人員將對重點進行重新分類甚至刪除,這些重點常常重疊幾層。用戶可以根據自己的喜好更改顏色。在社會科學中,這個過程被稱爲「[編碼](https://en.wikipedia.org/wiki/Coding_(social_sciences))」訪談。我懷疑我的問題是在setPosition,movePosition,設置格式行。但是,如果代碼太難讀,我可以嘗試一個更基本的例子並重新發布。 – davideps
只有一個突出顯示時,代碼似乎可以正常工作。所以,當它重複的過程中,某些事情會發生錯誤。 – davideps