45da7090f522c014637c9997c94793c0d85edbda
[scilab.git] / scilab / modules / scicos / macros / scicos_scicos / do_delete1.sci
1 //  Scicos
2 //
3 //  Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 //
19 // See the file ../license.txt
20 //
21
22 function [scs_m,DEL,DELL]=do_delete1(scs_m,K,gr)
23     //!
24     //
25     //**  30 August 2007 :
26     //**                BEWARE: this code is BUGGED : see the residual split after delete
27     //**                        this bug is older than Simone (see scilab 3.1.1)
28     //**
29     //** 22 June 2006 : New Graphics Interface update
30     //
31     // Perform deletion of scs_m object whose index are given in the vector
32     // K and all other relevant objects (link, splits,..)
33     //
34     // deleted objects:
35     //  - are replaced by the value : mlist('Deleted')
36     //  - not to change the indexing: use do_purge to suppress them and to renumber objects
37     //
38     //**  Perform deletion of [scs_m] object whose index are given in the vector
39     //**  [K] and all other relevant objects (link, splits,..) in recursive way
40     //
41     //**  Deleted objects are replaced by the value : mlist('Deleted')
42     //**  ---> not to change the indexing:
43     //**  ---> use [do_purge] to suppress them and to renumber objects
44     //**  Similar strategy is used for the ghraphics object: to mantain the coherency
45     //**  from the grphics object "gh_curwin.children.children(gr_k)" [Compund] list and the
46     //**  scs_o.objs(k) data structure with the equation
47     //**
48     //**         gr_k = get_gri(k, o_size(1))
49     //**
50     //**  where
51     //**
52     //**       o_size = size(gh_curwin.children.children); // o_size(1) => length  o_size(2) => 1
53     //**
54     //**  the graphics "Compound" object are NOT deleted BUT set to "invisible" with the propriety
55     //**
56     //**       gh_object.visible = "off" // You can't see me, by John Cena :) ... ask enrico.mannori
57     //**
58     //**  OK: ...looks simple... until you begin to fight with the code ...
59     //**
60
61     DEL = []  // table of deleted objects
62     DELL= [] // table of redefined links
63     outin = ["out","in"]
64
65     //**        Delete object until "K" is empty
66     //** ----------------------------------------------------------------------------------------------
67     while K<>[] do
68
69         k = K(1); K(1) = [] ;
70
71         o = scs_m.objs(k);
72
73         if find(DEL==k)==[] then
74             typ = typeof(o);
75         else
76             typ = "Deleted"
77         end
78
79         DEL=[DEL k]
80
81         //**----------------------------------- Link -------------------------------------------
82         if typ=="Link" then
83
84             [ct,from,to]=(o.ct,o.from,o.to)
85
86             //  free connected ports
87             if ct(2)==2 then //Implicit link
88
89                 if or(scs_m.objs(from(1)).graphics.pin==k) & from(3)==1 then
90                     scs_m.objs(from(1))=mark_prt(scs_m.objs(from(1)),from(2),"in",ct(2),0)
91                 else //or(scs_m.objs(from(1)).graphics.pout==k) then
92                     scs_m.objs(from(1))=mark_prt(scs_m.objs(from(1)),from(2),"out",ct(2),0)
93                 end
94
95                 if or(scs_m.objs(to(1)).graphics.pin==k) & to(3)==1 then
96                     scs_m.objs(to(1))=mark_prt(scs_m.objs(to(1)),to(2),"in",ct(2),0)
97                 else // or(scs_m.objs(to(1)).graphics.pout==k) then
98                     scs_m.objs(to(1))=mark_prt(scs_m.objs(to(1)),to(2),"out",ct(2),0)
99                 end
100
101             else
102                 scs_m.objs(from(1))=mark_prt(scs_m.objs(from(1)),from(2),"out",ct(2),0)
103                 scs_m.objs(to(1))=mark_prt(scs_m.objs(to(1)),to(2),"in",ct(2),0)
104             end
105
106             fromblock=scs_m.objs(from(1));
107             toblock=scs_m.objs(to(1));
108
109             if or(fromblock.gui==["SPLIT_f" "CLKSPLIT_f" "IMPSPLIT_f"]) then
110                 //user kills a split  output link:
111
112                 //get links connected to the split block
113                 connected=get_connected(scs_m,from(1))
114                 if size(setdiff(connected,K),"*")==2 then
115                     //RN: 22-7-2004 setdiff added above
116                     //create a unique link with the split input and remaining
117                     //output link and suppress the split block
118                     if find(connected(2)==DEL)<>[] then // delete split
119                         K = [from(1) K]
120                     else
121                         ksplit=from(1)
122                         DEL=[DEL  ksplit]       //suppress split block
123                         o1=scs_m.objs(connected(1));from1=o1.to;
124                         o2=scs_m.objs(connected(2));
125
126                         //create a unique link
127                         if o1.from(1)==ksplit&o2.from(1)==ksplit then
128                             //the two links come from the split: invert o1
129                             w=o1.to;o1.to=o1.from;o1.from=w;
130                             o1.xx=o1.xx($:-1:1);o1.yy=o1.yy($:-1:1);
131                         elseif o1.to(1)==ksplit(1)&o2.to(1)==ksplit then
132                             //the two links go from the split: invert o2
133                             w=o2.to;o2.to=o2.from;o2.from=w;
134                             o2.xx=o2.xx($:-1:1);o2.yy=o2.yy($:-1:1);
135                         elseif o1.to(1)<>ksplit then
136                             //o1 comes from the split while o2 goes to the split,
137                             //switch o1 and o2
138                             [o1,o2]=(o2,o1),
139                             connected=connected([2 1]);
140                         end
141
142                         //now we can assume that o1 goes to the split and o2 comes
143                         //from the split
144                         from1=o1.to;to2=o2.to;ct2=o2.ct
145                         //the links comes from connected(1) block and
146                         //goes to connected(2) block
147                         x1=o1.xx;y1=o1.yy
148                         if x1($-1)==x1($)&o2.xx(1)==o2.xx(2)|.. // both segs are vertical
149                             y1($-1)==y1($)&o2.yy(1)==o2.yy(2) then //both segs are horiz
150                             o1.xx=[x1(1:$-1);o2.xx(2:$)];
151                             o1.yy=[y1(1:$-1);o2.yy(2:$)];
152                         else
153                             o1.xx=[x1(1:$-1);o2.xx];
154                             o1.yy=[y1(1:$-1);o2.yy];
155                         end
156                         o1.to=o2.to;
157
158                         DEL = [DEL connected(1)] // supress one link
159                         DELL=[DELL  connected(1)]
160                         scs_m.objs(connected(2))=o1 //change link
161
162                         scs_m.objs(to2(1))=mark_prt(scs_m.objs(to2(1)),to2(2),outin(to2(3)+1),ct2(2),..
163                         connected(2))
164                         scs_m.objs(o1.from(1))=mark_prt(scs_m.objs(o1.from(1)),o1.from(2),..
165                         outin(o1.from(3)+1),o1.ct(2),connected(2))
166
167                     end // delete split
168                 end //get links connected to the split block
169             end // erase and delete link
170
171             if toblock.gui=="SPLIT_f"|toblock.gui=="CLKSPLIT_f" then //
172                 //user kills a split input link
173                 //ask for split deletion
174                 K=[K to(1)]
175             elseif toblock.gui=="IMPSPLIT_f" then
176                 //user kills a IMPsplit  link:
177
178                 //get links connected to the split block
179                 connected=get_connected(scs_m,to(1))
180                 if size(connected,"*")==2 then
181                     //create a unique link with the split input and remaining
182                     //output link and suppress the split block
183                     if find(connected(2)==DEL)<>[] then // delete split
184                         K=[to(1) K]
185                     else
186
187                         DEL=[DEL  to(1)]       //suppress split block
188
189                         o1=scs_m.objs(connected(1));
190                         o2=scs_m.objs(connected(2));
191                         //o1 and o2 are the links that comes from the split
192
193                         //create a unique link
194                         to1=o1.to; //number of block at the boundary of link o1
195                         to2=o2.to; //number of block at the boundary of link o2
196                         if (o1.xx(1)==o1.xx(2)& o2.xx(1)==o2.xx(2))| ..
197                             (o1.yy(1)==o1.yy(2)& o2.yy(1)==o2.yy(2)) then
198                             o1.xx=o1.xx(2:$);o1.yy=o1.yy(2:$)
199                         end
200                         o1.xx=[o1.xx($:-1:1);o2.xx]
201                         o1.yy=[o1.yy($:-1:1);o2.yy]
202                         o1.from=o1.to
203                         o1.to=o2.to;
204
205
206                         DEL=[DEL connected(1)] // supress one link
207                         DELL=[DELL  connected(1)]
208                         scs_m.objs(connected(2))=o1 //change link
209
210                         scs_m.objs(to1(1))=mark_prt(scs_m.objs(to1(1)),..
211                         to1(2),outin(to1(3)+1),o1.ct(2),connected(2))
212                         scs_m.objs(to2(1))=mark_prt(scs_m.objs(to2(1)),to2(2),..
213                         outin(to2(3)+1),ct(2), ...
214                         connected(2))
215                     end //if find(connected(2)==DEL)<>[]
216
217                 end //if size(connected,'*')==2
218
219             end //elseif toblock.gui=='IMPSPLIT_f'
220
221             //** -------------------------------------- Block ---------------------------------------
222         elseif typ=="Block" then
223
224             // get connected links
225             connected=get_connected(scs_m,k)
226             //ask for connected links deletion
227             K=[K connected]
228
229
230             //**--------------------------------------- Text ----------------------------------------
231         elseif typ=="Text" then
232             //** do nothing :)
233
234             //** ---------------------------- already "Deleted" object :) ----------------------------
235         elseif typ=="Deleted" then
236             //** do nothing :)
237
238             //** ---------------------------  in all the other possibles cases -----------------------
239         else
240
241             messagebox("This object can''t be deleted","modal");
242
243         end //** link / Block / Text / Deleted / other...
244
245     end //** ... end of while ()
246     //**---------------------------------- end of main while() loop ---------------------------
247
248
249     //** Scan all the deleted elements and update the graphics datastrucure
250     for k = DEL
251         scs_m.objs(k) = mlist("Deleted"); //** mark the object as "Deleted"
252     end
253 endfunction
254
255