12
12
# pylint: disable=R0201
14
14
from spyderlib.qt.QtGui import (QHBoxLayout, QGridLayout, QCheckBox, QLabel,
15
QWidget, QSizePolicy, QShortcut, QKeySequence)
15
QWidget, QSizePolicy, QShortcut, QKeySequence,
16
17
from spyderlib.qt.QtCore import SIGNAL, Qt, QTimer
19
from spyderlib.utils.qthelpers import get_std_icon, create_toolbutton
22
from spyderlib.utils.qthelpers import (get_icon, get_std_icon,
20
24
from spyderlib.widgets.comboboxes import PatternComboBox
21
25
from spyderlib.baseconfig import _
22
from spyderlib.config import get_icon
28
def is_position_sup(pos1, pos2):
29
"""Return True is pos1 > pos2"""
32
def is_position_inf(pos1, pos2):
33
"""Return True is pos1 < pos2"""
25
37
class FindReplace(QWidget):
49
61
self.search_text = PatternComboBox(self, tip=_("Search string"),
50
62
adjust_to_minimum=False)
63
self.connect(self.search_text, SIGNAL('valid(bool)'),
65
self.find(changed=False, forward=True, rehighlight=False))
51
66
self.connect(self.search_text.lineEdit(),
52
67
SIGNAL("textEdited(QString)"), self.text_has_been_edited)
264
279
state = self.find(changed=False, forward=False, rehighlight=False)
265
280
self.editor.setFocus()
268
283
def text_has_been_edited(self, text):
269
284
"""Find text has been edited (this slot won't be triggered when
270
285
setting the search pattern combo box text programmatically"""
298
313
found = self.editor.find_text(text, changed, forward, case=case,
299
314
words=words, regexp=regexp)
300
315
self.search_text.lineEdit().setStyleSheet(self.STYLE[found])
316
if self.is_code_editor and found:
302
317
if rehighlight or not self.editor.found_results:
303
318
self.highlight_timer.stop()
304
319
if start_highlight_timer:
312
327
def replace_find(self):
313
328
"""Replace and find"""
314
329
if (self.editor is not None):
315
replace_text = self.replace_text.currentText()
316
search_text = self.search_text.currentText()
330
replace_text = unicode(self.replace_text.currentText())
331
search_text = unicode(self.search_text.currentText())
317
332
pattern = search_text if self.re_button.isChecked() else None
333
case = self.case_button.isChecked()
322
if self.editor.has_selected_text() and \
323
self.editor.get_selected_text() == unicode(search_text):
339
seltxt = unicode(self.editor.get_selected_text())
340
cmptxt1 = search_text if case else search_text.lower()
341
cmptxt2 = seltxt if case else seltxt.lower()
342
if self.editor.has_selected_text() and cmptxt1 == cmptxt2:
324
343
# Text was already found, do nothing
332
351
position = self.editor.get_position('cursor')
333
352
position0 = position
353
cursor = self.editor.textCursor()
354
cursor.beginEditBlock()
335
356
position1 = self.editor.get_position('cursor')
337
358
if position1 == position or \
338
self.editor.is_position_sup(position1, position):
359
is_position_sup(position1, position):
339
360
# Avoid infinite loop: replace string includes
340
361
# part of the search string
342
363
if position1 == position0:
343
364
# Avoid infinite loop: single found occurence
345
if self.editor.is_position_inf(position1, position0):
366
if is_position_inf(position1, position0):
347
368
position0 = position1
348
self.editor.replace(replace_text, pattern=pattern)
349
if not self.find_next():
370
cursor.removeSelectedText()
371
cursor.insertText(replace_text)
373
seltxt = unicode(cursor.selectedText())
374
cursor.removeSelectedText()
375
cursor.insertText(re.sub(pattern, replace_text, seltxt))
377
found_cursor = self.editor.textCursor()
378
cursor.setPosition(found_cursor.selectionStart(),
379
QTextCursor.MoveAnchor)
380
cursor.setPosition(found_cursor.selectionEnd(),
381
QTextCursor.KeepAnchor)
351
384
if not self.all_check.isChecked():
353
386
self.all_check.setCheckState(Qt.Unchecked)
387
if cursor is not None:
388
cursor.endEditBlock()