<commit_before>#-------------------------------------------------------------------------------
#  Copyright (c) 20011, Enthought, Inc.
#  All rights reserved.
#
#  This software is provided without warranty under the terms of the BSD
#  license included in enthought/LICENSE.txt and may be redistributed only
#  under the conditions described in the aforementioned license.  The license
#  is also available online at http://www.enthought.com/licenses/BSD.txt
#
#  Thanks for using Enthought open source!
#
#  Author: <NAME>
#-------------------------------------------------------------------------------

# System library imports
from enthought.qt import QtCore, QtGui

# ETS imports
from editor import Editor


class SearchWidget(QtGui.QLineEdit):

    # FIXME: This widget needs a search button and a cancel button like the
    #        wxWidgets SearchControl.

    def __init__(self, desc):
        """ Store the descriptive text for the widget.
        """
        QtGui.QLineEdit.__init__(self)

        self._desc = unicode(desc)
        self._set_descriptive_text()

    def focusInEvent(self, event):
        """ Handles accepting focus.

        If the text box contains the default description string, reset the text
        color and clear the box.
        """
        palette = QtGui.QApplication.instance().palette()
        self.setPalette(palette)
        
        if self.text() == self._desc:
            self.setText('')
            self.update()

        super(SearchWidget, self).focusInEvent(event)

    def focusOutEvent(self, event):
        """ Handles losing focus.

        When focus is lost, if the user had typed something, keep that text,
        otherwise replace it with the default description string.
        """
        if len(self.text()) == 0:
            self._set_descriptive_text()

        super(SearchWidget, self).focusOutEvent(event)

    def _set_descriptive_text(self):
        """ Sets the greyed-out descriptive text.
        """
        palette = QtGui.QApplication.instance().palette()
        palette.setColor(QtGui.QPalette.Text,
                         palette.color(QtGui.QPalette.Dark))
        self.setPalette(palette)
        self.setText(self._desc)
        self.setModified(True)
        self.update()


class SearchEditor(Editor):

    def init(self, parent):
        """ Finishes initializing the editor by creating the underlying toolkit
            widget.
        """
        control = self.control = SearchWidget(self.factory.text)

        if self.factory.auto_set:
            control.textEdited.connect(self.update_object)
        if self.factory.enter_set:
            control.editingFinished.connect(self.update_object)

    def update_object(self, event=None):
        """ Handles the user entering input data in the edit control.
        """<commit_msg>Implement SearchEditor with builtin LineEdit when supoport available.<commit_after>#-------------------------------------------------------------------------------
#  Copyright (c) 20011, Enthought, Inc.
#  All rights reserved.
#
#  This software is provided without warranty under the terms of the BSD
#  license included in enthought/LICENSE.txt and may be redistributed only
#  under the conditions described in the aforementioned license.  The license
#  is also available online at http://www.enthought.com/licenses/BSD.txt
#
#  Thanks for using Enthought open source!
#
#  Author: <NAME>
#-------------------------------------------------------------------------------

# System library imports
from enthought.qt import QtCore, QtGui

# ETS imports
from editor import Editor


class SearchWidget(QtGui.QLineEdit):

    # FIXME: This widget needs a search button and a cancel button like the
    #        wxWidgets SearchControl.

    def __init__(self, desc):
        """ Store the descriptive text for the widget.
        """
        super(SearchWidget, self).__init__()
        self._desc = unicode(desc)
        self._set_descriptive_text()

    def focusInEvent(self, event):
        """ Handles accepting focus.

        If the text box contains the default description string, reset the text
        color and clear the box.
        """
        palette = QtGui.QApplication.instance().palette()
        self.setPalette(palette)
        
        if self.text() == self._desc:
            self.setText('')
            self.update()

        super(SearchWidget, self).focusInEvent(event)

    def focusOutEvent(self, event):
        """ Handles losing focus.

        When focus is lost, if the user had typed something, keep that text,
        otherwise replace it with the default description string.
        """
        if len(self.text()) == 0:
            self._set_descriptive_text()

        super(SearchWidget, self).focusOutEvent(event)

    def _set_descriptive_text(self):
        """ Sets the greyed-out descriptive text.
        """
        palette = QtGui.QApplication.instance().palette()
        palette.setColor(QtGui.QPalette.Text,
                         palette.color(QtGui.QPalette.Dark))
        self.setPalette(palette)
        self.setText(self._desc)
        self.update()


class SearchEditor(Editor):

    def init(self, parent):
        """ Finishes initializing the editor by creating the underlying toolkit
            widget.
        """
        if QtCore.QT_VERSION < 0x40700:
            control = self.control = SearchWidget(self.factory.text)
        else:
            control = self.control = QtGui.QLineEdit()
            control.setPlaceholderText(self.factory.text)

        if self.factory.auto_set:
            control.textEdited.connect(self.update_object)
        if self.factory.enter_set:
            control.editingFinished.connect(self.update_object)

    def update_object(self, event=None):
        """ Handles the user entering input data in the edit control.
        """
{'fraction_non_alphanumeric': 0.1383722914178468, 'fraction_numerical': 0.0030711482682136155, 'mean_word_length': 2.0836401893740137, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '659691', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1487, 'n_words': 449}