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