1# (C) Copyright 2005-2020 Enthought, Inc., Austin, TX
2# All rights reserved.
3#
4# This software is provided without warranty under the terms of the BSD
5# license included in LICENSE.txt and may be redistributed only under
6# the conditions described in the aforementioned license. The license
7# is also available online at http://www.enthought.com/licenses/BSD.txt
8#
9# Thanks for using Enthought open source!
10
11
12import weakref
13
14from pyface.qt import QtGui, QtCore
15
16
17class FindWidget(QtGui.QWidget):
18    def __init__(self, parent):
19        super(FindWidget, self).__init__(parent)
20        self.adv_code_widget = weakref.ref(parent)
21
22        # QFontMetrics.width() is deprecated and Qt docs suggest using
23        # horizontalAdvance() instead, but is only available since Qt 5.11
24        if QtCore.__version_info__ >= (5, 11):
25            self.button_size = self.fontMetrics().horizontalAdvance("Replace All") + 20
26        else:
27            self.button_size = self.fontMetrics().width("Replace All") + 20
28
29        form_layout = QtGui.QFormLayout()
30        form_layout.addRow("Fin&d", self._create_find_control())
31
32        layout = QtGui.QHBoxLayout()
33        layout.addLayout(form_layout)
34
35        close_button = QtGui.QPushButton("Close")
36        layout.addWidget(close_button, 1, QtCore.Qt.AlignRight)
37        close_button.clicked.connect(self.hide)
38
39        self.setLayout(layout)
40
41    def setFocus(self):
42        self.line_edit.setFocus()
43
44    def _create_find_control(self):
45        control = QtGui.QWidget(self)
46
47        self.line_edit = QtGui.QLineEdit()
48        self.next_button = QtGui.QPushButton("&Next")
49        self.next_button.setFixedWidth(self.button_size)
50        self.prev_button = QtGui.QPushButton("&Prev")
51        self.prev_button.setFixedWidth(self.button_size)
52        self.options_button = QtGui.QPushButton("&Options")
53        self.options_button.setFixedWidth(self.button_size)
54
55        options_menu = QtGui.QMenu(self)
56        self.case_action = QtGui.QAction("Match &case", options_menu)
57        self.case_action.setCheckable(True)
58        self.word_action = QtGui.QAction("Match words", options_menu)
59        self.word_action.setCheckable(True)
60        self.wrap_action = QtGui.QAction("Wrap search", options_menu)
61        self.wrap_action.setCheckable(True)
62        self.wrap_action.setChecked(True)
63        options_menu.addAction(self.case_action)
64        options_menu.addAction(self.word_action)
65        options_menu.addAction(self.wrap_action)
66        self.options_button.setMenu(options_menu)
67
68        layout = QtGui.QHBoxLayout()
69        layout.addWidget(self.line_edit)
70        layout.addWidget(self.next_button)
71        layout.addWidget(self.prev_button)
72        layout.addWidget(self.options_button)
73        layout.addStretch(2)
74        layout.setContentsMargins(0, 0, 0, 0)
75
76        control.setLayout(layout)
77        return control
78