PDA

View Full Version : Multiple selecting problem (PySide)



greenenvy
23rd May 2015, 19:13
I have a problem(or not so much a problem, more like a functionality i really want to implement). I am working with my colleagues on a first year of faculty on an advance Text editor program and we use PySide, and i'm working now on a find function which uses a Knuth-Morris-Pratt algorithm and text selection. The following function i made only selects the last found word which is logical, but i want to select all, or on any way highlight the words i have found. I have searched for so long, and i have found some articles with setExtraSelections but i didn't manage to implement it to work the way i want. Can someone help me with this problem?

x is a list with indexes of the first letter of the found word.

self.mdiWidget.currentWidget() is the active QTextEdit widget.


def find(self):
input = QInputDialog.getText(self.mdiWidget.currentWidget( ), unicode("Find"), unicode('Unesite Tekst:'))
txt = self.mdiWidget.currentWidget().toPlainText()
x = kmp(txt.encode("UTF-8"), input[0].encode("UTF-8"))
if x == []:
msgBox = QMessageBox()
msgBox.setText("Nothig is found")
msgBox.exec_()
c = self.mdiWidget.currentWidget().textCursor()
for i in x:
c.setPosition(i)
c.setPosition(i+len(input[0]), QTextCursor.KeepAnchor)
self.mdiWidget.currentWidget().setTextCursor(c)

That was what i have initional done.

Then i managed to do this, like its done in the video: https://vid.me/2yuT
The problem now is, i want to clear these extra selections on cursor position or selection change but i don't get it how to do it. Here is the code i edited:


def find(self):
input = QInputDialog.getText(self.mdiWidget.currentWidget( ), unicode("Find"), unicode('Unesite Tekst:'))
txt = self.mdiWidget.currentWidget().toPlainText()
x = kmp(txt.encode("UTF-8"), input[0].encode("UTF-8"))
if x == []:
msgBox = QMessageBox()
msgBox.setText("Nothig is found")
msgBox.exec_()
extraSelections = []
c = self.mdiWidget.currentWidget().textCursor()
for i in x:
c.setPosition(i)
c.setPosition(i+len(input[0]), QTextCursor.KeepAnchor)
self.mdiWidget.currentWidget().setTextCursor(c)
Color = QColor(191, 191, 191, 189)
currentWord = self.mdiWidget.currentWidget().ExtraSelection()
currentWord.format.setBackground(Color)
currentWord.cursor = c
extraSelections.append(currentWord)
self.mdiWidget.currentWidget().setExtraSelections( extraSelections)

EDIT: I know this is a C++ forum, but i don't know where to ask.