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