/*
// -----------------------------------------------------------------------
// 
// (c) Copyright 2001-2008, MeVis Research GmbH, Bremen, Germany
// ALL RIGHTS RESERVED
// 
// THIS FILE CONTAINS CONFIDENTIAL AND PROPRIETARY INFORMATION OF MEVIS 
// RESEARCH GMBH. ANY DUPLICATION, MODIFICATION, DISTRIBUTION, OR 
// DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY PROHIBITED 
// WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF MEVIS RESEARCH GMBH.
// 
*/
//----------------------------------------------------------------------------------
//! Macro module AngioOpenImage
/*!
// \file    AngioOpenImage.js
// \author  LinaLiu
// \date    2009-12-10
//
// This module is developed based on OpenImage module from MevisLab SDK. It is customized 
// for the needs of angioLocalize project.
*/
//----------------------------------------------------------------------------------

// --- Global objects initialization

var gStat = { initialized: false,          // Settings initialized
              lock: false                  // Lock notification handling
            };


// --- Initialization

// Initialize module
function InitModule ()
{
  gStat.lock = false;
  ctx.field("import.defaultOptions").touch();
  BrowserPreviewSelected();
  NewImageName();
  ctx.field("view2DExt.annotation.annotationMode").setValue("ANNO_MODE_USER");
  ctx.field("view2DExt.annotation.annotationUserBottomLeft").setValue("Timepoint: $(slice) \n$(sizex),$(sizey),$(sizec) \n$(voxelx),$(voxely) \n$(SeriesDescription)");
  ctx.field("view2DExt.annotation.showOrientation").setValue("ANNO_ORIENTATION_OFF");
          
}


// Initialize dialog
function InitDialog ()
{
  ctx.field("ok").setBoolValue(false);
}


// Initialize panel layout
function InitPanelLayout ()
{
  if (ctx.field("hideImport").boolValue()) {
    ctx.control("importButton").setVisible(false);
  }
  if (ctx.field("hidePreferences").boolValue()) {
    ctx.control("preferencesButton").setVisible(false);
  }
  if (ctx.field("hideCloseButtons").boolValue()) {
    ctx.control("closeButtons").setVisible(false);
  }
}


// Initialize Dicom browser paths and settings (before browser control is created)
function InitBrowser ()
{
  if (!gStat.initialized) {
    if (ctx.field("browserRootPath").stringValue() == "") {
      ctx.field("browserRootPath").setValue(MLAB.variable("DicomBrowserPath"));
    }
    ctx.field("settings.reset").touch();
    gStat.initialized = true;
  }
  ctx.field("dicomBrowser.filename").setStringValue(ctx.field("imageName").stringValue());
}


// --- Exit dialog

// Process OK/Cancel events
function CloseDialog (ok)
{
  var file = ctx.field("previewName").stringValue();
  //ctx.window().done(ok ? 1 : 0);
  MLAB.processEvents();
  ctx.field("ok").setBoolValue(ok);
  if (ok)
    ctx.field("imageName").setStringValue(file);
}

function HelpDialog()
{
   ctx.showHtmlHelp();
}

// Clean up on window close
function ExitDialog ()
{
  gStat.lock = true;
  ctx.field("previewName").setStringValue("");
  gStat.lock = false;
}


// --- Event handling

// Select current preview image as output image
function SelectCurrentPreview ()
{
  ctx.field("imageName").setStringValue(ctx.field("previewName").stringValue());
}


// A dataset has been clicked in the Dicom browser, select for preview
function BrowserPreviewSelected ()
{
  gStat.lock = true;
  ctx.field("previewName").setStringValue(ctx.field("dicomBrowser.previewName").stringValue());
  gStat.lock = false;
}


// A dataset has been selected in the Dicom browser, set new image and close
function BrowserDatasetSelected ()
{
  if (!ctx.field("hideCloseButtons").boolValue()) {
    CloseDialog(true);
  } else {
    SelectCurrentPreview();
  }
}


// Handle modifications of preview image field
function NewPreviewName ()
{
  /* If close buttons are suppressed and preview has not been selected using the Dicom
     browser, select preview as output. */
  if (!gStat.lock && ctx.field("hideCloseButtons").boolValue()) {
    SelectCurrentPreview();
  }
}


// Set new image filename, notify newImage field after new image has been loaded
function NewImageName ()
{
  ctx.field("imgLoad.filename").setStringValue(ctx.field("imageName").stringValue());
  ctx.field("newImage").touch();
  print("imageName:" + ctx.field("imageName").stringValue()); 
}


// --- User preferences handling

// Open preferences dialog
function PreferencesDialog ()
{
  ctx.module("settings").showModalDialog("OISettings", "OpenImage Preferences");
}


// --- DICOM Import

// Open Import Dicom dialog
function ImportDialog ()
{
  var importPath = ctx.field("previewName").stringValue();
  if (importPath) {
    importPath = MLABFileManager.splitPath(importPath).dir;
  } else {
    importPath = ctx.field("browserRootPath").stringValue();
  }
  ctx.field("import.target").setStringValue(importPath);
  ctx.module("import").showModalDialog();
}


//# MeVis signature v1
//# key: MFowDQYJKoZIhvcNAQEBBQADSQAwRgJBANEfsmYse2e1dRhkQ9AQbreCq9uxwzWLoGom13MNYmyfwoJqQOEXljLFAgw2eEjaT12G4CdqKWhRxh9ANP6n7GMCARE=:VI/mB8bT4u+mRtf/ru8yUQi8BzpaS3UeL2x62YxsUYnVqCWuLrVNLiukIIjnJMKQXlc8ezmgOIcVAV7pgvgKpQ==
//# owner: MeVis
//# date: 2009-06-10T04:12:25
//# hash: GR07ofKvUJxcWY2mZPAc+u8Uos0wOFSrGj71GYKLjshEsst7A2837GAdUyJwfkxMwiqDeUVcCaL8r4+m7JhPeA==
//# MeVis end

