From 2826b934324c56881798dbbfa9355afe455825d3 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Sat, 30 Oct 2021 20:28:19 +0200 Subject: [PATCH] color button with drop down --- .gitignore | 1 + colorbutton.py | 87 +++++++++++++++++++++++++++++++++++++++++--------- scribble.py | 10 ++---- 3 files changed, 75 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index bee8a64..0f44f7a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ __pycache__ +3rd-party diff --git a/colorbutton.py b/colorbutton.py index 4b66692..5c55235 100644 --- a/colorbutton.py +++ b/colorbutton.py @@ -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: diff --git a/scribble.py b/scribble.py index c16c6b1..89965b1 100644 --- a/scribble.py +++ b/scribble.py @@ -1,9 +1,3 @@ -import os -import urllib -from urllib.parse import urlparse +from PyQt6.QtGui import QColor -url = "file:///home/andi/.local/share/ravenlog/session.ini" - -p = urlparse(url) -final_path = os.path.abspath(os.path.join(p.netloc, p.path)) -print(final_path) +print(QColor().colorNames())