arcssheet.cpp
1 /*
2  name: lib/arcssheet.cpp
3 
4  This file is part of ARCS - Augmented Reality Component System
5  (version 2-current), written by Jean-Yves Didier
6  for IBISC Laboratory (http://www.ibisc.univ-evry.fr)
7 
8  Copyright (C) 2013 Université d'Evry-Val d'Essonne
9 
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Please send bugreports with examples or suggestions to
25  jean-yves.didier__at__ibisc.univ-evry.fr
26 */
27 
28 
29 #include <arcs/arcssheet.h>
30 #include <arcs/arcsinternaltypes.h>
31 
32 #include <QMutableListIterator>
33 #include <QListIterator>
34 #include <iostream>
35 #include <QSet>
36 
37 ARCSSheet ARCSSheet::null;
38 
39 
41 {
42  name = n;
43  activated = false;
44  mutex = new QMutex();
45  context = ctx;
46 
47 }
48 
49 
51 {
52  context = sheet.context;
53  name = sheet.name;
54  connections = sheet.connections;
55  preconnects = sheet.preconnects;
56  postconnects = sheet.postconnects;
57  cleanups = sheet.cleanups;
58  properties = sheet.properties;
59  activated = false;
60  mutex = new QMutex();
61 }
62 
63 
64 void ARCSSheet::reset()
65 {
66  preconnects.clear();
67  connections.clear();
68  postconnects.clear();
69  cleanups.clear();
70  properties.clear();
71 }
72 
73 
75 {
76  QSet<QString> componentSet ;
77 
78  QListIterator<ARCSConnection> i(connections);
79  while(i.hasNext())
80  {
81  ARCSConnection c = i.next();
82  componentSet.insert(c.getSourceName());
83  componentSet.insert(c.getDestinationName());
84  }
85 
86  listComponentInit(preconnects,componentSet);
87  listComponentInit(postconnects,componentSet);
88  listComponentInit(cleanups,componentSet);
89  return componentSet.toList();
90 }
91 
92 void ARCSSheet::removeInitWithComponent(QList<ARCSInit>& lst,QString name)
93 {
94  QMutableListIterator<ARCSInit> i(lst);
95  while(i.hasNext())
96  {
97  i.next();
98  if (i.value().getDestinationName() == name)
99  i.remove();
100  }
101 
102 }
103 
104 
105 void ARCSSheet::removeComponent(QString name)
106 {
107  QMutableListIterator<ARCSConnection> i(connections);
108 
109  while(i.hasNext())
110  {
111  ARCSConnection& connect = i.next();
112  if (connect.getSourceName() == name)
113  {
114  i.remove();
115  }
116  else
117  {
118  if (connect.getDestinationName() == name)
119  i.remove();
120  }
121  }
122  removeInitWithComponent(preconnects,name);
123  removeInitWithComponent(postconnects,name);
124  removeInitWithComponent(cleanups,name);
125 }
126 
127 void ARCSSheet::listComponentInit(QList<ARCSInit> &lst, QSet<QString> &set)
128 {
129  QListIterator<ARCSInit> i(lst);
130  while(i.hasNext())
131  {
132  ARCSInit init = i.next();
133  set.insert(init.getDestinationName());
134  }
135 }
136 
137 
138 void ARCSSheet::batchInit(QList<ARCSInit> & lst)
139 {
140  QListIterator<ARCSInit> i(lst);
141  while(i.hasNext())
142  {
143  ARCSInit init = i.next();
144  if (init.isConstant())
145  init.init(context->getConstant(init.getValue().value<ARCSConstant>()));
146  else
147  {
148  if (init.isComponent())
149  {
150  ARCSAbstractComponent* cmp = context->getComponent(init.getValue().value<ARCSComponent>());
151  if (cmp != 0)
152  init.init(cmp);
153  }
154  else
155  init.init();
156  }
157  }
158 }
159 
160 
162 {
163  QListIterator<ARCSConnection> i(connections);
164  while(i.hasNext())
165  {
166  ARCSConnection c = i.next();
167  c.connect();
168  }
169 }
170 
171 
173 {
174  QListIterator<ARCSConnection> i(connections);
175  while(i.hasNext())
176  {
177  ARCSConnection c = i.next();
178  c.disconnect();
179  }
180 }
181 
182 
183 ARCSConnection& ARCSSheet::addConnection(QString src, QString sgn, QString dst, QString slt, bool q,bool head)
184 {
185  ARCSAbstractComponent* source = context->getComponent(src);
186  ARCSAbstractComponent* destination = context->getComponent(dst);
187 
188 
189  if (!source)
190  {
191  std::cerr << "Cannot find component named" << qPrintable(src) << std::endl;
192  }
193 
194 
195  ARCSConnection c(source, sgn, destination, slt, q);
196  if (head)
197  {
198  connections.prepend(c);
199  return connections.first();
200  }
201 
202  connections.append(c);
203  return connections.last();
204 }
205 
206 ARCSConnection& ARCSSheet::getConnection(QString src, QString sgn, QString dst, QString slt)
207 {
208  ARCSAbstractComponent* source = context->getComponent(src);
209  ARCSAbstractComponent* destination = context->getComponent(dst);
210 
211  ARCSConnection c(source, sgn, destination, slt);
212 
213  int idx= connections.indexOf(c);
214  if (idx != -1)
215  {
216  return connections[idx];
217  }
218  else
219  return const_cast<ARCSConnection&>(ARCSConnection::null);
220 }
221 
222 
223 int ARCSSheet::getConnectionIndex(QString src, QString sgn, QString dst, QString slt)
224 {
225  ARCSAbstractComponent* source = context->getComponent(src);
226  ARCSAbstractComponent* destination = context->getComponent(dst);
227  ARCSConnection c(source, sgn, destination, slt);
228  return connections.indexOf(c);
229 }
230 
231 void ARCSSheet::swapConnections(int x, int y)
232 {
233  if (x < connections.count() && y < connections.count() && x >= 0 && y >=0 )
234  connections.swap(x,y);
235 }
236 
237 
238 ARCSInit& ARCSSheet::createInit(QList<ARCSInit> & lst, QString dst, QString slt, QString t, QString val)
239 {
240  ARCSAbstractComponent* destination = context->getComponent(dst);
241  QVariant value = context->createValue(t,val);
242  ARCSInit i(destination, slt, value);
243  lst.append(i);
244  return lst.last();
245 }
246 
247 
248 void ARCSSheet::getConnections(QStringList& sources, QStringList& signls,
249  QStringList& destinations, QStringList& slts)
250 {
251  sources.clear();
252  signls.clear();
253  destinations.clear();
254  slts.clear();
255 
256  QListIterator<ARCSConnection> i(connections);
257  while(i.hasNext())
258  {
259  ARCSConnection c = i.next();
260 
261  sources << c.getSourceName();
262  signls << c.getSignalName() ;
263  destinations << c.getDestinationName();
264  slts << c.getSlotName();
265  }
266 }
267 
268 
269 void ARCSSheet::getInits(QList<ARCSInit> &lst, QStringList & destinations,
270  QStringList & slts, QStringList & types, QStringList& values)
271 {
272  destinations.clear();
273  slts.clear();
274  types.clear();
275  values.clear();
276 
277  QListIterator<ARCSInit> i(lst);
278  while(i.hasNext())
279  {
280  ARCSInit init = i.next();
281 
282  destinations << init.getDestinationName();
283  slts << init.getSlotName();
284  types << init.getValueType();
285  values << init.getValueString();
286  }
287 }
288 
289 
290 ARCSInit& ARCSSheet::getInit(QList<ARCSInit> &lst, QString dst, QString slt, QString t, QString val)
291 {
292  ARCSAbstractComponent* destination = context->getComponent(dst);
293 
294  QVariant value = context->createValue(t,val);
295  ARCSInit i(destination, slt, value);
296 
297  int idx= lst.indexOf(i);
298  if (idx != -1)
299  return lst[idx];
300  else
301  {
302  return ARCSInit::null;
303  }
304 }
305 
306 
307 void ARCSSheet::swapInits(QList<ARCSInit> &lst,int x, int y)
308 {
309  if (x < lst.count() && y < lst.count() && x >=0 && y >=0)
310  lst.swap(x,y);
311 }
ARCSSheet(ARCSContext *ctx=0, QString n=QString::null)
Constructor.
Definition: arcssheet.cpp:40
QString getSlotName()
bool disconnect()
Disconnects the connection.
void disconnect()
Definition: arcssheet.cpp:172
Desribes an initialization over a component.
Definition: arcsinit.h:46
QString getValueString()
Definition: arcsinit.cpp:110
Describes a connection.
Class handling the generic description of a component.
bool isConstant()
Definition: arcsinit.h:106
bool isComponent()
Definition: arcsinit.h:108
QStringList getComponentList()
Retrieves the list of components that are used in this sheet.
Definition: arcssheet.cpp:74
QString getSignalName()
bool connect()
Make the connection happen.
ARCSAbstractComponent * getComponent(QString name)
Retrieves a component by its id.
void getConnections(QStringList &sources, QStringList &signls, QStringList &destinations, QStringList &slts)
Allows to retrieve connections.
Definition: arcssheet.cpp:248
QVariant getValue()
Definition: arcsinit.h:75
void connect()
Definition: arcssheet.cpp:161
QVariant getConstant(QString name)
Retrieves a constant by its id.
This class manages components and constants in a given context.
Definition: arcscontext.h:45
ARCSConnection & addConnection(QString src, QString sgn, QString dst, QString slt, bool q=false, bool head=false)
Adds a connection to the sheet structure.
Definition: arcssheet.cpp:183
QString getSlotName()
Definition: arcsinit.h:73
QString getValueType()
Definition: arcsinit.cpp:102
bool init()
Definition: arcsinit.cpp:72
ARCSConnection & getConnection(QString source, QString sgn, QString dst, QString slt)
returns a reference to a connection in the sheet structure
Definition: arcssheet.cpp:206
Maintains connections between objects.
Definition: arcssheet.h:48