Radium Engine  1.5.20
Loading...
Searching...
No Matches
VectorEditor.hpp
1#pragma once
2#include <Gui/RaGui.hpp>
3#include <Gui/Widgets/QtTypeWrapper.hpp>
4
5#include <QLabel>
6#include <QSpinBox>
7#include <QVBoxLayout>
8#include <QWidget>
9
10#include <memory>
11#include <string>
12#include <vector>
13
14namespace Ra {
15namespace Gui {
16namespace Widgets {
17
18class RA_GUI_API VectorEditorSignals : public QWidget
19{
20 Q_OBJECT
21 public:
22 explicit VectorEditorSignals( QWidget* parent = nullptr ) : QWidget( parent ) {}
23 signals:
24 void valueChanged( const std::vector<int>& );
25 void valueChanged( const std::vector<unsigned int>& );
26 void valueChanged( const std::vector<float>& );
27 void valueChanged( const std::vector<double>& );
28};
32template <typename T = Scalar>
33class VectorEditor : public VectorEditorSignals
34{
35 public:
36 using WidgetType = typename QtSpinBox::getType<T>::Type;
37 using SignalType = typename QtSpinBox::getType<T>::SignalType;
41 explicit VectorEditor( const std::vector<T>& vector, QWidget* parent = nullptr );
42 VectorEditor( const VectorEditor& ) = delete;
43 VectorEditor& operator=( const VectorEditor& ) = delete;
44 VectorEditor( VectorEditor&& ) = delete;
45 VectorEditor&& operator=( VectorEditor&& ) = delete;
51 const std::vector<T>& vector() const;
52
53 private:
54 std::vector<T> m_vector;
55};
56
57template <typename T>
58VectorEditor<T>::VectorEditor( const std::vector<T>& vector, QWidget* parent ) :
59 VectorEditorSignals( parent ), m_vector( vector ) {
60
61 QVBoxLayout* layout = new QVBoxLayout;
62
63 layout->setContentsMargins( 0, 0, 0, 0 );
64 layout->setSpacing( 0 );
65 for ( auto& elem : m_vector ) {
66 auto spinbox = new WidgetType( this );
67 spinbox->setMinimum( std::numeric_limits<T>::lowest() );
68 spinbox->setMaximum( std::numeric_limits<T>::max() );
69 spinbox->setValue( elem );
70 if constexpr ( std::is_floating_point_v<T> ) { spinbox->setDecimals( 3 ); }
71 layout->addWidget( spinbox );
72 auto updateVectorOnChange = [this, &elem]( SignalType value ) {
73 elem = T( value );
74 emit valueChanged( m_vector );
75 };
76 connect(
77 spinbox, QOverload<SignalType>::of( &WidgetType::valueChanged ), updateVectorOnChange );
78 }
79 setLayout( layout );
80}
81
82template <typename T>
84 return m_vector;
85}
86} // namespace Widgets
87} // namespace Gui
88} // namespace Ra
const std::vector< T > & vector() const
VectorEditor(const std::vector< T > &vector, QWidget *parent=nullptr)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3