This example requires Qt5 >= 5.9 setup for building.
7#include "main_widget.hpp"
9#include <cvb/image.hpp>
10#include <cvb/utilities/system_info.hpp>
11#include <cvb/ui/ui.hpp>
12#include <cvb/ui/opengl_image_renderer_factory.hpp>
15PTResultWidget::PTResultWidget()
18 setWindowIcon(
QIcon(
":/qttutorial.png"));
20 for (
int i = 0; i < 7; i++)
24 lables <<
"Image Size" <<
"View Size" <<
"Zoom" <<
"Format" <<
"Upload" <<
"Rendering" <<
"FPs";
25 setHorizontalHeaderLabels(lables);
27 auto* header = horizontalHeader();
28 header->setSectionResizeMode(QHeaderView::Stretch);
29 auto font = header->font();
31 header->setFont(font);
33 setWindowTitle(
"Performance Test Results");
37void PTResultWidget::AddResult(
const PTResult& result)
39 auto row = rowCount();
42 SetResultItem(row, 0, ImageSizeToString(result.ImageSize));
43 SetResultItem(row, 1, ViewSizeToString(result.ViewSize));
44 SetResultItem(row, 2, ZoomToString(result.Zoom));
45 SetResultItem(row, 3, PlanesToString(result.Planes));
46 SetResultItem(row, 4, UploadModeToString(result.UploadMode));
53 renderEngineToolTip =
QString(
"Rendered with \"QPainter\"");
60 renderEngineToolTip = vendor +
QString(
"\n") + renderer;
63 SetResultItem(row, 5, RenderEngineToString(result.RenderEngine), renderEngineToolTip);
64 SetResultItem(row, 6, FPsToString(result.FPS));
67void PTResultWidget::SetResultItem(
int row,
int column,
const QString & text,
const QString & toolTip)
70 cell->setTextAlignment(Qt::AlignCenter);
71 cell->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
73 cell->setToolTip(toolTip);
74 setItem(row, column, cell);
82QString PTResultWidget::ViewSizeToString(
QSize size)
const
89 switch (zoomDescriptor.
ID)
124QString PTResultWidget::PlanesToString(
int planes)
const
126 return (planes == 1) ?
"Mono" :
"Colour";
136 switch (renderEngine)
152QString PTResultWidget::FPsToString(
double fps)
const
157PTSetupWidget::PTSetupWidget(
QWidget * parent, PTResultWidget * ptResultWidget)
159 , ptResultWidget_(ptResultWidget)
161 spMeasurementCount_ =
new QSpinBox(
this);
162 spRefreshCount_ =
new QSpinBox(
this);
165 auto pbHideAndRunPT =
new QPushButton(
"Hide & Run");
167 auto groupBox =
new QGroupBox(
"Performance Test for Render Engine");
172 spLayout->
addWidget(spMeasurementCount_);
173 spLayout->addWidget(spRefreshCount_);
175 pbLayout->addStretch();
176 pbLayout->addWidget(pbHide);
177 pbLayout->addWidget(pbHideAndRunPT);
178 pbLayout->addStretch();
180 mainLayout->addLayout(spLayout);
181 mainLayout->addLayout(pbLayout);
183 groupBox->setLayout(mainLayout);
189 spMeasurementCount_->setPrefix(
"Measurement Count: ");
190 spMeasurementCount_->setRange(2, 10000);
191 spMeasurementCount_->setValue(measurementCount_);
192 spMeasurementCount_->setToolTip(
"Number of measurements used to get a result as mean.");
194 spRefreshCount_->setPrefix(
"Refresh Count: ");
195 spRefreshCount_->setRange(500, 1000000);
196 spRefreshCount_->setValue(refreshCount_);
197 spRefreshCount_->setToolTip(
"Number of display refreshes executed for a single measurement.");
199 connect(spMeasurementCount_, SIGNAL(valueChanged(
int)),
this, SLOT(HandleMeasurementCountChanged(
int)));
200 connect(spRefreshCount_, SIGNAL(valueChanged(
int)),
this, SLOT(HandleRefreshCountChanged(
int)));
202 connect(pbHide, SIGNAL(clicked()),
this, SLOT(HandleHide()));
203 connect(pbHideAndRunPT, SIGNAL(clicked()),
this, SLOT(HandleHideAndRunPT()));
206void PTSetupWidget::HandleHide() noexcept
211void PTSetupWidget::HandleHideAndRunPT() noexcept
214 if (!ptResultWidget_->isVisible())
217 ptResultWidget_->show();
222void PTSetupWidget::HandleRefreshCountChanged(
int value)
noexcept
224 refreshCount_ = value;
227void PTSetupWidget::HandleMeasurementCountChanged(
int value)
noexcept
229 measurementCount_ = value;
234MainWidget::MainWidget(PTResultWidget * ptResultWidget)
236 , ptResultWidget_(ptResultWidget)
239 ptSetupWidget_ =
new PTSetupWidget(
this, ptResultWidget_);
244 mainLayout->addWidget(
new QLabel(tr(
"Right click to get the context menu."),
this));
245 mainLayout->addWidget(ptSetupWidget_);
247 setLayout(mainLayout);
250 imageView_->Refresh(image);
253 performaceTimer_.setTimerType(Qt::TimerType::PreciseTimer);
255 connect(ptSetupWidget_, SIGNAL(RunPT()),
this, SLOT(HandlePT()));
256 connect(&performaceTimer_, SIGNAL(timeout()),
this, SLOT(HandleTimeout()));
258 ptSetupWidget_->hide();
263void MainWidget::HandleLoadImage()
266 if (fileName.isEmpty())
272 imageView_->Refresh(image);
281void MainWidget::HandlePixelValue()
284 if (!mouseMoveCookie_)
289 titleStream <<
"(X:" << mousePos.
X() <<
", Y:" << mousePos.
Y() <<
") ";
290 if (pixelVal.size() == 3)
291 titleStream <<
"(R:" << qFloor(pixelVal[0]) <<
", G:" << qFloor(pixelVal[1]) <<
", B:" << qFloor(pixelVal[2]) <<
")";
292 else if (pixelVal.size() == 1)
293 titleStream <<
"(V:" << qFloor(pixelVal[0]) <<
")";
300 imageView_->UnregisterEventMouseMoved(mouseMoveCookie_);
305void MainWidget::HandleRenderEngineChanged(
QAction* action)
310 imageView_->SetRenderEngine(renderEngine);
311 imageView_->Refresh();
320void MainWidget::HandleUploadModeChange(
QAction* action)
325 imageView_->SetUploadMode(uploadMode);
334void MainWidget::HandlePT()
336 ptState_ = PTState::Running;
337 rateCounter_.SetWindowSize(ptSetupWidget_->MeasurementCout());
340 performaceTimer_.start();
343void MainWidget::HandleTimeout()
345 if (testCycles_ % ptSetupWidget_->RefreshCount() == 0)
350 if (rateCounter_.NumSteps() == rateCounter_.WindowSize())
352 performaceTimer_.stop();
353 if (ptState_ == PTState::Running)
355 auto rate = rateCounter_.Rate();
356 AppendResult(rate *
static_cast<double>(ptSetupWidget_->RefreshCount()));
357 rateCounter_.Reset();
358 ptState_ = PTState::Idle;
364 imageView_->Refresh();
368void MainWidget::HandleClearResults()
370 while (ptResultWidget_->rowCount())
371 ptResultWidget_->removeRow(0);
374void MainWidget::ShowProgress() noexcept
376 ptResultWidget_->setWindowTitle(
QString(
"Performance Test ")
377 + ((ptState_ == PTState::Running) ?
QString(
"Running") :
QString(
"Setup"))
378 +
QString(
": %1").arg(rateCounter_.NumSteps())
379 +
QString(
" / %1").arg(rateCounter_.WindowSize()));
383void MainWidget::AppendResult(
double fps)
387 result.Planes = imageView_->Image()->PlanesCount();
389 result.RenderEngine = imageView_->RenderEngine();
390 result.ViewSize = imageView_->size();
391 result.ImageSize = imageView_->Image()->Size();
392 result.UploadMode = imageView_->UploadMode();
393 result.Zoom = imageView_->Zoom();
395 ptResultWidget_->setWindowTitle(
"Performance Test Results");
396 ptResultWidget_->AddResult(result);
404 if (windowState() & Qt::WindowFullScreen
405 || ptState_ != PTState::Idle)
410 auto acLoadImage = contextMenu.
addAction(
"Load Image");
413 auto acShowFullScreen = contextMenu.
addAction(
"Show Full Screen");
414 auto acPixelValues = contextMenu.
addAction(
"Pixel Values");
416 acPixelValues->setChecked(mouseMoveCookie_);
419 auto mnRenderEngine = contextMenu.
addMenu(
"Render Engine");
422 auto acRenderEngineRaster = agRenderEngine->
addAction(
"Raster");
425 auto acRenderEngineGL2 = agRenderEngine->
addAction(
"OpenGL 2");
428 auto acRenderEngineGL3 = agRenderEngine->
addAction(
"OpenGL 3");
432 switch (imageView_->RenderEngine())
436 acRenderEngineRaster->setChecked(
true);
440 acRenderEngineGL2->setChecked(
true);
444 acRenderEngineGL3->setChecked(
true);
448 mnRenderEngine->addActions(agRenderEngine->
actions());
450 auto mnUploadMode = contextMenu.
addMenu(
"Upload Mode");
453 auto acUploadModeViewPort = agUploadMode->
addAction(
"Viewport");
456 auto acUploadModeImage = agUploadMode->
addAction(
"Image");
460 switch (imageView_->UploadMode())
464 acUploadModeViewPort->setChecked(
true);
468 acUploadModeImage->setChecked(
true);
472 mnUploadMode->addActions(agUploadMode->
actions());
475 auto mnPT = contextMenu.
addMenu(
"Performance Test");
476 auto acSetupTest = mnPT->addAction(
"Setup Test");
477 auto acShowResults = mnPT->addAction(
"Show Results");
478 auto acClearResults = mnPT->addAction(
"Clear Results");
480 connect(acLoadImage, SIGNAL(triggered()),
this, SLOT(HandleLoadImage()));
481 connect(acPixelValues, SIGNAL(triggered()),
this, SLOT(HandlePixelValue()));
482 connect(agRenderEngine, SIGNAL(triggered(
QAction*)),
this, SLOT(HandleRenderEngineChanged(
QAction*)));
483 connect(agUploadMode, SIGNAL(triggered(
QAction*)),
this, SLOT(HandleUploadModeChange(
QAction*)));
484 connect(acShowFullScreen, SIGNAL(triggered()),
this, SLOT(showFullScreen()));
485 connect(acSetupTest, SIGNAL(triggered()), ptSetupWidget_, SLOT(show()));
486 connect(acShowResults, SIGNAL(triggered()), ptResultWidget_, SLOT(show()));
487 connect(acClearResults, SIGNAL(triggered()),
this, SLOT(HandleClearResults()));
489 contextMenu.
exec(mapToGlobal(event->
pos()));
492void MainWidget::keyPressEvent(
QKeyEvent *event)
494 if (ptState_ == PTState::Running)
502 if (ptSetupWidget_->isVisible())
503 ptSetupWidget_->hide();
505 ptSetupWidget_->show();
516 if (ptResultWidget_->isVisible())
517 ptResultWidget_->close();
519 ptResultWidget_->show();
521 else if (event->
key() == Qt::Key_Escape)
Cookie used to unregister event handlers.
Definition: global.hpp:535
static std::unique_ptr< Image > Load(const String &fileName)
Loads an image with the given file name.
Definition: detail_image.hpp:32
T X() const noexcept
Gets the x-component of the point.
Definition: point_2d.hpp:86
T Y() const noexcept
Gets the y-component of the point.
Definition: point_2d.hpp:106
T Height() const noexcept
Gets the vertical component of the size.
Definition: size_2d.hpp:79
T Width() const noexcept
Gets the horizontal component of the size.
Definition: size_2d.hpp:59
View to display an image.
Definition: decl_image_view.hpp:75
static String VendorName(int version)
Get the name of the OpenGL vendor.
Definition: detail_opengl_image_renderer_factory.hpp:88
static String RendererName(int version)
Gets the name of the OpenGL renderer.
Definition: detail_opengl_image_renderer_factory.hpp:93
Cvb::String QtToCvb(const QString text) noexcept
Convenience converter for strings.
Definition: ui.hpp:239
QString CvbToQt(const Cvb::String &text) noexcept
Convenience converter for strings.
Definition: ui.hpp:254
QVariant data() const const
QList< QAction * > actions() const const
QAction * addAction(QAction *action)
QDesktopWidget * desktop()
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
Qt::KeyboardModifiers keyboardModifiers()
QMessageBox::StandardButton critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isEmpty() const const
int toInt(bool *ok) const const
ZoomID ID
Zoom identifier for known factors.
Definition: detail_ui.hpp:161