|
| 1 | +# Copyright (C) 2024 - 2025 ANSYS, Inc. and/or its affiliates. |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | +"""Provides the dark mode button widget for the PyAnsys plotter.""" |
| 23 | +from pathlib import Path |
| 24 | +from typing import TYPE_CHECKING |
| 25 | + |
| 26 | +from vtk import vtkActor, vtkButtonWidget, vtkPNGReader |
| 27 | + |
| 28 | +from ansys.tools.visualization_interface.backends.pyvista.widgets.widget import PlotterWidget |
| 29 | + |
| 30 | +if TYPE_CHECKING: |
| 31 | + from ansys.tools.visualization_interface.backends.pyvista.pyvista import Plotter |
| 32 | + |
| 33 | + |
| 34 | +class DarkModeButton(PlotterWidget): |
| 35 | + """Provides the dark mode widget for the Visualization Interface Tool ``Plotter`` class. |
| 36 | +
|
| 37 | + Parameters |
| 38 | + ---------- |
| 39 | + plotter_helper : PlotterHelper |
| 40 | + Plotter to add the dark mode widget to. |
| 41 | + dark_mode : bool, optional |
| 42 | + Whether to activate the dark mode or not. |
| 43 | +
|
| 44 | + """ |
| 45 | + |
| 46 | + def __init__(self, plotter: "Plotter", dark_mode: bool = False) -> None: |
| 47 | + """Initialize the ``DarkModeButton`` class.""" |
| 48 | + # Call PlotterWidget ctor |
| 49 | + super().__init__(plotter._pl.scene) |
| 50 | + self._dark_mode = dark_mode |
| 51 | + # Initialize variables |
| 52 | + self._actor: vtkActor = None |
| 53 | + self._plotter = plotter |
| 54 | + self._button: vtkButtonWidget = self._plotter._pl.scene.add_checkbox_button_widget( |
| 55 | + self.callback, position=(43, 10), size=30, border_size=3 |
| 56 | + ) |
| 57 | + self.update() |
| 58 | + |
| 59 | + def callback(self, state: bool) -> None: |
| 60 | + """Remove or add the dark mode widget actor upon click. |
| 61 | +
|
| 62 | + Parameters |
| 63 | + ---------- |
| 64 | + state : bool |
| 65 | + Whether the state of the button, which is inherited from PyVista, is active. |
| 66 | +
|
| 67 | + """ |
| 68 | + if not self._dark_mode: |
| 69 | + self._plotter.scene.set_background("black") |
| 70 | + for widget in self._plotter._widgets: |
| 71 | + widget._dark_mode = True |
| 72 | + widget.update() |
| 73 | + |
| 74 | + # Using internal method to force the render to update |
| 75 | + self._plotter.scene.iren._mouse_right_button_click() |
| 76 | + else: |
| 77 | + self._plotter.scene.set_background("white") |
| 78 | + for widget in self._plotter._widgets: |
| 79 | + widget._dark_mode = False |
| 80 | + widget.update() |
| 81 | + |
| 82 | + # Using internal method to force the render to update |
| 83 | + self._plotter.scene.iren._mouse_right_button_click() |
| 84 | + |
| 85 | + def update(self) -> None: |
| 86 | + """Define the dark mode widget button parameters.""" |
| 87 | + if self._dark_mode: |
| 88 | + is_inv = "_inv" |
| 89 | + else: |
| 90 | + is_inv = "" |
| 91 | + |
| 92 | + show_vr = self._button.GetRepresentation() |
| 93 | + show_vison_icon_file = Path( |
| 94 | + Path(__file__).parent / "_images" / f"dark_mode{is_inv}.png" |
| 95 | + ) |
| 96 | + show_visoff_icon_file = Path( |
| 97 | + Path(__file__).parent / "_images" / f"dark_mode{is_inv}.png" |
| 98 | + ) |
| 99 | + show_r_on = vtkPNGReader() |
| 100 | + show_r_on.SetFileName(show_vison_icon_file) |
| 101 | + show_r_on.Update() |
| 102 | + image_on = show_r_on.GetOutput() |
| 103 | + |
| 104 | + show_r_off = vtkPNGReader() |
| 105 | + show_r_off.SetFileName(show_visoff_icon_file) |
| 106 | + show_r_off.Update() |
| 107 | + image_off = show_r_off.GetOutput() |
| 108 | + |
| 109 | + |
| 110 | + show_vr.SetButtonTexture(0, image_off) |
| 111 | + show_vr.SetButtonTexture(1, image_on) |
0 commit comments