1 | from PyQt5 import QtCore |
---|
2 | from PyQt5 import QtGui |
---|
3 | from PyQt5 import QtWidgets |
---|
4 | |
---|
5 | from sas.qtgui.Plotting.PlotUtilities import COLORS, SHAPES |
---|
6 | |
---|
7 | from sas.qtgui.UI import main_resources_rc |
---|
8 | from sas.qtgui.Plotting.UI.PlotPropertiesUI import Ui_PlotPropertiesUI |
---|
9 | |
---|
10 | class PlotProperties(QtWidgets.QDialog, Ui_PlotPropertiesUI): |
---|
11 | """ Dialog for modification of single plot properties """ |
---|
12 | def __init__(self, |
---|
13 | parent=None, |
---|
14 | color=0, |
---|
15 | marker=0, |
---|
16 | marker_size=5, |
---|
17 | legend=""): |
---|
18 | super(PlotProperties, self).__init__(parent) |
---|
19 | self.setupUi(self) |
---|
20 | self.setFixedSize(self.minimumSizeHint()) |
---|
21 | |
---|
22 | self._parent = parent |
---|
23 | self._marker = marker if marker else 0 |
---|
24 | self._color = color if color else 0 |
---|
25 | self._legend = legend |
---|
26 | self._markersize = marker_size if marker_size else 5 |
---|
27 | self.custom_color = False if isinstance(self._color, int) else True |
---|
28 | |
---|
29 | # Fill out the color combobox |
---|
30 | self.cbColor.addItems(list(COLORS.keys())[:-1]) |
---|
31 | # data1d.custom_color can now be a simple integer, |
---|
32 | # specifying COLORS dict index or a string containing |
---|
33 | # the hex RGB value, e.g. #00FF00 |
---|
34 | if isinstance(self._color, int): |
---|
35 | self.cbColor.setCurrentIndex(self._color) |
---|
36 | else: |
---|
37 | # Need the Custom entry here. "Custom" is always last. |
---|
38 | self.cbColor.addItems([list(COLORS.keys())[-1]]) |
---|
39 | self.cbColor.setCurrentIndex(list(COLORS.keys()).index("Custom")) |
---|
40 | |
---|
41 | # Fill out the marker combobox |
---|
42 | self.cbShape.addItems(list(SHAPES.keys())) |
---|
43 | try: |
---|
44 | self.cbShape.setCurrentIndex(self._marker) |
---|
45 | except TypeError: |
---|
46 | marker_index = self.cbShape.findText(self._marker) |
---|
47 | self.cbShape.setCurrentIndex(marker_index) |
---|
48 | if self._legend: |
---|
49 | self.txtLegend.setText(self._legend) |
---|
50 | self.sbSize.setValue(self._markersize) |
---|
51 | |
---|
52 | # Connect slots |
---|
53 | self.cmdCustom.clicked.connect(self.onColorChange) |
---|
54 | self.cbColor.currentIndexChanged.connect(self.onColorIndexChange) |
---|
55 | |
---|
56 | def legend(self): |
---|
57 | ''' return current legend ''' |
---|
58 | return str(self.txtLegend.text()) |
---|
59 | |
---|
60 | def marker(self): |
---|
61 | ''' return the current shape index in SHAPE ''' |
---|
62 | return self.cbShape.currentIndex() |
---|
63 | |
---|
64 | def markersize(self): |
---|
65 | ''' return marker size (int) ''' |
---|
66 | return self.sbSize.value() |
---|
67 | |
---|
68 | def color(self): |
---|
69 | ''' return current color: index in COLORS or a hex string ''' |
---|
70 | if self.custom_color: |
---|
71 | return self._color |
---|
72 | else: |
---|
73 | return self.cbColor.currentIndex() |
---|
74 | |
---|
75 | def onColorChange(self): |
---|
76 | """ |
---|
77 | Pop up the standard Qt color change dialog |
---|
78 | """ |
---|
79 | # Pick up the chosen color |
---|
80 | proposed_color = QtGui.QColorDialog.getColor(parent=self) |
---|
81 | # Update the text control |
---|
82 | if proposed_color.isValid(): |
---|
83 | # Block currentIndexChanged |
---|
84 | self.cbColor.blockSignals(True) |
---|
85 | # Add Custom to the color combo box |
---|
86 | self.cbColor.addItems(["Custom"]) |
---|
87 | self.cbColor.setCurrentIndex(list(COLORS.keys()).index("Custom")) |
---|
88 | # unblock currentIndexChanged |
---|
89 | self.cbColor.blockSignals(False) |
---|
90 | # Save the color as #RRGGBB |
---|
91 | self.custom_color = True |
---|
92 | self._color = str(proposed_color.name()) |
---|
93 | |
---|
94 | def onColorIndexChange(self): |
---|
95 | """ |
---|
96 | Dynamically add/remove "Custom" color index |
---|
97 | """ |
---|
98 | # Changed index - assure Custom is deleted |
---|
99 | custom_index = self.cbColor.findText("Custom") |
---|
100 | self.custom_color = False |
---|
101 | if custom_index > -1: |
---|
102 | self.cbColor.removeItem(custom_index) |
---|
103 | |
---|