# -*- coding: utf-8 -*-

# Copyright (c) 2002 - 2007 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the search dialog for the helpviewer.
"""

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from KdeQt import KQMessageBox

from Ui_SearchDialog import Ui_SearchDialog

class SearchDialog(QDialog, Ui_SearchDialog):
    """
    Module implementing the search dialog for the helpviewer.
    """
    def __init__(self, parent = None, name = QString(),
                 fl = Qt.WindowFlags(Qt.Widget)):
        """
        Constructor
        
        @param parent parent widget of this dialog (QWidget)
        @param name object name of the dialog (QString)
        @param fl window flags
        """
        QDialog.__init__(self, parent, fl)
        self.setupUi(self)
        self.setObjectName(name)
        
        self.findButton = self.buttonBox.addButton(\
            self.trUtf8("Find"), QDialogButtonBox.ActionRole)
        self.findButton.setEnabled(False)
        self.findButton.setDefault(True)
        
        self.findHistory = QStringList()
        self.havefound = False

    def on_findtextCombo_editTextChanged(self, txt):
        """
        Private slot to enable/disable the find buttons.
        """
        self.findButton.setEnabled(not txt.isEmpty())

    def on_buttonBox_clicked(self, button):
        """
        Private slot called by a button of the button box clicked.
        
        @param button button that was clicked (QAbstractButton)
        """
        if button == self.findButton:
            self.on_findButton_clicked()
        
    @pyqtSignature("")
    def on_findButton_clicked(self):
        """
        Private slot to find the entered text in the displayed page.
        """
        txt = self.findtextCombo.currentText()
        
        # This moves any previous occurence of this statement to the head
        # of the list and updates the combobox
        self.findHistory.removeAll(txt)
        self.findHistory.prepend(txt)
        self.findtextCombo.clear()
        self.findtextCombo.addItems(self.findHistory)
        
        self.__findNextPrev(self.backwardsButton.isChecked())

    def __findNextPrev(self, backwards):
        """
        Private slot to find the next occurance of text.
        
        @param backwards flag indicating a backwards search (boolean)
        """
        if not self.havefound or self.findtextCombo.currentText().isEmpty():
            self.showFind()
            return
        
        doc = self.parent().currentBrowser().document()
        cur = self.parent().currentBrowser().textCursor()
        txt = self.findtextCombo.currentText()
        
        findFlags = QTextDocument.FindFlags()
        if self.caseCheckBox.isChecked():
            findFlags |= QTextDocument.FindCaseSensitively
        if self.wordCheckBox.isChecked():
            findFlags |= QTextDocument.FindWholeWords
        if backwards:
            findFlags |= QTextDocument.FindBackward
        
        if cur.hasSelection():
            cur.setPosition(backwards and cur.anchor() or cur.position(), 
                            QTextCursor.MoveAnchor);
        newCur = doc.find(txt, cur, findFlags)
        if newCur.isNull():
            KQMessageBox.information(self, self.windowTitle(),
                self.trUtf8("'%1' was not found.").arg(txt))
        else:
            self.parent().currentBrowser().setTextCursor(newCur)

    def findNext(self):
        """
        Public slot to find the next occurance.
        """
        self.__findNextPrev(False)

    def findPrevious(self):
        """
        Public slot to find the previous occurance.
        """
        self.__findNextPrev(True)

    def showFind(self):
        """
        Public method to display this dialog.
        """
        self.havefound = True
        
        self.findtextCombo.clear()
        self.findtextCombo.addItems(self.findHistory)
        self.findtextCombo.setEditText('')
        
        self.caseCheckBox.setChecked(False)
        self.wordCheckBox.setChecked(False)
        self.forwardButton.setChecked(True)
        
        self.show()

