eric7/Preferences/ConfigurationPages/ConfigurationPageBase.py

branch
eric7
changeset 8312
800c432b34c8
parent 8218
7c09585bd960
child 8318
962bce857696
equal deleted inserted replaced
8311:4e8b98454baa 8312:800c432b34c8
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2006 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the base class for all configuration pages.
8 """
9
10 from PyQt5.QtCore import pyqtSlot, pyqtSignal
11 from PyQt5.QtGui import QIcon, QPixmap, QColor
12 from PyQt5.QtWidgets import QWidget, QColorDialog, QFontDialog
13
14
15 class ConfigurationPageBase(QWidget):
16 """
17 Class implementing the base class for all configuration pages.
18
19 @signal colourChanged(str, QColor) To inform about a new colour selection
20 """
21 colourChanged = pyqtSignal(str, QColor)
22
23 def __init__(self):
24 """
25 Constructor
26 """
27 super().__init__()
28
29 self.__coloursDict = {}
30
31 def polishPage(self):
32 """
33 Public slot to perform some polishing actions.
34 """
35 return
36
37 def saveState(self):
38 """
39 Public method to save the current state of the widget.
40 """
41 return
42
43 def setState(self, state):
44 """
45 Public method to set the state of the widget.
46
47 @param state state data generated by saveState
48 """
49 return
50
51 def initColour(self, colourKey, button, prefMethod, byName=False,
52 hasAlpha=False):
53 """
54 Public method to initialize a colour selection button.
55
56 @param colourKey key of the colour resource (string)
57 @param button reference to a button to show the colour on (QPushButton)
58 @param prefMethod preferences method to get the colour
59 @param byName flag indicating to retrieve/save by colour name
60 (boolean)
61 @param hasAlpha flag indicating to allow alpha channel (boolean)
62 """
63 colour = QColor(prefMethod(colourKey))
64 size = button.size()
65 pm = QPixmap(size.width() / 2, size.height() / 2)
66 pm.fill(colour)
67 button.setIconSize(pm.size())
68 button.setIcon(QIcon(pm))
69 button.setProperty("colorKey", colourKey)
70 button.setProperty("hasAlpha", hasAlpha)
71 button.clicked.connect(lambda: self.__selectColourSlot(button))
72 self.__coloursDict[colourKey] = [colour, byName]
73 self.colourChanged.emit(colourKey, colour)
74
75 @pyqtSlot()
76 def __selectColourSlot(self, button):
77 """
78 Private slot to select a color.
79
80 @param button reference to the button been pressed
81 @type QPushButton
82 """
83 colorKey = button.property("colorKey")
84 hasAlpha = button.property("hasAlpha")
85
86 colDlg = QColorDialog(self)
87 if hasAlpha:
88 colDlg.setOptions(QColorDialog.ColorDialogOption.ShowAlphaChannel)
89 # Set current colour last to avoid conflicts with alpha channel
90 colDlg.setCurrentColor(self.__coloursDict[colorKey][0])
91 colDlg.currentColorChanged.connect(
92 lambda col: self.colourChanged.emit(colorKey, col))
93 colDlg.exec()
94
95 if colDlg.result() == colDlg.Accepted:
96 colour = colDlg.selectedColor()
97 size = button.iconSize()
98 pm = QPixmap(size.width(), size.height())
99 pm.fill(colour)
100 button.setIcon(QIcon(pm))
101 self.__coloursDict[colorKey][0] = colour
102
103 # Update colour selection
104 self.colourChanged.emit(colorKey, self.__coloursDict[colorKey][0])
105
106 def saveColours(self, prefMethod):
107 """
108 Public method to save the colour selections.
109
110 @param prefMethod preferences method to set the colour
111 """
112 for key in self.__coloursDict:
113 if self.__coloursDict[key][1]:
114 prefMethod(key, self.__coloursDict[key][0].name())
115 else:
116 prefMethod(key, self.__coloursDict[key][0])
117
118 def selectFont(self, fontSample, fontVar, showFontInfo=False,
119 options=None):
120 """
121 Public method used by the font selection buttons.
122
123 @param fontSample reference to the font sample widget (QLineEdit)
124 @param fontVar reference to the variable containing the font (QFont)
125 @param showFontInfo flag indicating to show some font info
126 as the sample (boolean)
127 @param options options for the font dialog
128 (QFontDialog.FontDialogOptions)
129 @return selected font (QFont)
130 """
131 if options is None:
132 options = QFontDialog.FontDialogOptions(0)
133 font, ok = QFontDialog.getFont(fontVar, self, "", options)
134 if ok:
135 fontSample.setFont(font)
136 if showFontInfo:
137 fontSample.setText(
138 "{0} {1}".format(font.family(), font.pointSize()))
139 else:
140 font = fontVar
141 return font # __IGNORE_WARNING_M834__

eric ide

mercurial