"""
* Copyright (c) 2009 Stanford University, School of Medicine All rights reserved. 
* 
* STANFORD ACADEMIC SOFTWARE LICENSE AGREEMENT 
* FOR AngioLocalize SOURCE CODE: 
* 
* 1.  This is a legal agreement between you, RECIPIENT, and STANFORD UNIVERSITY.  
*       By accepting, receiving, and using AngioLocalize.including any accompanying information, materials or 
*       manuals (Program), you are agreeing to be bound by the terms of this Agreement.  
*       If you do not agree to the terms of this Agreement, promptly return the Program to the STANFORD.

* 2.  STANFORD grants to RECIPIENT a royalty-free, nonexclusive, and nontransferable license to use the Program 
*       and accompanying source code furnished hereunder, upon the terms and conditions set out below. 
*       documentation and/or other materials provided with the distribution. 
* 3.  RECIPIENT acknowledges that the Program is a research tool still in the development stage and that 
*       it is being supplied as is, without any accompanying services, support or improvements from STANFORD.
* 4.  RECIPIENT agrees to use the Program solely for internal non-commercial purposes. 
* 5.  RECIPIENT may create derivatives and further transfer the Program under the following terms and conditions: 
*        A) Recipient agrees to not commercially exploit the Program and agrees all uses will be for non-commercial purposes; and,
*        B) Recipient may not mark any derivatives with the title of Program and may not to use iPad mark for any derivatives; and,
*        C) Recipient agrees to mark and acknowledge all derivatives as follows: "This is a derivative work based on AngioLocalize:
*           Frameless AVM localization with 2D Digital Subtraction Angiography (DSA) and 3D Rotational Angiography (DRA) Images developed
*           by Dimitre Hristov, Lina Liu.
*        D) Recipient agrees to make all improvements, enhancements and derivatives available under 
*           similar terms and conditions to the research community. 
* 6.   RECIPIENT warrants that RECIPIENT will not remove or export any part of the software or Program from the United 
*       States except in full compliance with all United States and other applicable laws and regulations.
* 7.  RECIPIENT will use the Program in compliance with all applicable laws, policies and regulations including, but not 
*       limited to, any approvals, informed consent and patient confidentiality principles.
* 8.   RECIPIENT will indemnify, hold harmless, and defend Stanford against any claim of any kind arising out of or
*       related to the exercise of any rights granted under this Agreement or the breach of this Agreement by RECIPIENT.
* 9.  Title and copyright to the Program and any associated documentation shall at all times remain with STANFORD, 
*       and RECIPIENT agrees to preserve same.
""" 
#----------------------------------------------------------------------------------
#! Macro module AngioLocalize
#/*!
# \file    AngioLocalize.py
# \author  LinaLiu
# \date    2009-12-10
#
# 
#*/
#----------------------------------------------------------------------------------

from mevis import *
from PythonQt import *
import time;
import os;
currentPatient = None
title = ""

## initWindow():
def initWindow():
  ctx.field("unverified").setBoolValue(True);
  ctx.field("isAdmin").setBoolValue(False);
  
## copy log file when application is closed
def closeWindow():
  logFile = MLAB.variable("Logfile");
  if not os.path.exists(logFile):
    MLAB.showWarning("Please set the logfile path from Edit->Preferences->Developer in order to have AngioLocalize special logs.");
  else:
    MLABFileManager.copy(logFile, logFile +  "--AngioLocalize(" + time.strftime("%Y%m%d-%H%M%S") + ")");
    
#----------------------------------------------------------------------------------
# The command below does not execute properly on Mevis 2.1
#  MLAB.runCommand("ren", "c:/temp/mevisLab.log  c:/temp/mevisLab.log." +  time.strftime("%Y%m%d-%H%M%S") );
#----------------------------------------------------------------------------------
## this is the function called when user is verfied in login page 
def loggedIn():
  if (ctx.field("angioLogin.isAdmin").value == True):
    ctx.field("isAdmin").setBoolValue(True);
  ctx.field("unverified").setBoolValue(False);
  ctx.controlDebug("PatientSelection").setEnabled(True);
  
  
## this function is used to init DSA2D tab
def DSA2DInit():
   ## All patient related information is passed to the DSA2D module
   ctx.field("DSA2D.PatientID").setValue(ctx.field("PatientID").value);
   ctx.field("DSA2D.PatientsName").setValue(ctx.field("PatientsName").value);
   ctx.field("DSA2D.PatientsBirthDate").setValue(ctx.field("PatientsBirthDate").value);
   ctx.field("DSA2D.PatientsSex").setValue(ctx.field("PatientsSex").value);
   
## this function is used to init DSA3D tab   
def DSA3DInit():
   ## all the patient related information is passed to the DSA3D module
   ctx.field("DSA3D.PatientID").setValue(ctx.field("PatientID").value);
   ctx.field("DSA3D.PatientsName").setValue(ctx.field("PatientsName").value);
   ctx.field("DSA3D.PatientsBirthDate").setValue(ctx.field("PatientsBirthDate").value);
   ctx.field("DSA3D.PatientsSex").setValue(ctx.field("PatientsSex").value); 

