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