<commit_before>import sys
import os
from PyQt5 import QtCore, QtWidgets
import vtk
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
import chigger
import mooseutils
from ExodusPlugin import ExodusPlugin

class RetinaQVTKRenderWindowInteractor(QVTKRenderWindowInteractor):
    """
    Currently VTK7.1 and Qt5 do not work correctly on retina displays:
    http://public.kitware.com/pipermail/vtk-developers/2017-February/034738.html

    However, creating a custom resizeEvent method and wrapping the QVTKRenderWindowInteractor object
    in a QFrame allowed it to work for now. The idea for this wrapping came from:
        https://github.com/siudej/Eigenvalues/blob/master/qvtk.py
    """
    def resizeEvent(self, event):
        """
        Double the size on retina displays.
        This is not the right way to do it, but this works for framed widgets.
        We also need to modify all mouse events to adjust the interactor's
        center (e.g. for joystick mode).
        """
        super(RetinaQVTKRenderWindowInteractor, self).resizeEvent(event)

        ratio = self.devicePixelRatio()
        w = self.width()
        h = self.height()
        if (self.parent() is not None) and (w <= self.parent().width()):
            self.resize(ratio*self.size())
        self.GetRenderWindow().SetSize(ratio*w, ratio*h)
        self.GetRenderWindow().GetInteractor().SetSize(ratio*w, ratio*h)
        self.GetRenderWindow().GetInteractor().ConfigureEvent()
        self.update()


class VTKWindowPlugin(QtWidgets.QFrame, ExodusPlugin):
    """
    Plugin for volume rendering of ExodusII data with VTK via chigger.
    """

    #: pyqtSignal: Emitted when the result is first rendered
    windowCreated = QtCore.pyqtSignal(chigger.exodus.ExodusReader, chigger.exodus.ExodusResult, chigger.RenderWindow)

    #: pyqtSignal: Emitted with the window has been updated
    windowUpdated = QtCore.pyqtSignal()

    #: pyqtSignal: Emitted when the window is reset/cleared
    windowReset = QtCore.pyqtSignal()

    #: pyqtSignal: Emitted when the camera for this window has changed
    cameraChanged = QtCore.pyqtSignal(vtk.vtkCamera)

    def __init__(self, size=None, **kwargs):
        super(VTKWindowPlugin, self).__init__(**kwargs)

        # Setup widget
        self.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.MinimumExpanding)
        self.setMainLayoutName('WindowLayout')

        # Create the QVTK interactor
        if self.devicePixelRatio() > 1:
            self.__qvtkinteractor = RetinaQVTKRenderWindowInteractor(self)
        else:
            self.__qvtkinteractor = QVTKRenderWindowInteractor(self)

        # Member variables
        self._highlight = None
        self._reader = None
        self._result = None
        self._filename = str()
        self._initialized = False
        self._run_start_time = None
        self._window = chigger.RenderWindow(vtkwindow=self.__qvtkinteractor.GetRenderWindow(), vtkinteractor=self.__qvtkinteractor.GetRenderWindow().GetInteractor(), size=size)

        # Set to True when the window needs to be reset (e.g., when the input file was changed)
        self._reset_required = False

        # If size is provided, restrict the window
        if size != None:
            self.setFixedSize(QtCore.QSize(*size))

        # Define timers for initialization and updating data
        self._timers = dict()
        for name in ['update', 'initialize']:
            self._timers[name] = QtCore.QTimer()
            self._timers[name].setInterval(1000)
        self._timers['update'].timeout.connect(self.onWindowRequiresUpdate)
        self._timers['initialize'].timeout.connect(self.onReloadWindow)

        # Create a "tight" layout and add the QVTK widget
        self._layout = QtWidgets.QHBoxLayout()
        self._layout.setContentsMargins(0, 0, 0, 0)
        self._layout.addWidget(self.__qvtkinteractor)
        self.setLayout(self._layout)

        # Display items for when no file exists
        self._peacock_logo = chigger.annotations.ImageAnnotation(filename='peacock.png', opacity=0.33)
        self._peacock_text = chigger.annotations.TextAnnotation(text='No file selected.', justification='center', font_size=18)

        self.setup()

    def reset(self):
        """
        Clears the VTK windows and restarts the initialize timer.
        """
        self._window.clear()
        self._window.update()
        self._initialized = False
        self._reset_required = False
        self._adjustTimers(start=['initialize'], stop=['update'])
        self.windowReset.emit()

    def onReloadWindow(self):
        """
        Reloads the current file.
        """
        self.onFileChanged(self._filename)

    def onFileChanged(self, filename=str()):
        """
        Initialize the VTK window to read and display a file.

        If the file is not valid a timer is started to continue to attempt to initialize the window. When a result
        is found and rendered, the windowCreated signal is emitted containing the chigger reader and result.

        Inputs:
            filename[str]: The filename to open.
        """
        if (filename != self._filename) or self._reset_required:
            self.reset()

        # Do nothing if the widget is not visible or the file doesn't exist
        self._filename = filename
        file_exists = os.path.exists(self._filename)
        if not self.isVisible():
            return<commit_msg>Reset highlighting during a VTKWindowPlugin reset<commit_after>import sys
import os
from PyQt5 import QtCore, QtWidgets
import vtk
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
import chigger
import mooseutils
from ExodusPlugin import ExodusPlugin

