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 | # disable the context help icon |
---|
21 | self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowContextHelpButtonHint) |
---|
22 | |
---|
23 | self.setFixedSize(self.minimumSizeHint()) |
---|
24 | |
---|
25 | self._parent = parent |
---|
26 | self._marker = marker if marker else 0 |
---|
27 | self._color = color if color else 0 |
---|
28 | self._legend = legend |
---|
29 | self._markersize = marker_size if marker_size else 3 |
---|
30 | self.custom_color = False if isinstance(self._color, int) else True |
---|
31 | |
---|
32 | # Fill out the color combobox |
---|
33 | self.cbColor.addItems(list(COLORS.keys())[:-1]) |
---|
34 | # data1d.custom_color can now be a simple integer, |
---|
35 | # specifying COLORS dict index or a string containing |
---|
36 | # the hex RGB value, e.g. #00FF00 |
---|
37 | if isinstance(self._color, int): |
---|
38 | self.cbColor.setCurrentIndex(self._color) |
---|
39 | else: |
---|
40 | # Need the Custom entry here. "Custom" is always last. |
---|
41 | self.cbColor.addItems([list(COLORS.keys())[-1]]) |
---|
42 | self.cbColor.setCurrentIndex(list(COLORS.keys()).index("Custom")) |
---|
43 | |
---|
44 | # Fill out the marker combobox |
---|
45 | self.cbShape.addItems(list(SHAPES.keys())) |
---|
46 | try: |
---|
47 | self.cbShape.setCurrentIndex(self._marker) |
---|
48 | except TypeError: |
---|
49 | marker_index = self.cbShape.findText(self._marker) |
---|
50 | self.cbShape.setCurrentIndex(marker_index) |
---|
51 | if self._legend: |
---|
52 | self.txtLegend.setText(self._legend) |
---|
53 | self.sbSize.setValue(self._markersize) |
---|
54 | |
---|
55 | # Connect slots |
---|
56 | self.cmdCustom.clicked.connect(self.onColorChange) |
---|
57 | self.cbColor.currentIndexChanged.connect(self.onColorIndexChange) |
---|
58 | |
---|
59 | def legend(self): |
---|
60 | ''' return current legend ''' |
---|
61 | return str(self.txtLegend.text()) |
---|
62 | |
---|
63 | def marker(self): |
---|
64 | ''' return the current shape index in SHAPE ''' |
---|
65 | return self.cbShape.currentIndex() |
---|
66 | |
---|
67 | def markersize(self): |
---|
68 | ''' return marker size (int) ''' |
---|
69 | return self.sbSize.value() |
---|
70 | |
---|
71 | def color(self): |
---|
72 | ''' return current color: index in COLORS or a hex string ''' |
---|
73 | if self.custom_color: |
---|
74 | return self._color |
---|
75 | else: |
---|
76 | return self.cbColor.currentIndex() |
---|
77 | |
---|
78 | def onColorChange(self): |
---|
79 | """ |
---|
80 | Pop up the standard Qt color change dialog |
---|
81 | """ |
---|
82 | # Pick up the chosen color |
---|
83 | proposed_color = QtWidgets.QColorDialog.getColor(parent=self) |
---|
84 | # Update the text control |
---|
85 | if proposed_color.isValid(): |
---|
86 | # Block currentIndexChanged |
---|
87 | self.cbColor.blockSignals(True) |
---|
88 | # Add Custom to the color combo box |
---|
89 | self.cbColor.addItems(["Custom"]) |
---|
90 | self.cbColor.setCurrentIndex(list(COLORS.keys()).index("Custom")) |
---|
91 | # unblock currentIndexChanged |
---|
92 | self.cbColor.blockSignals(False) |
---|
93 | # Save the color as #RRGGBB |
---|
94 | self.custom_color = True |
---|
95 | self._color = str(proposed_color.name()) |
---|
96 | |
---|
97 | def onColorIndexChange(self): |
---|
98 | """ |
---|
99 | Dynamically add/remove "Custom" color index |
---|
100 | """ |
---|
101 | # Changed index - assure Custom is deleted |
---|
102 | custom_index = self.cbColor.findText("Custom") |
---|
103 | self.custom_color = False |
---|
104 | if custom_index > -1: |
---|
105 | self.cbColor.removeItem(custom_index) |
---|
106 | |
---|