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

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

"""
Module implementing the search and replace dialog.
"""

from qt import *

from SearchReplaceForm import SearchReplaceForm

class SearchReplaceDialog(SearchReplaceForm):
    """
    Class implementing the search and replace dialog.
    """
    def __init__(self,replace,vm,parent = None,name = None,modal = 0,fl = 0):
        """
        Constructor
        
        Arguments
        
            replace -- flag indicating a replace dialog is called
            
            vm -- reference to the viewmanager object
            
            parent -- parent widget of this dialog (QWidget)
            
            name -- name of this dialog (string or QString)
            
            moadl -- flag indicating a modal dialog
            
            fl -- window flags
        """
        SearchReplaceForm.__init__(self,parent,name,modal,fl)
        
        self.viewmanager = vm
        
        self.findHistory = vm.getSRHistory('search')
        if replace:
            self.replaceHistory = vm.getSRHistory('replace')
            self.setCaption(self.trUtf8("Find and Replace"))
            self.replace = 1
        else:
            self.replaceTextLabel.hide()
            self.replacetextCombo.hide()
            self.replaceButton.hide()
            self.replaceAllButton.hide()
            self.setCaption(self.trUtf8("Find"))
            self.replace = 0
            
        self.havefound = 0

    def enableFindButtons(self):
        """
        Private slot to enable/disable the find buttons.
        """
        if self.findtextCombo.currentText().isEmpty():
            self.findButton.setEnabled(0)
            self.findNextButton.setEnabled(0)
            self.replaceButton.setEnabled(0)
            self.replaceAllButton.setEnabled(0)
        else:
            self.findButton.setEnabled(1)
            self.findNextButton.setEnabled(1)
            if self.replace:
                self.replaceButton.setEnabled(0)
                self.replaceAllButton.setEnabled(1)

    def handleFind(self):
        """
        Private slot to find the entered text in the displayed editor.
        """
        txt = self.findtextCombo.currentText()
        
        # This moves any previous occurence of this statement to the head
        # of the list and updates the combobox
        self.findHistory.remove(txt)
        self.findHistory.prepend(txt)
        self.findtextCombo.clear()
        self.findtextCombo.insertStringList(self.findHistory)
        
        aw = self.viewmanager.activeWindow()
        ok = aw.findFirst(txt,
                self.regexpCheckBox.isChecked(),
                self.caseCheckBox.isChecked(),
                self.wordCheckBox.isChecked(),
                self.wrapCheckBox.isChecked(),
                not self.backwardsCheckBox.isChecked(),
                0, 0)
        
        if ok:
            self.replaceButton.setEnabled(1)
        else:
            QMessageBox.information(self, self.caption(),
                self.trUtf8("'%1' was not found.").arg(txt))

    def handleFindNext(self):
        """
        Public slot to find the next occurance of text.
        """
        aw = self.viewmanager.activeWindow()
        if not self.havefound:
            self.showFind(self.viewmanager.textForFind())
            return
        
        txt = self.findtextCombo.currentText()
        
        # This moves any previous occurence of this statement to the head
        # of the list and updates the combobox
        self.findHistory.remove(txt)
        self.findHistory.prepend(txt)
        self.findtextCombo.clear()
        self.findtextCombo.insertStringList(self.findHistory)
        
        ok = 1
        lineFrom, indexFrom, lineTo, indexTo = aw.getSelection()
        if self.backwardsCheckBox.isChecked():
            line = lineFrom
            index = indexFrom - 1
            if index < 0:
                line -= 1
                if line < 0:
                    if self.wrapCheckBox.isChecked():
                        line = aw.lines()-1
                    else:
                        ok = 0
                if ok:
                    index = aw.lineLength(line)
        else:
            line = lineTo
            index = indexTo
        if ok:
            ok = aw.findFirst(txt,
                self.regexpCheckBox.isChecked(),
                self.caseCheckBox.isChecked(),
                self.wordCheckBox.isChecked(),
                self.wrapCheckBox.isChecked(),
                not self.backwardsCheckBox.isChecked(),
                line, index)
        
        if ok:
            self.replaceButton.setEnabled(1)
        else:
            QMessageBox.information(self, self.caption(),
                self.trUtf8("'%1' was not found.").arg(txt))

    def showFind(self, text=''):
        """
        Public method to display this dialog in find mode.
        
        Arguments
        
            text -- text to be shown in the findtext edit
        """
        self.replace = 0
        
        self.findtextCombo.clear()
        self.findtextCombo.insertStringList(self.findHistory)
        self.findtextCombo.setCurrentText(text)
        
        self.caseCheckBox.setChecked(0)
        self.wordCheckBox.setChecked(0)
        self.backwardsCheckBox.setChecked(0)
        self.wrapCheckBox.setChecked(1)
        self.regexpCheckBox.setChecked(0)
        
        self.havefound = 1
        self.show()

    def handleReplace(self):
        """
        Public slot to replace one occurance of text.
        """
        ftxt = self.findtextCombo.currentText()
        rtxt = self.replacetextCombo.currentText()
        
        # This moves any previous occurence of this statement to the head
        # of the list and updates the combobox
        self.replaceHistory.remove(rtxt)
        self.replaceHistory.prepend(rtxt)
        self.replacetextCombo.clear()
        self.replacetextCombo.insertStringList(self.replaceHistory)
        
        aw = self.viewmanager.activeWindow()
        aw.replace(rtxt)
        ok = aw.findNext()
        
        if not ok:
            QMessageBox.information(self, self.caption(),
                self.trUtf8("'%1' was not found.").arg(ftxt))
        
    def handleReplaceAll(self):
        """
        Public slot to replace all occurances of text.
        """
        ftxt = self.findtextCombo.currentText()
        rtxt = self.replacetextCombo.currentText()
        
        # This moves any previous occurence of this statement to the head
        # of the list and updates the combobox
        self.findHistory.remove(ftxt)
        self.findHistory.prepend(ftxt)
        self.findtextCombo.clear()
        self.findtextCombo.insertStringList(self.findHistory)
        
        self.replaceHistory.remove(rtxt)
        self.replaceHistory.prepend(rtxt)
        self.replacetextCombo.clear()
        self.replacetextCombo.insertStringList(self.replaceHistory)
        
        aw = self.viewmanager.activeWindow()
        if self.wrapCheckBox.isChecked():
            ok = aw.findFirst(ftxt,
                    self.regexpCheckBox.isChecked(),
                    self.caseCheckBox.isChecked(),
                    self.wordCheckBox.isChecked(),
                    0, 1, 0, 0)
        else:
            ok = aw.findFirst(ftxt,
                    self.regexpCheckBox.isChecked(),
                    self.caseCheckBox.isChecked(),
                    self.wordCheckBox.isChecked(),
                    0,
                    not self.backwardsCheckBox.isChecked())
        found = ok
        
        while ok:
            aw.replace(rtxt)
            ok = aw.findNext()
        
        if not found:
            QMessageBox.information(self, self.caption(),
                self.trUtf8("Nothing replaced because '%1' was not found.")
                    .arg(ftxt))
        
    def showReplace(self, text=''):
        """
        Public slot to display this dialog in replace mode.
        
        Arguments
        
            text -- text to be shown in the findtext edit
        """
        self.replace = 1
        
        self.findtextCombo.clear()
        self.findtextCombo.insertStringList(self.findHistory)
        self.findtextCombo.setCurrentText(text)
        
        #self.replaceTextLabel.show()
        #self.replacetextCombo.show()
        
        self.replacetextCombo.clear()
        self.replacetextCombo.insertStringList(self.replaceHistory)
        self.replacetextCombo.setCurrentText('')
        
        self.caseCheckBox.setChecked(0)
        self.wordCheckBox.setChecked(0)
        self.backwardsCheckBox.setChecked(0)
        self.wrapCheckBox.setChecked(1)
        self.regexpCheckBox.setChecked(0)
        
        #self.replaceButton.show()
        #self.replaceAllButton.show()
        
        #self.setCaption(self.trUtf8("Find and Replace"))
        self.havefound = 1
        self.show()