class RetinaQVTKRenderWindowInteractor(QVTKRenderWindowInteractor):
    """
    Currently VTK7.1 and Qt5 do not work correctly on retina displays:
    http://public.kitware.com/pipermail/vtk-developers/2017-February/034738.html

    However, creating a custom resizeEvent method and wrapping the QVTKRenderWindowInteractor object
    in a QFrame allowed it to work for now. The idea for this wrapping came from:
        https://github.com/siudej/Eigenvalues/blob/master/qvtk.py
    """
    def resizeEvent(self, event):
        """
        Double the size on retina displays.
        This is not the right way to do it, but this works for framed widgets.
        We also need to modify all mouse events to adjust the interactor's
        center (e.g. for joystick mode).
        """
        super(RetinaQVTKRenderWindowInteractor, self).resizeEvent(event)

        ratio = self.devicePixelRatio()
        w = self.width()
        h = self.height()
        if (self.parent() is not None) and (w <= self.parent().width()):
            self.resize(ratio*self.size())
        self.GetRenderWindow().SetSize(ratio*w, ratio*h)
        self.GetRenderWindow().GetInteractor().SetSize(ratio*w, ratio*h)
        self.GetRenderWindow().GetInteractor().ConfigureEvent()
        self.update()


class VTKWindowPlugin(QtWidgets.QFrame, ExodusPlugin):
    """
    Plugin for volume rendering of ExodusII data with VTK via chigger.
    """

    #: pyqtSignal: Emitted when the result is first rendered
    windowCreated = QtCore.pyqtSignal(chigger.exodus.ExodusReader, chigger.exodus.ExodusResult, chigger.RenderWindow)

    #: pyqtSignal: Emitted with the window has been updated
    windowUpdated = QtCore.pyqtSignal()

    #: pyqtSignal: Emitted when the window is reset/cleared
    windowReset = QtCore.pyqtSignal()

    #: pyqtSignal: Emitted when the camera for this window has changed
    cameraChanged = QtCore.pyqtSignal(vtk.vtkCamera)

    def __init__(self, size=None, **kwargs):
        super(VTKWindowPlugin, self).__init__(**kwargs)

        # Setup widget
        self.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.MinimumExpanding)
        self.setMainLayoutName('WindowLayout')

        # Create the QVTK interactor
        if self.devicePixelRatio() > 1:
            self.__qvtkinteractor = RetinaQVTKRenderWindowInteractor(self)
        else:
            self.__qvtkinteractor = QVTKRenderWindowInteractor(self)

        # Member variables
        self._highlight = None
        self._reader = None
        self._result = None
        self._filename = str()
        self._initialized = False
        self._run_start_time = None
        self._window = chigger.RenderWindow(vtkwindow=self.__qvtkinteractor.GetRenderWindow(), vtkinteractor=self.__qvtkinteractor.GetRenderWindow().GetInteractor(), size=size)

        # Set to True when the window needs to be reset (e.g., when the input file was changed)
        self._reset_required = False

        # If size is provided, restrict the window
        if size != None:
            self.setFixedSize(QtCore.QSize(*size))

        # Define timers for initialization and updating data
        self._timers = dict()
        for name in ['update', 'initialize']:
            self._timers[name] = QtCore.QTimer()
            self._timers[name].setInterval(1000)
        self._timers['update'].timeout.connect(self.onWindowRequiresUpdate)
        self._timers['initialize'].timeout.connect(self.onReloadWindow)

        # Create a "tight" layout and add the QVTK widget
        self._layout = QtWidgets.QHBoxLayout()
        self._layout.setContentsMargins(0, 0, 0, 0)
        self._layout.addWidget(self.__qvtkinteractor)
        self.setLayout(self._layout)

        # Display items for when no file exists
        self._peacock_logo = chigger.annotations.ImageAnnotation(filename='peacock.png', opacity=0.33)
        self._peacock_text = chigger.annotations.TextAnnotation(text='No file selected.', justification='center', font_size=18)

        self.setup()

    def reset(self):
        """
        Clears the VTK windows and restarts the initialize timer.
        """
        self._window.clear()
        self._window.update()
        self._initialized = False
        self._reset_required = False
        self._highlight = None
        self._adjustTimers(start=['initialize'], stop=['update'])
        self.windowReset.emit()

    def onReloadWindow(self):
        """
        Reloads the current file.
        """
        self.onFileChanged(self._filename)

    def onFileChanged(self, filename=str()):
        """
        Initialize the VTK window to read and display a file.

        If the file is not valid a timer is started to continue to attempt to initialize the window. When a result
        is found and rendered, the windowCreated signal is emitted containing the chigger reader and result.

        Inputs:
            filename[str]: The filename to open.
        """
        if (filename != self._filename) or self._reset_required:
            self.reset()

        # Do nothing if the widget is not visible or the file doesn't exist
        self._filename = filename
        file_exists = os.path.exists(self._filename)
        if not self.isVisible():
            return
{'fraction_non_alphanumeric': 0.0887718979758555, 'fraction_numerical': 0.0050830534628301715, 'mean_word_length': 2.8470670391061454, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '412188', 'n_tokens_mistral': 3160, 'n_tokens_neox': 3017, 'n_words': 844}