RefactoringRope/HelpDialog.py

Sun, 23 Jan 2011 19:55:56 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 23 Jan 2011 19:55:56 +0100
changeset 1
9f687137a929
child 20
83b71483e198
permissions
-rw-r--r--

Started implementing the basic functions.

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

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

"""
Module implementing a dialog to show help about rope.
"""

from PyQt4.QtGui import QDialog, QDialogButtonBox, QTextDocument, QTextCursor

from Ui_HelpDialog import Ui_HelpDialog
from SearchDialog import SearchDialog

import Globals

class HelpDialog(QDialog, Ui_HelpDialog):
    """
    Class implementing a dialog to show help about rope.
    """
    def __init__(self, title, helpfile, parent = None):
        """
        Constructor
        
        @param title window title (string)
        @param helpfile filename of the helpfile (string)
        @param parent reference to the parent widget (QWidget)
        """
        QDialog.__init__(self, parent)
        self.setupUi(self)
        
        self.searchButton = self.buttonBox.addButton(
            self.trUtf8("Search..."), QDialogButtonBox.ActionRole)
        self.__searchDlg = None
        
        if Globals.isWindowsPlatform():
            self.helpEdit.setFontFamily("Lucida Console")
        else:
            self.helpEdit.setFontFamily("Monospace")
        
        try:
            f = open(helpfile, "r", encoding = "utf-8")
            txt = f.read()
            f.close()
            self.helpEdit.setPlainText(txt)
        except IOError as err:
            self.helpEdit.setPlainText(
                self.trUtf8("Could not read file {0}.\nReason: {1}")\
                    .format(helpfile, str(err)))

    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.searchButton:
            if self.__searchDlg is None:
                self.__searchDlg = SearchDialog(self)
            self.__searchDlg.showFind()
    
    def findNextPrev(self, txt, case, word, backwards):
        """
        Public slot to find the next occurrence of a text.
        
        @param txt text to search for (string)
        @param case flag indicating a case sensitive search (boolean)
        @param word flag indicating a word based search (boolean)
        @param backwards flag indicating a backwards search (boolean)
        """
        doc = self.helpEdit.document()
        cur = self.helpEdit.textCursor()
        
        findFlags = QTextDocument.FindFlags()
        if case:
            findFlags |= QTextDocument.FindCaseSensitively
        if word:
            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():
            return False
        else:
            self.helpEdit.setTextCursor(newCur)
            return True
    
    def helpWidget(self):
        """
        Public method to get a reference to the help widget.
        
        @return reference to the help widget (QTextEdit)
        """
        return self.helpEdit

eric ide

mercurial