summaryrefslogtreecommitdiffhomepage
path: root/options/slider.cpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2016-08-17 21:28:45 +0200
committerStanislaw Halik <sthalik@misaki.pl>2016-08-17 21:34:53 +0200
commitcb33be1c50b68d6022f344ddac923c7aac3a11d5 (patch)
tree1840e3f9829aa69f10f81804f655e97be3f2eca8 /options/slider.cpp
parent41ba8aa5329c16a797140dc23650ef45f42753a3 (diff)
move options framework into its own library
- adjust usages - add support for QList signals and metatype
Diffstat (limited to 'options/slider.cpp')
-rw-r--r--options/slider.cpp75
1 files changed, 75 insertions, 0 deletions
diff --git a/options/slider.cpp b/options/slider.cpp
new file mode 100644
index 00000000..8d46d51b
--- /dev/null
+++ b/options/slider.cpp
@@ -0,0 +1,75 @@
+#include "slider.hpp"
+#include <cmath>
+
+namespace options {
+
+slider_value::slider_value(float cur, float min, float max) :
+ cur_(cur),
+ min_(min),
+ max_(max)
+{
+ if (min_ > max_)
+ min_ = max_;
+ if (cur_ > max_)
+ cur_ = max;
+ if (cur_ < min_)
+ cur_ = min_;
+}
+
+slider_value::slider_value(const slider_value& v) : slider_value(v.cur(), v.min(), v.max())
+{
+}
+
+
+
+slider_value::slider_value() : slider_value(0.f, 0.f, 0.f)
+{
+}
+
+slider_value& slider_value::operator=(const slider_value& v)
+{
+ cur_ = v.cur_;
+
+ min_ = v.min_;
+ max_ = v.max_;
+
+ return *this;
+}
+
+bool slider_value::operator==(const slider_value& v) const
+{
+ using std::fabs;
+
+ static constexpr float eps = 1e-3f;
+
+ return (fabs(v.cur_ - cur_) < eps &&
+ fabs(v.min_ - min_) < eps &&
+ fabs(v.max_ - max_) < eps);
+}
+
+slider_value slider_value::update_from_slider(int pos, int q_min, int q_max) const
+{
+ slider_value v(*this);
+
+ const int q_diff = q_max - q_min;
+ const double sv_pos = q_diff == 0
+ ? -1e6
+ : (((pos - q_min) * (v.max() - v.min())) / q_diff + v.min());
+
+ if (sv_pos < v.min())
+ v = slider_value(v.min(), v.min(), v.max());
+ else if (sv_pos > v.max())
+ v = slider_value(v.max(), v.min(), v.max());
+ else
+ v = slider_value(sv_pos, v.min(), v.max());
+ return v;
+}
+
+int slider_value::to_slider_pos(int q_min, int q_max) const
+{
+ const int q_diff = q_max - q_min;
+
+ return int(std::round(((cur() - min() * q_diff) / (max() - min())) + q_min));
+}
+
+} // end ns options