|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2017 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Diff colours configuration page. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSlot |
|
11 from PyQt6.QtWidgets import QColorDialog |
|
12 |
|
13 from .ConfigurationPageBase import ConfigurationPageBase |
|
14 from .Ui_DiffColoursPage import Ui_DiffColoursPage |
|
15 |
|
16 import Preferences |
|
17 |
|
18 |
|
19 class DiffColoursPage(ConfigurationPageBase, Ui_DiffColoursPage): |
|
20 """ |
|
21 Class implementing the Diff colours configuration page. |
|
22 """ |
|
23 def __init__(self): |
|
24 """ |
|
25 Constructor |
|
26 """ |
|
27 super().__init__() |
|
28 self.setupUi(self) |
|
29 self.setObjectName("DiffColoursPage") |
|
30 |
|
31 self.__coloursDict = {} |
|
32 |
|
33 monospacedFont = Preferences.getEditorOtherFonts("MonospacedFont") |
|
34 self.__allSamples = ( |
|
35 self.textSample, |
|
36 self.addedSample, |
|
37 self.removedSample, |
|
38 self.replacedSample, |
|
39 self.contextSample, |
|
40 self.headerSample, |
|
41 self.whitespaceSample |
|
42 ) |
|
43 for sample in self.__allSamples: |
|
44 sample.setFont(monospacedFont) |
|
45 |
|
46 # set initial values |
|
47 self.__initColour( |
|
48 "TextColor", |
|
49 self.textButton, |
|
50 self.__updateSampleTextColour, |
|
51 lambda: self.__selectTextColour(self.textButton), |
|
52 self.textSample) |
|
53 self.__initColour( |
|
54 "AddedColor", |
|
55 self.addedButton, |
|
56 self.__updateSampleBackgroundColour, |
|
57 lambda: self.__selectBackgroundColour(self.addedButton), |
|
58 self.addedSample) |
|
59 self.__initColour( |
|
60 "RemovedColor", |
|
61 self.removedButton, |
|
62 self.__updateSampleBackgroundColour, |
|
63 lambda: self.__selectBackgroundColour(self.removedButton), |
|
64 self.removedSample) |
|
65 self.__initColour( |
|
66 "ReplacedColor", |
|
67 self.replacedButton, |
|
68 self.__updateSampleBackgroundColour, |
|
69 lambda: self.__selectBackgroundColour(self.replacedButton), |
|
70 self.replacedSample) |
|
71 self.__initColour( |
|
72 "ContextColor", |
|
73 self.contextButton, |
|
74 self.__updateSampleBackgroundColour, |
|
75 lambda: self.__selectBackgroundColour(self.contextButton), |
|
76 self.contextSample) |
|
77 self.__initColour( |
|
78 "HeaderColor", |
|
79 self.headerButton, |
|
80 self.__updateSampleBackgroundColour, |
|
81 lambda: self.__selectBackgroundColour(self.headerButton), |
|
82 self.headerSample) |
|
83 self.__initColour( |
|
84 "BadWhitespaceColor", |
|
85 self.whitespaceButton, |
|
86 self.__updateSampleBackgroundColour, |
|
87 lambda: self.__selectBackgroundColour(self.whitespaceButton), |
|
88 self.whitespaceSample) |
|
89 |
|
90 def save(self): |
|
91 """ |
|
92 Public slot to save the Diff colours configuration. |
|
93 """ |
|
94 for key in self.__coloursDict: |
|
95 Preferences.setDiffColour(key, self.__coloursDict[key][0]) |
|
96 |
|
97 def __initColour(self, colourKey, button, initSlot, selectSlot, |
|
98 sampleWidget): |
|
99 """ |
|
100 Private method to initialize a colour selection button. |
|
101 |
|
102 @param colourKey key of the diff colour |
|
103 @type str |
|
104 @param button reference to the button |
|
105 @type QPushButton |
|
106 @param initSlot slot to be called to initialize the sample |
|
107 @type func |
|
108 @param selectSlot slot to be called to select the colour |
|
109 @type func |
|
110 @param sampleWidget reference to the sample widget |
|
111 @type QLineEdit |
|
112 """ |
|
113 colour = Preferences.getDiffColour(colourKey) |
|
114 button.setProperty("colorKey", colourKey) |
|
115 button.clicked.connect(selectSlot) |
|
116 self.__coloursDict[colourKey] = [colour, sampleWidget] |
|
117 if initSlot: |
|
118 initSlot(colourKey) |
|
119 |
|
120 @pyqtSlot() |
|
121 def __selectTextColour(self, button): |
|
122 """ |
|
123 Private slot to select the text colour. |
|
124 |
|
125 @param button reference to the button been pressed |
|
126 @type QPushButton |
|
127 """ |
|
128 colorKey = button.property("colorKey") |
|
129 |
|
130 colour = QColorDialog.getColor(self.__coloursDict[colorKey][0], self) |
|
131 if colour.isValid(): |
|
132 self.__coloursDict[colorKey][0] = colour |
|
133 self.__updateSampleTextColour(colorKey) |
|
134 |
|
135 @pyqtSlot() |
|
136 def __selectBackgroundColour(self, button): |
|
137 """ |
|
138 Private slot to select a background colour. |
|
139 |
|
140 @param button reference to the button been pressed |
|
141 @type QPushButton |
|
142 """ |
|
143 colorKey = button.property("colorKey") |
|
144 |
|
145 colour = QColorDialog.getColor( |
|
146 self.__coloursDict[colorKey][0], self, "", |
|
147 QColorDialog.ColorDialogOption.ShowAlphaChannel) |
|
148 if colour.isValid(): |
|
149 self.__coloursDict[colorKey][0] = colour |
|
150 self.__updateSampleBackgroundColour(colorKey) |
|
151 |
|
152 @pyqtSlot() |
|
153 def __updateSampleTextColour(self, colourKey): |
|
154 """ |
|
155 Private slot to update the text colour of all samples. |
|
156 |
|
157 @param colourKey key of the diff colour |
|
158 @type str |
|
159 """ |
|
160 colour = self.__coloursDict[colourKey][0] |
|
161 for key in self.__coloursDict: |
|
162 if key == "TextColor": |
|
163 self.__coloursDict[key][1].setStyleSheet( |
|
164 "QLineEdit {{ color: {0}; }}".format(colour.name()) |
|
165 ) |
|
166 else: |
|
167 self.__coloursDict[key][1].setStyleSheet( |
|
168 "QLineEdit {{ color: {0}; background-color: {1}; }}" |
|
169 .format(colour.name(), self.__coloursDict[key][0].name()) |
|
170 ) |
|
171 |
|
172 def __updateSampleBackgroundColour(self, colourKey): |
|
173 """ |
|
174 Private slot to update the background colour of a sample. |
|
175 |
|
176 @param colourKey key of the diff colour |
|
177 @type str |
|
178 """ |
|
179 sample = self.__coloursDict[colourKey][1] |
|
180 if sample: |
|
181 colour = self.__coloursDict[colourKey][0] |
|
182 sample.setStyleSheet( |
|
183 "QLineEdit {{ color: {0}; background-color: {1}; }}" |
|
184 .format(self.__coloursDict["TextColor"][0].name(), |
|
185 colour.name()) |
|
186 ) |
|
187 |
|
188 |
|
189 def create(dlg): |
|
190 """ |
|
191 Module function to create the configuration page. |
|
192 |
|
193 @param dlg reference to the configuration dialog |
|
194 @return reference to the instantiated page (ConfigurationPageBase) |
|
195 """ |
|
196 page = DiffColoursPage() |
|
197 return page |