diff --git a/src/Model/Windowing.py b/src/Model/Windowing.py index a0082453..42a04ce8 100644 --- a/src/Model/Windowing.py +++ b/src/Model/Windowing.py @@ -23,17 +23,17 @@ def windowing_model(text, init): window = windowing_limits[0] level = windowing_limits[1] - windowing_model_direct(window, level, init) + windowing_model_direct(level, window, init) def windowing_model_direct(level, window, init): """ - Function triggered when a window is selected from the menu, - or when the windowing slider bars are adjusted - :param level: The desired level - :param window: The desired window - :param init: list of bool to determine which views are chosen - """ + Function triggered when a window is selected from the menu, + or when the windowing slider bars are adjusted + :param level: The desired level + :param window: The desired window + :param init: list of bool to determine which views are chosen + """ patient_dict_container = PatientDictContainer() moving_dict_container = MovingDictContainer() pt_ct_dict_container = PTCTDictContainer() diff --git a/src/View/mainpage/MainPage.py b/src/View/mainpage/MainPage.py index 866c7a1d..68cd49df 100644 --- a/src/View/mainpage/MainPage.py +++ b/src/View/mainpage/MainPage.py @@ -103,6 +103,8 @@ def setup_actions(self): self.toolbar = Toolbar(self.action_handler) self.main_window_instance.addToolBar( QtCore.Qt.TopToolBarArea, self.toolbar) + self.windowing_slider.set_action_handler( + self.action_handler) self.main_window_instance.setWindowTitle("OnkoDICOM") def setup_central_widget(self): diff --git a/src/View/mainpage/WindowingSlider.py b/src/View/mainpage/WindowingSlider.py index 3a4cee1a..acace08e 100644 --- a/src/View/mainpage/WindowingSlider.py +++ b/src/View/mainpage/WindowingSlider.py @@ -37,10 +37,11 @@ class WindowingSlider(QWidget): def __init__(self, width=50): """ Initialise the slider - :pram width: the fixed width of the widget + :param width: the fixed width of the widget """ super().__init__() + self.action_handler = None if WindowingSlider.SINGLETON is None: WindowingSlider.SINGLETON = self set_windowing_slider(self) @@ -92,7 +93,7 @@ def __init__(self, width=50): # Get the values for window and level from the dict patient_dict_container = PatientDictContainer() - # print(patient_dict_container.get("dict_windowing")) # testing + print(patient_dict_container.get("dict_windowing")) # testing windowing_limits = patient_dict_container.get("dict_windowing")['Normal'] # Set window and level to the new values @@ -111,6 +112,14 @@ def __init__(self, width=50): # Test self.set_density_histogram(gen_random_histogram()) + def set_action_handler(self, action_handler): + """ + Sets the action handler. + The action handler can call update_views() + :param action_handler: the action handler + """ + self.action_handler = action_handler + def resizeEvent(self, event): self.histogram.setPlotArea( QtCore.QRectF(0, 0, self.fixed_width, event.size().height())) @@ -132,12 +141,22 @@ def window_to_index(self, val): Converts a window value to a slider index :param val: a 0-2000 value """ - normalized_val = val / 2000 - # print(normalized_val) + normalized_val = val / 4096 + print(normalized_val) index = ceil(self.slider_density * (1 - normalized_val)) - 1 - # print(index) + print(index) return index + def index_to_window(self, index): + """ + Converts a slider index to a window value + :param index: a 0-2000 value + """ + + percent = index / self.slider_density + val = round((1 - percent) * 4096) + return val + def set_bars_from_window(self, window, level): """ Triggered when the user selects a windowing preset. @@ -170,6 +189,10 @@ def update_bar(self, index, top_bar=True): :param top_bar: whether to move the top or bottom bar """ + # Clamp index to range + index = max(index, 0) + index = min(index, self.slider_density-1) + if top_bar: self.slider_bars[self.top].setColor("white") self.top = index @@ -251,9 +274,17 @@ def mouse_release(self, event): False, False, False] - level = 100 - window = 100 - # windowing_model_direct(level, window, send) + + top_bar = self.index_to_window(self.top) + bottom_bar = self.index_to_window(self.bottom) + + level = (top_bar + bottom_bar) * 0.5 + window = 2 * (bottom_bar - level) + level = level - 1000 + + windowing_model_direct(level, window, send) + if self.action_handler is not None: + self.action_handler.update_views() def update_bar_position(self, event): if self.selected_bar == "middle": diff --git a/test/test_view_dicom_view.py b/test/test_view_dicom_view.py index 1b43dbd3..f2cc667a 100644 --- a/test/test_view_dicom_view.py +++ b/test/test_view_dicom_view.py @@ -74,8 +74,8 @@ def test_object(): def test_one_view_handling(qtbot, test_object, init_config): test_object.main_window.show() test_object.main_window.action_handler.action_one_view.trigger() - assert isinstance(test_object.main_window.dicom_single_view, DicomView) is True - assert test_object.main_window.dicom_view.currentWidget() == test_object.main_window.dicom_single_view + assert isinstance(test_object.main_window.dicom_single_view_widget, DicomView) is True + assert test_object.main_window.dicom_view.currentWidget() == test_object.main_window.dicom_single_view_widget def test_one_view_zoom(qtbot, test_object, init_config): @@ -95,8 +95,8 @@ def test_four_view_handling(qtbot, test_object, init_config): assert isinstance(test_object.main_window.dicom_axial_view, DicomView) is True assert isinstance(test_object.main_window.dicom_sagittal_view, DicomView) is True assert isinstance(test_object.main_window.dicom_coronal_view, DicomView) is True - assert isinstance(test_object.main_window.dicom_four_views_layout, QGridLayout) is True - assert test_object.main_window.dicom_view.currentWidget() == test_object.main_window.dicom_four_views + assert isinstance(test_object.main_window.dicom_four_views_slider_layout, QGridLayout) is True + assert test_object.main_window.dicom_view.currentWidget() == test_object.main_window.dicom_four_views_slider assert test_object.main_window.dicom_axial_view.pos().x(), test_object.main_window.dicom_axial_view.pos().y() == ( 0, 0) assert test_object.main_window.dicom_sagittal_view.pos().x(), \