Radium Engine  1.5.20
Loading...
Searching...
No Matches
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 <QBoxLayout>
9#include <QFrame>
10#include <QVBoxLayout>
11
12#include <functional>
13#include <stack>
14#include <string>
15
16#include <QLabel>
17
18namespace Ra {
19namespace Gui {
20namespace Widgets {
26class RA_GUI_API ControlPanel : public QFrame
27{
28 Q_OBJECT
29 public:
30 explicit ControlPanel( const std::string& name, bool hline = true, QWidget* parent = nullptr );
31 ControlPanel( const ControlPanel& ) = delete;
32 ControlPanel& operator=( const ControlPanel& ) = delete;
33 ControlPanel( ControlPanel&& ) = delete;
34 ControlPanel& operator=( ControlPanel&& ) = delete;
35 ~ControlPanel() override = default;
36
47 void addOption( const std::string& name,
48 std::function<void( bool )> callback,
49 bool set = false,
50 const std::string& tooltip = "" );
51
56 void addLabel( const std::string& text );
57
64 void addButton( const std::string& name,
65 std::function<void()> callback,
66 const std::string& tooltip = "" );
67
78 void addColorInput( const std::string& name,
79 const std::function<void( const Ra::Core::Utils::Color& clr )>& callback,
80 Ra::Core::Utils::Color color = Ra::Core::Utils::Color::Black(),
81 bool withAlpha = true,
82 const std::string& tooltip = "" );
83
95 template <typename T = Scalar>
96 void addNumberInput( const std::string& name,
97 std::function<void( T )> callback,
98 T initial,
101 const std::string& tooltip = "",
102 int dec = 3 );
103
114 template <typename T = Scalar>
115 void addConstrainedNumberInput( const std::string& name,
116 std::function<void( T )> callback,
117 Scalar initial,
118 std::function<bool( T )> predicate,
119 const std::string& tooltip = "",
120 int dec = 3 );
121
131 void addSliderInput( const std::string& name,
132 std::function<void( int )> callback,
133 int initial = 0,
134 int min = 0,
135 int max = 100,
136 const std::string& tooltip = "" );
137
147 void addPowerSliderInput( const std::string& name,
148 std::function<void( double )> callback,
149 double initial = 0,
150 double min = 0,
151 double max = 100,
152 const std::string& tooltip = "" );
153
161 template <typename T = Scalar>
162 void addVectorInput( const std::string& name,
163 std::function<void( const std::vector<T>& )> callback,
164 const std::vector<T>& initial,
165 const std::string& tooltip = "" );
166
175 void addMatrixInput( const std::string& name,
176 std::function<void( const Ra::Core::MatrixN& )> callback,
177 const Ra::Core::MatrixN& initial,
178 int dec = 3,
179 const std::string& tooltip = "" );
180
189 void addFileInput( const std::string& name,
190 std::function<void( std::string )> callback,
191 const std::string& rootDirectory,
192 const std::string& filters,
193 const std::string& tooltip = "" );
194
203 void addFileOutput( const std::string& name,
204 std::function<void( std::string )> callback,
205 const std::string& rootDirectory,
206 const std::string& filters,
207 const std::string& tooltip = "" );
208
217 void addComboBox( const std::string& name,
218 std::function<void( int )> callback,
219 int initial,
220 const std::vector<std::string>& items,
221 const std::string& tooltip = "" );
222 void addComboBox( const std::string& name,
223 std::function<void( const QString& )> callback,
224 const std::string& initial,
225 const std::vector<std::string>& items,
226 const std::string& tooltip = "" );
227
231 void addWidget( QWidget* newWidget );
232
235 void addSeparator();
236 void addStretch( int stretch );
237
238 void newLayout() {
239 m_contentLayout = new QGridLayout();
240 m_contentLayout->setObjectName( "new layout" );
241 m_mainLayout->addLayout( m_contentLayout );
242 }
243
244 private:
246 QVBoxLayout* m_mainLayout;
247 QGridLayout* m_contentLayout;
248};
249
250template <typename T>
252 std::function<void( T )> callback,
253 Scalar initial,
254 std::function<bool( T )> predicate,
255 const std::string& tooltip,
256 int dec ) {
257 auto inputLabel = new QLabel( tr( name.c_str() ) );
258
259 auto inputField = new ConstrainedNumericSpinBox<T>();
260 inputField->setValue( initial );
261 inputField->setMinimum( std::numeric_limits<T>::lowest() );
262 inputField->setMaximum( std::numeric_limits<T>::max() );
263 connect( inputField,
264 qOverload<typename QtSpinBox::getType<T>::SignalType>(
266 std::move( callback ) );
267 inputField->setPredicate( predicate );
268
269 if constexpr ( std::is_floating_point_v<T> ) { inputField->setDecimals( dec ); }
270
271 if ( !tooltip.empty() ) {
272 auto tooltipString =
273 QString( "<qt>%1</qt>" ).arg( QString( tooltip.c_str() ).toHtmlEscaped() );
274 inputLabel->setToolTip( tooltipString );
275 inputField->setToolTip( tooltipString );
276 }
277 auto index = m_contentLayout->rowCount();
278 m_contentLayout->addWidget( inputLabel, index, 0 );
279 m_contentLayout->addWidget( inputField, index, 1 );
280}
281
282template <typename T>
284 std::function<void( T )> callback,
285 T initial,
286 T min,
287 T max,
288 const std::string& tooltip,
289 int dec ) {
290
291 auto inputLabel = new QLabel( tr( name.c_str() ) );
292
293 using WidgetType = typename QtSpinBox::getType<T>::Type;
294 auto inputField = new WidgetType();
295 auto inputLayout = new QHBoxLayout();
296 inputLayout->addStretch();
297 inputLayout->addWidget( inputField );
298 // to prevent overflow
299 inputField->setRange(
300 min,
301 std::min( T( std::numeric_limits<typename QtSpinBox::getType<T>::SignalType>::max() ),
302 max ) );
303 inputField->setValue( initial );
304 connect( inputField,
305 qOverload<typename QtSpinBox::getType<T>::SignalType>( &WidgetType::valueChanged ),
306 std::move( callback ) );
307 if constexpr ( std::is_floating_point_v<T> ) { inputField->setDecimals( dec ); }
308
309 if ( !tooltip.empty() ) {
310 auto tooltipString =
311 QString( "<qt>%1</qt>" ).arg( QString( tooltip.c_str() ).toHtmlEscaped() );
312 inputLabel->setToolTip( tooltipString );
313 inputField->setToolTip( tooltipString );
314 }
315 auto index = m_contentLayout->rowCount();
316 m_contentLayout->addWidget( inputLabel, index, 0 );
317 m_contentLayout->addLayout( inputLayout, index, 1 );
318}
319
320template <typename T>
322 std::function<void( const std::vector<T>& )> callback,
323 const std::vector<T>& initial,
324 const std::string& tooltip ) {
325
326 auto inputLabel = new QLabel( tr( name.c_str() ) );
327 auto inputField = new VectorEditor<T>( initial );
328
329 if ( !tooltip.empty() ) {
330 auto tooltipString =
331 QString( "<qt>%1</qt>" ).arg( QString( tooltip.c_str() ).toHtmlEscaped() );
332 inputLabel->setToolTip( tooltipString );
333 inputField->setToolTip( tooltipString );
334 }
335 connect( inputField,
336 QOverload<const std::vector<T>&>::of( &VectorEditorSignals::valueChanged ),
337 std::move( callback ) );
338
339 auto index = m_contentLayout->rowCount();
340 m_contentLayout->addWidget( inputLabel, index, 0 );
341 m_contentLayout->addWidget( inputField, index, 1 );
342}
343
344} // namespace Widgets
345} // namespace Gui
346} // namespace Ra
T c_str(T... args)
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)
T empty(T... args)
T min(T... args)
T move(T... args)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3