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

Make image view mixins into abstract base classes #2374

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion mantidimaging/gui/utility/qt_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
from __future__ import annotations

import os
from abc import ABCMeta
from enum import IntEnum, auto
from logging import getLogger
from typing import Any
from collections.abc import Callable

from PyQt5 import uic # type: ignore
from PyQt5 import uic, sip # type: ignore
from PyQt5.QtCore import QObject, Qt
from PyQt5.QtWidgets import (QLabel, QLineEdit, QSpinBox, QDoubleSpinBox, QCheckBox, QWidget, QSizePolicy, QAction,
QMenu, QPushButton, QLayout, QFileDialog, QComboBox)
Expand Down Expand Up @@ -266,3 +267,8 @@ def populate_menu(menu: QMenu, actions_list: list[QAction]) -> None:
action = QAction(menu_text, menu)
action.triggered.connect(func)
menu.addAction(action)


class _metaclass_sip_abc(sip.wrappertype, ABCMeta):
"""Used for Mixins to avoid metaclass conflicts"""
...
12 changes: 9 additions & 3 deletions mantidimaging/gui/widgets/auto_colour_menu/auto_color_menu.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Copyright (C) 2024 ISIS Rutherford Appleton Laboratory UKRI
# SPDX - License - Identifier: GPL-3.0-or-later
from __future__ import annotations

from abc import abstractmethod, ABC
from typing import TYPE_CHECKING

from PyQt5.QtWidgets import QAction

from mantidimaging.gui.utility.qt_helpers import _metaclass_sip_abc
from mantidimaging.gui.widgets.palette_changer.view import PaletteChangerView

if TYPE_CHECKING:
Expand All @@ -13,21 +16,24 @@
from PyQt5.QtWidgets import QWidget


class AutoColorMenu:
class AutoColorMenu(ABC, metaclass=_metaclass_sip_abc):
"""
Mixin class to be used with MIImageView and MIMiniImageView
"""

def __init__(self) -> None:
super().__init__()
self.auto_color_action: QAction | None = None

@property
@abstractmethod
def histogram(self) -> HistogramLUTItem:
raise NotImplementedError('Required histogram property not implemented')
...

@property
@abstractmethod
def image_data(self) -> np.ndarray | None:
raise NotImplementedError('Required image_data property not implemented')
...

@property
def other_histograms(self) -> list[HistogramLUTItem]:
Expand Down
10 changes: 7 additions & 3 deletions mantidimaging/gui/widgets/bad_data_overlay/bad_data_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
# SPDX - License - Identifier: GPL-3.0-or-later
from __future__ import annotations

from abc import abstractmethod, ABC
from collections.abc import Callable

import numpy as np
from pyqtgraph import ColorMap, ImageItem, ViewBox

from mantidimaging.gui.utility.qt_helpers import _metaclass_sip_abc
from mantidimaging.gui.widgets.indicator_icon.view import IndicatorIconView
from mantidimaging.core.utility import finder

Expand Down Expand Up @@ -54,7 +56,7 @@ def clear(self) -> None:
self.overlay.clear()


class BadDataOverlay:
class BadDataOverlay(ABC, metaclass=_metaclass_sip_abc):
"""
Mixin class to be used with MIImageView and MIMiniImageView
"""
Expand All @@ -69,12 +71,14 @@ def __init__(self) -> None:
self.sigTimeChanged.connect(self.check_for_bad_data)

@property
@abstractmethod
def image_item(self) -> ImageItem:
raise NotImplementedError
...

@property
@abstractmethod
def viewbox(self) -> ViewBox:
raise NotImplementedError
...

def enable_nan_check(self, enable: bool = True, actions: list[tuple[str, Callable]] | None = None) -> None:
if enable:
Expand Down
Loading