Loading [MathJax]/extensions/TeX/AMSsymbols.js
Radium Engine  1.5.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
ControlPanel.hpp
1 #pragma once
2 #include <Core/Types.hpp>
3 #include <Core/Utils/Color.hpp>
4 #include <Gui/RaGui.hpp>
5 #include <Gui/Widgets/ConstrainedNumericSpinBox.hpp>
6 #include <Gui/Widgets/VectorEditor.hpp>
7 
8 #include <QFrame>
9 #include <QVBoxLayout>
10 
11 #include <functional>
12 #include <stack>
13 #include <string>
14 
15 #include <QLabel>
16 
17 namespace Ra {
18 namespace Gui {
19 namespace Widgets {
25 class RA_GUI_API ControlPanel : public QFrame
26 {
27  Q_OBJECT
28  public:
33  explicit ControlPanel( const std::string& name, bool hline = true, QWidget* parent = nullptr );
34  ControlPanel( const ControlPanel& ) = delete;
35  ControlPanel& operator=( const ControlPanel& ) = delete;
36  ControlPanel( ControlPanel&& ) = delete;
37  ControlPanel& operator=( ControlPanel&& ) = delete;
38  ~ControlPanel() override = default;
54  void beginLayout( QBoxLayout::Direction dir = QBoxLayout::LeftToRight );
55 
60  void endLayout( bool separator = false );
61 
65  void addSeparator();
66 
71  void addStretch( int stretch = 0 );
72 
79  void addOption( const std::string& name,
80  std::function<void( bool )> callback,
81  bool set = false,
82  const std::string& tooltip = "" );
83 
88  void addLabel( const std::string& text );
89 
96  void addButton( const std::string& name,
97  std::function<void()> callback,
98  const std::string& tooltip = "" );
99 
110  void addColorInput( const std::string& name,
111  const std::function<void( const Ra::Core::Utils::Color& clr )>& callback,
112  Ra::Core::Utils::Color color = Ra::Core::Utils::Color::Black(),
113  bool withAlpha = true,
114  const std::string& tooltip = "" );
115 
127  template <typename T = Scalar>
128  void addNumberInput( const std::string& name,
129  std::function<void( T )> callback,
130  T initial,
131  T min = std::numeric_limits<T>::lowest(),
132  T max = std::numeric_limits<T>::max(),
133  const std::string& tooltip = "",
134  int dec = 3 );
135 
146  template <typename T = Scalar>
147  void addConstrainedNumberInput( const std::string& name,
148  std::function<void( T )> callback,
149  Scalar initial,
150  std::function<bool( T )> predicate,
151  const std::string& tooltip = "",
152  int dec = 3 );
153 
164  void addSliderInput( const std::string& name,
165  std::function<void( int )> callback,
166  int initial = 0,
167  int min = 0,
168  int max = 100,
169  const std::string& tooltip = "" );
170 
181  void addPowerSliderInput( const std::string& name,
182  std::function<void( double )> callback,
183  double initial = 0,
184  double min = 0,
185  double max = 100,
186  const std::string& tooltip = "" );
187 
196  template <typename T = Scalar>
197  void addVectorInput( const std::string& name,
198  std::function<void( const std::vector<T>& )> callback,
199  const std::vector<T>& initial,
200  const std::string& tooltip = "" );
201 
210  void addMatrixInput( const std::string& name,
211  std::function<void( const Ra::Core::MatrixN& )> callback,
212  const Ra::Core::MatrixN& initial,
213  int dec = 3,
214  const std::string& tooltip = "" );
215 
224  void addFileInput( const std::string& name,
225  std::function<void( std::string )> callback,
226  const std::string& rootDirectory,
227  const std::string& filters,
228  const std::string& tooltip = "" );
229 
238  void addFileOutput( const std::string& name,
239  std::function<void( std::string )> callback,
240  const std::string& rootDirectory,
241  const std::string& filters,
242  const std::string& tooltip = "" );
243 
252  void addComboBox( const std::string& name,
253  std::function<void( int )> callback,
254  int initial,
255  const std::vector<std::string>& items,
256  const std::string& tooltip = "" );
257  void addComboBox( const std::string& name,
258  std::function<void( const QString& )> callback,
259  const std::string& initial,
260  const std::vector<std::string>& items,
261  const std::string& tooltip = "" );
262 
266  void addWidget( QWidget* newWidget );
267 
270  private:
272  QVBoxLayout* m_contentLayout;
273 
275  QBoxLayout* m_currentLayout;
276 
278  std::stack<QBoxLayout*> m_layouts;
279 };
280 
281 template <typename T>
282 void ControlPanel::addConstrainedNumberInput( const std::string& name,
283  std::function<void( T )> callback,
284  Scalar initial,
285  std::function<bool( T )> predicate,
286  const std::string& tooltip,
287  int dec ) {
288  auto inputLayout = new QHBoxLayout();
289  auto inputLabel = new QLabel( tr( name.c_str() ), this );
290 
291  auto inputField = new ConstrainedNumericSpinBox<T>( this );
292  inputField->setValue( initial );
293  inputField->setMinimum( std::numeric_limits<T>::lowest() );
294  inputField->setMaximum( std::numeric_limits<T>::max() );
295  connect( inputField,
296  qOverload<typename QtSpinBox::getType<T>::SignalType>(
298  std::move( callback ) );
299  inputField->setPredicate( predicate );
300 
301  if constexpr ( std::is_floating_point_v<T> ) { inputField->setDecimals( dec ); }
302 
303  if ( !tooltip.empty() ) {
304  inputLabel->setToolTip(
305  QString( "<qt>%1</qt>" ).arg( QString( tooltip.c_str() ).toHtmlEscaped() ) );
306  }
307 
308  inputLayout->addWidget( inputLabel );
309  // inputLayout->addStretch();
310  inputLayout->addWidget( inputField );
311 
312  m_currentLayout->addLayout( inputLayout );
313 }
314 
315 template <typename T>
316 void ControlPanel::addNumberInput( const std::string& name,
317  std::function<void( T )> callback,
318  T initial,
319  T min,
320  T max,
321  const std::string& tooltip,
322  int dec ) {
323  auto inputLayout = new QHBoxLayout();
324 
325  auto inputLabel = new QLabel( tr( name.c_str() ), this );
326 
327  using WidgetType = typename QtSpinBox::getType<T>::Type;
328  auto inputField = new WidgetType( this );
329  // to prevent overflow
330  inputField->setRange(
331  min,
332  std::min( T( std::numeric_limits<typename QtSpinBox::getType<T>::SignalType>::max() ),
333  max ) );
334  inputField->setValue( initial );
335  connect( inputField,
336  qOverload<typename QtSpinBox::getType<T>::SignalType>( &WidgetType::valueChanged ),
337  std::move( callback ) );
338  if constexpr ( std::is_floating_point_v<T> ) { inputField->setDecimals( dec ); }
339 
340  if ( !tooltip.empty() ) {
341  inputLabel->setToolTip(
342  QString( "<qt>%1</qt>" ).arg( QString( tooltip.c_str() ).toHtmlEscaped() ) );
343  }
344  inputLayout->addWidget( inputLabel );
345  // inputLayout->addStretch();
346  inputLayout->addWidget( inputField );
347  m_currentLayout->addLayout( inputLayout );
348 }
349 
350 template <typename T>
351 void ControlPanel::addVectorInput( const std::string& name,
352  std::function<void( const std::vector<T>& )> callback,
353  const std::vector<T>& initial,
354  const std::string& tooltip ) {
355  auto inputLayout = new QHBoxLayout();
356 
357  auto inputLabel = new QLabel( tr( name.c_str() ), this );
358  auto inputField = new VectorEditor<T>( initial, this );
359 
360  if ( !tooltip.empty() ) {
361  inputLabel->setToolTip(
362  QString( "<qt>%1</qt>" ).arg( QString( tooltip.c_str() ).toHtmlEscaped() ) );
363  }
364  inputLayout->addWidget( inputLabel );
365  inputLayout->addWidget( inputField );
366  connect( inputField,
367  QOverload<const std::vector<T>&>::of( &VectorEditorSignals::valueChanged ),
368  std::move( callback ) );
369  m_currentLayout->addLayout( inputLayout );
370 }
371 
372 } // namespace Widgets
373 } // namespace Gui
374 } // namespace Ra
Constrained input spin box. The constraint to apply to any input value is verified using the user-def...
void addNumberInput(const std::string &name, std::function< void(T)> callback, T initial, T min=std::numeric_limits< T >::lowest(), T max=std::numeric_limits< T >::max(), const std::string &tooltip="", int dec=3)
void addVectorInput(const std::string &name, std::function< void(const std::vector< T > &)> callback, const std::vector< T > &initial, const std::string &tooltip="")
void addConstrainedNumberInput(const std::string &name, std::function< void(T)> callback, Scalar initial, std::function< bool(T)> predicate, const std::string &tooltip="", int dec=3)
Definition: Cage.cpp:3