% BUILDMAINFIG build the main sigBuilder window
%
% M Hewitson 18-10-08
%
% $Id: buildMainfig.m,v 1.1 2008/10/19 11:42:40 hewitson Exp $
%
function mainfig = buildMainfig(mainfig, varargin)
%   %% Check if I exist already
%   id = findobj('Tag', 'SIGBUILDERmainfig');
%   if ~isempty(id)
%     figure(id)
%     return
%   end
  
  %% Some initial setup
  
  Screen = get(0,'screensize');
  mainfig.Gproperties.Gcol     = [255 255 255]/255;
  mainfig.Gproperties.Screen   = Screen;
  mainfig.Gproperties.Gwidth   = 0.5;
  mainfig.Gproperties.Gheight  = 0.5;
  mainfig.Gproperties.Gborder  = 10;
  mainfig.Gproperties.fontsize = 12;
  
  l = (0.5-mainfig.Gproperties.Gwidth/2);
  b = (0.5-mainfig.Gproperties.Gheight/2);
  w = mainfig.Gproperties.Gwidth;
  h = mainfig.Gproperties.Gheight;
  mainfig.Gproperties.Gposition = [l b w h];
  
  if ~isempty(varargin) && ishandle(varargin{1})
    mainfig.handle = varargin{1};
    set(mainfig.handle, 'Tag', 'SIGBUILDERmainfig');
    set(mainfig.handle, 'Units', 'normalized');
  else
    %  Initialize and hide the GUI as it is being constructed.
    mainfig.handle = figure('Name', 'LTPDA Signal Builder',...
      'NumberTitle', 'off',...
      'Visible','off',...
      'Units','normalized',...
      'Position',mainfig.Gproperties.Gposition,...
      'Toolbar', 'none',...
      'MenuBar', 'none',...
      'Color', mainfig.Gproperties.Gcol,...
      'Resize', 'on',...
      'Tag', 'SIGBUILDERmainfig');
  end
  
  % Set mainfig callbacks
  set(mainfig.handle, 'CloseRequestFcn', {'sigBuilder.cb_mainfigClose', mainfig});  
  
  %------------------------------------------------------------------------
  %- General properties
  %------------------------------------------------------------------------
  hmarg = 0.006;
  vmarg = 0.012;
  commHeight = 0.05;
  % l b w h
  
  % make settings panel
  w = 0.5-2*hmarg;
  h = 1-2*vmarg;
  l = hmarg;
  b = vmarg;
  span = uipanel(mainfig.handle,...
    'Title', 'Settings', ...
    'FontSize', mainfig.Gproperties.fontsize, ...
    'BackgroundColor', [0.9 0.9 0.9], ...
    'Position', [l b w h], ...
    'BorderType', 'line', ...
    'BorderWidth', 1, ...
    'Tag', 'SettingsPanel', ...
    'HighlightColor', [0.5 0.5 0.5]);
  
  % make list panel
  w = 0.5-2*hmarg;
  h = 1-2*vmarg;
  l = 0.5+hmarg;
  b = vmarg;
  ppan = uipanel(mainfig.handle,...
    'Title', 'Signal List', ...
    'FontSize', mainfig.Gproperties.fontsize, ...
    'BackgroundColor', [200 220 240]/255, ...
    'Position', [l b w h], ...
    'BorderType', 'line', ...
    'BorderWidth', 1, ...
    'HighlightColor', [0.5 0.5 0.5]);
  
  %------------------------------------------------------------------------
  % Sample rate
  
  % label
  l = hmarg;
  h = commHeight;
  b = 1-2*vmarg-h;
  w = 0.25;
  sth = uicontrol(span, 'Style','text',...
    'String', 'Sample Rate [Hz]',...
    'Units', 'normalized', ...
    'HorizontalAlignment', 'right',...
    'BackgroundColor', [0.9 0.9 0.9], ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  % edit box
  l = l + w + hmarg;
  w = 0.15;
  sth = uicontrol(span, 'Style','edit',...
    'String', '10',...
    'Units', 'normalized', ...
    'BackgroundColor', 'w', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h], ...
    'Tag', 'SampleRateEdit');
  
  %------------------------------------------------------------------------
  % T0
  
  % label
  l = l + w + hmarg;
  w = 0.15;
  sth = uicontrol(span, 'Style','text',...
    'String', 'Start Time',...
    'Units', 'normalized', ...
    'HorizontalAlignment', 'right',...
    'BackgroundColor', [0.9 0.9 0.9], ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  % edit box
  l = l + w + hmarg;
  w = 0.35;
  t0 = time();
  sth = uicontrol(span, 'Style','edit',...
    'String', char(t0),...
    'Units', 'normalized', ...
    'BackgroundColor', 'w', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h], ...
    'Tag', 'T0edit');
  
  %------------------------------------------------------------------------
  % Name
  
  % label
  b = b - h - vmarg;
  l = hmarg;
  w = 0.25;
  sth = uicontrol(span, 'Style','text',...
    'String', 'Signal Name',...
    'Units', 'normalized', ...
    'HorizontalAlignment', 'right',...
    'BackgroundColor', [0.9 0.9 0.9], ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  % edit box
  l = l + w + hmarg;
  w = 0.35;
  sth = uicontrol(span, 'Style','edit',...
    'String', 'None',...
    'Units', 'normalized', ...
    'BackgroundColor', 'w', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h], ...
    'Tag', 'NameEdit');
  
  %------------------------------------------------------------------------
  % Signal Type
  
  % label
  w = 0.25;
  l = hmarg;
  b = b - h - vmarg;
  sth = uicontrol(span, 'Style','text',...
    'String', 'Signal Type',...
    'Units', 'normalized', ...
    'BackgroundColor', [0.9 0.9 0.9], ...
    'HorizontalAlignment', 'right',...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  % drop down menu
  l = l + w + hmarg;
  w = 0.35;
  clsh = uicontrol(span, ...
    'Style', 'popupmenu', ...
    'Units', 'normalized',...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'BackgroundColor', 'w', ...
    'String', mainfig.getSignalTypes, ...
    'Position', [l b w h],...
    'Tag', 'SignalTypeSelect', ...
    'Callback', {'sigBuilder.cb_selectSignalType', mainfig});
  
  %------------------------------------------------------------------------
  % Panel for signal controls
  w = 1-4*hmarg;
  h = 0.65;
  l = 2*hmarg;
  b = 0.1+2*vmarg;
  conpan = uipanel(span,...
    'Title', 'Signal Properties', ...
    'FontSize', mainfig.Gproperties.fontsize, ...
    'FontWeight', 'bold', ...
    'BackgroundColor', 'w', ...
    'Position', [l b w h], ...
    'BorderType', 'line', ...
    'BorderWidth', 1, ...
    'Tag', 'ControlsPanel', ...
    'HighlightColor', [0.5 0.5 0.5]);
  
  % Fire the callback
  sigBuilder.cb_selectSignalType(clsh, mainfig);
  
  %------------------------------------------------------------------------
  % Add signal button
  
  h = 1.2*commHeight;
  w = 0.2;
  l = 1-w-hmarg;
  b = vmarg;
  pbh = uicontrol(span,'Style','pushbutton',...
    'String','Add Signal',...
    'Callback', {'sigBuilder.cb_addSignal', mainfig}, ...
    'Units', 'normalized', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  
  %------------------------------------------------------------------------
  %------------------------------------------------------------------------
  %------------------------------------------------------------------------
  %- Right Panel
  %------------------------------------------------------------------------
  %------------------------------------------------------------------------
  %------------------------------------------------------------------------
  
  %------------------------------------------------------------------------
  % Signal List
  
  h = 0.7;
  l = hmarg;
  b = 1-h-vmarg;
  w = 1-2*hmarg;
  t = uitable(ppan, 'Data', [], ...
    'ColumnName', {'Type', 'Name', 'Length', 'Start', 'Add'},...
    'ColumnFormat', {'char', 'char', 'numeric', 'char', 'logical'},...
    'ColumnWidth', {'auto', 'auto', 'auto', 'auto', 'auto'},...
    'ColumnEditable', [false false false false true], ...
    'units', 'normalized',...
    'RowStriping', 'on', ...
    'BackgroundColor', [1 1 1;0.8 0.8 0.8], ...
    'ForegroundColor', [0 0 0], ...
    'Fontsize', mainfig.Gproperties.fontsize,...
    'Position', [l b w h],...
    'CellSelectionCallback', {'sigBuilder.cb_sigListSelect', mainfig}, ...
    'ButtonDownFcn', {'sigBuilder.cb_sigListClick', mainfig}, ...
    'Tag', 'sigBuilderSignalList');
  
  %------------------------------------------------------------------------
  % buttons
  
  %   remove
  h = 1.2*commHeight;
  w = 0.2;
  l = hmarg;
  b = b - h - vmarg;
  pbh = uicontrol(ppan,'Style','pushbutton',...
    'String','Remove Signal',...
    'Callback', {'sigBuilder.cb_removeSignal', mainfig}, ...
    'Units', 'normalized', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  %   clear
  l = l + hmarg + w;
  w = 0.2;
  pbh = uicontrol(ppan,'Style','pushbutton',...
    'String','Clear List',...
    'Callback', {'sigBuilder.cb_clearSignals', mainfig}, ...
    'Units', 'normalized', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  % Toggle select
  l = l + hmarg + w;
  w = 0.2;
  pbh = uicontrol(ppan,'Style','pushbutton',...
    'String','Toggle State',...
    'Callback', {'sigBuilder.cb_toggleAddState', mainfig}, ...
    'Units', 'normalized', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  % Select all
  l = l + hmarg + w;
  w = 0.2;
  pbh = uicontrol(ppan,'Style','pushbutton',...
    'String','Add All',...
    'Callback', {'sigBuilder.cb_addAll', mainfig}, ...
    'Units', 'normalized', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  % Join type
  % label
  w = 0.25;
  l = hmarg;
  b = b - h - vmarg;
  sth = uicontrol(ppan, 'Style','text',...
    'String', 'Join Type',...
    'Units', 'normalized', ...
    'BackgroundColor', [200 220 240]/255, ...
    'HorizontalAlignment', 'right',...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  % drop down menu
  l = l + w + hmarg;
  w = 0.35;
  tip = sprintf('join: overlaps taken from first time-series. (help ao/join)\nsumjoin: overlaps are sum of overlapping time-series. (help ao/sumjoin)');
  clsh = uicontrol(ppan, ...
    'Style', 'popupmenu', ...
    'Units', 'normalized',...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'BackgroundColor', 'w', ...
    'String', {'sumjoin', 'join'}, ...
    'Position', [l b w h],...
    'Tag', 'JoinTypeSelect', ...
    'Tooltip', tip);
    
  
  %   export commands
  b = b - vmarg - h;
  l = hmarg;
  w = 0.25;
  pbh = uicontrol(ppan,'Style','pushbutton',...
    'String','Build Commands',...
    'Callback', {'sigBuilder.cb_exportCommands', mainfig}, ...
    'Units', 'normalized', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  %   Save
  l = l + w + hmarg;
  w = 0.2;
  pbh = uicontrol(ppan,'Style','pushbutton',...
    'String','Build & Save',...
    'Callback', {'sigBuilder.cb_buildSave', mainfig}, ...
    'Units', 'normalized', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  % View
  l = l + w + hmarg;
  w = 0.2;
  pbh = uicontrol(ppan,'Style','pushbutton',...
    'String','Build & View',...
    'Callback', {'sigBuilder.cb_buildView', mainfig}, ...
    'Units', 'normalized', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  % View
  l = l + w + hmarg;
  w = 0.2;
  pbh = uicontrol(ppan,'Style','pushbutton',...
    'String','Build & Export',...
    'Callback', {'sigBuilder.cb_buildExport', mainfig}, ...
    'Units', 'normalized', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
end
