function ViewHandle = systemview(sisodb)
%SYSTEMVIEW  Creates and manages the system data view.
%
%   See also SISOTOOL.

%   Author(s): K. Gondoly, modified P. Gahinet
%   Copyright 1986-2004 The MathWorks, Inc.
%   $Revision: 1.23.4.2 $  $Date: 2004/04/10 23:14:31 $

% Open sytem data view
ViewHandle = LocalOpenView(sisodb);


%----------------- Callback functions -----------------

%%%%%%%%%%%%%%%%
% LocalClose %
%%%%%%%%%%%%%%%%
function LocalClose(hSrc,event,h)
% Close request
delete(h)


%%%%%%%%%%%%%%%%%%%%%
% LocalShowTransfer %
%%%%%%%%%%%%%%%%%%%%%
function LocalShowTransfer(hSrc,event,Component,ViewHandle)
% Show transfer function in ZPK form

% Model is a @MODELDATA object
ModelData = zpk(Component.Model);
DataStr = evalc('ModelData');

% Create message box
h = msgbox(DataStr,Component.Name,'none');

% Listen to main window being closed
setappdata(h,'Listener',handle.listener(handle(ViewHandle),...
        'ObjectBeingDestroyed',{@LocalClose h}))


%%%%%%%%%%%%%%%%%
% LocalEditName %
%%%%%%%%%%%%%%%%%
function LocalEditName(hSrc,event,ComponentObj)
% Edit component name

NewName = get(hSrc,'String');

if isempty(NewName)
    % Revert to previous name
    set(hSrc,'String',get(hSrc,'UserData'))
else
    % System name
    ComponentObj.SystemName = NewName;
end


%%%%%%%%%%%%%%%%%%%
% LocalUpdateName %
%%%%%%%%%%%%%%%%%%%
function LocalUpdateName(hSrc,event,hBox)
% Callback for name listeners

NewName = event.NewValue;
set(hBox,'String',NewName,'UserData',NewName)


%----------------- Local functions -----------------


%%%%%%%%%%%%%%%%%
% LocalOpenView %
%%%%%%%%%%%%%%%%%
function fig = LocalOpenView(sisodb)
% Creates view

% REVISIT: * Revamp as three-tab dialog
%          * View options = TF, ZPK, pole+zeros

SISOfig = sisodb.Figure;
LoopData = sisodb.LoopData;
SysName = LoopData.SystemName;
Plant = LoopData.Plant;
Sensor = LoopData.Sensor;

AllPoles = {Plant.Pole(imag(Plant.Pole)>=0,:) ; ...
      Sensor.Pole(imag(Sensor.Pole)>=0,:)};
AllZeros = {Plant.Zero(imag(Plant.Zero)>=0,:) ; ...
      Sensor.Zero(imag(Sensor.Zero)>=0,:)};
AllGains = [Plant.Gain ; Sensor.Gain];  
nPoles = cellfun('length',AllPoles);
nZeros = cellfun('length',AllZeros);


StdColor = get(0,'DefaultUIcontrolBackgroundColor');
PointsToPixels = 72/get(0,'ScreenPixelsPerInch');
Heights = [75+20*max([1,nPoles(1),nZeros(1)]); % Plant
   75+20*max([1,nPoles(2),nZeros(2)])];	       % Sensor

FigureHeight=80+sum(Heights);
ScreenSize=get(0,'ScreenSize');

% Create figure
fig = figure('Color',StdColor, ...
   'IntegerHandle','off', ...
   'MenuBar','none', ...
   'Visible','off',...
   'HandleVisibility','callback',...
   'NumberTitle','off', ...
   'Position',[66 ScreenSize(4)-30-FigureHeight 300 FigureHeight],...
   'Tag','SystemView',...
   'Resize','off',...
   'Name','System Data',...
   'DockControls', 'off');

centerfig(fig,sisodb.Figure);
Listeners(1) = handle.listener(LoopData,...
    'ObjectBeingDestroyed',{@LocalClose fig});

uicontrol('Parent',fig, ...
   'Units','points', ...
   'BackgroundColor',StdColor, ...
   'enable','inactive',...
   'Position',PointsToPixels*[5 FigureHeight-30 290 25], ...
   'Style','frame');
uicontrol('Parent',fig, ...
   'Units','points', ...
   'BackgroundColor',StdColor, ...
   'HorizontalAlignment','left', ...
   'FontWeight','Bold', ...
   'Position',PointsToPixels*[15 FigureHeight-28 150 17], ...
   'String','System Name: ', ...
   'Style','text');
h = uicontrol('Parent',fig, ...
   'Units','points', ...
   'BackgroundColor',[1 1 1], ...
   'Callback',{@LocalEditName LoopData},...
   'HorizontalAlignment','left', ...
   'Position',PointsToPixels*[120 FigureHeight-28 120 20], ...
   'String',SysName, ...
   'UserData',SysName, ...
   'Style','edit');
