Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

#! /usr/bin/env python3 

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

 

#       Copyright 2011-2012, Milan Boers 

#       Copyright 2013, Marten de Vries 

# 

#       This file is part of OpenTeacher. 

# 

#       OpenTeacher is free software: you can redistribute it and/or modify 

#       it under the terms of the GNU General Public License as published by 

#       the Free Software Foundation, either version 3 of the License, or 

#       (at your option) any later version. 

# 

#       OpenTeacher is distributed in the hope that it will be useful, 

#       but WITHOUT ANY WARRANTY; without even the implied warranty of 

#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

#       GNU General Public License for more details. 

# 

#       You should have received a copy of the GNU General Public License 

#       along with OpenTeacher.  If not, see <http://www.gnu.org/licenses/>. 

 

def getDialog(): 

        class Dialog(QtWidgets.QWidget): 

                buttonClicked = QtCore.pyqtSignal() 

                def __init__(self, imagePath, text, redText=False, *args, **kwargs): 

                        super().__init__(*args, **kwargs) 

 

                        frame = QtWidgets.QFrame() 

                        frame.setFrameStyle(QtWidgets.QFrame.Panel | QtWidgets.QFrame.Raised) 

                        frame.setMidLineWidth(2) 

                        frame.setLineWidth(3) 

 

                        frameLayout = QtWidgets.QHBoxLayout() 

                        frameLayout.setAlignment(QtCore.Qt.AlignHCenter) 

 

                        imageLabel = QtWidgets.QLabel("<img src=\"" + imagePath + "\" />") 

                        imageLabel.setAlignment(QtCore.Qt.AlignHCenter) 

                        frameLayout.addWidget(imageLabel) 

 

                        textLabel = QtWidgets.QLabel(text) 

                        textLabel.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter) 

                        textLabel.setWordWrap(True) 

                        textLabel.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred) 

                        if redText: 

                                textLabel.setStyleSheet("color: #b60000; font-weight: bold;") 

                        else: 

                                textLabel.setStyleSheet("font-weight: bold;") 

                        frameLayout.addWidget(textLabel) 

 

                        backButton = QtWidgets.QPushButton("Close") 

                        backButton.clicked.connect(lambda: self.buttonClicked.emit()) 

                        frameLayout.addWidget(backButton) 

 

                        frame.setLayout(frameLayout) 

 

                        layout = QtWidgets.QHBoxLayout() 

                        layout.addWidget(frame) 

                        self.setLayout(layout) 

        return Dialog 

 

def getBigDialog(): 

        class BigDialog(QtWidgets.QWidget): 

                def __init__(self, imagePath, text, *args, **kwargs): 

                        super().__init__(*args, **kwargs) 

 

                        layout = QtWidgets.QVBoxLayout() 

 

                        image = QtGui.QPixmap(imagePath) 

                        imageLabel = QtWidgets.QLabel() 

                        imageLabel.setPixmap(image) 

                        imageLabel.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter) 

                        layout.addWidget(imageLabel) 

 

                        textLabel = QtWidgets.QLabel(text) 

                        textLabel.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignTop) 

                        textLabel.setWordWrap(True) 

                        textLabel.setStyleSheet("font-size: 18px; font-weight: bold;") 

                        layout.addWidget(textLabel) 

 

                        self.setLayout(layout) 

        return BigDialog 

 

