Perceptual Color

multispinboxsection.cpp
1// SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR MIT
3
4// Own headers
5// First the interface, which forces the header to be self-contained.
6#include "multispinboxsection.h"
7// Second, the private implementation.
8#include "multispinboxsection_p.h" // IWYU pragma: associated
9
10#include "constpropagatinguniquepointer.h"
11#include "helpermath.h"
12#include <qglobal.h>
13
14namespace PerceptualColor
15{
16/** @brief Constructor
17 *
18 * The object is initialized with default values. */
20 : d_pointer(new MultiSpinBoxSectionPrivate())
21{
22}
23
24/** @brief Destructor */
28
29/** @brief Copy constructor
30 *
31 * @param other the object to be copied */
33{
34 d_pointer.reset(
35 // Create a copy of the private implementation object
36 new MultiSpinBoxSectionPrivate(*other.d_pointer));
37}
38
39/** @brief Copy assignment operator
40 *
41 * @returns By convention, always returns <tt>*this</tt>.
42 *
43 * @param other the object to be copied */
45{
46 if (this != &other) { // protect against invalid self-assignment
47 d_pointer.reset(
48 // Create a copy of the private implementation object
49 new MultiSpinBoxSectionPrivate(*other.d_pointer));
50 }
51
52 return *this; // By convention, always return *this.
53}
54
55/** @brief Move constructor
56 *
57 * @param other the object to move */
59{
60 d_pointer.swap(other.d_pointer);
61}
62
63/** @brief Move assignment operator
64 *
65 * @returns By convention, always returns <tt>*this</tt>.
66 *
67 * @param other the object to move-assign */
69{
70 if (this != &other) { // protect against invalid self-assignment
71 d_pointer.swap(other.d_pointer);
72 }
73
74 return *this; // By convention, always return *this.
75}
76
77/** @brief The number of digits after the decimal point.
78 *
79 * This value can also be <tt>0</tt> to get integer-like behavior.
80 *
81 * @returns The property value.
82 *
83 * @sa @ref setDecimals */
85{
86 return d_pointer->m_decimals;
87}
88
89/** @brief Setter for @ref decimals property.
90 *
91 * @param newDecimals The new decimals values. */
93{
94 d_pointer->m_decimals = qBound(0, newDecimals, 323);
95}
96
97/** @brief Holds whether or not @ref MultiSpinBox::sectionValues wrap
98 * around when they reaches @ref minimum or @ref maximum.
99 *
100 * The default is <tt>false</tt>.
101 *
102 * If <tt>false</tt>, @ref MultiSpinBox::sectionValues shall be bound
103 * between @ref minimum and @ref maximum. If <tt>true</tt>,
104 * @ref MultiSpinBox::sectionValues shall be treated as a circular.
105 *
106 * Example: You have a section that displays a value measured in
107 * degree. @ref minimum is <tt>0</tt>. @ref maximum is <tt>360</tt>.
108 * The following corrections would be applied to input:
109 * | Input | isWrapping == false | isWrapping == true |
110 * | ----: | ------------------: | -----------------: |
111 * | -5 | 0 | 355 |
112 * | 0 | 0 | 0 |
113 * | 5 | 5 | 5 |
114 * | 355 | 355 | 355 |
115 * | 360 | 360 | 0 |
116 * | 365 | 360 | 5 |
117 * | 715 | 360 | 355 |
118 * | 720 | 360 | 0 |
119 * | 725 | 360 | 5 |
120 *
121 * @returns The property value.
122 *
123 * @sa @ref setWrapping */
125{
126 return d_pointer->m_isWrapping;
127}
128
129/** @brief Setter for @ref isWrapping property.
130 *
131 * @param newIsWrapping The new isWrapping value. */
132void MultiSpinBoxSection::setWrapping(bool newIsWrapping)
133{
134 d_pointer->m_isWrapping = newIsWrapping;
135}
136
137/** @brief The maximum possible value of the section.
138 *
139 * @returns The property value.
140 *
141 * @sa @ref setMaximum */
143{
144 return roundToDigits(d_pointer->m_maximum, d_pointer->m_decimals);
145}
146
147/** @brief Setter for @ref maximum property.
148 *
149 * @param newMaximum The new maximum value. */
150void MultiSpinBoxSection::setMaximum(double newMaximum)
151{
152 d_pointer->m_maximum = newMaximum;
153 if (d_pointer->m_minimum > d_pointer->m_maximum) {
154 d_pointer->m_minimum = d_pointer->m_maximum;
155 }
156}
157
158/** @brief The minimum possible value of the section.
159 *
160 * @returns The property value.
161 *
162 * @sa @ref setMinimum */
164{
165 return roundToDigits(d_pointer->m_minimum, d_pointer->m_decimals);
166}
167
168/** @brief Setter for @ref minimum property.
169 *
170 * @param newMinimum The new minimum value. */
171void MultiSpinBoxSection::setMinimum(double newMinimum)
172{
173 d_pointer->m_minimum = newMinimum;
174 if (d_pointer->m_maximum < d_pointer->m_minimum) {
175 d_pointer->m_maximum = d_pointer->m_minimum;
176 }
177}
178
179/** @brief A prefix to be displayed before the value.
180 *
181 * @returns The property value.
182 *
183 * @sa @ref setFormatString
184 */
186{
187 return d_pointer->m_prefix;
188}
189
190/** @brief A smaller of two natural steps.
191 *
192 * Valid range: >= 0
193 *
194 * When the user uses the arrows to change the spin box’s value
195 * the value will be incremented/decremented by the amount of the
196 * @ref singleStep.
197 *
198 * @returns The property value.
199 *
200 * @sa @ref setSingleStep */
202{
203 return d_pointer->m_singleStep;
204}
205
206/** @brief Setter for @ref singleStep property.
207 *
208 * @param newSingleStep The new single step value. */
209void MultiSpinBoxSection::setSingleStep(double newSingleStep)
210{
211 d_pointer->m_singleStep = qMax<double>(0, newSingleStep);
212}
213
214/** @brief The suffix to be displayed behind the value.
215 *
216 * @returns The property value.
217 *
218 * @sa @ref setFormatString
219 */
221{
222 return d_pointer->m_suffix;
223}
224
225/** @brief Adds QDebug() support for data type
226 * @ref PerceptualColor::MultiSpinBoxSection
227 *
228 * @param dbg Existing debug object
229 * @param value Value to stream into the debug object
230 * @returns Debug object with value streamed in */
232{
233 dbg.nospace() << "\nMultiSpinBoxSection(" // Opening line
234 << "\n formatString: " << value.formatString() //
235 << "\n prefix: " << value.prefix() //
236 << "\n minimum: " << value.minimum() //
237 << "\n decimals: " << value.decimals() //
238 << "\n isWrapping: " << value.isWrapping() //
239 << "\n maximum: " << value.maximum() //
240 << "\n suffix: " << value.suffix() //
241 << "\n)" // Closing line
242 ;
243 return dbg.maybeSpace();
244}
245
246/**
247 * @brief Setter for the @ref prefix(), @ref suffix() and @ref formatString()
248 * properties.
249 *
250 * @param formatString A string in the format "prefix%1suffix". It
251 * should contain exactly <em>one</em> place marker as described in
252 * <tt>QString::arg()</tt> like <tt>\%1</tt> or <tt>\%L2</tt>. This place
253 * marker represents the value. Example: “Prefix\%1Suffix”. Prefix and suffix
254 * may be empty.
255 */
257{
258 d_pointer->m_formatString = formatString;
259
260 // QString::arg() support for %L2, %5 etc which translators might expect:
261 const auto parts = formatString //
262 .arg(QStringLiteral("%1")) //
263 .split(QStringLiteral("%1"));
264
265 if (parts.count() == 2) {
266 d_pointer->m_prefix = parts.at(0);
267 d_pointer->m_suffix = parts.at(1);
268 } else {
269 d_pointer->m_prefix = QString();
270 d_pointer->m_suffix = QString();
271 }
272}
273
274/**
275 * @brief A string in the format "PREFIX%vSUFFIX".
276 *
277 * @returns The property value.
278 *
279 * @sa @ref setFormatString()
280 * @sa @ref prefix()
281 * @sa @ref suffix()
282 */
284{
285 return d_pointer->m_formatString;
286}
287
288} // namespace PerceptualColor
The configuration of a single section within a MultiSpinBox.
MultiSpinBoxSection & operator=(const MultiSpinBoxSection &other)
Copy assignment operator.
QString formatString() const
A string in the format "PREFIX%vSUFFIX".
QString prefix() const
A prefix to be displayed before the value.
int decimals() const
The number of digits after the decimal point.
double singleStep() const
A smaller of two natural steps.
double maximum() const
The maximum possible value of the section.
void setDecimals(int newDecimals)
Setter for decimals property.
double minimum() const
The minimum possible value of the section.
void setMaximum(double newMaximum)
Setter for maximum property.
void setMinimum(double newMinimum)
Setter for minimum property.
void setFormatString(const QString &formatString)
Setter for the prefix(), suffix() and formatString() properties.
QString suffix() const
The suffix to be displayed behind the value.
void setWrapping(bool newIsWrapping)
Setter for isWrapping property.
bool isWrapping() const
Holds whether or not MultiSpinBox::sectionValues wrap around when they reaches minimum or maximum.
void setSingleStep(double newSingleStep)
Setter for singleStep property.
The namespace of this library.
QDebug & maybeSpace()
QDebug & nospace()
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri May 2 2025 12:04:47 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.