Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP, ENH: Button to change the background color #9000

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions mne/viz/_brain/_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1368,9 +1368,16 @@ def _save_image(fname, img):
callback=partial(_save_image, img=img)
)

def _set_background(self, val):
self.plotter.background_color = val

def _configure_tool_bar(self):
self._renderer._tool_bar_load_icons()
self._renderer._tool_bar_initialize()
self._renderer._tool_bar_add_color_picker(
name="background",
func=self._set_background,
)
self._renderer._tool_bar_add_button(
name="screenshot",
desc="Take a screenshot",
Expand Down
4 changes: 4 additions & 0 deletions mne/viz/backends/_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,10 @@ def _tool_bar_add_button(self, name, desc, func, icon_name=None):
def _tool_bar_update_button_icon(self, name, icon_name):
pass

@abstractmethod
def _tool_bar_add_color_picker(self, name, func):
pass

@abstractmethod
def _tool_bar_add_text(self, name, value, placeholder):
pass
Expand Down
3 changes: 3 additions & 0 deletions mne/viz/backends/_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ def _tool_bar_add_button(self, name, desc, func, icon_name=None):
_ipy_add_widget(self.tool_bar, widget)
self.actions[name] = widget

def _tool_bar_add_color_picker(self, name, func):
pass

def _tool_bar_update_button_icon(self, name, icon_name):
self.actions[name].icon = self.icons[icon_name]

Expand Down
52 changes: 49 additions & 3 deletions mne/viz/backends/_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

from contextlib import contextmanager

import numpy as np
import pyvista

from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QIcon
from PyQt5.QtGui import QIcon, QColor
from PyQt5.QtWidgets import (QComboBox, QDockWidget, QDoubleSpinBox, QGroupBox,
QHBoxLayout, QLabel, QToolButton,
QHBoxLayout, QLabel, QPushButton, QToolButton,
QSlider, QSpinBox, QVBoxLayout, QWidget,
QSizePolicy, QScrollArea, QStyle,
QStyleOptionSlider)
QStyleOptionSlider, QColorDialog)

from ._pyvista import _PyVistaRenderer
from ._pyvista import (_close_all, _close_3d_figure, _check_3d_figure, # noqa: F401,E501 analysis:ignore
Expand Down Expand Up @@ -135,6 +136,45 @@ def _dock_add_group_box(self, name, layout=None):
return hlayout


# from https://github.com/GuillaumeFavelier/blockbuilder
class QColorButton(QPushButton):
"""Select a color interactively."""

colorChanged = pyqtSignal(list)

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 _rgb2str(self, color, is_int=False):
if not is_int:
color = np.asarray(color) * 255
color = color.astype(np.uint8)
return str(tuple(color))

def _qrgb2rgb(self, color):
return (
color.red(),
color.green(),
color.blue()
)

def setColor(self, color, is_int=True):
"""Set the current button color."""
if isinstance(color, QColor):
color = self._qrgb2rgb(color)
color = np.asarray(color)
self.setStyleSheet(
"#ColorButton{background-color: rgb" +
self._rgb2str(color, is_int) + "}")
if is_int:
self.colorChanged.emit(list(color / 255.))


class QFloatSlider(QSlider):
"""Slider that handles float values."""

Expand Down Expand Up @@ -236,6 +276,12 @@ def _tool_bar_add_button(self, name, desc, func, icon_name=None):
icon = self.icons[icon_name]
self.actions[name] = self.tool_bar.addAction(icon, desc, func)

def _tool_bar_add_color_picker(self, name, func):
widget = QColorButton()
widget.colorChanged.connect(func)
self.tool_bar.addWidget(widget)
self.actions[name] = widget

def _tool_bar_update_button_icon(self, name, icon_name):
self.actions[name].setIcon(self.icons[icon_name])

Expand Down