class MainWindow(QMainWindow): def __init__(self, window_width=None, window_height=None): super(MainWindow, self).__init__() if window_width != None and window_height != None: self.window_width = window_width self.window_height = window_height self.resize(window_width, window_height) self.stack_of_widget = QStackedWidget() self.central_widget = QWidget() self.main_layout = QVBoxLayout() self.username = "" # ui widgets self.message_visable_field = QTextEdit() self.message_input_field = QTextEdit() self.send_message = QPushButton("Send my message!") self.login_screen = LoginScreen() self.login_screen.auth_button.clicked.connect(self.login_button_click) # show all widgets self.build_ui() def build_ui(self): self.message_visable_field.setFont(QFont("Roboto", 12)) self.message_visable_field.setReadOnly(True) self.message_input_field.setFont(QFont("Roboto", 11)) self.message_input_field.setPlaceholderText( "Input your message here...") self.message_input_field.setMinimumHeight(50) self.message_input_field.setMaximumHeight(50) self.send_message.setFont(QFont("Roboto", 11)) self.send_message.clicked.connect(self.send_message_button_clicked) self.main_layout.addWidget(self.message_visable_field) self.main_layout.addWidget(self.message_input_field) self.main_layout.addWidget(self.send_message) self.central_widget.setLayout(self.main_layout) self.stack_of_widget.addWidget(self.login_screen) self.setMaximumSize(self.minimumSize()) self.stack_of_widget.addWidget(self.central_widget) self.setCentralWidget(self.stack_of_widget) def send_message_button_clicked(self): """ send a client message to the server, and append to the list message. """ message = self.message_input_field.toPlainText() if message != '': # send message to the server r_type = RequestInfo(type_request="MessageRequest", request_ts=f"{datetime.now()}") r_data = MessageRequest(from_=self.username, to="all", message=message) request = Request(request=[r_type], data=[r_data]) client_worker.send(request.json()) self.message_visable_field.append('You: ' + message) self.message_input_field.clear() def login_button_click(self): self.username = self.login_screen.login_field.text() r_type = RequestInfo(type_request="AuthRequest", request_ts=f"{datetime.now()}") r_data = AuthRequest( login=f"{self.username}", password=f"{self.login_screen.password_field.text()}") request = Request(request=[r_type], data=[r_data]) client_worker.send(request.json()) @Slot(str) def recieved_message_handler(self, message): msg = json.loads(message) print(msg) response = Request(**msg) if response.request[0].type_request == "AuthResponse": data = AuthResponse(**response.data[0]) if data.access is True: self.show_main_screen() elif response.request[0].type_request == "MessageRequest": data = MessageRequest(**response.data[0]) if data.to == "all": self.message_visable_field.append( f"[{data.from_}]: {data.message}") def show_main_screen(self): self.stack_of_widget.setCurrentWidget(self.central_widget) self.setMinimumSize(self.window_width, self.window_height) self.resize(self.minimumSize()) def closeEvent(self, event): client_worker.close() network_thread.exit() network_thread.wait(500) if network_thread.isRunning() is True: network_thread.terminate()
class UIMainWindow: """ The central class responsible for initializing most of the values stored in the PatientDictContainer model and defining the visual layout of the main window of OnkoDICOM. No class has access to the attributes belonging to this class, except for the class's ActionHandler, which is used to trigger actions within the main window. Components of this class (i.e. QWidget child classes such as StructureTab, DicomView, DicomTree, etc.) should not be able to reference this class, and rather should exist independently and only be able to communicate with the PatientDictContainer model. If a component needs to communicate with another component, that should be accomplished by emitting signals within that components, and having the slots for those signals within this class (as demonstrated by the update_views() method of this class). If a class needs to trigger one of the actions defined in the ActionHandler, then the instance of the ActionHandler itself can safely be passed into the class. """ pyradi_trigger = QtCore.Signal(str, dict, str) # Connect to GUIController image_fusion_main_window = QtCore.Signal() def setup_ui(self, main_window_instance): self.main_window_instance = main_window_instance self.call_class = MainPageCallClass() self.add_on_options_controller = AddOptions(self) ########################################## # IMPLEMENTATION OF THE MAIN PAGE VIEW # ########################################## if platform.system() == 'Darwin': self.stylesheet_path = "res/stylesheet.qss" else: self.stylesheet_path = "res/stylesheet-win-linux.qss" self.stylesheet = open(resource_path(self.stylesheet_path)).read() window_icon = QIcon() window_icon.addPixmap(QPixmap(resource_path( "res/images/icon.ico")), QIcon.Normal, QIcon.Off) self.main_window_instance.setMinimumSize(1080, 700) self.main_window_instance.setObjectName("MainOnkoDicomWindowInstance") self.main_window_instance.setWindowIcon(window_icon) self.main_window_instance.setStyleSheet(self.stylesheet) self.setup_central_widget() self.setup_actions() # Create SUV2ROI object and connect signals self.suv2roi = SUV2ROI() self.suv2roi_progress_window = \ ProgressWindow(self.main_window_instance, QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint) self.suv2roi_progress_window.signal_loaded.connect( self.on_loaded_suv2roi) def setup_actions(self): if hasattr(self, 'toolbar'): self.main_window_instance.removeToolBar(self.toolbar) self.action_handler = ActionHandler(self) self.menubar = MenuBar(self.action_handler) self.main_window_instance.setMenuBar(self.menubar) self.toolbar = Toolbar(self.action_handler) self.main_window_instance.addToolBar( QtCore.Qt.TopToolBarArea, self.toolbar) self.main_window_instance.setWindowTitle("OnkoDICOM") def setup_central_widget(self): patient_dict_container = PatientDictContainer() self.central_widget = QtWidgets.QWidget() self.central_widget_layout = QVBoxLayout() self.patient_bar = PatientBar() splitter = QtWidgets.QSplitter(QtCore.Qt.Horizontal) # Left panel contains stuctures tab, isodoses tab, # and structure information self.left_panel = QtWidgets.QTabWidget() self.left_panel.setMinimumWidth(300) self.left_panel.setMaximumWidth(500) # Add structures tab to left panel if not hasattr(self, 'structures_tab'): self.structures_tab = StructureTab() self.structures_tab.request_update_structures.connect( self.update_views) else: self.structures_tab.update_ui() self.left_panel.addTab(self.structures_tab, "Structures") if patient_dict_container.has_modality("rtdose"): self.isodoses_tab = IsodoseTab() self.isodoses_tab.request_update_isodoses.connect( self.update_views) self.isodoses_tab.request_update_ui.connect( self.structures_tab.fixed_container_structure_modified) self.left_panel.addTab(self.isodoses_tab, "Isodoses") elif hasattr(self, 'isodoses_tab'): del self.isodoses_tab # Right panel contains the different tabs of DICOM view, DVH, # clinical data, DICOM tree self.right_panel = QtWidgets.QTabWidget() # Create a Dicom View containing single-slice and 3-slice views self.dicom_view = DicomStackedWidget(self.format_data) roi_color_dict = self.structures_tab.color_dict if hasattr( self, 'structures_tab') else None iso_color_dict = self.isodoses_tab.color_dict if hasattr( self, 'isodoses_tab') else None self.dicom_single_view = DicomAxialView( roi_color=roi_color_dict, iso_color=iso_color_dict) self.dicom_axial_view = DicomAxialView( is_four_view=True, roi_color=roi_color_dict, iso_color=iso_color_dict, metadata_formatted=True, cut_line_color=QtGui.QColor(255, 0, 0)) self.dicom_sagittal_view = DicomSagittalView( roi_color=roi_color_dict, iso_color=iso_color_dict, cut_line_color=QtGui.QColor(0, 255, 0)) self.dicom_coronal_view = DicomCoronalView( roi_color=roi_color_dict, iso_color=iso_color_dict, cut_line_color=QtGui.QColor(0, 0, 255)) self.three_dimension_view = DicomView3D() # Rescale the size of the scenes inside the 3-slice views self.dicom_axial_view.zoom = INITIAL_FOUR_VIEW_ZOOM self.dicom_sagittal_view.zoom = INITIAL_FOUR_VIEW_ZOOM self.dicom_coronal_view.zoom = INITIAL_FOUR_VIEW_ZOOM self.dicom_axial_view.update_view(zoom_change=True) self.dicom_sagittal_view.update_view(zoom_change=True) self.dicom_coronal_view.update_view(zoom_change=True) self.dicom_four_views = QWidget() self.dicom_four_views_layout = QGridLayout() for i in range(2): self.dicom_four_views_layout.setColumnStretch(i, 1) self.dicom_four_views_layout.setRowStretch(i, 1) self.dicom_four_views_layout.addWidget(self.dicom_axial_view, 0, 0) self.dicom_four_views_layout.addWidget(self.dicom_sagittal_view, 0, 1) self.dicom_four_views_layout.addWidget(self.dicom_coronal_view, 1, 0) self.dicom_four_views_layout.addWidget(self.three_dimension_view, 1, 1) self.dicom_four_views.setLayout(self.dicom_four_views_layout) self.dicom_view.addWidget(self.dicom_four_views) self.dicom_view.addWidget(self.dicom_single_view) self.dicom_view.setCurrentWidget(self.dicom_single_view) # Add DICOM View to right panel as a tab self.right_panel.addTab(self.dicom_view, "DICOM View") # Add PETVT View to right panel as a tab self.pet_ct_tab = PetCtView() self.right_panel.addTab(self.pet_ct_tab, "PET/CT View") # Add DVH tab to right panel as a tab if patient_dict_container.has_modality("rtdose"): self.dvh_tab = DVHTab() self.right_panel.addTab(self.dvh_tab, "DVH") elif hasattr(self, 'dvh_tab'): del self.dvh_tab # Add DICOM Tree View tab self.dicom_tree = DicomTreeView() self.right_panel.addTab(self.dicom_tree, "DICOM Tree") # Connect SUV2ROI signal to handler function self.dicom_single_view.suv2roi_signal.connect(self.perform_suv2roi) # Add clinical data tab self.call_class.display_clinical_data(self.right_panel) splitter.addWidget(self.left_panel) splitter.addWidget(self.right_panel) # Create footer self.footer = QtWidgets.QWidget() self.create_footer() # Set layout self.central_widget_layout.addWidget(self.patient_bar) self.central_widget_layout.addWidget(splitter) self.central_widget_layout.addWidget(self.footer) self.central_widget.setLayout(self.central_widget_layout) self.main_window_instance.setCentralWidget(self.central_widget) def create_footer(self): self.footer.setFixedHeight(15) layout_footer = QtWidgets.QHBoxLayout(self.footer) layout_footer.setContentsMargins(0, 0, 0, 0) label_footer = QtWidgets.QLabel("@OnkoDICOM2021") label_footer.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignRight) layout_footer.addWidget(label_footer) def update_views(self, update_3d_window=False): """ This function is a slot for signals to request the updating of the DICOM View and DVH tabs in order to reflect changes made by other components of the main window (for example, when a structure in the structures tab is selected, this method needs to be called in order for the DICOM view window to be updated to show the new region of interest. :param update_3d_window: a boolean to mark if 3d model needs to be updated """ self.dicom_single_view.update_view() self.dicom_axial_view.update_view() self.dicom_coronal_view.update_view() self.dicom_sagittal_view.update_view() if update_3d_window: self.three_dimension_view.update_view() if hasattr(self, 'dvh_tab'): self.dvh_tab.update_plot() if hasattr(self, 'pet_ct_tab'): if self.pet_ct_tab.initialised: self.pet_ct_tab.update_view() if hasattr(self, 'image_fusion_view'): if self.image_fusion_view_axial is not None: self.image_fusion_single_view.update_view() self.image_fusion_view_axial.update_view() self.image_fusion_view_coronal.update_view() self.image_fusion_view_sagittal.update_view() def toggle_cut_lines(self): if self.dicom_axial_view.horizontal_view is None or \ self.dicom_axial_view.vertical_view is None or \ self.dicom_coronal_view.horizontal_view is None or \ self.dicom_coronal_view.vertical_view is None or \ self.dicom_sagittal_view.horizontal_view is None or \ self.dicom_sagittal_view.vertical_view is None: self.dicom_axial_view.set_views(self.dicom_coronal_view, self.dicom_sagittal_view) self.dicom_coronal_view.set_views(self.dicom_axial_view, self.dicom_sagittal_view) self.dicom_sagittal_view.set_views(self.dicom_axial_view, self.dicom_coronal_view) else: self.dicom_axial_view.set_views(None, None) self.dicom_coronal_view.set_views(None, None) self.dicom_sagittal_view.set_views(None, None) if hasattr(self, 'image_fusion_view'): if self.image_fusion_view is not None: if self.image_fusion_view_axial.horizontal_view is None or \ self.image_fusion_view_axial.vertical_view is None or \ self.image_fusion_view_coronal.horizontal_view is None \ or self.image_fusion_view_coronal.vertical_view is None \ or \ self.image_fusion_view_sagittal.horizontal_view is None \ or \ self.image_fusion_view_sagittal.vertical_view is None: self.image_fusion_view_axial.set_views( self.image_fusion_view_coronal, self.image_fusion_view_sagittal) self.image_fusion_view_coronal.set_views( self.image_fusion_view_axial, self.image_fusion_view_sagittal) self.image_fusion_view_sagittal.set_views( self.image_fusion_view_axial, self.image_fusion_view_coronal) else: self.image_fusion_view_axial.set_views(None, None) self.image_fusion_view_coronal.set_views(None, None) self.image_fusion_view_sagittal.set_views(None, None) def zoom_in(self, is_four_view, image_reg_single, image_reg_four): """ This function calls the zooming in function on the four view's views or the single view depending on what view is showing on screen. is_four_view: Whether the four view is showing """ if is_four_view: self.dicom_axial_view.zoom_in() self.dicom_coronal_view.zoom_in() self.dicom_sagittal_view.zoom_in() else: self.dicom_single_view.zoom_in() if image_reg_single: self.image_fusion_single_view.zoom_in() if image_reg_four: self.image_fusion_view_axial.zoom_in() self.image_fusion_view_coronal.zoom_in() self.image_fusion_view_sagittal.zoom_in() if self.pet_ct_tab.initialised: self.pet_ct_tab.zoom_in() def zoom_out(self, is_four_view, image_reg_single, image_reg_four): """ This function calls the zooming out function on the four view's views or the single view depending on what view is showing on screen. is_four_view: Whether the four view is showing """ if is_four_view: self.dicom_axial_view.zoom_out() self.dicom_coronal_view.zoom_out() self.dicom_sagittal_view.zoom_out() else: self.dicom_single_view.zoom_out() if image_reg_single: self.image_fusion_single_view.zoom_out() if image_reg_four: self.image_fusion_view_axial.zoom_out() self.image_fusion_view_coronal.zoom_out() self.image_fusion_view_sagittal.zoom_out() if self.pet_ct_tab.initialised: self.pet_ct_tab.zoom_out() def format_data(self, size): """ This function is used to update the meta data's font size and margin based on the height and width of the viewports. size: The size of the DicomStackedWidget """ self.dicom_axial_view.format_metadata(size) def create_image_fusion_tab(self): """ This function is used to create the tab for image fusion. Function checks if the moving dict container contains rtss to load rtss. Views are created and stacked into three window view. """ # Set a flag for Zooming self.action_handler.has_image_registration_four = True # Instance of Moving Model moving_dict_container = MovingDictContainer() if moving_dict_container.has_modality("rtss"): if len(self.structures_tab.rois.items()) == 0: self.structures_tab.update_ui(moving=True) # else: # TODO: Display both ROIs in the same tab self.image_fusion_single_view \ = ImageFusionAxialView() self.image_fusion_view = QStackedWidget() self.image_fusion_view_axial = ImageFusionAxialView( metadata_formatted=False, cut_line_color=QtGui.QColor(255, 0, 0)) self.image_fusion_view_sagittal = ImageFusionSagittalView( cut_line_color=QtGui.QColor(0, 255, 0)) self.image_fusion_view_coronal = ImageFusionCoronalView( cut_line_color=QtGui.QColor(0, 0, 255)) self.image_fusion_roi_transfer_option_view = ROITransferOptionView( self.structures_tab.fixed_container_structure_modified, self.structures_tab.moving_container_structure_modified) # Rescale the size of the scenes inside the 3-slice views self.image_fusion_view_axial.zoom = INITIAL_FOUR_VIEW_ZOOM self.image_fusion_view_sagittal.zoom = INITIAL_FOUR_VIEW_ZOOM self.image_fusion_view_coronal.zoom = INITIAL_FOUR_VIEW_ZOOM self.image_fusion_view_axial.update_view(zoom_change=True) self.image_fusion_view_sagittal.update_view(zoom_change=True) self.image_fusion_view_coronal.update_view(zoom_change=True) self.image_fusion_four_views = QWidget() self.image_fusion_four_views_layout = QGridLayout() for i in range(2): self.image_fusion_four_views_layout.setColumnStretch(i, 1) self.image_fusion_four_views_layout.setRowStretch(i, 1) self.image_fusion_four_views_layout.addWidget( self.image_fusion_view_axial, 0, 0) self.image_fusion_four_views_layout.addWidget( self.image_fusion_view_sagittal, 0, 1) self.image_fusion_four_views_layout.addWidget( self.image_fusion_view_coronal, 1, 0) self.image_fusion_four_views_layout.addWidget( self.image_fusion_roi_transfer_option_view, 1, 1 ) self.image_fusion_four_views.setLayout( self.image_fusion_four_views_layout) self.image_fusion_view.addWidget(self.image_fusion_four_views) self.image_fusion_view.addWidget(self.image_fusion_single_view) self.image_fusion_view.setCurrentWidget(self.image_fusion_four_views) # Add Image Fusion Tab self.right_panel.addTab(self.image_fusion_view, "Image Fusion") self.right_panel.setCurrentWidget(self.image_fusion_view) # Update the Add On Option GUI self.add_on_options_controller.update_ui() def perform_suv2roi(self): """ Performs the SUV2ROI process. """ # Get patient weight - needs to run first as GUI cannot run in # threads, like the ProgressBar patient_dict_container = PatientDictContainer() dataset = patient_dict_container.dataset[0] self.suv2roi.get_patient_weight(dataset) if self.suv2roi.patient_weight is None: return # Start the SUV2ROI process self.suv2roi_progress_window.start(self.suv2roi.start_conversion) def on_loaded_suv2roi(self): """ Called when progress bar has finished. Closes the progress window and refreshes the main screen. """ if self.suv2roi.suv2roi_status: patient_dict_container = PatientDictContainer() self.structures_tab.fixed_container_structure_modified(( patient_dict_container.get('dataset_rtss'), {"draw": None})) else: # Alert user that SUV2ROI failed and for what reason if self.suv2roi.failure_reason == "UNIT": failure_reason = \ "PET units are not Bq/mL. OnkoDICOM can currently only\n" \ "perform SUV2ROI on PET images stored in these units." elif self.suv2roi.failure_reason == "DECY": failure_reason = \ "PET is not decay corrected. OnkoDICOM can currently " \ "only\nperform SUV2ROI on PET images that are decay " \ "corrected." else: failure_reason = "The SUV2ROI process has failed." button_reply = \ QtWidgets.QMessageBox( QtWidgets.QMessageBox.Icon.Warning, "SUV2ROI Failed", failure_reason, QtWidgets.QMessageBox.StandardButton.Ok, self) button_reply.button( QtWidgets.QMessageBox.StandardButton.Ok).setStyleSheet( self.stylesheet) button_reply.exec_() # Close progress window self.suv2roi_progress_window.close()