|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2011 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to show help about rope. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtGui import QDialog, QDialogButtonBox, QTextDocument, QTextCursor |
|
11 |
|
12 from Ui_HelpDialog import Ui_HelpDialog |
|
13 from SearchDialog import SearchDialog |
|
14 |
|
15 import Globals |
|
16 |
|
17 class HelpDialog(QDialog, Ui_HelpDialog): |
|
18 """ |
|
19 Class implementing a dialog to show help about rope. |
|
20 """ |
|
21 def __init__(self, title, helpfile, parent = None): |
|
22 """ |
|
23 Constructor |
|
24 |
|
25 @param title window title (string) |
|
26 @param helpfile filename of the helpfile (string) |
|
27 @param parent reference to the parent widget (QWidget) |
|
28 """ |
|
29 QDialog.__init__(self, parent) |
|
30 self.setupUi(self) |
|
31 |
|
32 self.searchButton = self.buttonBox.addButton( |
|
33 self.trUtf8("Search..."), QDialogButtonBox.ActionRole) |
|
34 self.__searchDlg = None |
|
35 |
|
36 if Globals.isWindowsPlatform(): |
|
37 self.helpEdit.setFontFamily("Lucida Console") |
|
38 else: |
|
39 self.helpEdit.setFontFamily("Monospace") |
|
40 |
|
41 try: |
|
42 f = open(helpfile, "r", encoding = "utf-8") |
|
43 txt = f.read() |
|
44 f.close() |
|
45 self.helpEdit.setPlainText(txt) |
|
46 except IOError as err: |
|
47 self.helpEdit.setPlainText( |
|
48 self.trUtf8("Could not read file {0}.\nReason: {1}")\ |
|
49 .format(helpfile, str(err))) |
|
50 |
|
51 def on_buttonBox_clicked(self, button): |
|
52 """ |
|
53 Private slot called by a button of the button box clicked. |
|
54 |
|
55 @param button button that was clicked (QAbstractButton) |
|
56 """ |
|
57 if button == self.searchButton: |
|
58 if self.__searchDlg is None: |
|
59 self.__searchDlg = SearchDialog(self) |
|
60 self.__searchDlg.showFind() |
|
61 |
|
62 def findNextPrev(self, txt, case, word, backwards): |
|
63 """ |
|
64 Public slot to find the next occurrence of a text. |
|
65 |
|
66 @param txt text to search for (string) |
|
67 @param case flag indicating a case sensitive search (boolean) |
|
68 @param word flag indicating a word based search (boolean) |
|
69 @param backwards flag indicating a backwards search (boolean) |
|
70 """ |
|
71 doc = self.helpEdit.document() |
|
72 cur = self.helpEdit.textCursor() |
|
73 |
|
74 findFlags = QTextDocument.FindFlags() |
|
75 if case: |
|
76 findFlags |= QTextDocument.FindCaseSensitively |
|
77 if word: |
|
78 findFlags |= QTextDocument.FindWholeWords |
|
79 if backwards: |
|
80 findFlags |= QTextDocument.FindBackward |
|
81 |
|
82 if cur.hasSelection(): |
|
83 cur.setPosition(backwards and cur.anchor() or cur.position(), |
|
84 QTextCursor.MoveAnchor) |
|
85 newCur = doc.find(txt, cur, findFlags) |
|
86 if newCur.isNull(): |
|
87 return False |
|
88 else: |
|
89 self.helpEdit.setTextCursor(newCur) |
|
90 return True |
|
91 |
|
92 def helpWidget(self): |
|
93 """ |
|
94 Public method to get a reference to the help widget. |
|
95 |
|
96 @return reference to the help widget (QTextEdit) |
|
97 """ |
|
98 return self.helpEdit |