|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2007 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the QMessageBox wizard plugin. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import QObject, SIGNAL |
|
11 from PyQt4.QtGui import QDialog, QMessageBox |
|
12 |
|
13 from E4Gui.E4Application import e4App |
|
14 from E4Gui.E4Action import E4Action |
|
15 |
|
16 from WizardPlugins.MessageBoxWizard.MessageBoxWizardDialog import \ |
|
17 MessageBoxWizardDialog |
|
18 |
|
19 # Start-Of-Header |
|
20 name = "QMessageBox Wizard Plugin" |
|
21 author = "Detlev Offenbach <detlev@die-offenbachs.de>" |
|
22 autoactivate = True |
|
23 deactivateable = True |
|
24 version = "5.0.0" |
|
25 className = "MessageBoxWizard" |
|
26 packageName = "__core__" |
|
27 shortDescription = "Show the QMessageBox wizard." |
|
28 longDescription = """This plugin shows the QMessageBox wizard.""" |
|
29 pyqtApi = 2 |
|
30 # End-Of-Header |
|
31 |
|
32 error = "" |
|
33 |
|
34 class MessageBoxWizard(QObject): |
|
35 """ |
|
36 Class implementing the QMessageBox wizard plugin. |
|
37 """ |
|
38 def __init__(self, ui): |
|
39 """ |
|
40 Constructor |
|
41 |
|
42 @param ui reference to the user interface object (UI.UserInterface) |
|
43 """ |
|
44 QObject.__init__(self, ui) |
|
45 self.__ui = ui |
|
46 |
|
47 def activate(self): |
|
48 """ |
|
49 Public method to activate this plugin. |
|
50 |
|
51 @return tuple of None and activation status (boolean) |
|
52 """ |
|
53 self.__initAction() |
|
54 self.__initMenu() |
|
55 |
|
56 return None, True |
|
57 |
|
58 def deactivate(self): |
|
59 """ |
|
60 Public method to deactivate this plugin. |
|
61 """ |
|
62 menu = self.__ui.getMenu("wizards") |
|
63 if menu: |
|
64 menu.removeAction(self.action) |
|
65 self.__ui.removeE4Actions([self.action], 'wizards') |
|
66 |
|
67 def __initAction(self): |
|
68 """ |
|
69 Private method to initialize the action. |
|
70 """ |
|
71 self.action = E4Action(self.trUtf8('QMessageBox Wizard'), |
|
72 self.trUtf8('Q&MessageBox Wizard...'), 0, 0, self, |
|
73 'wizards_qmessagebox') |
|
74 self.action.setStatusTip(self.trUtf8('QMessageBox Wizard')) |
|
75 self.action.setWhatsThis(self.trUtf8( |
|
76 """<b>QMessageBox Wizard</b>""" |
|
77 """<p>This wizard opens a dialog for entering all the parameters""" |
|
78 """ needed to create a QMessageBox. The generated code is inserted""" |
|
79 """ at the current cursor position.</p>""" |
|
80 )) |
|
81 self.connect(self.action, SIGNAL('triggered()'), self.__handle) |
|
82 |
|
83 self.__ui.addE4Actions([self.action], 'wizards') |
|
84 |
|
85 def __initMenu(self): |
|
86 """ |
|
87 Private method to add the actions to the right menu. |
|
88 """ |
|
89 menu = self.__ui.getMenu("wizards") |
|
90 if menu: |
|
91 menu.addAction(self.action) |
|
92 |
|
93 def __callForm(self, editor): |
|
94 """ |
|
95 Private method to display a dialog and get the code. |
|
96 |
|
97 @param editor reference to the current editor |
|
98 @return the generated code (string) |
|
99 """ |
|
100 dlg = MessageBoxWizardDialog(None) |
|
101 if dlg.exec_() == QDialog.Accepted: |
|
102 line, index = editor.getCursorPosition() |
|
103 indLevel = editor.indentation(line)/editor.indentationWidth() |
|
104 if editor.indentationsUseTabs(): |
|
105 indString = '\t' |
|
106 else: |
|
107 indString = editor.indentationWidth() * ' ' |
|
108 return (dlg.getCode(indLevel, indString), True) |
|
109 else: |
|
110 return (None, False) |
|
111 |
|
112 def __handle(self): |
|
113 """ |
|
114 Private method to handle the wizards action |
|
115 """ |
|
116 editor = e4App().getObject("ViewManager").activeWindow() |
|
117 |
|
118 if editor == None: |
|
119 QMessageBox.critical(None, |
|
120 self.trUtf8('No current editor'), |
|
121 self.trUtf8('Please open or create a file first.')) |
|
122 else: |
|
123 code, ok = self.__callForm(editor) |
|
124 if ok: |
|
125 line, index = editor.getCursorPosition() |
|
126 # It should be done on this way to allow undo |
|
127 editor.beginUndoAction() |
|
128 editor.insertAt(code, line, index) |
|
129 editor.endUndoAction() |