Loading [MathJax]/jax/input/TeX/config.js
Radium Engine  1.5.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
ConstrainedNumericSpinBox.hpp
1 #pragma once
2 #include <Gui/RaGui.hpp>
3 
4 #include <QWidget>
5 
6 #include <Gui/Widgets/QtTypeWrapper.hpp>
7 
8 namespace Ra {
9 namespace Gui {
10 namespace Widgets {
11 
17 template <typename T>
18 class ConstrainedNumericSpinBox : public QtSpinBox::getType<T>::Type
19 {
20  public:
21  using BaseWidget = typename QtSpinBox::getType<T>::Type;
22  using Predicate = std::function<bool( T )>;
23  using BaseWidget::BaseWidget;
24  QValidator::State validate( QString& input, int& ) const override;
25 
27  inline void setPredicate( Predicate p ) { m_p = p; }
33  inline bool isValid( T s ) const { return m_p( s ); }
34 
35  private:
36  Predicate m_p = []( T ) { return true; };
37 };
38 
39 template <typename T>
40 QValidator::State ConstrainedNumericSpinBox<T>::validate( QString& input, int& ) const {
41  auto valid = this->isValid( this->valueFromText( input ) );
42  auto& spin = const_cast<ConstrainedNumericSpinBox&>( *this );
43  spin.blockSignals( !valid );
44  if ( valid ) { spin.setStyleSheet( "" ); }
45  else { spin.setStyleSheet( "background-color: #FF8080" ); }
46  return valid ? QValidator::Acceptable : QValidator::Invalid;
47 }
48 
49 } // namespace Widgets
50 } // namespace Gui
51 } // namespace Ra
Constrained input spin box. The constraint to apply to any input value is verified using the user-def...
void setPredicate(Predicate p)
Set the predicate to evaluate.
Definition: Cage.cpp:3