File: non_linear_slider.cpp

package info (click to toggle)
sight 25.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 43,108 kB
  • sloc: cpp: 306,170; xml: 18,037; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (133 lines) | stat: -rw-r--r-- 4,108 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/************************************************************************
 *
 * Copyright (C) 2023-2024 IRCAD France
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "non_linear_slider.hpp"

#include <core/spy_log.hpp>

#include <QResizeEvent>

namespace sight::ui::qt::widget
{

non_linear_slider::non_linear_slider(QWidget* _parent) :
    QWidget(_parent)
{
    m_slider->setOrientation(Qt::Horizontal);
    m_slider->setSingleStep(1);
    m_slider->setPageStep(1);
    setSizePolicy(m_slider->sizePolicy().horizontalPolicy(), m_slider->sizePolicy().verticalPolicy());
    QObject::connect(
        m_slider.get(),
        &QAbstractSlider::valueChanged,
        [this](int _v)
        {
            Q_EMIT value_changed(m_values[std::size_t(_v)]);
        });
}

//------------------------------------------------------------------------------

void non_linear_slider::set_orientation(Qt::Orientation _orientation)
{
    m_slider->setOrientation(_orientation);
}

//------------------------------------------------------------------------------

void non_linear_slider::set_values(const std::vector<int>& _values)
{
    SIGHT_ASSERT("The values list should be sorted", std::ranges::is_sorted(_values));
    m_values = _values;
    m_slider->setRange(0, static_cast<int>(_values.size() - 1));
    m_slider->setValue(0);
    Q_EMIT range_changed(_values.front(), _values.back());
    Q_EMIT value_changed(value());
}

//------------------------------------------------------------------------------

void non_linear_slider::set_value(int _value)
{
    auto it = std::ranges::find(m_values, _value);
    SIGHT_ASSERT("The value should exist in the list of possible values", it != m_values.end());
    m_slider->setValue(static_cast<int>(std::distance(m_values.begin(), it)));
}

//------------------------------------------------------------------------------

void non_linear_slider::set_index(std::size_t _index)
{
    m_slider->setValue(static_cast<int>(_index));
}

//------------------------------------------------------------------------------

void non_linear_slider::set_tracking(bool _tracking)
{
    m_slider->setTracking(_tracking);
}

//------------------------------------------------------------------------------

int non_linear_slider::value()
{
    SIGHT_ASSERT(
        "Internal slider value should be within range",
        0 <= m_slider->value() && m_slider->value() < int(m_values.size())
    );
    return m_values[static_cast<std::size_t>(m_slider->value())];
}

//------------------------------------------------------------------------------

std::size_t non_linear_slider::index()
{
    return static_cast<std::size_t>(m_slider->value());
}

//------------------------------------------------------------------------------

QSize non_linear_slider::minimumSizeHint() const
{
    return m_slider->minimumSizeHint();
}

//------------------------------------------------------------------------------

QSize non_linear_slider::sizeHint() const
{
    return m_slider->sizeHint();
}

//------------------------------------------------------------------------------

bool non_linear_slider::event(QEvent* _event)
{
    if(auto* resize_event = dynamic_cast<QResizeEvent*>(_event); resize_event != nullptr && m_slider != nullptr)
    {
        m_slider->resize(resize_event->size());
    }

    return QWidget::event(_event);
}

} // namespace sight::ui::qt::widget