31 #include <QProgressBar>
34 #include <SimoxUtility/algorithm/string/string_tools.h>
51 const Progress invalid_progress{0, 1, -1};
52 const Progress indefinite_progress{0, 0, 0};
54 void set_progress(QProgressBar* progress_bar,
const Progress& progress)
56 progress_bar->setMinimum(progress.min);
57 progress_bar->setMaximum(progress.max);
58 progress_bar->setValue(progress.val);
66 m_image_provider_name{image_provider_name}
68 m_widget.setupUi(
this);
70 connect(m_widget.remove_image_provider, &QPushButton::clicked,
this, &ImageProviderConfigWidget::image_provider_removed);
72 const QString q_image_provider_name = QString::fromStdString(m_image_provider_name);
73 m_widget.configs->setTitle(QString{
"Image provider: "} + q_image_provider_name);
76 set_progress(m_widget.write_progress, ::invalid_progress);
77 m_widget.ice_proxy_finder_placeholder->deleteLater();
80 m_blink_timer =
new QTimer(
this);
81 m_blink_timer->setSingleShot(
false);
82 m_blink_timer->setInterval(750);
83 connect(m_blink_timer, &QTimer::timeout,
this, &ImageProviderConfigWidget::blink);
87 m_widget.ice_proxy_finder->layout()->addWidget(m_ice_proxy_finder);
88 m_ice_proxy_finder->setIceManager(iceman);
89 m_ice_proxy_finder->setDefaultSelectedProxyAsItem(q_image_provider_name);
90 m_ice_proxy_finder->setSearchMask(
"*");
91 connect(m_ice_proxy_finder->getProxyNameComboBox(), SIGNAL(editTextChanged(QString)),
this, SLOT(image_provider_renamed(QString)));
94 std::vector<imrec::ChannelPreferences> channel_prefs_list;
95 std::vector<imrec::ChannelPreferences> reported_channel_prefs_list = get_channel_prefs();
96 const bool use_default_channel_prefs = reported_channel_prefs_list.size() != channel_configs.size();
97 for (
unsigned int i = 0; i < channel_configs.size(); ++i)
99 imrec::ChannelPreferences channel_prefs;
100 if (use_default_channel_prefs)
103 channel_prefs.requiresLossless =
false;
107 channel_prefs = reported_channel_prefs_list.at(i);
109 channel_prefs_list.push_back(channel_prefs);
112 build_ui_from_configs(channel_configs, channel_prefs_list);
125 m_widget.configs->setTitle(QString::fromStdString(
"Image provider: " + new_name));
126 m_ice_proxy_finder->setDefaultSelectedProxyAsItem(QString::fromStdString(new_name));
127 m_image_provider_name = new_name;
128 update_channel_list();
136 ARMARX_WARNING <<
"Invalid name '" << new_name <<
"' for image provider (already taken).";
140 std::vector<visionx::imrec::ChannelConfig>
143 const bool disabled =
false ;
144 std::vector<visionx::imrec::ChannelConfig> ccs;
145 std::transform(m_channel_config_widgets.begin(), m_channel_config_widgets.end(),
146 std::back_inserter(ccs), [&](
const auto * ccw)
148 return ccw->toChannelConfig(disabled);
157 const std::optional<imrec::State> old_state = m_current_state;
158 std::optional<imrec::State> new_state;
161 new_state =
status.value().type;
168 const imrec::Status
s =
status.value();
170 if (
s.type == imrec::State::ready and
s.framesWritten == 0 and
s.framesBuffered == 0)
172 progress = ::invalid_progress;
174 else if (
s.type == imrec::State::scheduled)
176 progress = ::indefinite_progress;
181 progress.max =
s.framesWritten +
s.framesBuffered;
182 progress.val =
s.framesWritten;
187 progress = ::invalid_progress;
190 set_progress(m_widget.write_progress, progress);
192 m_current_state = new_state;
193 if (old_state != new_state)
195 emit imageProviderStateChanged(old_state, new_state);
206 m_widget.line->show();
207 m_widget.channel_configs->show();
208 m_widget.buttons->show();
209 m_widget.label_name->show();
210 m_widget.ice_proxy_finder->show();
213 m_widget.line->hide();
214 m_widget.channel_configs->hide();
215 m_widget.buttons->hide();
216 m_widget.label_name->hide();
217 m_widget.ice_proxy_finder->hide();
224 visionx::ImageProviderConfigWidget::build_ui_from_configs(
const std::vector<imrec::ChannelConfig>& configs,
const std::vector<imrec::ChannelPreferences>& prefs_list)
230 m_widget.channel_configs->layout()->removeWidget(config_widget);
231 config_widget->deleteLater();
234 for (
unsigned int i = 0; i < configs.size(); ++i)
236 add_channel_conf_widget(configs.at(i), prefs_list.at(i));
242 visionx::ImageProviderConfigWidget::add_channel_conf_widget(
const imrec::ChannelConfig& channel_config,
const imrec::ChannelPreferences& channel_prefs)
245 ARMARX_CHECK(is_channel_name_unique(channel_config.name));
247 ChannelConfigWidget* ccw =
new ChannelConfigWidget{m_widget.channel_configs, channel_config, channel_prefs};
249 m_channel_config_widgets.push_back(ccw);
250 m_widget.channel_configs->layout()->addWidget(ccw);
257 visionx::ImageProviderConfigWidget::update_channel_list()
259 std::vector<imrec::ChannelPreferences> prefs_list = get_channel_prefs();
261 if (prefs_list.empty())
266 const bool image_provider_changed = m_channel_config_widgets.size() != prefs_list.size();
267 if (image_provider_changed)
270 for (ChannelConfigWidget* ccw : m_channel_config_widgets)
272 m_widget.channel_configs->layout()->removeWidget(ccw);
275 m_channel_config_widgets.clear();
278 std::vector<imrec::ChannelConfig> ccs;
279 for (
const imrec::ChannelPreferences& prefs : prefs_list)
281 imrec::ChannelConfig cc;
283 cc.name = prefs.name;
287 build_ui_from_configs(ccs, prefs_list);
292 std::vector<visionx::imrec::ChannelPreferences>
293 visionx::ImageProviderConfigWidget::get_channel_prefs()
297 return m_iceman->getProxy<RecordingImageProviderInterfacePrx>(m_image_provider_name,
"")->getImageRecordingChannelPreferences();
307 visionx::ImageProviderConfigWidget::is_channel_name_unique(
const std::string& name)
309 return std::find_if(m_channel_config_widgets.begin(), m_channel_config_widgets.end(), [&](
const auto & e)
311 return e->getChannelName() == name;
312 }) == m_channel_config_widgets.end();
317 visionx::ImageProviderConfigWidget::state_changed(std::optional<imrec::State>, std::optional<imrec::State> new_state)
319 if (new_state.has_value() and (new_state.value() == imrec::State::running or new_state.value() == imrec::State::writing))
321 if (new_state.value() == imrec::State::running)
323 m_current_icon_name =
"imrec_running";
327 m_current_icon_name =
"imrec_write";
329 m_blink_timer->start();
333 m_blink_timer->stop();
338 if (new_state.has_value())
340 switch (new_state.value())
342 case imrec::State::ready:
343 m_widget.status->setText(
"ready");
344 icon_name =
":/icons/imrec_ready.ico";
346 case imrec::State::scheduled:
347 m_widget.status->setText(
"scheduled");
348 icon_name =
":/icons/imrec_scheduled.ico";
350 case imrec::State::running:
351 m_widget.status->setText(
"recording");
352 icon_name =
":/icons/imrec_running.ico";
354 case imrec::State::stopping:
355 m_widget.status->setText(
"stopping");
356 icon_name =
":/icons/imrec_stopping.ico";
358 case imrec::State::writing:
359 m_widget.status->setText(
"writing");
360 icon_name =
":/icons/imrec_write.ico";
363 m_widget.status->setText(
"offline");
364 icon_name =
":/icons/imrec_offline.ico";
370 m_widget.status->setText(
"not configured");
371 icon_name =
":/icons/imrec_not_configured.ico";
375 icon.addFile(icon_name, QSize(), QIcon::Normal,
QIcon::On);
376 m_widget.icon_placeholder->setPixmap(icon.pixmap(20, 20));
381 visionx::ImageProviderConfigWidget::blink()
386 icon_name = QString::fromStdString(
":/icons/" + m_current_icon_name +
".ico");
390 icon_name = QString::fromStdString(
":/icons/" + m_current_icon_name +
"_dark.ico");
392 m_icon_blink = not m_icon_blink;
395 icon.addFile(icon_name, QSize(), QIcon::Normal,
QIcon::On);
396 m_widget.icon_placeholder->setPixmap(icon.pixmap(20, 20));
401 visionx::ImageProviderConfigWidget::channel_renamed(
const std::string& old_name,
const std::string& new_name)
403 auto it = std::find_if(m_channel_config_widgets.begin(), m_channel_config_widgets.end(), [&](
const auto &
c)
405 return c->getChannelName() == old_name;
407 if (it == m_channel_config_widgets.end())
409 ARMARX_ERROR <<
"Internal error, a channel was renamed which is not known to parent "
410 <<
"widget. Old name: '" << old_name <<
"'. New name: '" << new_name <<
"'.";
413 ChannelConfigWidget* ccw = *it;
414 is_channel_name_unique(new_name) ? ccw->confirmChannelName(new_name) : ccw->rejectChannelName(new_name);
419 visionx::ImageProviderConfigWidget::image_provider_renamed(
const QString& q_new_name)
421 const std::string new_name = simox::alg::trim_copy(q_new_name.toStdString());
422 const std::string old_name = m_image_provider_name;
424 if (not q_new_name.isEmpty())
426 if (new_name != old_name)
428 emit imageProviderRenamed(old_name, new_name);
433 m_ice_proxy_finder->setDefaultSelectedProxy(QString::fromStdString(old_name));
439 visionx::ImageProviderConfigWidget::image_provider_removed()
441 emit imageProviderRemoved(m_image_provider_name);