view m-toolbox/m/gui/gltpda/g_UpdateInports.m @ 2:18e956c96a1b database-connection-manager

Add LTPDADatabaseConnectionManager implementation. Matlab code
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Sun, 04 Dec 2011 21:23:09 +0100
parents f0afece42f48
children
line wrap: on
line source

function g_UpdateInports(varargin)
   % Update the MaskDisplay and the number of inports when the user
   % changes the params from Simulink.
   
   global params
   
   paramKey = varargin{1};
   if numel(paramKey)>7 && strcmpi(paramKey(1:7),'addPar_'), paramKey(1:7)=[]; end
   
   % List of parameters coming from Simulink:
   simKey = {};
   for i=1:nparams(params)
      if isa(params.params(i).val,'char') && strcmp(params.params(i).val,'-->')
         name = params.params(i).key;
         try if strcmpi(name(1:7),'addPar_'), name(1:7)=[];  end; catch, end
         simKey{end+1} = name;
      elseif isa(params.params(i).val,'cell') && isa(params.params(i).val{1},'char') && strcmp(params.params(i).val{1},'-->')
         name = params.params(i).key;
         try if strcmpi(name(1:7),'addPar_'), name(1:7)=[];  end; catch, end
         simKey{end+1} = name;
      end
   end
   
   % ===================================================================
   % If varargin{2}==2 no block has been added or subtracted, just
   % renamed an existing param; the only thing to perform is update the
   % drawing command.
   if varargin{2}~=2
      
      % Remove ALL inner inport blocks.
      if varargin{2}==-1, simKey2remove = [simKey,{paramKey}]; else simKey2remove = simKey; end
      
      for i=1:numel(simKey2remove)
         try %#ok<ALIGN>
            block2delete = utils.prog.find_in_models(gcb,'SearchDepth',1,'LookUnderMasks','all','Name',lower(simKey2remove{i}));
            if numel(block2delete)>1, block2delete(1)=[]; end
            blockLines   = get_param(block2delete{1},'LineHandles');
            if (blockLines.Outport(1)~=-1 && ~isempty(blockLines.Outport(1))),   delete_line(blockLines.Outport(1)); end
            delete_block(block2delete);
         catch, end
      end
      
      % Add ALL inner inport blocks anew:
      for i=1:numel(simKey)
         newBlock = add_block('simulink/Sources/In1', [gcb,'/Inport1'],'MakeNameUnique','on');
         nameAlreadyPresent = utils.prog.find_in_models(gcb,'SearchDepth',1,'LookUnderMasks','all','Name',lower(simKey{i}));
         if ~isempty(nameAlreadyPresent)
            for j=1:numel(nameAlreadyPresent)
               if ~strcmp(nameAlreadyPresent{j},gcb),
                  set_param(nameAlreadyPresent{j},'Name',[lower(simKey{i}),'_fun']);
               end
            end
         end
         set(newBlock,'Name',lower(simKey{i}))
      end
      
      % Now update the inner structure of lines and mux.
      muxblock   = find_system(gcb,'SearchDepth',1,'LookUnderMasks','all','BlockType','Mux');
      try %#ok<ALIGN>
         muxLines = get_param(muxblock{1},'LineHandles');
         for k=1:numel(muxLines.Inport),  if (muxLines.Inport(k)~=-1 && ~isempty(muxLines.Inport(k))),   delete_line(muxLines.Inport(k)); end
         end
         for k=1:numel(muxLines.Outport), if (muxLines.Outport(k)~=-1 && ~isempty(muxLines.Outport(k))), delete_line(muxLines.Outport(k)); end
         end
      catch, end
      delete_block(muxblock);
      
      inports  = find_system(gcb,'SearchDepth',1,'LookUnderMasks','all','BlockType','Inport');
      fakeInp  = find_system(gcb,'SearchDepth',1,'LookUnderMasks','all','BlockType','Ground');
      inports  = [fakeInp;inports];
      if numel(inports)>1
         muxblock = add_block('built-in/Mux', [gcb,'/Mux']);
         set(muxblock,'Position',[70 , 10 , 73 , 150])
         set(muxblock,'Inputs',num2str(numel(inports)))
      end
      for k=1:numel(inports)
         inports{k}=get_param(inports{k},'Handle');
         blkLines = get(inports{k},'LineHandles');
         for j=1:numel(blkLines.Outport), if (blkLines.Outport(j)~=-1 && ~isempty(blkLines.Outport(j))), delete_line(blkLines.Outport(j)); end
         end
         set(inports{k},'Position',[10 , 10+30*(k-1) , 30 , 30+30*(k-1)])
         set(inports{k},'Tag',num2str(k))
         if numel(inports)>1
            add_line(gcb,[get(inports{k},'Name'),'/1'],[get(muxblock,'Name'),'/',num2str(k)]);
         end
      end
      funcBlock = find_system(gcb,'SearchDepth',1,'LookUnderMasks','all','BlockType','M-S-Function');
      funcBlock = get_param(funcBlock,'Name');
      if numel(inports)>1
         add_line(gcb,[get(muxblock,'Name'),'/1'],[funcBlock{1},'/1']);
      else
         add_line(gcb,[get(inports{1},'Name'),'/1'],[funcBlock{1},'/1']);
      end
      
   end
   % ===================================================================
   
   % Updating the drawing command for the block Mask:
   fakeInp  = find_system(gcb,'SearchDepth',1,'LookUnderMasks','all','BlockType','Ground');
   drawingCom = '';
   if numel(simKey)>0 % then there are params 'from simulink' added
      if numel(fakeInp)>0 || strcmp(get_param(gcbh,'Tag'),'arithmetic'), currInp=1; else currInp=str2double(get(findobj('Tag','numbDataInputs'),'String'))+1; end
      for nn=1:numel(simKey)
         drawingCom = [drawingCom,'port_label(''input'',',num2str(currInp),',''',lower(simKey{1}),''');'];
         simKey(1)  = [];
         currInp    = currInp+1;
      end
   else % then the only inport is the regular one
      funcBlock  = find_system(gcb,'SearchDepth',1,'LookUnderMasks','all','BlockType','M-S-Function');
      funcName   = get_param(funcBlock{1},'Tag');
      if numel(funcName)>6 && strcmp(funcName(1:6),'ltpda_'), funcName(1:6)=[]; end
      drawingCom = strcat('disp(''',lower(funcName),''')');
   end
   set(gcbh,'MaskDisplay',drawingCom)
   
end