Loading...
Searching...
No Matches
Stepper.hpp
1#pragma once
2#include <score/model/Skin.hpp>
3
4#include <QPainter>
5#include <QRectF>
6
7#include <cmath>
8
9namespace score
10{
14struct Stepper
15{
16 static constexpr double width = 12.;
18 static constexpr double min_stacked_height = 18.;
19
20 static bool sideBySide(const QRectF& box) noexcept
21 {
22 return box.height() < min_stacked_height;
23 }
24
26 static double stripWidth(const QRectF& box) noexcept
27 {
28 return sideBySide(box) ? 2. * width : width;
29 }
30
32 static bool visible(const QRectF& box) noexcept
33 {
34 return box.width() > stripWidth(box) + 2. * width;
35 }
36
37 static QRectF rect(const QRectF& box) noexcept
38 {
39 const double left = std::max(box.left(), box.right() - stripWidth(box));
40 return QRectF{QPointF{left, box.top()}, QPointF{box.right(), box.bottom()}};
41 }
42
44 static QRectF half(const QRectF& box, int step) noexcept
45 {
46 const QRectF strip = rect(box);
47 if(sideBySide(box))
48 return step > 0 ? QRectF{QPointF{strip.center().x(), strip.top()}, strip.bottomRight()}
49 : QRectF{strip.topLeft(), QPointF{strip.center().x(), strip.bottom()}};
50 return step > 0 ? QRectF{strip.topLeft(), QPointF{strip.right(), strip.center().y()}}
51 : QRectF{QPointF{strip.left(), strip.center().y()}, strip.bottomRight()};
52 }
53
55 static int stepAt(const QRectF& box, QPointF pos) noexcept
56 {
57 if(!visible(box))
58 return 0;
59 if(half(box, +1).contains(pos))
60 return +1;
61 if(half(box, -1).contains(pos))
62 return -1;
63 return 0;
64 }
65
67 static void paint(
68 QPainter& painter, const score::Skin& skin, const QRectF& box, int pressedStep)
69 {
70 const QRectF halves[2] = {half(box, +1), half(box, -1)};
71
72 // Half the glyph's arm length, so that + and - are the same width.
73 const double arm = 2.;
74 for(int i = 0; i < 2; i++)
75 {
76 const int step = i == 0 ? +1 : -1;
77 const QRectF& half = halves[i];
78
79 // A pressed half flashes in the text's orange, its glyph in the
80 // background colour.
81 const bool pressed = pressedStep == step;
82 if(pressed)
83 {
84 painter.setPen(skin.NoPen);
85 painter.setBrush(skin.Base4.main.brush);
86 painter.drawRect(half);
87 }
88
89 // Rounded: a one-pixel pen with antialiasing off needs a whole pixel,
90 // and the two glyphs have to line up with each other.
91 const QPointF c{std::round(half.center().x()), std::round(half.center().y())};
92 painter.setPen(pressed ? skin.Emphasis2.main.pen1 : skin.Base4.main.pen1);
93 painter.drawLine(QPointF{c.x() - arm, c.y()}, QPointF{c.x() + arm, c.y()});
94 if(step > 0)
95 painter.drawLine(QPointF{c.x(), c.y() - arm}, QPointF{c.x(), c.y() + arm});
96 }
97 }
98};
99}
Definition Skin.hpp:100
Base toolkit upon which the software is built.
Definition Application.cpp:85
Definition Stepper.hpp:15
static bool visible(const QRectF &box) noexcept
Shown only when the box leaves room for its content next to it.
Definition Stepper.hpp:32
static double stripWidth(const QRectF &box) noexcept
Width of the strip in that box.
Definition Stepper.hpp:26
static int stepAt(const QRectF &box, QPointF pos) noexcept
+1 or -1 for the half under pos, 0 outside the strip.
Definition Stepper.hpp:55
static void paint(QPainter &painter, const score::Skin &skin, const QRectF &box, int pressedStep)
pressedStep: the half held down (0 for none) while the pointer is on it.
Definition Stepper.hpp:67
static QRectF half(const QRectF &box, int step) noexcept
The half that steps by step: +1 or -1.
Definition Stepper.hpp:44
static constexpr double min_stacked_height
Below this height the halves sit side by side.
Definition Stepper.hpp:18