Source

pyqtwidgets / qtwidgets / findwidget.py

# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from flowlayout import FlowLayout
from freedesktop import find_icon


try:
    i18n
except NameError:
    i18n = lambda x: x



class FindWidget(QWidget):


    def __init__(self, parent, textedit=None, forward=True):
        QWidget.__init__(self, parent)
        self.textedit = textedit or parent.view
        self.forward_direction = forward
        self.flow = FlowLayout(self)
        self.setLayout(self.flow)
        self.closeButton = QToolButton(self)
        self.closeButton.setAutoRaise(True)
        self.closeButton.setIcon(QIcon(find_icon(i18n("fileclose")) or QIcon()))
        self.connect(self.closeButton, SIGNAL("clicked()"), self.hide)
        self.flow.addWidget(self.closeButton)
        self.label = QLabel(self)
        self.label.setText(i18n("&Search:"))
        self.flow.addWidget(self.label)
        self.text = QLineEdit(self)
        self.text_pal = QPalette(self.text.palette())
        self.connect(self.text, 
                     SIGNAL("textChanged(const QString&)"), self.search_inc)
        self.connect(self.text, SIGNAL("returnPressed()"), self.search_next)
        self.label.setBuddy(self.text)
        self.flow.addWidget(self.text)
        self.forward = QToolButton(self)
        self.connect(self.forward, SIGNAL("clicked()"), self.search_forward)
        self.forward.setText(i18n("> Forward"))
        self.forward.setIcon(QIcon(find_icon(i18n("forward")) or QIcon()))
        self.flow.addWidget(self.forward)
        self.backward = QToolButton(self)
        self.backward.setText(i18n("<"))
        self.backward.setIcon(QIcon(find_icon(i18n("backard")) or QIcon()))
        self.connect(self.backward, SIGNAL("clicked()"), self.search_backward)
        self.flow.addWidget(self.backward)
        self.inc = QCheckBox(i18n("&Incremental"), self)
        self.inc.setChecked(True)
        self.flow.addWidget(self.inc)
        self.cs = QCheckBox(i18n("&Case sensitive"), self)
        self.flow.addWidget(self.cs)
        self.words = QCheckBox(i18n("Whole &words"), self)
        self.flow.addWidget(self.words)


    def search_inc(self, text):
        cursor = self.textedit.textCursor()
        if cursor.hasSelection():
            cursor.setPosition(cursor.selectionStart(), QTextCursor.MoveAnchor)
            self.textedit.setTextCursor(cursor)
        self.search_next()

    
    def search_forward(self):
        self.forward_direction = True
        self.search_next()


    def search_backward(self):
        self.forward_direction = False
        self.search_next()


    def search_next(self, *args):
        # XXX: look into qt/tools/assistant/tabbedbrowser.cpp:find
        options = QTextDocument.FindFlags()
        if self.cs.isChecked():
            options |= QTextDocument.FindCaseSensitively
        if self.words.isChecked():
            options |= QTextDocument.FindWholeWords
        if not self.forward_direction:
            options |= QTextDocument.FindBackward
        text = unicode(self.text.text())
        if not text or self.textedit.find(text, options):
            self.text.setPalette(self.text_pal)
        else:
            self.text.palette().setColor(
                self.text.backgroundRole(), QColor("red"))
            self.text.palette().setColor(
                self.text.foregroundRole(), QColor("white"))