NCL Composer  0.1.5
 All Classes Functions Variables Pages
qnstutil.cpp
1 #include "qnstutil.h"
2 
3 #include "qnstgraphicsmedia.h"
4 #include "qnstgraphicsbody.h"
5 #include "qnstgraphicscontext.h"
6 #include "qnstgraphicsswitch.h"
7 #include "qnstgraphicsport.h"
8 #include "qnstgraphicsswitchport.h"
9 #include "qnstgraphicsarea.h"
10 #include "qnstgraphicsproperty.h"
11 #include "qnstgraphicslink.h"
12 
13 /* Initialize icon from type Map */
14 std::map <Qnst::EntityType, QString> QnstUtil::iconFromTypeMap =
16  (Qnst::Text, ":/icon/text")
17  (Qnst::Image, ":/icon/image")
18  (Qnst::Audio, ":/icon/audio")
19  (Qnst::Video, ":/icon/video")
20  (Qnst::Html, ":/icon/html")
21  (Qnst::NCL, ":/icon/ncl")
22  (Qnst::Settings, ":/icon/settings")
23  (Qnst::NCLua, ":/icon/script")
24  (Qnst::Media, ":/icon/media")
25 
26  (Qnst::Context, ":icon/context")
27  (Qnst::Switch, ":icon/switch")
28  (Qnst::Body, ":icon/body")
29 
30  (Qnst::Port, ":icon/port")
31  (Qnst::SwitchPort, ":icon/switchport")
32  (Qnst::Area, ":icon/area")
33  (Qnst::Aggregator, ":icon/aggregator") // \fixme This is here only for
34  // compatibility with versions prior
35  // to 0.1.3
36  (Qnst::Link, ":icon/aggregator");
37 
38 /* Initialize type from extension Map */
39 std::map <QString, Qnst::EntityType> QnstUtil::typeFromExtMap =
41  ("txt", Qnst::Text)
42 
43  ("png", Qnst::Image)
44  ("jpg", Qnst::Image)
45  ("jpeg", Qnst::Image)
46  ("gif", Qnst::Image)
47 
48  ("mp3", Qnst::Audio)
49  ("wav", Qnst::Audio)
50 
51  ("mp4", Qnst::Video)
52  ("mpeg4", Qnst::Video)
53  ("mpeg", Qnst::Video)
54  ("mpg", Qnst::Video)
55  ("mov", Qnst::Video)
56  ("avi", Qnst::Video)
57 
58  ("htm", Qnst::Html)
59  ("html", Qnst::Html)
60 
61  ("ncl", Qnst::NCL)
62 
63  ("lua", Qnst::NCLua);
64 
65 /* Initialize map from str type to qnsttype */
66 std::map <QString, Qnst::EntityType> QnstUtil::typeFromStr =
68  ("body", Qnst::Body)
69  ("context", Qnst::Context)
70  ("port", Qnst::Port)
71 
72  ("switch", Qnst::Switch)
73  ("switchPort", Qnst::SwitchPort)
74  ("mapping", Qnst::Mapping)
75 
76  ("media", Qnst::Media)
77  ("area", Qnst::Area)
78  ("property", Qnst::Property)
79 
80  ("link", Qnst::Link)
81  ("bind", Qnst::Bind)
82  ("bindParam", Qnst::BindParam)
83 
84  /*("causalConnector", Qnst::)
85  ("simpleCondition", Qnst::Condition)
86  ("simpleAction", Qnst::Action)*/
87 
88  ("aggregator", Qnst::Aggregator);
89 
90 std::map <Qnst::EntityType, QString> QnstUtil::strFromType =
91  invert_map<Qnst::EntityType, QString>(QnstUtil::typeFromStr);
92 
93 /* Initialize prefix id from type Map */
94 std::map <Qnst::EntityType, QString> QnstUtil::prefixIdFromType =
96  (Qnst::Media, "m")
97  (Qnst::Image, "m")
98  (Qnst::Audio, "m")
99  (Qnst::Video, "m")
100  (Qnst::Html, "m")
101  (Qnst::NCL, "m")
102  (Qnst::Settings, "m")
103  (Qnst::NCLua, "m")
104  (Qnst::Media, "m")
105 
106  (Qnst::Context, "ctx")
107  (Qnst::Switch, "swt")
108  (Qnst::Body, "body")
109 
110  (Qnst::Port, "p")
111  (Qnst::SwitchPort, "swp")
112  (Qnst::Area, "area")
113 
114  (Qnst::Aggregator, "link") // \fixme This is here only for compatibility
115  // with versions prior to 0.1.3.
116  (Qnst::Link, "link");
117 
118 /* Initialize BindType from Role string Map */
119 std::map <QString, Qnst::BindType> QnstUtil::bindTypeFromRoleStr =
121  ("onBegin", Qnst::onBegin)
122  ("onEnd", Qnst::onEnd)
123  ("onSelection", Qnst::onSelection)
124  ("onResume", Qnst::onResume)
125  ("onPause", Qnst::onPause)
126  ("onBeginAttribution", Qnst::onBeginAttribution)
127  ("onEndAttribution", Qnst::onEndAttribution)
128  ("onPauseAttribution", Qnst::onPauseAttribution)
129  ("onResumeAttribution", Qnst::onResumeAttribution)
130 
131  ("start", Qnst::Start)
132  ("stop", Qnst::Stop)
133  ("resume", Qnst::Resume)
134  ("pause", Qnst::Pause)
135  ("set", Qnst::Set);
136 
137 /* Initialize String from Qnst::BindType Map */
138 std::map <Qnst::BindType, QString> QnstUtil::strFromBindType =
139  invert_map<Qnst::BindType, QString> (QnstUtil::bindTypeFromRoleStr);
140 
141 
143  QnstGraphicsEntity *parent,
144  const QMap <QString, QString> &props)
145 {
146  QnstGraphicsEntity *entity = NULL;
147 
148  switch (type)
149  {
150  case Qnst::Image:
151  case Qnst::Audio:
152  case Qnst::Video:
153  case Qnst::Text:
154  case Qnst::NCLua:
155  case Qnst::Settings:
156  case Qnst::Html:
157  case Qnst::NCL:
158  case Qnst::Media:
159  entity = new QnstGraphicsMedia(parent);
160  entity->setnstType(type);
161  break;
162 
163  case Qnst::Body:
164  entity = new QnstGraphicsBody(parent);
165  break;
166 
167  case Qnst::Context:
168  entity = new QnstGraphicsContext(parent);
169  break;
170 
171  case Qnst::Switch:
172  entity = new QnstGraphicsSwitch(parent);
173  break;
174 
175  case Qnst::Port:
176  entity = new QnstGraphicsPort(parent);
177  break;
178 
179  case Qnst::SwitchPort:
180  entity = new QnstGraphicsSwitchPort(parent);
181  break;
182 
183  case Qnst::Area:
184  entity = new QnstGraphicsArea(parent);
185  break;
186 
187  case Qnst::Property:
188  entity = new QnstGraphicsProperty(parent);
189  break;
190 
191  case Qnst::Aggregator: // \fixme Aggregator is here only for compatibility
192  // with versions prior to 0.1.3.
193  case Qnst::Link:
194  entity = new QnstGraphicsLink(parent);
195  break;
196 
197  default:
198  // do nothing
199  // We will return NULL;
200  break;
201  }
202 
203  if(entity)
204  entity->setProperties(props);
205 
206  return entity;
207 }
208 
209 Qnst::EntityType QnstUtil::getnstTypeFromMime(const QString &mimeType)
210 {
211  Qnst::EntityType type;
212 
213  if (mimeType.startsWith("image"))
214  type = Qnst::Image;
215  else if (mimeType.startsWith("audio"))
216  type = Qnst::Audio;
217  else if (mimeType.startsWith("video"))
218  type = Qnst::Video;
219  else if (mimeType == "text/html")
220  type = Qnst::Html;
221  else if (mimeType == "application/x-ginga-NCL")
222  type = Qnst::NCL;
223  else if (mimeType.startsWith("text"))
224  type = Qnst::Text;
225  else if (mimeType == "application/x-ncl-settings" ||
226  mimeType == "application/x-ginga-settings")
227  type = Qnst::Settings;
228  else if (mimeType == "application/x-ginga-NCLua")
229  type = Qnst::NCLua;
230  else
231  type = Qnst::Media;
232 
233  return type;
234 }
235 
236 QString QnstUtil::iconFromMediaType(Qnst::EntityType type)
237 {
238  QString path;
239  if(iconFromTypeMap.count(type))
240  path = iconFromTypeMap[type];
241  else
242  path = ":/icon/media";
243 
244  return path;
245 }
246 
247 Qnst::EntityType QnstUtil::getnstTypeFromExtension(const QString &ext)
248 {
249  if(typeFromExtMap.count(ext))
250  return typeFromExtMap[ext];
251 
252  return Qnst::Media;
253 }
254 
255 Qnst::EntityType QnstUtil::getnstTypeFromStr(const QString &strType)
256 {
257  if(typeFromStr.count(strType))
258  return typeFromStr[strType];
259 
260  return Qnst::NoType;
261 }
262 
263 QString QnstUtil::getStrFromNstType(Qnst::EntityType type)
264 {
265  if(strFromType.count(type))
266  return strFromType[type];
267 
268  else return "";
269 }
270 
271 QString QnstUtil::getPrefixIdFromType(Qnst::EntityType type)
272 {
273  if(prefixIdFromType.count(type))
274  return prefixIdFromType[type];
275 
276  return "unknown";
277 }
278 
279 Qnst::BindType QnstUtil::getBindTypeFromStr(const QString &role)
280 {
281  if(bindTypeFromRoleStr.count(role))
282  return bindTypeFromRoleStr[role];
283 
284  return Qnst::NoBindType;
285 }
286 
287 QString QnstUtil::getStrFromBindType(Qnst::BindType type)
288 {
289  if(strFromBindType.count(type))
290  return strFromBindType[type];
291 
292  return QString("NoBindType");
293 }