color button with drop down

This commit is contained in:
2021-10-30 20:28:19 +02:00
parent f3f700f737
commit 2826b93432
3 changed files with 75 additions and 23 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
__pycache__ __pycache__
3rd-party

View File

@@ -1,26 +1,75 @@
import re import re
from PyQt6.QtGui import QColor from PyQt6.QtCore import QSize, Qt, QRect
from PyQt6.QtWidgets import QWidget, QHBoxLayout, QPushButton, QColorDialog, QSizePolicy 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): def __init__(self, color: str):
super(QPushButton, self).__init__() super(QWidget, self).__init__()
self.color = color
self.setStyleSheet("background-color: #%s" % color)
self.pressed.connect(self._update_color)
self.setSizePolicy(QSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)) 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): def set_color(self, color: str):
if self._is_hex_color(color): self.color = color
self.setStyleSheet("background-color: #%s" % color) for index in range(0, self.color_drop_down.count()):
self.setText(color) item_value = self.color_drop_down.itemData(index)
self.color = color if item_value == color:
else: self.color_drop_down.setCurrentIndex(index)
self.setStyleSheet("background-color: none") return
self.setText(self.tr("not set"))
self.color = "None" # 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): def _update_color(self):
new_color = QColorDialog.getColor(self._to_qcolor(self.color)) new_color = QColorDialog.getColor(self._to_qcolor(self.color))
@@ -32,12 +81,20 @@ class ColorButton(QPushButton):
def _is_hex_color(color: str): def _is_hex_color(color: str):
return re.match("[0-9a-f]{6}", color, flags=re.IGNORECASE) 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): def _to_qcolor(self, color: str):
if self._is_hex_color(color): if self._is_hex_color(color):
red = int(color[0:2], 16) red = int(color[0:2], 16)
green = int(color[2:4], 16) green = int(color[2:4], 16)
blue = int(color[4:6], 16) blue = int(color[4:6], 16)
return QColor(red, green, blue) return QColor(red, green, blue)
elif color in QColor().colorNames():
return QColor(color)
return QColor(255, 255, 255) return QColor(255, 255, 255)
def _to_hex(self, color: QColor) -> str: def _to_hex(self, color: QColor) -> str:

View File

@@ -1,9 +1,3 @@
import os from PyQt6.QtGui import QColor
import urllib
from urllib.parse import urlparse
url = "file:///home/andi/.local/share/ravenlog/session.ini" print(QColor().colorNames())
p = urlparse(url)
final_path = os.path.abspath(os.path.join(p.netloc, p.path))
print(final_path)