function varargout = Main(varargin)
% MAIN M-file for Main.fig
%      MAIN, by itself, creates a new MAIN or raises the existing
%      singleton*.
%
%      H = MAIN returns the handle to a new MAIN or the handle to
%      the existing singleton*.
%
%      MAIN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MAIN.M with the given input arguments.
%
%      MAIN('Property','Value',...) creates a new MAIN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Main_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Main_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 Main

% Last Modified by GUIDE v2.5 05-May-2005 11:43:08

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Main_OpeningFcn, ...
                   'gui_OutputFcn',  @Main_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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 Main is made visible.
function Main_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 Main (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Main wait for user response (see UIRESUME)
% uiwait(handles.figure1);
warning off all

if nargin ~=4
    handles.ProjState = 0;
    handles.fileload = 0;
    guidata(hObject,handles)
end

if nargin == 4
    handles.prefs = varargin{1};
    handles.ProjState = 1;
    guidata(hObject, handles);
elseif nargin ~= 4 & handles.fileload == 1
    handles.prefs = prefs;
    guidata(hObject, handles);
else
    load('default_prefs');
    handles.prefs=prefs; guidata(hObject, handles);
end
%load('default_handles.prefs.pref','-mat')

refresh(hObject, handles)

% --- Outputs from this function are returned to the command line.
function varargout = Main_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 button press in FindCalls.
function FindCalls_Callback(hObject, eventdata, handles)
% hObject    handle to FindCalls (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if handles.ProjState == 1
    %cd(handles.prefs.func_dir)
    close(handles.output)
    window_1(handles.prefs)
else
    dial.text1='Error: Please load or start new project';
    dial.butt_text='OK';
    dial.win_title='Error';
    DB=dial_box(dial);
end

% --- Executes on button press in pushbutton3.
function Classify_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if handles.ProjState == 1 & handles.prefs.FiltSt == 1
    %cd(handles.prefs.func_dir)
    close(handles.output)
    Classify(handles.prefs)
elseif handles.ProjState == 1 & handles.prefs.FiltSt == 0
    dial.text1='Error: Apply filtering first';
    dial.butt_text='OK';
    dial.win_title='Error';
    DB=dial_box(dial);
else
    dial.text1='Error: Please load or start new project';
    dial.butt_text='OK';
    dial.win_title='Error';
    DB=dial_box(dial);
end

% --- Executes on button press in pushbutton4.
function PCA_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if handles.ProjState == 1 & handles.prefs.FiltSt == 1
    %cd(handles.prefs.func_dir)
    close(handles.output)
    Window_3(handles.prefs)
elseif handles.ProjState == 1 & handles.prefs.FiltSt == 0
    dial.text1='Error: Apply filtering first';
    dial.butt_text='OK';
    dial.win_title='Error';
    DB=dial_box(dial);
else
    dial.text1='Error: Please load or start new project';
    dial.butt_text='OK';
    dial.win_title='Error';
    DB=dial_box(dial);
end

% --- Executes on button press in NewProj.
function NewProj_Callback(hObject, eventdata, handles)
% hObject    handle to NewProj (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[filename, pathname] = uiputfile( ...
    {'*.mat';'*.*'}, ...
    'Save Project');

if length(filename)<4 & length(filename) > 1
    filename(length(filename)+1:length(filename)+4)='.mat';
elseif length(filename) == 1
    filename = 0;
elseif filename(length(filename)-3:length(filename))~='.mat'
    filename(length(filename)+1:length(filename)+4)='.mat';
end

if exist(strcat(pathname,strrep(filename,'.mat','')),'dir')
    dial.text1='Warning: Please delete old files in folder or rename project';
    dial.butt_text='OK';
    dial.win_title='Warning';
    DB=dial_box(dial);
end

if isequal([filename,pathname],[0,0])
    return
else
    load('default_prefs');
    handles.prefs=prefs; 
    guidata(hObject, handles);
    % save prefs file 
    % save class file ??? already done in classify
    % save param file ??? already done in window_1
    % save PCA stuff - not sure if done 
    File = fullfile(pathname,filename);
    %save = [];
    prefs = handles.prefs;
    %save.param = handles.param;
    %save.class = handles.class;
    save(File,'prefs')
    handles.LastFile = File;
    %guidata(hObject,handles)
    handles.ProjState = 1;
    %guidata(hObject,handles)
    handles.prefs.func_dir = pathname;
    handles.prefs.proj_dir = strcat(pathname,strrep(filename,'.mat','\'));
    handles.prefs.proj_name = filename;
    handles.prefs.filenames=[];
    handles.prefs.paths=[];
    handles.prefs.FiltSt = 0;
    handles.prefs.ClassSt = 0;
    handles.prefs.PCASt = 0;
    mkdir(pathname,strrep(filename,'.mat',''))
    guidata(hObject,handles)
end

refresh(hObject, handles)

% --- Executes on button press in OpenProj.
function OpenProj_Callback(hObject, eventdata, handles)
% hObject    handle to OpenProj (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[filename, pathname] = uigetfile( ...
    {'*.mat', 'All Mat Files (*.mat)'; ...
     '*.*', 'All Files (*.*)'}, ...
    'Select Project File');

if isequal([filename,pathname],[0,0])
    return
else
    % open prefs file
    % open class file ??? already done in classify
    % open param file ??? already done in window_1
    % open PCA stuff - not sure if done 
    File = fullfile(pathname,filename);
    load(File);
    handles.LastFile = File;
    %guidata(hObject,handles)
    handles.ProjState = 1;
    handles.fileload = 1;
    handles.prefs = prefs;
    handles.prefs.func_dir = pathname;
    handles.prefs.proj_dir = strcat(pathname,strrep(filename,'.mat','\'));
    handles.prefs.proj_name = filename;
    guidata(hObject,handles)
    %end
end

refresh(hObject, handles)

% --- Executes on button press in pushbutton8.
function Done_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


filename = strcat(handles.prefs.func_dir, handles.prefs.proj_name);
if exist(filename)
    prefs = handles.prefs;
    save(filename,'prefs')  
    close(handles.output)
    %disp('here')
else
    close(handles.output)
end
    
function refresh(hObject, handles)

if handles.ProjState == 1
    set(handles.ProjOp,'String',strcat('Project:',handles.prefs.proj_name,' Open'))
else
    set(handles.ProjOp,'String','No Project Open')
end

if handles.prefs.FiltSt == 1
    set(handles.FiltDone,'String','Filtering done')
else
    set(handles.FiltDone,'String','')
end

if handles.prefs.ClassSt == 1
    set(handles.ClassDone,'String','Classification done')
    set(handles.Extra,'String',strcat(num2str(handles.prefs.NoOfCalls),'calls found'))
else
    set(handles.ClassDone,'String','')
    set(handles.Extra,'String','')
end

if handles.prefs.PCASt == 1
    set(handles.PCADone,'String','PCA done')
else
    set(handles.PCADone,'String','')
end


% --- Executes on button press in prefs.
function prefs_Callback(hObject, eventdata, handles)
% hObject    handle to prefs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close(handles.output)
pref_screen(handles.prefs)

% --- Executes on button press in show_calls.
function show_calls_Callback(hObject, eventdata, handles)
% hObject    handle to show_calls (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if handles.ProjState == 1 & handles.prefs.FiltSt == 1
    %cd(handles.prefs.func_dir)
    close(handles.output)
    show_calls(handles.prefs)
elseif handles.ProjState == 1 & handles.prefs.FiltSt == 0
    dial.text1='Error: Apply filtering first';
    dial.butt_text='OK';
    dial.win_title='Error';
    DB=dial_box(dial);
else
    dial.text1='Error: Please load or start new project';
    dial.butt_text='OK';
    dial.win_title='Error';
    DB=dial_box(dial);
end


% --- Executes on button press in markers.
function markers_Callback(hObject, eventdata, handles)
% hObject    handle to markers (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

close(handles.output)
set_markers(handles.prefs); guidata(hObject,handles)


