def __init__(self,window=None,parent=None):
     super(Volume_Viewer,self).__init__(parent) ## Create window with ImageView widget
     g.m.volume_viewer=self
     window.lostFocusSignal.connect(self.hide)
     window.gainedFocusSignal.connect(self.show_wo_focus)
     self.window=window
     self.setWindowTitle('Light Sheet Volume View Controller')
     self.setWindowIcon(QIcon('images/favicon.png'))
     self.setGeometry(QRect(422, 35, 222, 86))
     self.layout = QVBoxLayout()
     self.vol_shape=window.volume.shape
     mv,mz,mx,my=window.volume.shape
     self.currentAxisOrder=[0,1,2,3]
     self.current_v_Index=0
     self.current_z_Index=0
     self.current_x_Index=0
     self.current_y_Index=0
     
     self.formlayout=QFormLayout()
     self.formlayout.setLabelAlignment(Qt.AlignRight)
     
     self.xzy_position_label=QLabel('Z position')
     self.zSlider=SliderLabel(0)
     self.zSlider.setRange(0,mz-1)
     self.zSlider.label.valueChanged.connect(self.zSlider_updated)
     self.zSlider.slider.mouseReleaseEvent=self.zSlider_release_event
     
     self.sideViewOn=CheckBox()
     self.sideViewOn.setChecked(False)
     self.sideViewOn.stateChanged.connect(self.sideViewOnClicked)
     
     self.sideViewSide = QComboBox(self)
     self.sideViewSide.addItem("X")
     self.sideViewSide.addItem("Y")
     
     self.MaxProjButton = QPushButton('Max Intenstiy Projection')
     self.MaxProjButton.pressed.connect(self.make_maxintensity)
     
     self.exportVolButton = QPushButton('Export Volume')
     self.exportVolButton.pressed.connect(self.export_volume)
     
     self.formlayout.addRow(self.xzy_position_label,self.zSlider)
     self.formlayout.addRow('Side View On',self.sideViewOn)
     self.formlayout.addRow('Side View Side',self.sideViewSide)
     self.formlayout.addRow('', self.MaxProjButton)
     self.formlayout.addRow('', self.exportVolButton)
     
     self.layout.addWidget(self.zSlider)
     self.layout.addLayout(self.formlayout)
     self.setLayout(self.layout)
     self.setGeometry(QRect(381, 43, 416, 110))
     self.show()
