YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
qtyarpview.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6#include "qtyarpview.h"
7#include <QThread>
8using namespace yarp::os;
9
10QtYARPView::QtYARPView(QQuickItem *parent):
11 QQuickItem(parent), yarp(yarp::os::YARP_CLOCK_SYSTEM), sigHandler(this)
12{
13 ptr_portCallback = nullptr;
14 setOptionsToDefault();
15 _pOutPort = nullptr;
16 _pOutRightPort = nullptr;
17
18 connect(&sigHandler,SIGNAL(sendFrame(QVideoFrame*)),&videoProducer,
19 SLOT(onNewVideoContentReceived(QVideoFrame*)),Qt::DirectConnection);
20
21 connect(&sigHandler,SIGNAL(sendFps(double,double,double,double,double,double)),
22 this, SLOT(onSendFps(double,double,double,double,double,double)));
23
24 connect(&videoProducer,SIGNAL(resizeWindowRequest()),
25 this, SLOT(onWindowSizeChangeRequested()));
26
27 createObjects();
28}
29
31{
32 closePorts();
33 deleteObjects();
34
35 if (_options.m_saveOnExit != 0){
36 saveOptFile(_options.m_fileName);
37 }
38}
39
44void QtYARPView::freeze(bool check)
45{
46 sigHandler.freeze(check);
47}
48
54{
55 return &videoProducer;
56}
57
60{
61 return _options.m_posX;
62}
63
66{
67 return _options.m_posY;
68}
69
72{
73 return _options.m_windWidth;
74}
75
78{
79 return _options.m_windHeight;
80}
81
82
88{
89 sigHandler.synchDisplayPeriod(check);
90}
91
97{
98 sigHandler.synchDisplaySize(check);
99}
100
109
112{
113 sigHandler.saveCurrentFrame();
114}
115
121{
122 sigHandler.setFileName(url);
123}
124
130{
131 sigHandler.setFileNames(url);
132}
133
136{
137 sigHandler.startDumpFrames();
138}
139
142{
143 sigHandler.stopDumpFrames();
144}
145
152
153 QString rgbToReturn = videoProducer.getPixelAsStr(x,y);
154
155 return rgbToReturn;
156}
157
158void QtYARPView::periodToFreq(double avT, double mT, double MT, double &avH, double &mH, double &MH)
159{
160 if (avT != 0) {
161 avH=1.0/avT;
162 } else {
163 avH = 0;
164 }
165
166 if (mT != 0) {
167 MH=1.0/mT;
168 } else {
169 MH = 0;
170 }
171
172 if (MT != 0) {
173 mH=1.0/MT;
174 } else {
175 mH = 0;
176 }
177}
178
179void QtYARPView::onSendFps(double portAvg, double portMin, double portMax,
180 double dispAvg, double dispMin, double dispMax)
181{
182 double pAvg,pMin,pMax,dAvg,dMin,dMax;
183
184 periodToFreq(portAvg,portMin,portMax,pAvg,pMin,pMax);
185 periodToFreq(dispAvg,dispMin,dispMax,dAvg,dMin,dMax);
186
187
188 emit sendPortFps(QString::number(pAvg,'f',1),
189 QString::number(pMin,'f',1),
190 QString::number(pMax,'f',1));
191
192 emit sendDisplayFps(QString::number(dAvg,'f',1),
193 QString::number(dMin,'f',1),
194 QString::number(dMax,'f',1));
195
196}
197
202{
203 return _options.m_refreshTime;
204}
205
206
208void QtYARPView::createObjects() {
209#ifdef YARP_LITTLE_ENDIAN
211#else
213#endif
214 ptr_portCallback = new InputCallback;
215 ptr_portCallback->setSignalHandler(&sigHandler);
216}
217
219void QtYARPView::deleteObjects() {
220 if (ptr_inputPort != nullptr) {
221 delete ptr_inputPort;
222 }
223 if (ptr_portCallback != nullptr) {
224 delete ptr_portCallback;
225 }
226}
227
232{
233 Property options;
234 int c = params.count();
235 char **v;
236 v = (char**)malloc(sizeof(char*) * c);
237
238 for(int i=0;i<params.count();i++){
239 v[i] = strdup(params.at(i).toLatin1().data());
240 }
241
242 options.fromCommand(c,v,false);
243
244 for(int i=0;i<params.count();i++) {
245 free(v[i]);
246 }
247 free(v);
248
249 // If the user asks for help, let's print it and return.
250 if (options.check("help"))
251 {
252 printHelp();
253 return false;
254 }
255
257 qCritical("Cannot connect to yarp server");
258 return false;
259 }
260
261 // Otherwise, simply set the options asked
262 setOptions(options);
263
264 if (!openPorts()){
265 qDebug("Error open ports");
266 return false;
267 }
268 ptr_inputPort->useCallback(*ptr_portCallback);
269 return true;
270}
271
273void QtYARPView::setOptions(yarp::os::Searchable& options) {
274 // switch to subsections if available
275 yarp::os::Value *val;
276 if (options.check("PortName",val)||options.check("name",val)) {
277 qsnprintf(_options.m_portName, 256, "%s", val->asString().c_str());
278 qDebug("%s", val->asString().c_str());
279 }
280 if (options.check("Title",val)||options.check("title",val)) {
281 qsnprintf(_options.m_title, 256, "%s", val->asString().c_str());
282 qDebug("%s", val->asString().c_str());
283 }
284 else{
285 if(options.check("compact")){
286 qsnprintf(_options.m_title, 256, "%s", _options.m_portName);
287 }
288 }
289 if (options.check("NetName",val)||options.check("n",val)) {
290 qsnprintf(_options.m_networkName, 256, "%s", val->asString().c_str());
291 }
292 if (options.check("OutPortName",val)||options.check("out",val)) {
293 qsnprintf(_options.m_outPortName, 256, "%s", val->asString().c_str());
294 }
295 if (options.check("OutRightPortName",val)||options.check("rightout",val)) {
296 qsnprintf(_options.m_outRightPortName, 256, "%s", val->asString().c_str());
297 _options.m_rightEnabled = true;
298 }
299 if (options.check("OutNetName",val)||options.check("neto",val)) {
300 qsnprintf(_options.m_outNetworkName, 256, "%s", val->asString().c_str());
301 }
302 if (options.check("RefreshTime",val)||options.check("p",val)) {
303 _options.m_refreshTime = val->asInt32();
304 sigHandler.changeRefreshInterval(_options.m_refreshTime);
306 }
307 if (options.check("PosX",val)||options.check("x",val)) {
308 _options.m_posX = val->asInt32();
310 }
311 if (options.check("PosY",val)||options.check("y",val)) {
312 _options.m_posY = val->asInt32();
314 }
315 if (options.check("Width",val)||options.check("w",val)) {
316 _options.m_windWidth = val->asInt32();
318 }
319 if (options.check("Height",val)||options.check("h",val)) {
320 _options.m_windHeight = val->asInt32();
322 }
323 if (options.check("OutputEnabled",val)) {
324 _options.m_outputEnabled = val->asInt32();
325 }
326 if (options.check("out",val)) {
327 _options.m_outputEnabled = true;
328 }
329 if (options.check("SaveOptions",val)||options.check("saveoptions",val)) {
330 _options.m_outputEnabled = val->asInt32();
331 }
332 if (options.check("synch"))
333 {
334 _options.m_synchRate=true;
335 synchDisplayPeriod(true);
336 emit synchRate(true);
337 }
338 if (options.check("autosize"))
339 {
340 _options.m_autosize = true;
341 synchDisplaySize(true);
342 emit autosize(true);
343 }
344
346}
347
349void QtYARPView::printHelp()
350{
351 qDebug("yarpview usage:");
352 qDebug(" --name: input port name (default: /yarpview/img:i)");
353 qDebug(" --title: A title for the yarpview window");
354 qDebug(" (default");
355 qDebug(" - compact flag enabled: input port name");
356 qDebug(" - compact flag disabled: \"YARP Qt Image Viewer\"");
357 qDebug(" )");
358 qDebug(" --x: x position of the window in the screen");
359 qDebug(" --y: y position of the window in the screen");
360 qDebug(" --w: width of the window");
361 qDebug(" --h: height of the window");
362 qDebug(" --p: refresh time [ms]");
363 qDebug(" --synch: synchronous display, every image received by the input port is displayed");
364 qDebug(" --autosize: the display automatically resizes on every new frame");
365 qDebug(" --out: output port name (no default is given, if this option is not specified the port is not created)");
366 qDebug(" --rightout: output port for right click name (no default is given, if this option is not specified the port is not created)");
367 qDebug(" --neto: output network");
368 qDebug(" --neti: input network");
369 qDebug(" --compact: if this flag is enabled, no status bar will be showed");
370 qDebug(" --minimal: if this flag is enabled, neither the status bars nor the title bar/menu bar will be showed. It overwrites --compact.");
371 qDebug(" --keep-above: keep windows above others");
372}
373
375void QtYARPView::setOptionsToDefault()
376{
377 // Options defaults
378 _options.m_refreshTime = 100;
379 qsnprintf(_options.m_portName, 256, "%s","/yarpview/img:i");
380 qsnprintf(_options.m_title, 256, "%s","");
381 qsnprintf(_options.m_networkName, 256, "%s", "default");
382 qsnprintf(_options.m_outPortName, 256, "%s","/yarpview/o:point");
383 qsnprintf(_options.m_outRightPortName, 256, "%s","/yarpview/r:o:point");
384 qsnprintf(_options.m_outNetworkName, 256, "%s", "default");
385 _options.m_outputEnabled = 0;
386 _options.m_rightEnabled = 0;
387 _options.m_windWidth = 300;
388 _options.m_windHeight = 300;
389 _options.m_posX = 100;
390 _options.m_posY = 100;
391 _options.m_autosize = false;
392 qsnprintf(_options.m_fileName, 256, "%s","yarpview.conf");
393 _options.m_saveOnExit = 0;
394
399
400 sigHandler.changeRefreshInterval(_options.m_refreshTime);
402}
403
404
408bool QtYARPView::openPorts()
409{
410 bool ret = false;
411
412 ptr_inputPort->setReadOnly();
413 ret= ptr_inputPort->open(_options.m_portName);
414 emit setName(ptr_inputPort->getName().c_str());
415 emit setTitle(_options.m_title);
416
417 if (!ret){
418 qDebug("Error: port failed to open, quitting.");
419 return false;
420 }
421
422 if (_options.m_outputEnabled == 1){
424 qDebug("Registering port %s on network %s...", _options.m_outPortName, _options.m_outNetworkName);
425 bool ok = _pOutPort->open(_options.m_outPortName);
426 if (ok) {
427 qDebug("Port registration succeed!");
428 }
429 else{
430 _pOutPort = nullptr;
431 qDebug("ERROR: Port registration failed.\nQuitting, sorry.");
432 return false;
433 }
434 }
435
436 if (_options.m_rightEnabled == 1){
437 _pOutRightPort = new yarp::os::BufferedPort<yarp::os::Bottle>;
438 qDebug("Registering port %s on network %s...", _options.m_outRightPortName, _options.m_outNetworkName);
439 bool ok = _pOutRightPort->open(_options.m_outRightPortName);
440 if (ok) {
441 qDebug("Port registration succeed!");
442 }
443 else{
444 _pOutRightPort = nullptr;
445 qDebug("ERROR: Port registration failed.\nQuitting, sorry.");
446 return false;
447 }
448 }
449
450 return true;
451}
452
454void QtYARPView::closePorts()
455{
456
457 ptr_inputPort->close();
458
459 if (_options.m_outputEnabled == 1 && _pOutPort){
460 _pOutPort->close();
461 bool ok = true;
462 if (ok) {
463 qDebug("Port %s unregistration succeed!\n", _options.m_outPortName);
464 } else {
465 qDebug("ERROR: Port %s unregistration failed.\n", _options.m_outPortName);
466 }
467 delete _pOutPort;
468 _pOutPort = nullptr;
469 }
470
471 if (_options.m_rightEnabled == 1 && _pOutRightPort){
472 _pOutRightPort->close();
473 bool ok = true;
474 if (ok) {
475 qDebug("Port %s unregistration succeed!\n", _options.m_outRightPortName);
476 } else {
477 qDebug("ERROR: Port %s unregistration failed.\n", _options.m_outRightPortName);
478 }
479 delete _pOutRightPort;
480 _pOutRightPort = nullptr;
481 }
482}
483
487void QtYARPView::saveOptFile(char *fileName)
488{
489 //FILE *optFile = NULL;
490 QFile optFile(QString("%1").arg(fileName));
491
492 bool opened = optFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
493 if (!opened){
494 qDebug("ERROR: Impossible to save to option file.");
495 return;
496 }
497 optFile.write((QString("PortName %1\n").arg(_options.m_portName)).toLatin1().data());
498 optFile.write((QString("NetName %1\n").arg(_options.m_networkName)).toLatin1().data());
499 optFile.write((QString("OutPortName %1\n").arg(_options.m_outPortName)).toLatin1().data());
500 optFile.write((QString("OutNetName %s\n").arg(_options.m_outNetworkName)).toLatin1().data());
501 optFile.write((QString("RefreshTime %d\n").arg(_options.m_refreshTime)).toLatin1().data());
502 optFile.write((QString("PosX %d\n").arg(_options.m_posX)).toLatin1().data());
503 optFile.write((QString("PosY %d\n").arg(_options.m_posY)).toLatin1().data());
504 optFile.write((QString("Width %d\n").arg(_options.m_windWidth)).toLatin1().data());
505 optFile.write((QString("Height %d\n").arg(_options.m_windHeight)).toLatin1().data());
506 optFile.write((QString("OutputEnables %d\n").arg(_options.m_outputEnabled)).toLatin1().data());
507 optFile.write((QString("SaveOptions %d\n").arg(_options.m_saveOnExit)).toLatin1().data());
508 optFile.write((QString("synchRate %d\n").arg(_options.m_synchRate)).toLatin1().data());
509 optFile.write((QString("autosize %d\n").arg(_options.m_autosize)).toLatin1().data());
510 optFile.close();
511}
512
514{
515 int imageWidth, imageHeight;
516
517 imageWidth = videoProducer.getWidth();
518 imageHeight = videoProducer.getHeight();
519
520 if ((imageWidth != 0) && (imageHeight != 0)) {
521 qDebug("Transmitting click information...");
522 if (_pOutPort != nullptr) {
523 yarp::os::Bottle& bot = _pOutPort->prepare();
524 bot.clear();
525 bot.addInt32(start_x);
526 bot.addInt32(start_y);
527 bot.addInt32(end_x);
528 bot.addInt32(end_y);
529 //_pOutPort->Content() = _outBottle;
530 _pOutPort->write();
531 }
532
533 }
534 else {
535 qDebug("I would send a position, but there's no image for scaling");
536 }
537}
538
540{
541 int imageWidth, imageHeight;
542
543 imageWidth = videoProducer.getWidth();
544 imageHeight = videoProducer.getHeight();
545
546 if ( (imageWidth != 0) && (imageHeight != 0) ) {
547 qDebug("Transmitting click information...");
548 if (_pOutPort!=nullptr) {
549 yarp::os::Bottle& bot = _pOutPort->prepare();
550 bot.clear();
551 bot.addInt32(x);
552 bot.addInt32(y);
553 //_pOutPort->Content() = _outBottle;
554 _pOutPort->write();
555 }
556
557 } else {
558 qDebug("I would send a position, but there's no image for scaling");
559 }
560}
561
563{
564 int imageWidth, imageHeight;
565
566 imageWidth = videoProducer.getWidth();
567 imageHeight = videoProducer.getHeight();
568
569 if ((imageWidth != 0) && (imageHeight != 0)) {
570 qDebug("Transmitting click information...");
571 if (_pOutRightPort != nullptr) {
572 yarp::os::Bottle& bot = _pOutRightPort->prepare();
573 bot.clear();
574 bot.addInt32(start_x);
575 bot.addInt32(start_y);
576 bot.addInt32(end_x);
577 bot.addInt32(end_y);
578 //_pOutPort->Content() = _outBottle;
579 _pOutRightPort->write();
580 }
581
582 }
583 else {
584 qDebug("I would send a position, but there's no image for scaling");
585 }
586}
587
589{
590 int imageWidth, imageHeight;
591
592 imageWidth = videoProducer.getWidth();
593 imageHeight = videoProducer.getHeight();
594
595 if ( (imageWidth != 0) && (imageHeight != 0) ) {
596 qDebug("Transmitting click information...");
597 if (_pOutRightPort!=nullptr) {
598 yarp::os::Bottle& bot = _pOutRightPort->prepare();
599 bot.clear();
600 bot.addInt32(x);
601 bot.addInt32(y);
602 //_pOutPort->Content() = _outBottle;
603 _pOutRightPort->write();
604 }
605
606 } else {
607 qDebug("I would send a position, but there's no image for scaling");
608 }
609}
610
612{
613 return _options.m_rightEnabled;
614}
615
616void QtYARPView::onWindowSizeChangeRequested()
617{
618 if (sigHandler.getAutosizeMode())
619 {
621 }
622}
bool ret
int SIGNAL(int pid, int signum)
This class is a callback class that receives the video frame from the YARP backend.
Definition ImagePort.h:31
void setSignalHandler(SignalHandler *)
sets the signalhandler to the class
Definition ImagePort.cpp:84
int posX
Returns the x position from the options.
Definition qtyarpview.h:62
Q_INVOKABLE void setFilesName(QUrl url)
Sets the current filename of the frame set is going to be saved.
void setName(QString name)
Q_INVOKABLE void clickCoords_4(int start_x, int start_y, int end_x, int end_y)
Q_INVOKABLE QString getPixelAsStr(int x, int y)
Pics the rgb value of the pixel specified by x and y and return it as a string.
Q_INVOKABLE void saveFrame()
Saves the current frame.
void setTitle(QString inputTitle)
Q_INVOKABLE void clickCoords_2(int x, int y)
Q_INVOKABLE void rightClickCoords_2(int x, int y)
int posY
Returns the y position from the options.
Definition qtyarpview.h:63
void autosize(bool check)
Q_INVOKABLE void startDumpFrames()
Saves the current frame set.
int windowWidth
Returns the width from the options.
Definition qtyarpview.h:64
Q_INVOKABLE bool rightClickEnabled()
int windowHeight
Returns the height from the options.
Definition qtyarpview.h:65
void refreshIntervalChanged()
void synchRate(bool check)
void optionsSet()
Q_INVOKABLE void stopDumpFrames()
Stops the current frame set save mode.
void sendPortFps(QString avg, QString min, QString max)
Q_INVOKABLE void rightClickCoords_4(int start_x, int start_y, int end_x, int end_y)
void sizeChanged()
void posXChanged()
Q_INVOKABLE void synchDisplayPeriod(bool check)
Synchs the video stream to the display.
void widthChanged()
QtYARPView(QQuickItem *parent=0)
Q_INVOKABLE void setFileName(QUrl url)
Sets the current filename of the frame is going to be saved.
void heightChanged()
Q_INVOKABLE void changeRefreshInterval(int)
Changes the refresh interval.
void sendDisplayFps(QString avg, QString min, QString max)
QObject * videoProducer
Definition qtyarpview.h:61
void posYChanged()
Q_INVOKABLE void synchDisplaySize(bool check)
Synchs the size of the window with the size of the video stream.
Q_INVOKABLE bool parseParameters(QStringList)
parse the parameters received from the main container in QstringList form
QObject * getVideoProducer()
returns the videoproducer that acts as bridge between C++ code and QML VideoOutput.
int refreshInterval
Gets the refresh interval from options.
Definition qtyarpview.h:66
Q_INVOKABLE void freeze(bool check)
Freeze the video stream.
void setFileNames(QUrl url)
Sets the filename used for saving a video frame set.
void stopDumpFrames()
Stops the Dump frame modality (Save frame set).
void saveCurrentFrame()
Enable the save curretn frame mode.
void changeRefreshInterval(int ineterval)
Sets the refresh interval.
void freeze(bool check)
Enable/Disable the freeze mode.
void setFileName(QUrl url)
Sets the filename used for saving a video frame.
void startDumpFrames()
Enables the Dump frame modality (Save frame set).
void synchDisplayPeriod(bool check)
Enable/Disable the synch mode.
void synchDisplaySize(bool check)
Enable/Disable the synch size mode.
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
Definition Bottle.cpp:140
A mini-server for performing network communication in the background.
std::string getName() const override
Get name of port.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void useCallback(TypedReaderCallback< T > &callback) override
Set an object whose onRead method will be called when data is available.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
void setReadOnly()
Shorthand for setInputMode(true), setOutputMode(false), setRpcMode(false)
static bool checkNetwork()
Check if the YARP Network is up and running.
Definition Network.cpp:1370
A class for storing options and configuration information.
Definition Property.h:33
bool check(const std::string &key) const override
Check if there exists a property of the given name.
void fromCommand(int argc, char *argv[], bool skipFirst=true, bool wipe=true)
Interprets a list of command arguments as a list of properties.
A base class for nested structures that can be searched.
Definition Searchable.h:31
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
A single value (typically within a Bottle).
Definition Value.h:43
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition Value.cpp:204
virtual std::string asString() const
Get string value.
Definition Value.cpp:234
An interface to the operating system, including Port based communication.
@ YARP_CLOCK_SYSTEM
Definition Time.h:28
The main, catch-all namespace for YARP.
Definition dirs.h:16
int m_windHeight
Definition qtyarpview.h:34
bool m_synchRate
Definition qtyarpview.h:44
char m_outPortName[256]
Definition qtyarpview.h:39
bool m_autosize
Definition qtyarpview.h:45
unsigned int m_refreshTime
Definition qtyarpview.h:29
int m_saveOnExit
Definition qtyarpview.h:38
char m_outNetworkName[256]
Definition qtyarpview.h:41
char m_outRightPortName[256]
Definition qtyarpview.h:40
int m_rightEnabled
Definition qtyarpview.h:43
char m_fileName[256]
Definition qtyarpview.h:37
int m_posY
Definition qtyarpview.h:36
int m_posX
Definition qtyarpview.h:35
char m_title[256]
Definition qtyarpview.h:31
char m_portName[256]
Definition qtyarpview.h:30
char m_networkName[256]
Definition qtyarpview.h:32
int m_windWidth
Definition qtyarpview.h:33
int m_outputEnabled
Definition qtyarpview.h:42