RateWidget.hpp
1 #pragma once
2 #include <score/widgets/MarginLess.hpp>
3 
4 #include <ossia/detail/optional.hpp>
5 
6 #include <QCheckBox>
7 #include <QHBoxLayout>
8 #include <QSpinBox>
9 
10 #include <verdigris>
11 
12 namespace Protocols
13 {
14 class RateWidget final : public QWidget
15 {
16  W_OBJECT(RateWidget)
17 
18 public:
19  RateWidget(QWidget* parent = nullptr) noexcept
20  : QWidget{parent}
21  , m_check{new QCheckBox{this}}
22  , m_spin{new QSpinBox{this}}
23  {
24  auto lay = new score::MarginLess<QHBoxLayout>;
25 
26  lay->addWidget(m_check);
27  lay->addWidget(m_spin);
28  m_spin->setSizePolicy(QSizePolicy::MinimumExpanding, {});
29  m_spin->setSuffix("ms");
30  m_spin->setRange(1, 5000);
31  lay->setStretch(0, 1);
32  lay->setStretch(1, 20);
33 
34  connect(m_check, &QCheckBox::toggled, this, [this](bool t) {
35  rateChanged(std::optional<int>{});
36  m_spin->setEnabled(t);
37  });
38 
39  m_check->setChecked(false);
40  m_spin->setEnabled(false);
41 
42  setLayout(lay);
43  }
44 
45  std::optional<int> rate() const noexcept
46  {
47  if(!m_check->isChecked())
48  {
49  return std::optional<int>{};
50  }
51  else
52  {
53  return m_spin->value();
54  }
55  }
56 
57  void setRate(std::optional<int> r) noexcept
58  {
59  if(r)
60  {
61  m_check->setChecked(true);
62  m_spin->setValue(*r);
63  }
64  else
65  {
66  m_check->setChecked(false);
67  }
68  }
69 
70  void rateChanged(std::optional<int> v) W_SIGNAL(rateChanged, v);
71 
72 private:
73  QCheckBox* m_check{};
74  QSpinBox* m_spin{};
75 };
76 
77 }
78 
79 W_REGISTER_ARGTYPE(std::optional<int>)
Definition: RateWidget.hpp:15