Issue
I'm trying to make a more complete example of semi-transparent highlights using lists for:
(1) aggregate R,G,B,A, and highlight counts (five lists)
(2) average R,G,B,A (four lists)
The values in the average list get assigned as background colors. The aggregate lists are used to compute the average by adding and subtracting highlight colors. Each list has as many elements as characters in the text widget.
I'm using a pandas dataframe to store the highlights in memory, since there may be a ton of them to process in various ways as well as other data eventually. Placing the mouse over a character prints out its location, number of highlights, and RGBA values. Except, a problem pops up sometimes--as if a paint brush gets dragged for a second when moving from one highlight location to another. Look at the turquoise color on the "e" at the end of the text "PySide.QtCore". I think the problem is how I'm using the set and move positions of the cursor--but I'm not certain. Do I need to reset the position of the cursor after adding each highlight? Am I not selecting a single character correctly?
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_())
Solution
If you had used the code I gave you in my previous answer, the current example would work correctly. As it is, your changes have introduced lots of off-by-one errors.
To debug this, you should have first checked to see exactly which blocks of text should be highlighted. Taking the first 65 characters of the text and the start/end values from the database, this gives:
>>> 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'
If you compare this to the highlights in the actual output, you'll see that none of the sections match up correctly (e.g. look at "S" in each "PySide").
To get this to work properly, you must get the text-cursor once at the beginning, use that to make all the necessary changes, and then re-set it once at the end:
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)
This is analogous to updating a database: you use a cursor to schedule a series of changes, and then commit them as a single operation at the end.
Answered By - ekhumoro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.