color button with drop down
This commit is contained in:
@@ -1,26 +1,75 @@
|
||||
import re
|
||||
|
||||
from PyQt6.QtGui import QColor
|
||||
from PyQt6.QtWidgets import QWidget, QHBoxLayout, QPushButton, QColorDialog, QSizePolicy
|
||||
from PyQt6.QtCore import QSize, Qt, QRect
|
||||
from PyQt6.QtGui import QColor, QPainter, QPixmap, QIcon, QBrush, QPen, QPaintEngine
|
||||
from PyQt6.QtWidgets import QWidget, QHBoxLayout, QPushButton, QColorDialog, QSizePolicy, QComboBox
|
||||
|
||||
|
||||
class ColorButton(QPushButton):
|
||||
class ColorButton(QWidget):
|
||||
def __init__(self, color: str):
|
||||
super(QPushButton, self).__init__()
|
||||
self.color = color
|
||||
self.setStyleSheet("background-color: #%s" % color)
|
||||
self.pressed.connect(self._update_color)
|
||||
super(QWidget, self).__init__()
|
||||
|
||||
self.setSizePolicy(QSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed))
|
||||
self.layout = QHBoxLayout(self)
|
||||
|
||||
self.color = color
|
||||
|
||||
colors = ['aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige', 'bisque', 'black',
|
||||
'blanchedalmond', 'blue', 'blueviolet', 'brown', 'burlywood', 'cadetblue', 'chartreuse', 'chocolate',
|
||||
'coral', 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan', 'darkgoldenrod',
|
||||
'darkgray', 'darkgreen', 'darkgrey', 'darkkhaki', 'darkmagenta', 'darkolivegreen', 'darkorange',
|
||||
'darkorchid', 'darkred', 'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray',
|
||||
'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue', 'dimgray', 'dimgrey',
|
||||
'dodgerblue', 'firebrick', 'floralwhite', 'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite', 'gold',
|
||||
'goldenrod', 'gray', 'green', 'greenyellow', 'grey', 'honeydew', 'hotpink', 'indianred', 'indigo',
|
||||
'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen', 'lemonchiffon', 'lightblue', 'lightcoral',
|
||||
'lightcyan', 'lightgoldenrodyellow', 'lightgray', 'lightgreen', 'lightgrey', 'lightpink',
|
||||
'lightsalmon', 'lightseagreen', 'lightskyblue', 'lightslategray', 'lightslategrey', 'lightsteelblue',
|
||||
'lightyellow', 'lime', 'limegreen', 'linen', 'magenta', 'maroon', 'mediumaquamarine', 'mediumblue',
|
||||
'mediumorchid', 'mediumpurple', 'mediumseagreen', 'mediumslateblue', 'mediumspringgreen',
|
||||
'mediumturquoise', 'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin',
|
||||
'navajowhite', 'navy', 'oldlace', 'olive', 'olivedrab', 'orange', 'orangered', 'orchid',
|
||||
'palegoldenrod', 'palegreen', 'paleturquoise', 'palevioletred', 'papayawhip', 'peachpuff', 'peru',
|
||||
'pink', 'plum', 'powderblue', 'purple', 'red', 'rosybrown', 'royalblue', 'saddlebrown', 'salmon',
|
||||
'sandybrown', 'seagreen', 'seashell', 'sienna', 'silver', 'skyblue', 'slateblue', 'slategray',
|
||||
'slategrey', 'snow', 'springgreen', 'steelblue', 'tan', 'teal', 'thistle', 'tomato', 'transparent',
|
||||
'turquoise', 'violet', 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen']
|
||||
colors = {
|
||||
self.tr('Yellow'): 'fbe7c6',
|
||||
self.tr('Mint'): 'b4f8c8',
|
||||
self.tr('Tiffany Blue'): 'a0e7e5',
|
||||
self.tr('Hot Pink'): 'ffaebc',
|
||||
}
|
||||
|
||||
self.color_drop_down = QComboBox()
|
||||
self.layout.addWidget(self.color_drop_down)
|
||||
self.color_drop_down.addItem("transparent", "None")
|
||||
for color_name in colors.keys():
|
||||
color_value = colors[color_name]
|
||||
self.color_drop_down.addItem(QIcon(self._color_pixmap(color_value)), color_name, color_value)
|
||||
|
||||
self.color_drop_down.currentIndexChanged.connect(self._color_selected)
|
||||
|
||||
self.btn_color_picker = QPushButton(self.tr("custom"))
|
||||
self.layout.addWidget(self.btn_color_picker)
|
||||
self.btn_color_picker.pressed.connect(self._update_color)
|
||||
self.btn_color_picker.setSizePolicy(QSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed))
|
||||
|
||||
def _color_selected(self, index: int):
|
||||
self.color = self.color_drop_down.currentData()
|
||||
print(self.color)
|
||||
|
||||
def set_color(self, color: str):
|
||||
if self._is_hex_color(color):
|
||||
self.setStyleSheet("background-color: #%s" % color)
|
||||
self.setText(color)
|
||||
self.color = color
|
||||
else:
|
||||
self.setStyleSheet("background-color: none")
|
||||
self.setText(self.tr("not set"))
|
||||
self.color = "None"
|
||||
self.color = color
|
||||
for index in range(0, self.color_drop_down.count()):
|
||||
item_value = self.color_drop_down.itemData(index)
|
||||
if item_value == color:
|
||||
self.color_drop_down.setCurrentIndex(index)
|
||||
return
|
||||
|
||||
# color not yet present -> add it
|
||||
self.color_drop_down.addItem(QIcon(self._color_pixmap(color)), color, color)
|
||||
self.color_drop_down.setCurrentIndex(self.color_drop_down.count() - 1)
|
||||
|
||||
def _update_color(self):
|
||||
new_color = QColorDialog.getColor(self._to_qcolor(self.color))
|
||||
@@ -32,12 +81,20 @@ class ColorButton(QPushButton):
|
||||
def _is_hex_color(color: str):
|
||||
return re.match("[0-9a-f]{6}", color, flags=re.IGNORECASE)
|
||||
|
||||
def _color_pixmap(self, color: str) -> QPixmap:
|
||||
pixmap = QPixmap(40, 40)
|
||||
qcolor = self._to_qcolor(color)
|
||||
pixmap.fill((qcolor))
|
||||
return pixmap
|
||||
|
||||
def _to_qcolor(self, color: str):
|
||||
if self._is_hex_color(color):
|
||||
red = int(color[0:2], 16)
|
||||
green = int(color[2:4], 16)
|
||||
blue = int(color[4:6], 16)
|
||||
return QColor(red, green, blue)
|
||||
elif color in QColor().colorNames():
|
||||
return QColor(color)
|
||||
return QColor(255, 255, 255)
|
||||
|
||||
def _to_hex(self, color: QColor) -> str:
|
||||
|
||||
Reference in New Issue
Block a user