Cvb/QtStreamDisplayPair

This example requires Qt5 >= 5.9 setup for building.

You may build it with Ubuntu 18.04's default Qt5 after installing:

1 // ---------------------------------------------------------------------------
5 // ---------------------------------------------------------------------------
6 
7 #include <iostream>
8 
9 #include <QDebug>
10 #include <QApplication>
11 #include <QWidget>
12 #include <QHBoxLayout>
13 #include <QIcon>
14 
15 #include <cvb/device.hpp>
16 #include <cvb/driver/stream.hpp>
17 #include <cvb/driver/stream.hpp>
18 #include <cvb/device_factory.hpp>
19 
20 #include <cvb/ui/image_view.hpp>
21 #include <cvb/async/single_stream_handler.hpp>
22 
23 // MainWidget with two displays (original and plane 0)
24 class MainWidget
25  : public QWidget
26 {
27  public:
28 
29  MainWidget()
30  : QWidget()
31  {
32  masterView_ = new Cvb::UI::ImageView(this);
33  slaveView_ = new Cvb::UI::ImageView(this);
34 
35  masterView_->SetUploadMode(Cvb::UI::UploadMode::Viewport);
36  masterView_->CustomWaitForRepaint([&]()
37  {
38  masterView_->viewport()->repaint();
39  slaveView_->viewport()->repaint();
40  });
41 
42  slaveView_->SetRefreshMode(Cvb::UI::RefreshMode::UploadOnly);
43 
44  auto mainLayout = new QHBoxLayout(this);
45  setLayout(mainLayout);
46 
47  mainLayout->addWidget(masterView_);
48  mainLayout->addWidget(slaveView_);
49  }
50 
51  void Run(const Cvb::Device & device)
52  {
53  // the display of the image
54  slaveView_->Refresh(device.DeviceImage(), Cvb::UI::AutoRefresh::On);
55  // the display of the image with plane 0
56  masterView_->Refresh(device.DeviceImage()->Plane(0).Map());
57  customAcquisitonHandler_ = CustomStreamHandler::Create(device.Stream(), *this);
58  customAcquisitonHandler_->Run();
59  }
60 
61  protected:
62 
63  void closeEvent(QCloseEvent *event) override
64  {
65  masterView_->SetWaitForRepaintEnabled(false);
66  QWidget::closeEvent(event);
67  }
68 
69 
70  private:
71 
72  class CustomStreamHandler
74  {
75  public:
76 
77  static std::unique_ptr<CustomStreamHandler> Create(const Cvb::StreamPtr& stream, MainWidget& mainWidget)
78  {
79  return std::unique_ptr<CustomStreamHandler>(new CustomStreamHandler(stream, mainWidget));
80  }
81 
82  ~CustomStreamHandler()
83  {
84  TryFinish();
85  }
86 
87  private:
88 
89  CustomStreamHandler(const Cvb::StreamPtr& stream, MainWidget& mainWidget)
90  : Cvb::SingleStreamHandler(stream)
91  , mainWidget_(mainWidget)
92  {
93  }
94 
95 
96 
97  void HandleAsyncStream(const Cvb::StreamPtr & stream) override
98  {
99  auto waitResult = stream->WaitFor(std::chrono::milliseconds(10000));
100  if (!waitResult.Image)
101  return;
102 
103  mainWidget_.masterView_->Refresh(waitResult.Image->Plane(0).Map());
104  }
105 
106  MainWidget& mainWidget_;
107 
108  };
109 
110  Cvb::UI::ImageView* masterView_ = nullptr;
111  Cvb::UI::ImageView* slaveView_ = nullptr;
112 
113  std::unique_ptr<CustomStreamHandler> customAcquisitonHandler_;
114 };
115 
116 int main(int argc, char* argv[])
117 {
118  try
119  {
120 
121  QApplication app(argc, argv);
122  auto path = Cvb::InstallPath();
123  path += CVB_LIT("drivers/GenICam.vin");
124  // use command line argument if provided
125  if (argc > 1)
126  {
127  std::string inputPath(argv[1]);
128  path = Cvb::String(inputPath.begin(), inputPath.end());
129  }
130 
131  // expand environment variables in path
132  path = Cvb::ExpandPath(path);
133 
134  // open a device
135  auto device = Cvb::DeviceFactory::Open(path, Cvb::AcquisitionStack::Vin);
136 
137  // connect the device image to the UI
138  MainWidget mainWidget;
139  mainWidget.setWindowIcon(QIcon(":/qttutorial.png"));
140  mainWidget.Run(*device);
141  mainWidget.resize(880, 600);
142  mainWidget.show();
143 
144  return app.exec();
145  }
146  catch (const std::exception& error)
147  {
148  std::cout << error.what() << std::endl;
149  }
150 }
std::string String
String for wide characters or unicode characters.
Definition: string.hpp:45
STL class.
void setLayout(QLayout *layout)
Handler object for a single stream.
Definition: decl_single_stream_handler.hpp:32
STL class.
virtual StreamPtr Stream() const
Get the stream for this device.
Definition: detail_device.hpp:24
STL class.
std::shared_ptr< T > DeviceImage() const
Gets, if available, the device image pointing to the last synchronized image.
Definition: decl_device.hpp:139
QWidget(QWidget *parent, Qt::WindowFlags f)
View to display an image.
Definition: decl_image_view.hpp:70
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
virtual void closeEvent(QCloseEvent *event)
Generic CVB physical device.
Definition: decl_device.hpp:38