-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_replace_widget.py
287 lines (232 loc) · 10.7 KB
/
find_replace_widget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import enum
import os
import sys
from typing import Any, Dict, List, Optional, Tuple, Union
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class SearchFlags(enum.IntFlag):
"""
Find and replace flags
Members
- NoFlags: search has no special flags
- CaseSensitive: case sensitive search
- WholeWord: match whole word only
- Regex: treat search expression as a regular expression and return all occurrences that fulfill it
- AllOccurrences: apply to all occurrences that match search expression
"""
NoFlags = 0
CaseSensitive = enum.auto()
WholeWord = enum.auto()
Regex = enum.auto()
AllOccurrences = enum.auto()
class FindAndReplaceWidget(QWidget):
"""
Non modal find and replace widget for QTextEdit
pyqtSignals:
- SearchQuery(str, SearchFlags): emitted when find or find all buttons are clicked
- ReplaceQuery(str, str, SearchFlags): emitted when replace or replace all buttons are clicked
"""
SearchQuery: pyqtSignal = pyqtSignal(str, SearchFlags)
ReplaceQuery: pyqtSignal = pyqtSignal(str, str, SearchFlags)
class StyleProxy(QProxyStyle):
def sizeFromContents(self, type: QStyle.ContentsType, option: QStyleOption, size: QSize,
widget: QWidget) -> QSize:
"""
Get widget size from its contents, overridden to increase push button width by 20
:param type: content type
:type type: QStyle.ContentsType
:param option: style option
:type option: QStyleOption
:param size: contents size
:type size: QSize
:param widget: widget
:type widget: QWidget
:return: widget size from its contents
:rtype: QSize
"""
widget_size: QSize = super(FindAndReplaceWidget.StyleProxy, self).sizeFromContents(
type,
option,
size,
widget
)
if type == QStyle.ContentsType.CT_PushButton:
widget_size.setWidth(widget_size.width() + 20)
return widget_size
def __init__(self, editor: QWidget, *args: Tuple[Any], **kwargs: Dict[str, Any]) -> None:
"""
Initialize a find and replace widget
:param editor: text editor instance
:type editor: QTextEdit
:param args: arguments list
:type args: Tuple[Any]
:param kwargs: keyword arguments
:type kwargs: Dict[Any, Any]
"""
super(FindAndReplaceWidget, self).__init__(*args, **kwargs)
# container
self.container: QWidget = editor
# set minimum height
self.setMinimumHeight(120)
# set font size
font = self.font()
font.setPointSize(12)
self.setFont(font)
# layout
self.widget_layout: QGridLayout = QGridLayout(self)
self.setLayout(self.widget_layout)
# ----------------------------------------------------------------------
# -------------------------------- Find --------------------------------
# ----------------------------------------------------------------------
# find label
self.find_label: QLabel = QLabel()
self.find_label.setText("Find")
# find text field (line edit)
self.search_text: QLineEdit = QLineEdit(self)
size_policy: QSizePolicy = self.search_text.sizePolicy()
size_policy.setHorizontalPolicy(QSizePolicy.Expanding)
self.search_text.setSizePolicy(size_policy)
self.search_text.setToolTip("Find What?")
# find next button
self.find_next_button: QPushButton = QPushButton()
self.find_next_button.setText("Find Next")
self.find_next_button.setToolTip("Find Next")
self.find_next_button.setStyle(FindAndReplaceWidget.StyleProxy())
# find prev button
self.mark_all_button: QPushButton = QPushButton()
self.mark_all_button.setText("Mark All")
self.mark_all_button.setToolTip("MarK All")
self.mark_all_button.setStyle(FindAndReplaceWidget.StyleProxy())
# close/hide button
self.close_button: QPushButton = QPushButton()
self.close_button.setIcon(QIcon(os.path.join("images", "close.png")))
self.close_button.setFlat(True)
self.close_button.setFlat(True)
self.close_button.setToolTip("Close")
self.close_button.clicked.connect(self.close)
# case sensitive button
self.case_sensitive_button: QPushButton = QPushButton()
self.case_sensitive_button.setIcon(QIcon(os.path.join("images", "match-case.png")))
self.case_sensitive_button.setFlat(True)
self.case_sensitive_button.setCheckable(True)
self.case_sensitive_button.setToolTip("Match Case")
self.case_sensitive_button.setIconSize(QSize(20, 20))
# whole word button
self.whole_word_button: QPushButton = QPushButton()
self.whole_word_button.setIcon(QIcon(os.path.join("images", "whole-word.png")))
self.whole_word_button.setFlat(True)
self.whole_word_button.setCheckable(True)
self.whole_word_button.setToolTip("Match Whole Word")
self.whole_word_button.setIconSize(QSize(20, 20))
# regex button
self.regex_button: QPushButton = QPushButton()
self.regex_button.setIcon(QIcon(os.path.join("images", "regex.png")))
self.regex_button.setFlat(True)
self.regex_button.setCheckable(True)
self.regex_button.setToolTip("Regular Expression")
self.regex_button.setIconSize(QSize(20, 20))
# ----------------------------------------------------------------------
# ------------------------------ Replace -------------------------------
# ----------------------------------------------------------------------
# replace label
self.replace_label: QLabel = QLabel()
self.replace_label.setText("Replace")
# replace text field (line edit)
self.replace_text: QLineEdit = QLineEdit()
size_policy: QSizePolicy = self.replace_text.sizePolicy()
size_policy.setHorizontalPolicy(QSizePolicy.Expanding)
self.replace_text.setSizePolicy(size_policy)
self.replace_text.setToolTip("Replace With What?")
# replace next button
self.replace_next_button: QPushButton = QPushButton()
self.replace_next_button.setText("Replace")
self.replace_next_button.setToolTip("Replace Next")
self.replace_next_button.setStyle(FindAndReplaceWidget.StyleProxy())
# replace all button
self.replace_all_button: QPushButton = QPushButton()
self.replace_all_button.setText("Replace All")
self.replace_all_button.setToolTip("Replace All")
self.replace_all_button.setStyle(FindAndReplaceWidget.StyleProxy())
# ----------------------------------------------------------------------
# ---------------------------- Place items -----------------------------
# ----------------------------------------------------------------------
# add find buttons & labels
self.widget_layout.addWidget(self.case_sensitive_button, 0, 0)
self.widget_layout.addWidget(self.whole_word_button, 0, 1)
self.widget_layout.addWidget(self.regex_button, 0, 2)
self.widget_layout.addWidget(self.find_label, 0, 3)
self.widget_layout.addWidget(self.search_text, 0, 4)
self.widget_layout.addWidget(self.find_next_button, 0, 5)
self.widget_layout.addWidget(self.mark_all_button, 0, 6)
self.widget_layout.addWidget(self.close_button, 0, 76)
# add replace buttons & labels
self.widget_layout.addWidget(self.replace_label, 1, 3)
self.widget_layout.addWidget(self.replace_text, 1, 4)
self.widget_layout.addWidget(self.replace_next_button, 1, 5)
self.widget_layout.addWidget(self.replace_all_button, 1, 6)
# ----------------------------------------------------------------------
# -------------------------- Connect Signals ---------------------------
# ----------------------------------------------------------------------
# find buttons
self.find_next_button.clicked.connect(self.find_next_clicked)
self.mark_all_button.clicked.connect(self.mark_all_button_clicked)
# replace buttons
self.replace_next_button.clicked.connect(self.replace_next_button_clicked)
self.replace_all_button.clicked.connect(self.replace_all_button_clicked)
def get_search_flags(self) -> SearchFlags:
"""
Get current search criteria
:return: search flags
:rtype: SearchFlags
"""
search_flags: SearchFlags = SearchFlags.NoFlags
if self.case_sensitive_button.isChecked():
search_flags |= SearchFlags.CaseSensitive
if self.whole_word_button.isChecked():
search_flags |= SearchFlags.WholeWord
if self.regex_button.isChecked():
search_flags |= SearchFlags.Regex
return search_flags
@pyqtSlot(bool)
def find_next_clicked(self, checked: bool) -> None:
search_criteria: SearchFlags = self.get_search_flags()
search_expr: str = self.search_text.text()
if not search_expr:
return
self.SearchQuery.emit(search_expr, search_criteria)
@pyqtSlot(bool)
def mark_all_button_clicked(self, checked: bool) -> None:
search_criteria: SearchFlags = self.get_search_flags()
search_criteria |= SearchFlags.AllOccurrences
search_expr: str = self.search_text.text()
if not search_expr:
return
self.SearchQuery.emit(search_expr, search_criteria)
@pyqtSlot(bool)
def replace_next_button_clicked(self, checked: bool) -> None:
search_criteria: SearchFlags = self.get_search_flags()
search_expr: str = self.search_text.text()
if not search_expr:
return
replace_expr: str = self.replace_text.text()
self.ReplaceQuery.emit(search_expr, replace_expr, search_criteria)
@pyqtSlot(bool)
def replace_all_button_clicked(self, checked: bool) -> None:
search_criteria: SearchFlags = self.get_search_flags()
search_criteria |= SearchFlags.AllOccurrences
search_expr: str = self.search_text.text()
if not search_expr:
return
replace_expr: str = self.replace_text.text()
self.ReplaceQuery.emit(search_expr, replace_expr, search_criteria)
if __name__ == "__main__":
from mindustry_editor import MindustryLogicEditor
app = QApplication(sys.argv)
main = QMainWindow()
edit = MindustryLogicEditor(main)
main.setCentralWidget(edit)
main.show()
sys.exit(app.exec_())