4141
4242import os
4343from PyQt5 import uic
44- from PyQt5 .QtCore import pyqtSlot , QFile , QTextStream , QUrl
45- from PyQt5 .QtWidgets import (QDialog , QFileDialog , QMessageBox ,
46- QColorDialog )
47- from PyQt5 import QtGui , QtCore
44+ from PyQt5 .QtCore import pyqtSlot , QFile , QTextStream , QUrl , Qt
45+ from PyQt5 .QtGui import QStandardItem , QTextCursor , QTextCharFormat , QTextDocument , QColor
46+ from PyQt5 .QtWidgets import QDialog , QFileDialog , QMessageBox , QColorDialog
4847
4948from qtvcp .core import Path
5049from qtvcp .qt_makegui import VCPWindow
@@ -73,57 +72,107 @@ def __init__(self, parent=WIDGETS, path=None, addBuiltinStyles = True):
7372 if PATH :
7473 self .setPath ()
7574 self .styleSheetCombo .currentIndexChanged .connect (self .selectionChanged )
75+ self .search_box .textChanged .connect (self .highlight_all_matches )
76+ self .btn_next .clicked .connect (self .find_next )
77+ self .btn_prev .clicked .connect (self .find_previous )
7678 self .preferencePath = 'DEFAULT'
7779
7880 def load_dialog (self ):
7981 if WIDGETS .PREFS_ :
8082 path = WIDGETS .PREFS_ .getpref ('style_QSS_Path' , 'DEFAULT' , str , 'BOOK_KEEPING' )
8183 self .preferencePath = path
82- self .loadedItem .setData ( path , role = QtCore . Qt .UserRole + 1 )
84+ self .loadedItem .setData ( path , role = Qt .UserRole + 1 )
8385 self .lineEdit_path .setText (path )
8486 self .styleSheetCombo .setToolTip ('<b>{}</b>' .format (path ))
8587 self .origStyleSheet = self .parent .styleSheet ()
8688 self .styleTextView .setPlainText (self .origStyleSheet )
8789 self .show ()
8890 self .activateWindow ()
8991
90- # keep areference to loadedItem because the path will be added when the
92+ # keep a reference to loadedItem because the path will be added when the
9193 # dialog is shown.
9294 # Model holds a title and a path
9395 # search in the builtin folder for the screen and
9496 # in the users's config directory
9597 def setPath (self ):
9698 model = self .styleSheetCombo .model ()
9799
98- # ad an 'As Loaded' entry to follow the preference file's entry
99- self .loadedItem = QtGui . QStandardItem ('As Loaded' )
100- self .loadedItem .setData ( 'As Loaded' , role = QtCore . Qt .UserRole + 1 )
101- self .loadedItem .setData ("Use the preference loaded Stylesheet" , role = QtCore . Qt .ToolTipRole )
100+ # add an 'As Loaded' entry to follow the preference file's entry
101+ self .loadedItem = QStandardItem ('As Loaded' )
102+ self .loadedItem .setData ( 'As Loaded' , role = Qt .UserRole + 1 )
103+ self .loadedItem .setData ("Use the preference loaded Stylesheet" , role = Qt .ToolTipRole )
102104 model .appendRow (self .loadedItem )
103105
104106 # add 'None' to cancel all stylesheet changes
105- item = QtGui . QStandardItem ('None' )
106- item .setData ( 'None' , role = QtCore . Qt .UserRole + 1 )
107- item .setData ("Use system default Stylesheet" , role = QtCore . Qt .ToolTipRole )
107+ item = QStandardItem ('None' )
108+ item .setData ( 'None' , role = Qt .UserRole + 1 )
109+ item .setData ("Use system default Stylesheet" , role = Qt .ToolTipRole )
108110 model .appendRow (item )
109111
110112 # call PATH function to get the found default and local qss files
111113 try :
112114 for group in (PATH .getQSSPaths (self .addBuiltinStyles )):
113115 for directory , name in (group ):
114- item = QtGui . QStandardItem (name )
115- item .setData (os .path .join (directory , name ), role = QtCore . Qt .UserRole + 1 )
116- item .setData (os .path .join (directory , name ), role = QtCore . Qt .ToolTipRole )
116+ item = QStandardItem (name )
117+ item .setData (os .path .join (directory , name ), role = Qt .UserRole + 1 )
118+ item .setData (os .path .join (directory , name ), role = Qt .ToolTipRole )
117119 model .appendRow (item )
118120 except Exception as e :
119121 print (e )
120122
121123 def selectionChanged (self ,i ):
122- path = self .styleSheetCombo .itemData (i ,role = QtCore .Qt .UserRole + 1 )
123- name = self .styleSheetCombo .itemData (i ,role = QtCore .Qt .DisplayRole )
124+ self .search_box .clear ()
125+ path = self .styleSheetCombo .itemData (i ,role = Qt .UserRole + 1 )
126+ name = self .styleSheetCombo .itemData (i ,role = Qt .DisplayRole )
124127 self .styleSheetCombo .setToolTip ('<b>{}</b>' .format (path ))
125128 self .loadStyleSheet (path )
126129
130+ def highlight_all_matches (self ):
131+ self .clear_highlights ()
132+
133+ query = self .search_box .text ()
134+ if not query :
135+ return
136+
137+ cursor = self .styleTextEdit .textCursor ()
138+ cursor .movePosition (QTextCursor .Start )
139+
140+ format = QTextCharFormat ()
141+ format .setBackground (QColor ('#808080' ))
142+
143+ self .matches = []
144+ while True :
145+ cursor = self .styleTextEdit .document ().find (query , cursor )
146+ if cursor .isNull ():
147+ break
148+ cursor .mergeCharFormat (format )
149+ self .matches .append (QTextCursor (cursor ))
150+
151+ self .current_match_index = - 1
152+ if self .matches :
153+ self .find_next ()
154+
155+ def clear_highlights (self ):
156+ cursor = self .styleTextEdit .textCursor ()
157+ cursor .select (QTextCursor .Document )
158+ cursor .setCharFormat (QTextCharFormat ())
159+
160+ def find_next (self ):
161+ if not self .matches :
162+ return
163+ self .current_match_index = (self .current_match_index + 1 ) % len (self .matches )
164+ self .select_match ()
165+
166+ def find_previous (self ):
167+ if not self .matches :
168+ return
169+ self .current_match_index = (self .current_match_index - 1 ) % len (self .matches )
170+ self .select_match ()
171+
172+ def select_match (self ):
173+ cursor = self .matches [self .current_match_index ]
174+ self .styleTextEdit .setTextCursor (cursor )
175+
127176 @pyqtSlot ()
128177 def on_styleTextView_textChanged (self ):
129178 self .applyButton .setEnabled (True )
@@ -132,10 +181,11 @@ def on_styleTextView_textChanged(self):
132181 def on_applyButton_clicked (self ):
133182 self .parent .setStyleSheet ("" )
134183 if self .tabWidget .currentIndex () == 0 :
184+ self .styleTextEdit .clear ()
135185 self .parent .setStyleSheet (self .styleTextView .toPlainText ())
136186 if WIDGETS .PREFS_ :
137187 index = self .styleSheetCombo .currentIndex ()
138- path = self .styleSheetCombo .itemData (index ,role = QtCore . Qt .UserRole + 1 )
188+ path = self .styleSheetCombo .itemData (index ,role = Qt .UserRole + 1 )
139189 WIDGETS .PREFS_ .putpref ('style_QSS_Path' , path , str , 'BOOK_KEEPING' )
140190 else :
141191 self .parent .setStyleSheet (self .styleTextEdit .toPlainText ())
@@ -144,7 +194,7 @@ def on_applyButton_clicked(self):
144194 # make sure one can still read the combo box
145195 self .styleSheetCombo .setFixedWidth (200 )
146196 try :
147- path = self .styleSheetCombo .itemData (index ,role = QtCore . Qt .UserRole + 1 )
197+ path = self .styleSheetCombo .itemData (index ,role = Qt .UserRole + 1 )
148198 self .parent .statusbar .showMessage (f"Stylesheet set to { path } " )
149199 except :
150200 pass
@@ -185,8 +235,8 @@ def on_openButton_clicked(self):
185235 styleSheet = file .readAll ()
186236 self .styleTextView .setPlainText (str (styleSheet , encoding = 'utf8' ))
187237 model = self .styleSheetCombo .model ()
188- item = QtGui . QStandardItem (os .path .basename (fileName ))
189- item .setData ( fileName , role = QtCore . Qt .UserRole + 1 )
238+ item = QStandardItem (os .path .basename (fileName ))
239+ item .setData ( fileName , role = Qt .UserRole + 1 )
190240 model .appendRow (item )
191241 self .styleSheetCombo .setCurrentIndex (self .styleSheetCombo .count ()- 1 )
192242
0 commit comments