class DialogShower: 

        def __init__(self, logoImagePath, brokenImagePath, bigLogoImagePath, bigBrokenImagePath, uiModule, *args, **kwargs): 

                super().__init__(*args, **kwargs) 

 

                self.logoImagePath = logoImagePath 

                self.brokenImagePath = brokenImagePath 

                self.bigLogoImagePath = bigLogoImagePath 

                self.bigBrokenImagePath = bigBrokenImagePath 

 

                self.uiModule = uiModule 

 

        def showError(self, tab, text): 

                dialog = Dialog(self.brokenImagePath, text, True) 

                self.showDialog(tab, dialog) 

 

        def showMessage(self, tab, text): 

                dialog = Dialog(self.logoImagePath, text) 

                self.showDialog(tab, dialog) 

 

        def showBigMessage(self, text): 

                dialog = BigDialog(self.bigLogoImagePath, text) 

                self.showBigDialog(dialog) 

 

        def showBigError(self, text): 

                dialog = BigDialog(self.bigBrokenImagePath, text) 

                self.showBigDialog(dialog) 

 

        def showBigDialog(self, dialog): 

                tab = self.uiModule.addCustomTab(dialog, previousTabOnClose=True) 

                tab.closeRequested.handle(tab.close) 

 

        def showDialog(self, tab, dialog): 

                tabLayout = tab.wrapperWidget.layout() 

 

                # First, remove all other errors if any are there 

                for i in range(tabLayout.count() - 1): 

                        it = tabLayout.itemAt(i) 

                        w = tabLayout.itemAt(i).widget() 

                        if isinstance(w, Dialog): 

                                # Old error is still there, remove it. 

                                tabLayout.removeItem(it) 

                                w.setParent(None) 

 

                # What happens when you click the button on the dialog 

                def removeWidget(): 

                        tabLayout.removeWidget(dialog) 

                        dialog.setParent(None) 

 

                dialog.buttonClicked.connect(removeWidget) 

 

                tabLayout.insertWidget(0, dialog) 

 

class DialogShowerModule: 

        """This module allows to show a message in a in the main window 

           embedded dialog. 

 

           To do that, you need to call .send() on one of the events defined 

           in this class. The arguments are the tab on which it needs to be 

           shown, and the text of the message. 

 

        """ 

        def __init__(self, moduleManager, *args, **kwargs): 

                super().__init__(*args, **kwargs) 

                self._mm = moduleManager 

 

                self.type = "dialogShower" 

 

                self.requires = ( 

                        self._mm.mods(type="ui"), 

                        self._mm.mods(type="event"), 

                ) 

 

        def enable(self): 

                global QtCore, QtGui, QtWidgets 

                try: 

                        from PyQt5 import QtCore, QtGui, QtWidgets 

                except ImportError: 

                        return 

                global Dialog, BigDialog 

                Dialog = getDialog() 

                BigDialog = getBigDialog() 

                self._modules = set(self._mm.mods(type="modules")).pop() 

 

                _event = self._modules.default(type="event") 

 

                logoImagePath = self._mm.resourcePath("images/ot240.png") 

                brokenImagePath = self._mm.resourcePath("images/otbroken240.png") 

                bigLogoImagePath = self._mm.resourcePath("images/ot2300.png") 

                bigBrokenImagePath = self._mm.resourcePath("images/otbroken2300.png") 

 

                uiModule = self._modules.default("active", type="ui") 

 

                self.dialogShower = DialogShower(logoImagePath, brokenImagePath, bigLogoImagePath, bigBrokenImagePath, uiModule) 

 

                self.showError = _event.createEvent() 

                self.showMessage = _event.createEvent() 

                self.showBigMessage = _event.createEvent() 

                self.showBigError = _event.createEvent() 

                self.showBigDialog = _event.createEvent() 

                self.showDialog = _event.createEvent() 

 

                self.showError.handle(self.dialogShower.showError) 

                self.showMessage.handle(self.dialogShower.showMessage) 

                self.showBigMessage.handle(self.dialogShower.showBigMessage) 

                self.showBigError.handle(self.dialogShower.showBigError) 

                self.showBigDialog.handle(self.dialogShower.showBigDialog) 

                self.showDialog.handle(self.dialogShower.showDialog) 

 

                self.active = True 

 

        def disable(self): 

                self.active = False 

 

                del self.dialogShower 

 

                del self.showError 

                del self.showMessage 

                del self.showBigMessage 

                del self.showBigError 

                del self.showBigDialog 

                del self.showDialog 

 

                del self._modules 

 

def init(moduleManager): 

        return DialogShowerModule(moduleManager)