iCub-main
qticubskinguiplugin.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 RobotCub Consortium
3  * Author: Alessandro Scalzo alessandro.scalzo@iit.it
4  * CopyPolicy: Released under the terms of the GNU GPL v2.0.
5  *
6  */
7 
103 #include "qticubskinguiplugin.h"
104 #include <QDebug>
105 
106 using namespace yarp::os;
107 
108 
110  QQuickPaintedItem(parent)
111 {
112  // By default, QQuickItem does not draw anything. If you subclass
113  // QQuickItem to create a visual item, you will need to uncomment the
114  // following line and re-implement updatePaintNode()
115 
116  setFlag(ItemHasContents, true);
117 
118  gXpos = 32;
119  gYpos = 32;
120 
121  gWidth = 300;
122  gHeight = 300;
123  gRowStride = 0;
124  gImageArea = 0;
125  gImageSize = 0;
126  gMapSize = 0;
127 
128  gpActivationMap = NULL;
129  gpImageBuff = NULL;
130  gpSkinMeshThreadCan = NULL;
131  gpSkinMeshThreadPort = NULL;
132 
133  window_title = "QtICubSkinGui";
134 
135 
136  TheadType = TYPE_PORT;
137  timer.setInterval(50);
138  timer.setSingleShot(false);
139  connect(&timer,SIGNAL(timeout()),this,SLOT(onTimeout()));
140 
141 }
142 
144 {
145  timer.stop();
146  mutex.lock();
147 
148  if(gpSkinMeshThreadCan){
149  gpSkinMeshThreadCan->stop();
150  delete gpSkinMeshThreadCan;
151  }
152  if(gpSkinMeshThreadPort){
153  gpSkinMeshThreadPort->stop();
154  delete gpSkinMeshThreadPort;
155  }
156 
157  mutex.unlock();
158 }
159 
160 
161 void QtICubSkinGuiPlugin::paint(QPainter *painter)
162 {
163 
164  if(!timer.isActive()){
165  return;
166  }
167 
168  int width=painter->device()->width();
169  int height=painter->device()->height();
170 
171 
172  bDrawing=false;
173 
174  if (bDrawing){
175  return;
176  }
177 
178  bDrawing=true;
179 
180  mutex.lock();
181  if (width!=gWidth || height!=gHeight){
182  gWidth=width;
183  gHeight=height;
184 
185  gRowStride=3*gWidth;
186  gImageSize=gRowStride*gHeight;
187  gImageArea=gWidth*gHeight;
188  gMapSize=gWidth*gHeight*sizeof(double);
189 
190  if (gpActivationMap){
191  delete [] gpActivationMap;
192  }
193  if (gpImageBuff){
194  delete [] gpImageBuff;
195  }
196  gpActivationMap = new double[gImageArea];
197  gpImageBuff = new uchar[gImageSize];
198 
199  if(TheadType == TYPE_CAN && gpSkinMeshThreadCan && gWidth>=180 && gHeight>=180){
200  gpSkinMeshThreadCan->resize(gWidth,gHeight);
201  } else if (TheadType == TYPE_PORT && gpSkinMeshThreadPort && gWidth>=180 && gHeight>=180){
202  gpSkinMeshThreadPort->resize(gWidth,gHeight);
203  }
204  }
205 
206  if (TheadType == TYPE_CAN && gpSkinMeshThreadCan){
207  memset(gpImageBuff,0,gImageSize);
208 
209  if (gWidth>=180 && gHeight>=180){
210  gpSkinMeshThreadCan->eval(gpImageBuff);
211  gpSkinMeshThreadCan->draw(gpImageBuff);
212  }
213 
214  QImage img = QImage(gpImageBuff,gWidth,gHeight,gRowStride,QImage::Format_RGB32);
215  painter->beginNativePainting();
216  painter->fillRect(0,0,gWidth,gHeight,QColor("black"));
217  painter->drawImage(0,0,img);
218  painter->endNativePainting();
219 
220  }else if (TheadType == TYPE_PORT && gpSkinMeshThreadPort) {
221  memset(gpImageBuff,0,gImageSize);
222 
223  if (gWidth>=180 && gHeight>=180)
224  {
225  gpSkinMeshThreadPort->eval(gpImageBuff);
226  gpSkinMeshThreadPort->draw(gpImageBuff);
227  }
228 
229 
230  QImage img = QImage(gpImageBuff,gWidth,gHeight,gRowStride,QImage::Format_RGB888);
231 
232  painter->beginNativePainting();
233  painter->fillRect(0,0,gWidth,gHeight,QColor("black"));
234  painter->drawImage(0,0,img);
235  painter->endNativePainting();
236  }
237 
238  mutex.unlock();
239  bDrawing=false;
240 
241 }
242 
243 
247 bool QtICubSkinGuiPlugin::parseParameters(QStringList params)
248 {
249  Network yarp;
250 
251  rf.setDefaultContext("skinGui/skinGui");
252  rf.setDefaultConfigFile("left_hand.ini");
253 
254  // Transform Qt Params array in standard argc & argv
255  int c = params.count();
256  char **v;
257  v = (char**)malloc(sizeof(char*) * c);
258  for(int i=0;i<params.count();i++){
259  v[i] = (char*)malloc(sizeof(char) * params.at(i).length()+1);
260  strcpy(v[i],params.at(i).toLatin1().data());
261  }
262 
263  if(!rf.configure(c, v)){
264  free(v);
265  return false;
266  }
267 
268  gWidth =rf.find("width" ).asInt32();
269  gHeight=rf.find("height").asInt32();
270  if (rf.check("xpos")){
271  gXpos=rf.find("xpos").asInt32();
272  }
273  if (rf.check("ypos")){
274  gYpos=rf.find("ypos").asInt32();
275  }
276 
277  bool useCan = rf.check("useCan");
278  if (useCan==true){
279  yInfo("CAN version: Reading data directly from CAN");
280  TheadType=TYPE_CAN;
281  } else {
282  yInfo("YARP version: reading data from a Yarp port");
283  TheadType=TYPE_PORT;
284  }
285 
286 
287  window_title=QString("SkinGui (part: %1)").arg(rf.find("robotPart").asString().data());
289  posXChanged();
290  posYChanged();
291  widthChanged();
292  heightChanged();
293 
294  yDebug("RF: %s",rf.toString().data());
295 
296  gRowStride=3*gWidth;
297  gImageSize=gRowStride*gHeight;
298  gMapSize=gWidth*gHeight*sizeof(double);
299  gImageArea=gWidth*gHeight;
300 
301  gpActivationMap=new double[gImageArea];
302  gpImageBuff=new uchar[gImageSize];
303 
304  /***********/
305  // TODO
306  /***********/
307 
308  timer.start();
309 
310  connect(this,SIGNAL(init()),this,SLOT(onInit()),Qt::QueuedConnection);
311  init();
312 
313 
314 // if (gpActivationMap) delete [] gpActivationMap;
315 // if (gpImageBuff) delete [] gpImageBuff;
316 
317 
318 
319  // TODO DESTROY
320 
321  return true;
322 }
323 
324 
325 void QtICubSkinGuiPlugin::onTimeout()
326 {
327  update();
328 }
329 
330 void QtICubSkinGuiPlugin::onInit()
331 {
332  int period = rf.check("period")?rf.find("period").asInt32():50;
333 
334  if (TheadType==TYPE_CAN)
335  {
336  gpSkinMeshThreadCan=new SkinMeshThreadCan(rf,period);
337  gpSkinMeshThreadCan->start();
338 
339  }
340  else if (TheadType==TYPE_PORT)
341  {
342  gpSkinMeshThreadPort=new SkinMeshThreadPort(rf,period);
343  gpSkinMeshThreadPort->start();
344  }
345 
346  done();
347 }
348 
351 {
352  return window_title;
353 }
354 
357 {
358  return gXpos;
359 }
360 
363 {
364  return gYpos;
365 }
366 
369 {
370  return gWidth;
371 }
372 
375 {
376  return gHeight;
377 }
void paint(QPainter *painter)
int windowHeight
Returns the height.
int posX
Returns the x position.
QtICubSkinGuiPlugin(QQuickItem *parent=0)
QString windowTitle
Returns the title.
Q_INVOKABLE bool parseParameters(QStringList params)
parse the parameters received from the main container in QstringList form
int posY
Returns the y position.
int windowWidth
Returns the width.
void draw(unsigned char *image)
void eval(unsigned char *image)
void resize(int width, int height)
void eval(unsigned char *image)
void resize(int width, int height)
void draw(unsigned char *image)
Copyright (C) 2008 RobotCub Consortium.