ChannelConfigWidget.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package VisionX::gui-plugins::ImageRecorder
19  * @author Christian R. G. Dreher <c.dreher@kit.edu>
20  * @date 2020
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 
27 
28 
29 // Qt
30 #include <QStandardItemModel>
31 
32 // Simox
33 #include <SimoxUtility/algorithm/string/string_tools.h>
34 
36  QWidget* parent,
37  const visionx::imrec::ChannelConfig& channel_config,
38  const visionx::imrec::ChannelPreferences& channel_prefs) :
39  QWidget(parent), m_channel_name{channel_config.name}, m_channel_prefs{channel_prefs}
40 {
41  m_widget.setupUi(this);
42 
43  const QString q_channel_name = QString::fromStdString(channel_config.name);
44  m_widget.channels_config->setTitle(QString{"Channel: "} + q_channel_name);
45  m_widget.channels_config->setChecked(not channel_config.disabled);
46  m_widget.name->setText(q_channel_name);
47 
48  for (const auto& [format, reg_entry] : visionx::imrec::getFormatsMap())
49  {
50  m_widget.file_format->addItem(QString::fromStdString(reg_entry.hr_name));
51  }
52  const QString q_format_str =
53  QString::fromStdString(imrec::format2hrstr(imrec::str2format(channel_config.format)));
54  m_widget.file_format->setCurrentText(q_format_str);
55 
56  update_file_formats(channel_prefs.requiresLossless);
57  set_visible_file_fps(m_widget.file_format->currentText());
58 
59  connect(m_widget.name, SIGNAL(textEdited(QString)), this, SLOT(channel_name_changed(QString)));
60  connect(m_widget.file_format,
61  SIGNAL(currentTextChanged(QString)),
62  this,
63  SLOT(file_format_changed(QString)));
64 
65  m_widget.lossless->setChecked(channel_prefs.requiresLossless);
66  connect(m_widget.lossless, SIGNAL(stateChanged(int)), this, SLOT(lossless_state_changed(int)));
67 }
68 
70 {
71 }
72 
73 const std::string&
75 {
76  return m_channel_name;
77 }
78 
79 void
81 {
82  m_widget.channels_config->setTitle(QString::fromStdString("Channel: " + new_name));
83  m_channel_name = new_name;
84 }
85 
86 void
88 {
89  // TODO: Display fancy graphics.
90  ARMARX_WARNING << "Invalid name '" << new_name << "' for channel (already taken).";
91 }
92 
93 void
94 visionx::ChannelConfigWidget::setChannelPreferences(const imrec::ChannelPreferences& channel_prefs)
95 {
96  update_file_formats(channel_prefs.requiresLossless);
97 }
98 
99 visionx::imrec::ChannelConfig
101 {
102  imrec::ChannelConfig cc;
103  cc.name = m_channel_name;
104  cc.disabled = disabled or not m_widget.channels_config->isChecked();
105  const imrec::Format format =
106  imrec::str2format(m_widget.file_format->currentText().toStdString());
107  cc.format = imrec::format2hrstr(format);
108  cc.fps = m_widget.file_fps->value();
109  return cc;
110 }
111 
112 void
113 visionx::ChannelConfigWidget::update_file_formats(bool require_lossless)
114 {
115  QStandardItemModel* model = qobject_cast<QStandardItemModel*>(m_widget.file_format->model());
116  for (int i = 0; i < m_widget.file_format->count(); ++i)
117  {
118  QStandardItem* item = model->item(i);
119  const imrec::Format format = imrec::str2format(item->text().toStdString());
120  const bool disabled = require_lossless and not imrec::isFormatLossless(format);
121  item->setFlags(disabled ? item->flags() & ~Qt::ItemIsEnabled
122  : item->flags() | Qt::ItemIsEnabled);
123  }
124 
125  // If require_lossless is set and current selected value is lossy, choose bmp image sequence.
126  if (require_lossless)
127  {
128  const std::string format_str = m_widget.file_format->currentText().toStdString();
129  const imrec::Format format = imrec::str2format(format_str);
130  if (not imrec::isFormatLossless(format))
131  {
132  const QString q_format_str =
133  QString::fromStdString(imrec::format2hrstr(imrec::Format::bmp_img_seq));
134  m_widget.file_format->setCurrentText(q_format_str);
135  }
136  }
137 }
138 
139 void
140 visionx::ChannelConfigWidget::set_visible_file_fps(const QString& q_format_str)
141 {
142  const std::string format_str = q_format_str.toStdString();
143  const imrec::Format format = imrec::str2format(format_str);
144  set_visible_file_fps(not imrec::isFormatDynamicFramerate(format));
145 }
146 
147 void
148 visionx::ChannelConfigWidget::set_visible_file_fps(bool visible)
149 {
150  m_widget.file_fps->setVisible(visible);
151  m_widget.label_file_fps->setVisible(visible);
152 }
153 
154 void
155 visionx::ChannelConfigWidget::channel_name_changed(const QString& q_new_name)
156 {
157  const std::string new_name = simox::alg::trim_copy(q_new_name.toStdString());
158  const std::string old_name = m_channel_name;
159  if (new_name != old_name)
160  {
161  emit channelRenamed(old_name, new_name);
162  }
163 }
164 
165 void
166 visionx::ChannelConfigWidget::lossless_state_changed(int state)
167 {
168  update_file_formats(state == Qt::Checked);
169 }
170 
171 void
172 visionx::ChannelConfigWidget::file_format_changed(const QString& new_format_str)
173 {
174  set_visible_file_fps(new_format_str);
175 }
visionx::imrec::Format::bmp_img_seq
@ bmp_img_seq
visionx::imrec::getFormatsMap
std::map< Format, RegistryEntry > getFormatsMap()
Definition: public_api.cpp:144
visionx::ChannelConfigWidget::toChannelConfig
imrec::ChannelConfig toChannelConfig(bool disabled) const
Definition: ChannelConfigWidget.cpp:100
visionx::imrec::Format
Format
Supported recording Formats.
Definition: public_api.h:53
visionx::ChannelConfigWidget::getChannelName
const std::string & getChannelName() const
Definition: ChannelConfigWidget.cpp:74
visionx::imrec::isFormatLossless
bool isFormatLossless(Format format)
Definition: public_api.cpp:150
visionx::ChannelConfigWidget::ChannelConfigWidget
ChannelConfigWidget(QWidget *parent, const visionx::imrec::ChannelConfig &channel_config, const visionx::imrec::ChannelPreferences &channel_prefs)
Definition: ChannelConfigWidget.cpp:35
ChannelConfigWidget.h
visionx::ChannelConfigWidget::~ChannelConfigWidget
virtual ~ChannelConfigWidget()
Definition: ChannelConfigWidget.cpp:69
visionx::imrec::format2hrstr
const std::string & format2hrstr(const Format format)
Definition: public_api.cpp:115
visionx::ChannelConfigWidget::rejectChannelName
void rejectChannelName(const std::string &new_name)
Definition: ChannelConfigWidget.cpp:87
visionx::imrec::str2format
Format str2format(const std::string &format_str)
Definition: public_api.cpp:121
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
visionx::imrec::isFormatDynamicFramerate
bool isFormatDynamicFramerate(Format format)
Definition: public_api.cpp:156
visionx::ChannelConfigWidget::setChannelPreferences
void setChannelPreferences(const imrec::ChannelPreferences &channel_prefs)
Definition: ChannelConfigWidget.cpp:94
visionx::ChannelConfigWidget::confirmChannelName
void confirmChannelName(const std::string &new_name)
Definition: ChannelConfigWidget.cpp:80