class Volume_Viewer(QWidget):
    closeSignal=Signal()

    def show_wo_focus(self):
        self.show()
        self.window.activateWindow()  # for Windows
        self.window.raise_()  # for MacOS

    def __init__(self,window=None,parent=None):
        super(Volume_Viewer,self).__init__(parent) ## Create window with ImageView widget
        g.m.volume_viewer=self
        window.lostFocusSignal.connect(self.hide)
        window.gainedFocusSignal.connect(self.show_wo_focus)
        self.window=window
        self.setWindowTitle('Light Sheet Volume View Controller')
        self.setWindowIcon(QIcon('images/favicon.png'))
        self.setGeometry(QRect(422, 35, 222, 86))
        self.layout = QVBoxLayout()
        self.vol_shape=window.volume.shape
        mv,mz,mx,my=window.volume.shape
        self.currentAxisOrder=[0,1,2,3]
        self.current_v_Index=0
        self.current_z_Index=0
        self.current_x_Index=0
        self.current_y_Index=0
        
        self.formlayout=QFormLayout()
        self.formlayout.setLabelAlignment(Qt.AlignRight)
        
        self.xzy_position_label=QLabel('Z position')
        self.zSlider=SliderLabel(0)
        self.zSlider.setRange(0,mz-1)
        self.zSlider.label.valueChanged.connect(self.zSlider_updated)
        self.zSlider.slider.mouseReleaseEvent=self.zSlider_release_event
        
        self.sideViewOn=CheckBox()
        self.sideViewOn.setChecked(False)
        self.sideViewOn.stateChanged.connect(self.sideViewOnClicked)
        
        self.sideViewSide = QComboBox(self)
        self.sideViewSide.addItem("X")
        self.sideViewSide.addItem("Y")
        
        self.MaxProjButton = QPushButton('Max Intenstiy Projection')
        self.MaxProjButton.pressed.connect(self.make_maxintensity)
        
        self.exportVolButton = QPushButton('Export Volume')
        self.exportVolButton.pressed.connect(self.export_volume)
        
        self.formlayout.addRow(self.xzy_position_label,self.zSlider)
        self.formlayout.addRow('Side View On',self.sideViewOn)
        self.formlayout.addRow('Side View Side',self.sideViewSide)
        self.formlayout.addRow('', self.MaxProjButton)
        self.formlayout.addRow('', self.exportVolButton)
        
        self.layout.addWidget(self.zSlider)
        self.layout.addLayout(self.formlayout)
        self.setLayout(self.layout)
        self.setGeometry(QRect(381, 43, 416, 110))
        self.show()

    def closeEvent(self, event):
        event.accept() # let the window close
        
    def zSlider_updated(self,z_val):
        self.current_v_Index=self.window.currentIndex
        vol=self.window.volume
        testimage=np.squeeze(vol[self.current_v_Index,z_val,:,:])
        viewRect = self.window.imageview.view.targetRect()
        self.window.imageview.setImage(testimage,autoLevels=False)
        self.window.imageview.view.setRange(viewRect, padding = 0)
        self.window.image = testimage
        
    def zSlider_release_event(self,ev):
        vol=self.window.volume
        if self.currentAxisOrder[1]==1: # 'z'
            self.current_z_Index=self.zSlider.value()
            image=np.squeeze(vol[:,self.current_z_Index,:,:])
        elif self.currentAxisOrder[1]==2: # 'x'
            self.current_x_Index=self.zSlider.value()
            image=np.squeeze(vol[:,self.current_x_Index,:,:])
        elif self.currentAxisOrder[1]==3: # 'y'
            self.current_y_Index=self.zSlider.value()
            image=np.squeeze(vol[:,self.current_y_Index,:,:])

        viewRect = self.window.imageview.view.viewRect()
        self.window.imageview.setImage(image,autoLevels=False)
        self.window.imageview.view.setRange(viewRect, padding=0)
        self.window.image = image
        self.window.imageview.setCurrentIndex(self.current_v_Index)
        self.window.activateWindow()  # for Windows
        self.window.raise_()  # for MacOS
        QSlider.mouseReleaseEvent(self.zSlider.slider, ev)
    
    def sideViewOnClicked(self, checked):
        self.current_v_Index=self.window.currentIndex
        vol=self.window.volume
        if checked==2: #checked=True
            assert self.currentAxisOrder==[0,1,2,3]
            side = self.sideViewSide.currentText()
            if side=='X':
                vol=vol.swapaxes(1,2)
                self.currentAxisOrder=[0,2,1,3]
                vol=vol.swapaxes(2,3)
                self.currentAxisOrder=[0,2,3,1]
            elif side=='Y':
                vol=vol.swapaxes(1,3)
                self.currentAxisOrder=[0,3,2,1]
        else: #checked=False
            if self.currentAxisOrder == [0,3,2,1]:
                vol=vol.swapaxes(1,3)
                self.currentAxisOrder=[0,1,2,3]
            elif self.currentAxisOrder == [0,2,3,1]:
                vol=vol.swapaxes(2,3)
                vol=vol.swapaxes(1,2)
                self.currentAxisOrder=[0,1,2,3]
        if self.currentAxisOrder[1]==1: # 'z'
            idx=self.current_z_Index
            self.xzy_position_label.setText('Z position')
            self.zSlider.setRange(0,self.vol_shape[1]-1)
        elif self.currentAxisOrder[1]==2: # 'x'
            idx=self.current_x_Index
            self.xzy_position_label.setText('X position')
            self.zSlider.setRange(0,self.vol_shape[2]-1)
        elif self.currentAxisOrder[1]==3: # 'y'
            idx=self.current_y_Index
            self.xzy_position_label.setText('Y position')
            self.zSlider.setRange(0,self.vol_shape[3]-1)
        image=np.squeeze(vol[:,idx,:,:])
        self.window.imageview.setImage(image,autoLevels=False)
        self.window.volume=vol
        self.window.imageview.setCurrentIndex(self.current_v_Index)
        self.zSlider.setValue(idx)

    def make_maxintensity(self):
        vol=self.window.volume
        new_vol=np.max(vol,1)
        if self.currentAxisOrder[1]==1: # 'z'
            name='Max Z projection'
        elif self.currentAxisOrder[1]==2: # 'x'
            name = 'Max X projection'
        elif self.currentAxisOrder[1]==3: # 'y'
            name = 'Max Y projection'
        Window(new_vol, name=name)
        
    def export_volume(self):
        vol=self.window.volume
        export_path = QFileDialog.getExistingDirectory(g.m, "Select a parent folder to save into.", expanduser("~"), QFileDialog.ShowDirsOnly)
        export_path = os.path.join(export_path, 'light_sheet_vols')
        i=0
        while os.path.isdir(export_path+str(i)):
            i+=1
        export_path=export_path+str(i)
        os.mkdir(export_path) 
        for v in np.arange(len(vol)):
            A=vol[v]
            filename=os.path.join(export_path,str(v)+'.tiff')
            if len(A.shape)==3:
                A=np.transpose(A,(0,2,1)) # This keeps the x and the y the same as in FIJI
            elif len(A.shape)==2:
                A=np.transpose(A,(1,0))
            tifffile.imsave(filename, A)