def __init__(self, parent: Optional[QWidget] = None): super(ColorPicker, self).__init__(parent) self.events = ColorPicker.Events() self.setAutoFillBackground(True) self._color_dialog = QColorDialog(self) self._color_dialog.setWindowFlags(Qt.Widget) self._color_dialog.setOptions(QColorDialog.DontUseNativeDialog | QColorDialog.NoButtons) self._menu = QMenu(self) action = QWidgetAction(self) action.setDefaultWidget(self._color_dialog) self._menu.addAction(action) self.setMenu(self._menu) # noinspection PyUnresolvedReferences self._menu.aboutToShow.connect(lambda: self._color_dialog.show()) # noinspection PyUnresolvedReferences self._color_dialog.currentColorChanged.connect( self.events.color_changed) # noinspection PyUnresolvedReferences self._color_dialog.currentColorChanged.connect( lambda color: self.update()) self.update()
def __init__(self, settings: ViewSettings): super().__init__() self.settings = settings self.color_picker = QColorDialog() self.color_picker.setWindowFlag(Qt.Widget) self.color_picker.setOptions(QColorDialog.DontUseNativeDialog | QColorDialog.NoButtons) self.opacity_spin = QDoubleSpinBox() self.opacity_spin.setRange(0, 1) self.opacity_spin.setSingleStep(0.1) self.opacity_spin.setDecimals(2) self.change_mask_color_btn = QPushButton("Change mask color") self.current_mask_color_preview = ColorShow( self.settings.get_from_profile("mask_presentation_color", [255, 255, 255])) self.opacity_spin.setValue( self.settings.get_from_profile("mask_presentation_opacity", 1)) self.current_mask_color_preview.setAutoFillBackground(True) self.change_mask_color_btn.clicked.connect(self.change_color) self.opacity_spin.valueChanged.connect(self.change_opacity) layout = QVBoxLayout() layout.addWidget(self.color_picker) layout2 = QHBoxLayout() layout2.addWidget(self.change_mask_color_btn) layout2.addWidget(self.current_mask_color_preview, 1) layout2.addWidget(QLabel("Mask opacity")) layout2.addWidget(self.opacity_spin) layout.addLayout(layout2) self.setLayout(layout)
def __init__(self, settings: ViewSettings): super().__init__() self.settings = settings self.color_list = [] self.chosen = None self.prohibited_names = set(self.settings.label_color_dict.keys() ) # Prohibited name is added to reduce # probability of colormap cache collision self.color_picker = QColorDialog() self.color_picker.setWindowFlag(Qt.Widget) self.color_picker.setOptions(QColorDialog.DontUseNativeDialog | QColorDialog.NoButtons) self.add_color_btn = QPushButton("Add color") self.add_color_btn.clicked.connect(self.add_color) self.remove_color_btn = QPushButton("Remove last color") self.remove_color_btn.clicked.connect(self.remove_color) self.save_btn = QPushButton("Save") self.save_btn.clicked.connect(self.save) self.color_layout = QHBoxLayout() layout = QVBoxLayout() layout.addWidget(self.color_picker) btn_layout = QHBoxLayout() btn_layout.addWidget(self.add_color_btn) btn_layout.addWidget(self.remove_color_btn) btn_layout.addWidget(self.save_btn) layout.addLayout(btn_layout) layout.addLayout(self.color_layout) self.setLayout(layout)
def launch_qcolor_dialog(self): color_dialog = QColorDialog(self) color_dialog.setCurrentColor(QColor(self.get_color())) color_dialog.colorSelected.connect( lambda: self.set_line_edit(color_dialog.selectedColor().name())) color_dialog.accepted.connect( lambda: self.set_prev_color(color_dialog.selectedColor().name())) color_dialog.setModal(True) color_dialog.show()
def choose_default_color(self): palette = self.default_color_btn.palette() initial = palette.color(QPalette.Button) dialog = QColorDialog(self) color: str = dialog.getColor(initial) if color.isValid(): palette.setColor(QPalette.Button, color) self.default_color_btn.setPalette(palette)
def __init__(self, parent=None): """Initialize the ColorButton.""" super().__init__(parent=parent) self.color_dialog = QColorDialog(self) self.clicked.connect(self.color_dialog.show) self.setObjectName("ColorButton") self.color_dialog.colorSelected.connect(self.setColor)
def showColorDialoge(self): col = QColorDialog.getColor() # 获取colorDialog的颜色 if col.isValid(): self.label.setText(self.colorText.format(col.name())) self.frm.setStyleSheet("QWidget { background-color: %s}" % col.name())
def handleDoubleClick(self, index): if self.table_model.needsColorDialog(index): # The table model returns a QBrush for BackgroundRole, not a QColor init_color = self.table_model.data(index, Qt.BackgroundRole).color() color = QColorDialog.getColor(init_color, self) if color.isValid(): self.table_model.setData(index, color, role=Qt.EditRole)
def mouseReleaseEvent(self, QMouseEvent): color = QColorDialog.getColor(self._color, self, "Select color", QColorDialog.ShowAlphaChannel) if color.isValid(): self._color = color self.update() self.colorChanged.emit(self._color)
def __set_rgb(self) -> None: """Add a custom color from current color.""" color = QColorDialog.getColor( color_qt(self.color_box.currentText()), self ) if color.isValid(): add_custom_color(self.color_box, color)
def button_clicked(self): sender = self.sender() stylesheet: str = sender.styleSheet() old_color = stylesheet.split(";")[-1].split(":")[1] new_color = QColor(old_color.replace("#", "")) new_color = QColorDialog.getColor(new_color, self) if new_color.isValid(): sender.setStyleSheet( stylesheet.replace(old_color, new_color.name()))
def __set_rgb(self) -> None: """Add a custom color from current color.""" color = QColorDialog.getColor(color_qt(self.color_box.currentText()), self) if not color.isValid(): return rgb_str = str((color.red(), color.green(), color.blue())) self.color_box.addItem(color_icon(rgb_str), rgb_str) self.color_box.setCurrentIndex(self.color_box.count() - 1)
def select_color(self): """Open a color selection dialog box""" color = text_to_qcolor(self.edit.text()) if not color.isValid(): color = Qt.gray color = QColorDialog.getColor(color, self.parent_layout.parent) if color.isValid(): value = color.name() self.edit.setText(value) self.update(value)
class ColorPicker(QPushButton): class Events(QObject): color_changed = Signal(QColor) def __init__(self, parent: Optional[QWidget] = None): super(ColorPicker, self).__init__(parent) self.events = ColorPicker.Events() self.setAutoFillBackground(True) self._color_dialog = QColorDialog(self) self._color_dialog.setWindowFlags(Qt.Widget) self._color_dialog.setOptions(QColorDialog.DontUseNativeDialog | QColorDialog.NoButtons) self._menu = QMenu(self) action = QWidgetAction(self) action.setDefaultWidget(self._color_dialog) self._menu.addAction(action) self.setMenu(self._menu) # noinspection PyUnresolvedReferences self._menu.aboutToShow.connect(lambda: self._color_dialog.show()) # noinspection PyUnresolvedReferences self._color_dialog.currentColorChanged.connect( self.events.color_changed) # noinspection PyUnresolvedReferences self._color_dialog.currentColorChanged.connect( lambda color: self.update()) self.update() def update(self): color = self._color_dialog.currentColor() self.setText(color.name()) text_color_name = 'black' if qGray(color.rgb()) > 127 else 'white' self.setStyleSheet( f'ColorPicker {{ color: {text_color_name}; background-color: {color.name()}; }}' ) super(ColorPicker, self).update() @property def color(self) -> QColor: return self._color_dialog.currentColor() @color.setter def color(self, color: QColor): self._color_dialog.setCurrentColor(color)
def handle_curve_color_button_clicked(self): selected_color = QColorDialog.getColor() curve = self.chart.findCurve(self.pv_name) if curve: self.curve_original_color = curve.color curve.color = selected_color self.curve_color_btn.setStyleSheet("background-color: " + curve.color.name()) self.chart.refreshCurve(curve) self.channel_map[self.pv_name] = curve
def format_textBackgroundColor(self): selectedText = self.editor.textCursor().selectedText() if selectedText: color = QColorDialog.getColor(Qt.yellow, self) if color.isValid(): if self.html: self.editor.setTextBackgroundColor(color) else: self.editor.insertPlainText('<span style="background-color:{0};">{1}</span>'.format(color.name(), selectedText)) else: self.selectTextFirst()
def _change_plot_color(self): plot = self.active_window.get_plot(self.current_layer) col = QColorDialog.getColor(plot._pen_stash['pen_on'].color(), self.contents.tree_widget_layer_list) if col.isValid(): plot.pen = col dispatch.on_updated_plot.emit(plot=plot) else: logging.warning("Color is not valid.")
def open_color_dialog(self, attr_name, mouse_event): d = QColorDialog(self) d.setCurrentColor(getattr(self, attr_name)) f = partial(self.set_color, attr_name) d.colorSelected.connect( f) # d.open(f) doesn't pass color for some reason d.open()
def __init__(self): super().__init__() self.color_picker = QColorDialog() self.color_picker.setWindowFlag(Qt.Widget) self.color_picker.setOptions(QColorDialog.DontUseNativeDialog | QColorDialog.NoButtons) self.show_colormap = ColormapEdit() self.clear_btn = QPushButton("Clear") self.save_btn = QPushButton("Save") self.distribute_btn = QPushButton("Distribute evenly") self.reverse_btn = QPushButton("Reverse") self.info_label = InfoLabel( [ "<strong>Tip:</strong> Select color and double click on below color bar. " + "Then repeat to add another colors.", "<strong>Tip:</strong> Double click on marker to remove it.", "<strong>Tip:</strong> Press and hold mouse left button on marker to move it on color bar.", ], delay=10000, ) layout = QVBoxLayout() layout.addWidget(self.color_picker) layout.addWidget(self.info_label) layout.addWidget(self.show_colormap) btn_layout = QHBoxLayout() btn_layout.addStretch(1) btn_layout.addWidget(self.reverse_btn) btn_layout.addWidget(self.distribute_btn) btn_layout.addWidget(self.clear_btn) btn_layout.addWidget(self.save_btn) layout.addLayout(btn_layout) self.setLayout(layout) self.show_colormap.double_clicked.connect(self.add_color) self.clear_btn.clicked.connect(self.show_colormap.clear) self.save_btn.clicked.connect(self.save) self.reverse_btn.clicked.connect(self.show_colormap.reverse) self.distribute_btn.clicked.connect( self.show_colormap.distribute_evenly)
def on_color(self): obj = self.out_data['point_properties'] rgb_color_ints = obj.color msg = 'Points' col = QColorDialog.getColor(QtGui.QColor(*rgb_color_ints), self, "Choose a %s color" % msg) if col.isValid(): color = col.getRgbF()[:3] obj.color = color #print('new_color =', color) self.color_edit.setStyleSheet("QPushButton {" "background-color: rgb(%s, %s, %s);" % tuple(obj.color) + #"border:1px solid rgb(255, 170, 255); " "}")
def changeColor(self): from qtpy.QtGui import QColor from qtpy.QtWidgets import QColorDialog color = QColorDialog.getColor(QColor(config.presentationColorOnDarkTheme if config.theme in ("dark", "night") else config.presentationColorOnLightTheme), self) if color.isValid(): colorName = color.name() if config.theme in ("dark", "night"): config.presentationColorOnDarkTheme = colorName else: config.presentationColorOnLightTheme = colorName buttonStyle = "QPushButton {0}background-color: {2}; color: {3};{1}".format("{", "}", colorName, "white" if config.theme in ("dark", "night") else "black") self.changecolorbutton.setStyleSheet(buttonStyle)
def changeColor(self, index): color = QColorDialog.getColor( QColor(config.highlightDarkThemeColours[index] if config.theme == "dark" else config.highlightLightThemeColours[index]), self) if color.isValid(): colorName = color.name() if config.theme in ("dark", "night"): config.highlightDarkThemeColours[index] = colorName else: config.highlightLightThemeColours[index] = colorName button = self.collectionColourButtons[index] buttonStyle = "QPushButton {0}background-color: {2}; color: {3};{1}".format( "{", "}", colorName, "white" if config.theme == "dark" else "black") button.setStyleSheet(buttonStyle)
def on_color(self): """called when the user clicks on the color box""" name = self.active_key obj = self.out_data[name] rgb_color_ints = obj.color msg = name col = QColorDialog.getColor(QtGui.QColor(*rgb_color_ints), self, "Choose a %s color" % msg) if col.isValid(): color_float = col.getRgbF()[:3] obj.color = color_float color_int = [int(colori * 255) for colori in color_float] self.color_edit.setStyleSheet("QPushButton {" "background-color: rgb(%s, %s, %s);" % tuple(color_int) + #"border:1px solid rgb(255, 170, 255); " "}") self.on_apply(force=self.force)
def on_color(self, color_edit, rgb_color_ints, title): """pops a color dialog""" col = QColorDialog.getColor(QtGui.QColor(*rgb_color_ints), self, title) if not col.isValid(): return False, rgb_color_ints, None color_float = col.getRgbF()[:3] # floats color_int = [int(colori * 255) for colori in color_float] assert isinstance(color_float[0], float), color_float assert isinstance(color_int[0], int), color_int color_edit.setStyleSheet("QPushButton {" "background-color: rgb(%s, %s, %s);" % tuple(color_int) + #"border:1px solid rgb(255, 170, 255); " "}") return True, color_int, color_float
def _pop_color_dialog(parent, color_edit: QPushButtonColor, rgb_color_ints: List[int], title: str) -> Tuple[bool, List[int], List[float]]: """pops a color dialog""" col = QColorDialog.getColor(QtGui.QColor(*rgb_color_ints), parent, title) if not col.isValid(): return False, rgb_color_ints, None color_float = col.getRgbF()[:3] # floats color_int = [int(colori * 255) for colori in color_float] assert isinstance(color_float[0], float), color_float assert isinstance(color_int[0], int), color_int color_edit.setStyleSheet("QPushButton {" "background-color: rgb(%s, %s, %s);" % tuple(color_int) + #"border:1px solid rgb(255, 170, 255); " "}") return True, color_int, color_float
def _on_change_color(self): """ Listens for color changed events in plot windows, gets the currently selected item in the data list view, and changes the stored color value. """ # If there is no currently selected rows, raise an error if self.current_item is None: message_box = QMessageBox() message_box.setText("No item selected, cannot change color.") message_box.setIcon(QMessageBox.Warning) message_box.setInformativeText( "There is currently no item selected. Please select an item " "before changing its plot color.") message_box.exec() return color = QColorDialog.getColor() if color.isValid(): self.current_item.color = color.name()
def _on_change_color(self): """ Listens for color changed events in plot windows, gets the currently selected item in the data list view, and changes the stored color value. """ # If there is no currently selected rows, raise an error if self.current_item is None: message_box = QMessageBox() message_box.setText("No item selected, cannot change color.") message_box.setIcon(QMessageBox.Warning) message_box.setInformativeText( "There is currently no item selected. Please select an item " "before changing its plot color.") message_box.exec() return color = QColorDialog.getColor(options=QColorDialog.ShowAlphaChannel) if color.isValid(): self.current_item.color = color.toRgb() self.color_changed.emit(self.current_item, self.current_item.color)
class LabelEditor(QWidget): """Widget for create label scheme.""" def __init__(self, settings: ViewSettings): super().__init__() self.settings = settings self.color_list = [] self.chosen = None self.prohibited_names = set(self.settings.label_color_dict.keys() ) # Prohibited name is added to reduce # probability of colormap cache collision self.color_picker = QColorDialog() self.color_picker.setWindowFlag(Qt.Widget) self.color_picker.setOptions(QColorDialog.DontUseNativeDialog | QColorDialog.NoButtons) self.add_color_btn = QPushButton("Add color") self.add_color_btn.clicked.connect(self.add_color) self.remove_color_btn = QPushButton("Remove last color") self.remove_color_btn.clicked.connect(self.remove_color) self.save_btn = QPushButton("Save") self.save_btn.clicked.connect(self.save) self.color_layout = QHBoxLayout() layout = QVBoxLayout() layout.addWidget(self.color_picker) btn_layout = QHBoxLayout() btn_layout.addWidget(self.add_color_btn) btn_layout.addWidget(self.remove_color_btn) btn_layout.addWidget(self.save_btn) layout.addLayout(btn_layout) layout.addLayout(self.color_layout) self.setLayout(layout) @Slot(list) def set_colors(self, colors: list): for _ in range(self.color_layout.count()): el = self.color_layout.takeAt(0) if el.widget(): el.widget().deleteLater() for color in colors: self.color_layout.addWidget(ColorShow(color, self)) def remove_color(self): if self.color_layout.count(): el = self.color_layout.takeAt(self.color_layout.count() - 1) el.widget().deleteLater() def add_color(self): color = self.color_picker.currentColor() self.color_layout.addWidget( ColorShow([color.red(), color.green(), color.blue()], self)) def get_colors(self): count = self.color_layout.count() return [ self.color_layout.itemAt(i).widget().color for i in range(count) ] def save(self): count = self.color_layout.count() if not count: return rand_name = custom_name_generate(self.prohibited_names, self.settings.label_color_dict) self.prohibited_names.add(rand_name) self.settings.label_color_dict[rand_name] = self.get_colors() def mousePressEvent(self, e: QMouseEvent): child = self.childAt(e.pos()) if not isinstance(child, ColorShow): self.chosen = None return self.chosen = child def mouseMoveEvent(self, e: QMouseEvent): if self.chosen is None: return index = self.color_layout.indexOf(self.chosen) index2 = int(e.x() / self.width() * self.color_layout.count() + 0.5) if index2 != index: self.color_layout.insertWidget(index2, self.chosen) def mouseReleaseEvent(self, e: QMouseEvent): self.chosen = None
def choose_color(self): color = QColorDialog.getColor(self._color, self.parentWidget()) if color.isValid(): self.set_color(color)
def ButColorClick(self): color = QColorDialog.getColor() if color.isValid(): self.CurrentSelColor = color style = 'QPushButton {background-color:' + color.name() + '}' self.ButColor.setStyleSheet(style)
def choose_color(self): color = QColorDialog.getColor(self._color, self.parentWidget(), 'Select Color', QColorDialog.ShowAlphaChannel) if color.isValid(): self.set_color(color)
def on_edit_color(self): c = [int(255 * i) for i in self.text_col] #print('c =', c) col = QColorDialog.getColor(QtGui.QColor(*c), self, "Choose a text color") self.color.SetColor(col)
def setColour(self, widget, initialColour): colour = QColorDialog.getColor(QColor(initialColour), self) if colour.isValid(): widget.setColour(colour)
def _on_clicked(self): dialog = QColorDialog(self.color()) if not dialog.exec_(): return self.setColor(dialog.selectedColor())