Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix searching text in our Web widgets #3266

Merged
merged 4 commits into from
Jun 28, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spyderlib/plugins/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def __init__(self, parent):

self.webview = FrameWebView(self)
self.find_widget = FindReplace(self)
self.find_widget.set_editor(self.webview)
self.find_widget.set_editor(self.webview._webview)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a private variable is not good practice. Maybe just add a property to self.webview to access _webview ? This way no other code has to change.

Copy link
Member Author

@ccordoba12 ccordoba12 Jun 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a change made by @goanpeca, so the real webview widget is saved in a private variable.

I'll write a new method to access it :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I remember now!, but @Nodd is ok, just add a property :-p

self.find_widget.hide()

layout = QVBoxLayout()
Expand Down
35 changes: 17 additions & 18 deletions spyderlib/widgets/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,24 @@ def find_text(self, text, changed=True,
forward=True, case=False, words=False,
regexp=False):
"""Find text"""
findflag = QWebEnginePage.FindWrapsAroundDocument
if not WEBENGINE:
findflag = QWebEnginePage.FindWrapsAroundDocument
else:
findflag = None

if not forward:
findflag = findflag | QWebEnginePage.FindBackward
if findflag:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting findflag to 0 instead of None would avoid this if switch since the bitwise or would work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

findflag = findflag | QWebEnginePage.FindBackward
else:
findflag = QWebEnginePage.FindBackward
if case:
findflag = findflag | QWebEnginePage.FindCaseSensitively
return self.findText(text, findflag)


if findflag is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If findflags is 0 you don't need the switch here.

return self.findText(text, findflag)
else:
return self.findText(text)

def get_selected_text(self):
"""Return text selected by current text cursor"""
return self.selectedText()
Expand Down Expand Up @@ -290,20 +301,8 @@ def __init__(self, parent):
else:
self._webview.linkClicked.connect(self.linkClicked)

def set_font(self, font, fixed_font=None):
self._webview.set_font(font, fixed_font=fixed_font)

def setHtml(self, html_text, base_url):
self._webview.setHtml(html_text, base_url)

def url(self):
return self._webview.url()

def load(self, url):
self._webview.load(url)

def page(self):
return self._webview.page()
def __getattr__(self, name):
return getattr(self._webview, name)


def test():
Expand Down
21 changes: 15 additions & 6 deletions spyderlib/widgets/findreplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ def is_position_inf(pos1, pos2):
class FindReplace(QWidget):
"""Find widget"""
STYLE = {False: "background-color:rgb(255, 175, 90);",
True: ""}
True: "",
None: ""}
visibility_changed = Signal(bool)

def __init__(self, parent, enable_replace=False):
QWidget.__init__(self, parent)
self.enable_replace = enable_replace
Expand Down Expand Up @@ -202,11 +203,16 @@ def show(self):
if self.editor is not None:
text = self.editor.get_selected_text()

# If no text is highlighted for search, use whatever word is under the cursor
# If no text is highlighted for search, use whatever word is under
# the cursor
if not text:
cursor = self.editor.textCursor()
cursor.select(QTextCursor.WordUnderCursor)
text = to_text_string(cursor.selectedText())
try:
cursor = self.editor.textCursor()
cursor.select(QTextCursor.WordUnderCursor)
text = to_text_string(cursor.selectedText())
except AttributeError:
# We can't do this for all widgets, e.g. WebView's
pass

# Now that text value is sorted out, use it for the search
if text:
Expand Down Expand Up @@ -314,6 +320,9 @@ def find(self, changed=True, forward=True,
text = self.search_text.currentText()
if len(text) == 0:
self.search_text.lineEdit().setStyleSheet("")
if not self.is_code_editor:
# Clears the selection for WebEngine
self.editor.find_text('')
return None
else:
case = self.case_button.isChecked()
Expand Down