Skip to content

Commit 628c8c0

Browse files
committed
qtvcp -stylesheet editor: add search capabilities
Thank you Jim! (Persei802)
1 parent b58e63f commit 628c8c0

2 files changed

Lines changed: 176 additions & 35 deletions

File tree

lib/python/qtvcp/widgets/stylesheeteditor.py

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@
4141

4242
import os
4343
from 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

4948
from qtvcp.core import Path
5049
from 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

share/qtvcp/widgets_ui/style_dialog.ui

Lines changed: 104 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>568</width>
10-
<height>374</height>
9+
<width>916</width>
10+
<height>334</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -55,7 +55,7 @@
5555
<property name="minimumSize">
5656
<size>
5757
<width>120</width>
58-
<height>24</height>
58+
<height>30</height>
5959
</size>
6060
</property>
6161
<property name="text">
@@ -87,7 +87,7 @@
8787
<property name="minimumSize">
8888
<size>
8989
<width>80</width>
90-
<height>24</height>
90+
<height>30</height>
9191
</size>
9292
</property>
9393
<property name="text">
@@ -116,7 +116,7 @@
116116
<property name="minimumSize">
117117
<size>
118118
<width>120</width>
119-
<height>24</height>
119+
<height>30</height>
120120
</size>
121121
</property>
122122
</widget>
@@ -141,6 +141,12 @@
141141
</item>
142142
<item>
143143
<widget class="QLineEdit" name="lineEdit_path">
144+
<property name="minimumSize">
145+
<size>
146+
<width>0</width>
147+
<height>30</height>
148+
</size>
149+
</property>
144150
<property name="readOnly">
145151
<bool>true</bool>
146152
</property>
@@ -186,13 +192,13 @@
186192
<property name="minimumSize">
187193
<size>
188194
<width>80</width>
189-
<height>24</height>
195+
<height>30</height>
190196
</size>
191197
</property>
192198
<property name="maximumSize">
193199
<size>
194200
<width>16777215</width>
195-
<height>24</height>
201+
<height>30</height>
196202
</size>
197203
</property>
198204
<property name="text">
@@ -224,13 +230,13 @@
224230
<property name="minimumSize">
225231
<size>
226232
<width>100</width>
227-
<height>24</height>
233+
<height>30</height>
228234
</size>
229235
</property>
230236
<property name="maximumSize">
231237
<size>
232238
<width>16777215</width>
233-
<height>24</height>
239+
<height>30</height>
234240
</size>
235241
</property>
236242
<property name="text">
@@ -251,6 +257,91 @@
251257
</property>
252258
</spacer>
253259
</item>
260+
<item>
261+
<widget class="QLineEdit" name="search_box">
262+
<property name="minimumSize">
263+
<size>
264+
<width>0</width>
265+
<height>30</height>
266+
</size>
267+
</property>
268+
<property name="maximumSize">
269+
<size>
270+
<width>16777215</width>
271+
<height>30</height>
272+
</size>
273+
</property>
274+
<property name="placeholderText">
275+
<string>Search...</string>
276+
</property>
277+
<property name="clearButtonEnabled">
278+
<bool>true</bool>
279+
</property>
280+
</widget>
281+
</item>
282+
<item>
283+
<widget class="QPushButton" name="btn_prev">
284+
<property name="sizePolicy">
285+
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
286+
<horstretch>0</horstretch>
287+
<verstretch>0</verstretch>
288+
</sizepolicy>
289+
</property>
290+
<property name="minimumSize">
291+
<size>
292+
<width>80</width>
293+
<height>30</height>
294+
</size>
295+
</property>
296+
<property name="maximumSize">
297+
<size>
298+
<width>80</width>
299+
<height>30</height>
300+
</size>
301+
</property>
302+
<property name="text">
303+
<string>Previous</string>
304+
</property>
305+
</widget>
306+
</item>
307+
<item>
308+
<widget class="QPushButton" name="btn_next">
309+
<property name="sizePolicy">
310+
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
311+
<horstretch>0</horstretch>
312+
<verstretch>0</verstretch>
313+
</sizepolicy>
314+
</property>
315+
<property name="minimumSize">
316+
<size>
317+
<width>80</width>
318+
<height>30</height>
319+
</size>
320+
</property>
321+
<property name="maximumSize">
322+
<size>
323+
<width>80</width>
324+
<height>30</height>
325+
</size>
326+
</property>
327+
<property name="text">
328+
<string>Next</string>
329+
</property>
330+
</widget>
331+
</item>
332+
<item>
333+
<spacer name="horizontalSpacer_6">
334+
<property name="orientation">
335+
<enum>Qt::Horizontal</enum>
336+
</property>
337+
<property name="sizeHint" stdset="0">
338+
<size>
339+
<width>40</width>
340+
<height>20</height>
341+
</size>
342+
</property>
343+
</spacer>
344+
</item>
254345
<item>
255346
<widget class="QPushButton" name="saveButton">
256347
<property name="sizePolicy">
@@ -262,13 +353,13 @@
262353
<property name="minimumSize">
263354
<size>
264355
<width>80</width>
265-
<height>24</height>
356+
<height>30</height>
266357
</size>
267358
</property>
268359
<property name="maximumSize">
269360
<size>
270361
<width>16777215</width>
271-
<height>24</height>
362+
<height>30</height>
272363
</size>
273364
</property>
274365
<property name="text">
@@ -310,7 +401,7 @@
310401
<property name="minimumSize">
311402
<size>
312403
<width>60</width>
313-
<height>24</height>
404+
<height>30</height>
314405
</size>
315406
</property>
316407
<property name="text">
@@ -355,7 +446,7 @@
355446
<property name="minimumSize">
356447
<size>
357448
<width>60</width>
358-
<height>24</height>
449+
<height>30</height>
359450
</size>
360451
</property>
361452
<property name="text">

0 commit comments

Comments
 (0)