This example requires Qt5 >= 5.9 setup for building.
1#include "main_widget.hpp"
7#include <QDesktopWidget>
11#include <QMutexLocker>
13#include <QRadioButton>
14#include <QWeakPointer>
16#include <cvb/device_factory.hpp>
18MainWidget::MainWidget()
21 chkGrab_(new
QCheckBox(
"&Grab", this)), gbSelectedPort_(new
QGroupBox(
"Selected Port", this)),
22 gbStatistics_(new
QGroupBox(
"Statistic", this)), cbSelectedPort_(new
QComboBox(gbSelectedPort_)),
23 view_(new
Cvb::UI::ImageView(this)), device_(
Cvb::DeviceFactory::Open(
Cvb::InstallPath() + CVB_LIT(
"drivers/CVMock.vin"),
24 Cvb::AcquisitionStack::Vin)),
25 streamH_(
Cvb::Async::SingleStreamHandler::Create(device_->Stream())), updateStatTimer_(new
QTimer(this))
27 qDebug() <<
"Please wait for the setup to load...";
51 gbStatistics_->setLayout(gbStatEntriesLayout_);
58 gbSelectedPortLayout->
addWidget(cbSelectedPort_);
60 gbSelectedPort_->setLayout(gbSelectedPortLayout);
71 setLayout(mainLayout);
74 connect(pbOpen, SIGNAL(clicked()),
this, SLOT(onOpen()));
75 connect(pbOpenDev, SIGNAL(clicked()),
this, SLOT(onDiscover()));
76 connect(pbSave, SIGNAL(clicked()),
this, SLOT(onSave()));
77 connect(pbSnap_, SIGNAL(clicked()),
this, SLOT(onSnaped()));
78 connect(chkGrab_, SIGNAL(stateChanged(
int)),
this, SLOT(onGrabChanged(
int)));
79 connect(cbSelectedPort_, SIGNAL(currentIndexChanged(
int)),
this, SLOT(onPortIndexChanged(
const int)));
81 connect(updateStatTimer_, SIGNAL(timeout()), SLOT(updateStatistics()));
82 updateStatTimer_->start(100);
88 view_->SetWaitForRepaintEnabled(
false);
94MainWidget::~MainWidget()
100void MainWidget::onGrabChanged(
int state)
103 state == Qt::Unchecked ? streamH_->Finish() : streamH_->Run();
106void MainWidget::onOpen()
109 SignalGuard guard(
this);
114 if (streamH_->IsActive())
117 chkGrab_->setChecked(
false);
122 this, tr(
"Open Image"),
QString(
"/"), tr(
"CImages (*.bmp *.jpg *.png *.tif *.tiff)"));
138 std::cout << e.what() <<
"\nError opening image!" << std::endl;
142void MainWidget::onDiscover()
144 SignalGuard guard(
this);
145 std::cout <<
"Discovering devices..." << std::endl;
148 cbSelectedPort_->clear();
149 gbSelectedPort_->setEnabled(
true);
150 for (
const auto& device : discover_)
158 cbSelectedPort_->addItem(
QString(
"Device %1").arg(device.Index()),
static_cast<int32_t
>(device.Index()));
160 cbSelectedPort_->setCurrentIndex(0);
162 loadDiscoveredDevice(0ul);
165void MainWidget::onSave()
168 SignalGuard guard(
this);
171 if (streamH_->IsActive())
174 chkGrab_->setChecked(
false);
179 this, tr(
"Save Image"),
QString(
"~/"), tr(
"CImages (*.bmp *.jpg *.png *.tif *.tiff)"));
184void MainWidget::onSnaped()
187 if (streamH_->IsActive())
190 chkGrab_->setChecked(
false);
193 auto waitResult = device_->Stream()->GetSnapshot();
195 if (waitResult.Image ==
nullptr)
199void MainWidget::onPortIndexChanged(
const int index)
202 loadDiscoveredDevice(index);
205void MainWidget::loadDiscoveredDevice(
const unsigned long devNum)
207 SignalGuard guard(
this);
208 std::cout <<
"Loading device..." << std::endl;
211 if (streamH_->IsActive())
214 chkGrab_->setChecked(
false);
223 std::cout <<
"Loading device " << discover_.at(devNum).AccessToken() << std::endl;
234 initializeStatistics();
239 std::cout << e.what() <<
"\nError changing discovered device!" << std::endl;
243void MainWidget::updateStatistics()
245 SignalGuard guard(
this);
246 unsigned long currentStat = 0;
247 for (
auto const& stat : statisticsList_)
250 auto const statEnumName = std::get<0>(stat);
251 auto value = device_->Stream()->Statistics(statEnumName);
253 statValues_.at(currentStat)->setText(statValue);
258void MainWidget::initializeStatistics()
260 std::cout <<
"Creating device statistic..." << std::endl;
265 for (
auto& valWdg : statValues_)
267 gbStatEntriesLayout_->removeWidget(valWdg);
270 for (
auto& nameWdg : statNames_)
272 gbStatEntriesLayout_->removeWidget(nameWdg);
275 for (
auto& lay : gbHStatNameValLayout_)
278 gbStatEntriesLayout_->removeItem(lay);
283 statisticsList_.clear();
284 statisticsList_.push_back(tuple_element(StreamInfo::IsCameraDetected,
"IsCameraDetected"));
285 statisticsList_.push_back(tuple_element(StreamInfo::NumBuffersAcquired,
"NumBuffersAcquired"));
286 statisticsList_.push_back(tuple_element(StreamInfo::NumBuffersAnnounced,
"NumBuffersAnnounced"));
287 statisticsList_.push_back(tuple_element(StreamInfo::NumBuffersPending,
"NumBuffersPending"));
288 statisticsList_.push_back(tuple_element(StreamInfo::NumBuffersBeingFilled,
"NumBuffersBeingFilled"));
289 statisticsList_.push_back(tuple_element(StreamInfo::NumBuffersDelivered,
"NumBuffersDelivered"));
290 statisticsList_.push_back(tuple_element(StreamInfo::NumBuffersLocked,
"NumBuffersLocked"));
291 statisticsList_.push_back(tuple_element(StreamInfo::NumBuffersLost,
"NumBuffersLost"));
292 statisticsList_.push_back(tuple_element(StreamInfo::NumBuffersLostLocked,
"NumBuffersLostLocked"));
293 statisticsList_.push_back(tuple_element(StreamInfo::NumBuffersLostTransfer,
"NumBuffersLostTransfer"));
294 statisticsList_.push_back(tuple_element(StreamInfo::NumBuffersQueued,
"NumBuffersQueued"));
295 statisticsList_.push_back(tuple_element(StreamInfo::NumPacketsReceived,
"NumPacketsReceived"));
296 statisticsList_.push_back(tuple_element(StreamInfo::NumResends,
"NumResends"));
297 statisticsList_.push_back(tuple_element(StreamInfo::NumTriggersLost,
"NumTriggersLost"));
302 gbHStatNameValLayout_.clear();
303 unsigned long currentStat = 0;
304 int unavailableStatisticPos = -1;
306 for (
auto const& stat : statisticsList_)
310 ++unavailableStatisticPos;
312 auto const statEnumName = std::get<0>(stat);
313 auto value = device_->Stream()->Statistics(statEnumName);
315 statValues_.push_back(
new QLabel(tr(statValue.c_str())));
316 statValues_.back()->setAlignment(Qt::AlignLeft);
317 statValues_.back()->updatesEnabled();
320 statNames_.push_back(
new QLabel(tr(std::get<1>(stat).c_str())));
324 gbHStatNameValLayout_.at(currentStat)->addWidget(statNames_.back());
325 statValues_.back()->setAlignment(Qt::AlignRight);
326 gbHStatNameValLayout_.at(currentStat)->addWidget(statValues_.back());
328 gbStatEntriesLayout_->addLayout(gbHStatNameValLayout_.at(currentStat));
333 std::cout <<
"Removing statistic due to error: " << e.what() << std::endl;
335 delEntries.push_back(unavailableStatisticPos);
340 for (
auto ri = delEntries.rbegin(); ri != delEntries.rend(); ++ri)
341 statisticsList_.erase(statisticsList_.begin() + *ri);
343 std::cout <<
"Done initializing" << std::endl;
346void MainWidget::initalizeTooltip()
349 view_->setMouseTracking(
true);
357 if (pixelVal.size() == 3)
359 titleStream <<
"(X:" << mousePos.X() <<
", Y:" << mousePos.Y() <<
") (R:" << qFloor(pixelVal[0])
360 <<
", G:" << qFloor(pixelVal[1]) <<
", B:" << qFloor(pixelVal[2]) <<
")";
362 else if (pixelVal.size() == 1)
364 titleStream <<
"(X:" << mousePos.X() <<
", Y:" << mousePos.Y() <<
") (Mono:" << qFloor(pixelVal[0]) <<
")";
368 titleStream <<
"(X:" << mousePos.X() <<
", Y:" << mousePos.Y() <<
") (Mono:";
369 for (auto& pix : pixelVal)
370 titleStream << qFloor(pix) <<
" | ";
378void MainWidget::blockUI(
const bool block)
380 chkGrab_->blockSignals(block);
381 pbSnap_->blockSignals(block);
382 cbSelectedPort_->blockSignals(block);
383 view_->blockSignals(block);
384 gbSelectedPort_->blockSignals(block);
385 gbStatistics_->blockSignals(block);
386 updateStatTimer_->blockSignals(block);
static std::unique_ptr< SingleStreamHandler > Create(const Driver::StreamPtr &stream)
Create a stream handler object.
Definition: decl_single_stream_handler.hpp:46
Special runtime exception to carry a native error code.
Definition: exception.hpp:139
static std::vector< DiscoveryInformation > Discover()
Discovers available devices (not vins) with a default time span of 300ms.
Definition: decl_device_factory.hpp:216
static std::shared_ptr< T > Open(const String &provider, AcquisitionStack acquisitionStack=AcquisitionStack::PreferVin)
Opens a device with the given provider with its default board and port (if applicable).
Definition: decl_device_factory.hpp:50
static std::unique_ptr< Image > Load(const String &fileName)
Loads an image with the given file name.
Definition: detail_image.hpp:32
Namespace for driver or device related operations.
Definition: decl_composite.hpp:28
@ DeviceModel
Device only : Model name.
@ UpToLevelVin
Limit discovery depth to vin-driver level.
@ UploadAndScheduleRepaint
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
void addLayout(QLayout *layout, int stretch)
void addStretch(int stretch)
std::string toStdString() const const
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
void setOverrideCursor(const QCursor &cursor)
QString fromStdString(const std::string &str)
bool isEmpty() const const
QString number(int n, int base)
QByteArray toLatin1() const const
std::string toStdString() const const