Listeners(end+1) = handle.listener(LoopData,findprop(LoopData,'SystemName'),...
    'PropertyPostSet',{@LocalUpdateName h});

NameText = {xlate('Plant Model');xlate('Sensor Model')};
Types = {'plant';'sensor'};
Names = {Plant.Name;Sensor.Name};

FrameBottom=30;
for ct=1:2,
    Component = get(LoopData,Types{ct});
    FrameBottom=FrameBottom+Heights(ct)+5;
    uicontrol('Parent',fig, ...
        'Units','points', ...
        'BackgroundColor',StdColor, ...
        'enable','inactive',...
        'Position',PointsToPixels*[5 FigureHeight-FrameBottom 290 Heights(ct)], ...
        'Style','frame');
    uicontrol('Parent',fig, ...
        'Units','points', ...
        'Position',PointsToPixels*[80 FigureHeight+5-FrameBottom 140 20], ...
        'Callback',{@LocalShowTransfer Component fig},...
        'String','Show Transfer Function');
    uicontrol('Parent',fig, ...
        'Units','points', ...
        'BackgroundColor',StdColor, ...
        'HorizontalAlignment','left', ...
        'FontWeight','Bold', ...
        'Position',PointsToPixels*[15 FigureHeight-FrameBottom+Heights(ct)-25 260 17], ...
        'String',sprintf('%s:  %s',NameText{ct},Names{ct}), ...
        'Style','text');
    
    uicontrol('Parent',fig, ...
        'Units','points', ...
        'BackgroundColor',StdColor, ...
        'HorizontalAlignment','left', ...
        'Enable','inactive',...
        'FontWeight','Bold', ...
        'Position',PointsToPixels*[15 FigureHeight-FrameBottom+Heights(ct)-45 40 17], ...
        'String','Zeros:', ...
        'Style','text');
    b = uicontrol('Parent',fig, ...
        'Units','points', ...
        'BackgroundColor',StdColor, ...
        'Enable','inactive',...
        'FontWeight','Bold', ...
        'HorizontalAlignment','left', ...
        'Position',PointsToPixels*[145 FigureHeight-FrameBottom+Heights(ct)-45 40 17], ...
        'String','Poles:', ...
        'Style','text');
    
    %----Add the Poles and Zeros
    if ~nZeros(ct),
        b = uicontrol('Parent',fig, ...
            'Units','points', ...
            'BackgroundColor',StdColor, ...
            'HorizontalAlignment','left', ...
            'Position',PointsToPixels*[15 FigureHeight-FrameBottom+Heights(ct)-65 130 17], ...
            'String','<None>', ...
            'Style','text');
    else
        for ctZ=1:nZeros(ct),
            Zero = AllZeros{ct}(ctZ);
            if imag(Zero)
                Zstr = sprintf('%0.3g %s %0.3gi',real(Zero),char(177),imag(Zero));
            else
                Zstr = sprintf('%0.3g',Zero);
            end
            b = uicontrol('Parent',fig, ...
                'Units','points', ...
                'BackgroundColor',StdColor, ...
                'HorizontalAlignment','left', ...
                'Position',PointsToPixels*[15 FigureHeight-FrameBottom+Heights(ct)-45-(ctZ*20) 130 17], ...
                'String',Zstr, ...
                'Style','text');
        end % for ctZ
    end % if/else isempty(AllZeros{ct})
    
    if ~nPoles(ct),
        b = uicontrol('Parent',fig, ...
            'Units','points', ...
            'BackgroundColor',StdColor, ...
            'HorizontalAlignment','left', ...
            'Position',PointsToPixels*[145 FigureHeight-FrameBottom+Heights(ct)-65 130 17], ...
            'String','<None>', ...
            'Style','text');
    else
        for ctP=1:nPoles(ct),
            Pole = AllPoles{ct}(ctP);
            if imag(Pole)
                Pstr = sprintf('%0.3g %s %0.3gi',real(Pole),char(177),imag(Pole));
            else
                Pstr = sprintf('%0.3g',Pole);
            end
            b = uicontrol('Parent',fig, ...
                'Units','points', ...
                'BackgroundColor',StdColor, ...
                'HorizontalAlignment','left', ...
                'Position',PointsToPixels*[145 FigureHeight-FrameBottom+Heights(ct)-45-(ctP*20) 130 17], ...
                'String',Pstr, ...
                'Style','text');
        end % for ctPreal
    end % if/else isempty(AllPoles{ct})
end % for ct

uicontrol('Parent',fig, ...
    'Units','points', ...
    'Position',PointsToPixels*[125 5 50 25], ...
    'Callback',{@LocalClose fig},...
    'String','OK');

% Install CS help
cshelp(fig,sisodb.Figure);

% Save listeners
set(fig,'Visible','on','UserData',Listeners)