## this function is used to init Registration tab    
def RegistrationInit():
   ## all the patient related information is passed to Registration module
   check();
   ctx.field("Registration.PatientID").setValue(ctx.field("PatientID").value);
   ctx.field("Registration.PatientsName").setValue(ctx.field("PatientsName").value);
   ctx.field("Registration.PatientsBirthDate").setValue(ctx.field("PatientsBirthDate").value);
   ctx.field("Registration.PatientsSex").setValue(ctx.field("PatientsSex").value); 
   ctx.module("Registration").call("initWindow");
   ctx.module("Registration2D3D").call("initWindow");
   
## this function is used to init Registration2D3D tab 
def Registration2D3DInit():
   ## all the patient related information is passed to Registration2D3D module
   check();
   ctx.field("Registration2D3D.PatientID").setValue(ctx.field("PatientID").value);
   ctx.field("Registration2D3D.PatientsName").setValue(ctx.field("PatientsName").value);
   ctx.field("Registration2D3D.PatientsBirthDate").setValue(ctx.field("PatientsBirthDate").value);
   ctx.field("Registration2D3D.PatientsSex").setValue(ctx.field("PatientsSex").value); 
   ctx.module("Registration2D3D").call("initWindow");
   
   
## this function is used to init Localization tab 
def LocalizationInit():
   ## all the patient related information is passed to Localization module
   check();
   ctx.field("LocalizationWrapper.PatientID").setValue(ctx.field("PatientID").value);
   ctx.field("LocalizationWrapper.PatientsName").setValue(ctx.field("PatientsName").value);
   ctx.field("LocalizationWrapper.PatientsBirthDate").setValue(ctx.field("PatientsBirthDate").value);
   ctx.field("LocalizationWrapper.PatientsSex").setValue(ctx.field("PatientsSex").value); 
   ctx.module("LocalizationWrapper").call("initWindow");
 

## this function is used to clean up Localization tab 
def LocalizationDone():
  locM = ctx.module("LocalizationWrapper");
  locM.call("cancel");
  dbM = locM.module("DBConn");
  dbM.call("close");
  
## this function is used to clean up DSA2D tab 
def DSA2DDone():
  dsa2dM = ctx.module("DSA2D");
  dsa2dM.call("cleanUp");
  dbM = dsa2dM.module("DBConn");
  dbM.call("close");
  
## this function is used to clean up DSA3D tab 
def DSA3DDone():
  dsa3dM = ctx.module("DSA3D");
  dsa3dM.call("cleanUp");
  dbM = dsa3dM.module("DBConn");
  dbM.call("close");
  
## this function is used to clean up DSA tab, which includes DSA2D and DSA3D   
def DSATabDone():
   DSA2DDone();
   DSA3DDone();
   
## this function is used to clean up registration tab.   
def RegistrationDone():
  dsa2dM = ctx.module("Registration");
  dsa2dM.call("cleanUp");
  dbM = dsa2dM.module("DBConn");
  dbM.call("close");
  
## this function is used to clean up Registration2D3D tab.   
def Registration2D3DDone():
  dsa2dM = ctx.module("Registration2D3D");
  dsa2dM.call("cleanUp");
  dbM = dsa2dM.module("DBConn");
  dbM.call("close");
  
## this function is used to clean up Registration Tab, which includes Registration 3D-3D and Registration 2D-3D subTabs    
def RegistrationTabDone():
  RegistrationDone();
  Registration2D3DDone();
  
## this function is used to check if there is a patient selected.     
def check():
  title = ctx.window().title();
  if (title == "Angio-Localize angioLocalize"):
    MLAB.showInformation("Please select a patient in Patient Selection Tab first.");
    return;
  DSA2DInit();
  
## When a user selects a patient, the tabs "Image Selection", "Registration" and "Localization" will be shown  
def selectPatient():
  selected = ctx.field("PatientsRecords.itemSelected").value;
  ctx.field("patientSelected").setValue(selected);

## this function is used to init Localization tab 
def ExportInit():
   ## all the patient related information is passed to Localization module
   check();
   ctx.field("AngioExport.PatientID").setValue(ctx.field("PatientID").value);
   ctx.field("AngioExport.PatientsName").setValue(ctx.field("PatientsName").value);
   ctx.field("AngioExport.PatientsBirthDate").setValue(ctx.field("PatientsBirthDate").value);
   ctx.field("AngioExport.PatientsSex").setValue(ctx.field("PatientsSex").value); 
   ctx.module("AngioExport").call("initWindow");
 

## this function is used to clean up Localization tab 
def ExportDone():
  locM = ctx.module("AngioExport");
  locM.call("cleanUp");
  dbM = locM.module("DBConn");
  dbM.call("close");


#//# MeVis signature v1
#//# key: MFowDQYJKoZIhvcNAQEBBQADSQAwRgJBANEfsmYse2e1dRhkQ9AQbreCq9uxwzWLoGom13MNYmyfwoJqQOEXljLFAgw2eEjaT12G4CdqKWhRxh9ANP6n7GMCARE=:VI/mB8bT4u+mRtf/ru8yUQi8BzpaS3UeL2x62YxsUYnVqCWuLrVNLiukIIjnJMKQXlc8ezmgOIcVAV7pgvgKpQ==
#//# owner: MeVis
#//# date: 2009-06-10T04:12:25
#//# hash: vHSfj+Qt8xrIkDVP4W0Rep0YH6a3+MySa0+Cs+AoqEK/IbB+f5szwmyVTENOjzz/JEgm2t31FtHmUUMFcQg5VQ==
#//# MeVis end

