Preferences/ConfigurationPages/DiffColoursPage.py

changeset 5765
39d8b26ff557
child 6048
82ad8ec9548c
equal deleted inserted replaced
5764:51ceecf32585 5765:39d8b26ff557
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2017 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("TextColor", self.textButton,
46 self.__updateSampleTextColour,
47 self.__selectTextColour, self.textSample)
48 self.__initColour("AddedColor", self.addedButton,
49 self.__updateSampleBackgroundColour,
50 self.__selectBackgroundColour, self.addedSample)
51 self.__initColour("RemovedColor", self.removedButton,
52 self.__updateSampleBackgroundColour,
53 self.__selectBackgroundColour, self.removedSample)
54 self.__initColour("ReplacedColor", self.replacedButton,
55 self.__updateSampleBackgroundColour,
56 self.__selectBackgroundColour, self.replacedSample)
57 self.__initColour("ContextColor", self.contextButton,
58 self.__updateSampleBackgroundColour,
59 self.__selectBackgroundColour, self.contextSample)
60 self.__initColour("HeaderColor", self.headerButton,
61 self.__updateSampleBackgroundColour,
62 self.__selectBackgroundColour, self.headerSample)
63 self.__initColour("BadWhitespaceColor", self.whitespaceButton,
64 self.__updateSampleBackgroundColour,
65 self.__selectBackgroundColour, self.whitespaceSample)
66
67 def save(self):
68 """
69 Public slot to save the Diff colours configuration.
70 """
71 for key in self.__coloursDict:
72 Preferences.setDiffColour(key, self.__coloursDict[key][0])
73
74 def __initColour(self, colourKey, button, initSlot, selectSlot,
75 sampleWidget):
76 """
77 Private method to initialize a colour selection button.
78
79 @param colourKey key of the diff colour
80 @type str
81 @param button reference to the button
82 @type QPushButton
83 @param initSlot slot to be called to initialize the sample
84 @type func
85 @param selectSlot slot to be called to select the colour
86 @type func
87 @param sampleWidget reference to the sample widget
88 @type QLineEdit
89 """
90 colour = Preferences.getDiffColour(colourKey)
91 button.setProperty("colorKey", colourKey)
92 button.clicked.connect(selectSlot)
93 self.__coloursDict[colourKey] = [colour, sampleWidget]
94 if initSlot:
95 initSlot(colourKey)
96
97 @pyqtSlot()
98 def __selectTextColour(self):
99 """
100 Private slot to select the text colour.
101 """
102 button = self.sender()
103 colorKey = button.property("colorKey")
104
105 colour = QColorDialog.getColor(self.__coloursDict[colorKey][0], self)
106 if colour.isValid():
107 self.__coloursDict[colorKey][0] = colour
108 self.__updateSampleTextColour(colorKey)
109
110 @pyqtSlot()
111 def __selectBackgroundColour(self):
112 """
113 Private slot to select a background colour.
114 """
115 button = self.sender()
116 colorKey = button.property("colorKey")
117
118 colour = QColorDialog.getColor(self.__coloursDict[colorKey][0], self,
119 "", QColorDialog.ShowAlphaChannel)
120 if colour.isValid():
121 self.__coloursDict[colorKey][0] = colour
122 self.__updateSampleBackgroundColour(colorKey)
123
124 @pyqtSlot()
125 def __updateSampleTextColour(self, colourKey):
126 """
127 Private slot to update the text colour of all samples.
128
129 @param colourKey key of the diff colour
130 @type str
131 """
132 colour = self.__coloursDict[colourKey][0]
133 for sample in self.__allSamples:
134 pl = sample.palette()
135 pl.setColor(QPalette.Text, colour)
136 sample.setPalette(pl)
137 sample.repaint()
138
139 def __updateSampleBackgroundColour(self, colourKey):
140 """
141 Private slot to update the background colour of a sample.
142
143 @param colourKey key of the diff colour
144 @type str
145 """
146 sample = self.__coloursDict[colourKey][1]
147 if sample:
148 colour = self.__coloursDict[colourKey][0]
149 pl = sample.palette()
150 pl.setColor(QPalette.Base, colour)
151 sample.setPalette(pl)
152 sample.repaint()
153
154
155 def create(dlg):
156 """
157 Module function to create the configuration page.
158
159 @param dlg reference to the configuration dialog
160 @return reference to the instantiated page (ConfigurationPageBase)
161 """
162 page = DiffColoursPage()
163 return page

eric ide

mercurial