function varargout = component_view_gui(varargin)
% View the component (the same component of all subjects)
% Input: appConfig - structure containing all parameters 
%-----------------------------------------------------------
%   Copyright (C) 2009
%   Neuroimage Computing Group, State Key Laboratory of
%   Cognitive Neuroscience and Learning, Beijing normal University
%	Written by Guiwen Chen
%	Version=1.0;
%	Release=20090925;
%-----------------------------------------------------------
%
% COMPONENT_VIEW_GUI M-file for component_view_gui.fig
%      COMPONENT_VIEW_GUI, by itself, creates a new COMPONENT_VIEW_GUI or raises the existing
%      singleton*.
%
%      H = COMPONENT_VIEW_GUI returns the handle to a new COMPONENT_VIEW_GUI or the handle to
%      the existing singleton*.
%
%      COMPONENT_VIEW_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in COMPONENT_VIEW_GUI.M with the given input arguments.
%
%      COMPONENT_VIEW_GUI('Property','Value',...) creates a new COMPONENT_VIEW_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before component_view_gui_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to component_view_gui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help component_view_gui

% Last Modified by GUIDE v2.5 25-Sep-2009 21:08:54

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @component_view_gui_OpeningFcn, ...
                   'gui_OutputFcn',  @component_view_gui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before component_view_gui is made visible.
function component_view_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to component_view_gui (see VARARGIN)

% Choose default command line output for component_view_gui
handles.output = hObject;

if(length(varargin)>0)
    handles.parameters=varargin{1};
    if(length(varargin)>1)
        handles.appConfig=varargin{2};
    end
end

set(handles.ImagesPerFigureHandle,'Value',2);
set(handles.SlicerangHandle,'String',handles.parameters.slicerangestr);
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes component_view_gui wait for user response (see UIRESUME)
% uiwait(handles.componentViewFig);


% --- Outputs from this function are returned to the command line.
function varargout = component_view_gui_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on selection change in ImageValuesHandle.
function ImageValuesHandle_Callback(hObject, eventdata, handles)
% hObject    handle to ImageValuesHandle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns ImageValuesHandle contents as cell array
%        contents{get(hObject,'Value')} returns selected item from ImageValuesHandle


% --- Executes during object creation, after setting all properties.
function ImageValuesHandle_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ImageValuesHandle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in anatomicalHandle.
function anatomicalHandle_Callback(hObject, eventdata, handles)
% hObject    handle to anatomicalHandle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns anatomicalHandle contents as cell array
%        contents{get(hObject,'Value')} returns selected item from anatomicalHandle
index=get(hObject,'Value');
slicePlane=get(hObject,'String');
% get the slices in mm for the corresponding plane
parameters=handles.parameters;
if(isfield(handles.parameters,'imagVol'))
    imagVol=handles.parametes.imagVol;
else
    [data,imagVol] = nic_ReadNiftiImage(structuralFile);
    handles.parameters.imagVol=imagVol;
    clear  data;
end
[sliceParameters] = icatb_get_slice_def(imagVol, slicePlane);
% get the slices in mm
slices_in_mm = sliceParameters.slices;
clear sliceParameters;
% construct string
slices_in_mm = nic_rsdd_constructString(slices_in_mm);
set(handles.slicerangHandle,'String',slices_in_mm);
guidata(hObejct,handles);

% --- Executes during object creation, after setting all properties.
function anatomicalHandle_CreateFcn(hObject, eventdata, handles)
% hObject    handle to anatomicalHandle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function SlicerangHandle_Callback(hObject, eventdata, handles)
% hObject    handle to SlicerangHandle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of SlicerangHandle as text
%        str2double(get(hObject,'String')) returns contents of SlicerangHandle as a double


% --- Executes during object creation, after setting all properties.
function SlicerangHandle_CreateFcn(hObject, eventdata, handles)
% hObject    handle to SlicerangHandle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ThresholdHandle_Callback(hObject, eventdata, handles)
% hObject    handle to ThresholdHandle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ThresholdHandle as text
%        str2double(get(hObject,'String')) returns contents of ThresholdHandle as a double


% --- Executes during object creation, after setting all properties.
function ThresholdHandle_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ThresholdHandle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in ImagesPerFigureHandle.
function ImagesPerFigureHandle_Callback(hObject, eventdata, handles)
% hObject    handle to ImagesPerFigureHandle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns ImagesPerFigureHandle contents as cell array
%        contents{get(hObject,'Value')} returns selected item from ImagesPerFigureHandle


% --- Executes during object creation, after setting all properties.
function ImagesPerFigureHandle_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ImagesPerFigureHandle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in ButOKHandle.
function ButOKHandle_Callback(hObject, eventdata, handles)
% hObject    handle to ButOKHandle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% response to the button of OK
parameters=handles.parameters;
imagev=get(handles.ImageValuesHandle,'Value');
contents=get(handles.ImageValuesHandle,'String');


parameters.imagevalues=contents{imagev};

parameters.converttozscores='Yes';
parameters.thresholdvalue=str2num(get(handles.ThresholdHandle,'String'));

v=get(handles.ImagesPerFigureHandle,'Value');
contents=get(handles.ImagesPerFigureHandle,'String');
parameters.imagesperfigure=str2num(contents{v});

v=get(handles.anatomicalHandle,'Value');
contents=get(handles.anatomicalHandle,'String');

parameters.anatomicalplane=contents{v};
parameters.slicerange=str2num(get(handles.SlicerangHandle,'String'));

if(imagev==1)
    imagev=2;
elseif(imagev==2)
    imagev=1;
end
parameters.returnValue=imagev;

parameters.convertToZ=1;

if(parameters.viewType==1)
    
    nic_icatb_componentExplore([],parameters);
    
elseif(parameters.viewType==2)
    nic_rsdd_displayComponent(handles.appConfig,parameters);
end
delete(handles.output);


% --- Executes on button press in ButCancelHandle.
function ButCancelHandle_Callback(hObject, eventdata, handles)
% hObject    handle to ButCancelHandle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if(ishandle(handles.output))
    objs=findobj('-regexp','Name','^gc_\w+$');

    if(~isempty(objs))
        delete(objs);
    end
    delete(handles.output);
end



% --- Executes during object creation, after setting all properties.
function componentViewFig_CreateFcn(hObject, eventdata, handles)
% hObject    handle to componentViewFig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

