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

#! /usr/bin/env python3 

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

 

#       Copyright 2011-2012, 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 getGraph(): 

        class Graph(QtWidgets.QWidget): 

                BAR_SIZE = 50 

                SPACING = 20 

                FONT_MARGIN = 5 

 

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

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

 

                        self._notes = notes 

 

                        self.setSizePolicy( 

                                QtWidgets.QSizePolicy.MinimumExpanding, 

                                QtWidgets.QSizePolicy.Minimum 

                        ) 

 

                def sizeHint(self): 

                        w = len(self._notes) * self.BAR_SIZE + len(self._notes) * self.SPACING 

                        h = self.fontMetrics().height() + 2 * self.FONT_MARGIN 

                        return QtCore.QSize(w, h) 

 

                def paintEvent(self, e): 

                        h = self.height() 

 

                        p = QtGui.QPainter() 

                        p.begin(self) 

 

                        #draw the bars 

                        p.setBrush(self.palette().highlight()) 

 

                        horPos = int(round(self.SPACING / 2.0)) 

                        for note in self._notes: 

                                barHeight = int(round(note / 100.0 * h)) 

                                p.setPen(QtCore.Qt.NoPen) 

                                p.drawRect(horPos, h, self.BAR_SIZE, -barHeight) 

                                p.setPen(QtGui.QPen(self.palette().highlightedText().color())) 

                                text = "%s%%" % note 

                                y = h - self.FONT_MARGIN 

                                #center on the bar 

                                x = horPos + self.BAR_SIZE / 2.0 - self.fontMetrics().width(text) / 2.0 

                                p.drawText(x, y, text) 

                                horPos += self.BAR_SIZE + self.SPACING 

 

                        p.end() 

        return Graph 

 

def getPercentNotesViewer(): 

        class PercentNotesViewer(QtWidgets.QScrollArea): 

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

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

 

                        self.setFrameStyle(QtWidgets.QFrame.NoFrame) 

 

                        graph = Graph(notes) 

                        self.setWidget(graph) 

 

                def resizeEvent(self, *args, **kwargs): 

                        graphHeight = self.viewport().height() 

                        self.widget().resize(self.widget().width(), graphHeight) 

 

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

        return PercentNotesViewer 

 

class PercentNotesViewerModule: 

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

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

                self._mm = moduleManager 

 

                self.type = "percentNotesViewer" 

                self.requires = ( 

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

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

                ) 

 

        def enable(self): 

                global QtCore, QtGui, QtWidgets 

                try: 

                        from PyQt5 import QtCore, QtGui, QtWidgets 

                except ImportError: 

                        return 

                global Graph, PercentNotesViewer 

                Graph = getGraph() 

                PercentNotesViewer = getPercentNotesViewer() 

 

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

 

                self.active = True 

 

        def _percentNotesFor(self, tests): 

                calculatePercents = self._modules.default( 

                        "active", 

                        type="percentsCalculator" 

                ).calculatePercents 

                return [calculatePercents(test) for test in tests] 

 

        def createPercentNotesViewer(self, tests, *args, **kwargs): 

                return PercentNotesViewer(self._percentNotesFor(tests), *args, **kwargs) 

 

        def disable(self): 

                self.active = False 

 

                del self._modules 

 

def init(moduleManager): 

        return PercentNotesViewerModule(moduleManager)