Logo    
KDWinUtils
Helper library for MFC to Qt migration
Loading...
Searching...
No Matches
knumber.h
Go to the documentation of this file.
1/****************************************************************************
2**
3** This file is part of KDWinutils, KDAB's MFC to Qt migration tool.
4**
5** Copyright (C) 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6** All rights reserved.
7**
8** This file is intended solely for use by the migration tools and services
9** provided by Klarälvdalens Datakonsult AB.
10**
11** Any other use or distribution of this software that is not otherwise agreed
12** upon in writing and signed by an authorized representative of Klarälvdalens
13** Datakonsult AB, KDAB (USA) LLC, KDAB (Deutschland) GmbH & Co. K.G.,
14** KDAB (France) SAS, KDAB (UK), Ltd., or any future subsidiary of Klarälvdalens
15** Datakonsult AB is hereby prohibited.
16**
17** Contact info@kdab.com if any conditions stated above are unclear to you.
18**
19****************************************************************************/
20
21#pragma once
22
24
25#include <QComboBox>
26#include <QLabel>
27#include <QLineEdit>
28#include <QString>
29#include <QValidator>
30
31#include <type_traits>
32
34
46template <typename T>
48{
49public:
50 using Type = T;
51 using value_type = T;
52
54 explicit KNumber(T value)
55 : m_value(std::move(value))
56 {
57 }
58
60 QWidget *widget() const;
62 void setWidget(QWidget *label);
63
68 void setValidator(QValidator *validator);
69
71 KNumber<T> &operator=(const T &value);
73 operator T() const { return value(); };
74
75private:
76 [[nodiscard]] T convertToValue(const QString &text) const;
77 [[nodiscard]] QString convertToString(const T &value) const;
78
79 QString stringValue() const;
80 T value() const;
81 void setValue(const QString &text);
82 void setValue(const T &value);
83
84private:
85 T m_value; // used only for the initialization, then move to the widget
86 QWidget *m_widget = nullptr;
87};
88
89template <typename T>
90inline QWidget *KNumber<T>::widget() const
91{
92 return m_widget;
93}
94
95template <typename T>
96inline void KNumber<T>::setWidget(QWidget *label)
97{
98 Q_ASSERT(label);
99 m_widget = label;
100 setValue(m_value);
101}
102
103template <typename T>
104inline void KNumber<T>::setValidator(QValidator *validator)
105{
106 Q_ASSERT(m_widget);
107
108 if (auto lineEdit = qobject_cast<QLineEdit *>(m_widget))
109 lineEdit->setValidator(validator);
110 else if (auto comboBox = qobject_cast<QComboBox *>(m_widget))
111 comboBox->setValidator(validator);
112 else
113 validator->deleteLater();
114}
115
116template <typename T>
117inline KNumber<T> &KNumber<T>::operator=(const T &value)
118{
119 setValue(value);
120 return *this;
121}
122
123template <typename T>
124inline T KNumber<T>::convertToValue(const QString &text) const
125{
126 if constexpr (std::is_same_v<std::decay_t<T>, short>)
127 return text.toShort();
128 if constexpr (std::is_same_v<std::decay_t<T>, int>)
129 return text.toInt();
130 if constexpr (std::is_same_v<std::decay_t<T>, long>)
131 return text.toLong();
132 if constexpr (std::is_same_v<std::decay_t<T>, long long>)
133 return text.toLongLong();
134 if constexpr (std::is_same_v<std::decay_t<T>, unsigned int>)
135 return text.toUInt();
136 if constexpr (std::is_integral_v<T>)
137 return text.toInt();
138 if constexpr (std::is_same_v<std::decay_t<T>, float>)
139 return text.toFloat();
140 if constexpr (std::is_same_v<std::decay_t<T>, double>)
141 return text.toDouble();
142
143 // We shouldn't reach this, or a type is missing
144 Q_UNREACHABLE();
145}
146
147template <typename T>
148inline QString KNumber<T>::convertToString(const T &value) const
149{
150 if constexpr (std::is_integral_v<T> || std::is_floating_point_v<T>)
151 return QString::number(value);
152
153 // We shouldn't reach this, or a type is missing
154 Q_UNREACHABLE();
155}
156
157template <typename T>
158inline QString KNumber<T>::stringValue() const
159{
160 Q_ASSERT(m_widget);
161 if (auto lineEdit = qobject_cast<QLineEdit *>(m_widget))
162 return lineEdit->text();
163 if (auto comboBox = qobject_cast<QComboBox *>(m_widget))
164 return comboBox->currentText();
165 if (auto label = qobject_cast<QLabel *>(m_widget))
166 return label->text();
167 // We shouldn't reach this, or a widget is missing
168 Q_UNREACHABLE();
169}
170
171template <typename T>
172inline T KNumber<T>::value() const
173{
174 Q_ASSERT(m_widget);
175 return convertToValue(stringValue());
176}
177
178template <typename T>
179inline void KNumber<T>::setValue(const QString &text)
180{
181 Q_ASSERT(m_widget);
182 if (auto lineEdit = qobject_cast<QLineEdit *>(m_widget))
183 lineEdit->setText(text);
184 else if (auto comboBox = qobject_cast<QComboBox *>(m_widget))
185 comboBox->setCurrentText(text);
186 else if (auto label = qobject_cast<QLabel *>(m_widget))
187 label->setText(text);
188 // We shouldn't reach this, or a widget is missing
189 else
190 Q_UNREACHABLE();
191}
192
193template <typename T>
194inline void KNumber<T>::setValue(const T &value)
195{
196 setValue(convertToString(value));
197}
198
The KNumber< T > class is used to replace DDX_Text data exchange for numeric types.
Definition knumber.h:48
KNumber(T value)
Definition knumber.h:54
void setWidget(QWidget *label)
Definition knumber.h:96
void setValidator(QValidator *validator)
Definition knumber.h:104
T Type
Definition knumber.h:50
KNumber< T > & operator=(const T &value)
Definition knumber.h:117
QWidget * widget() const
Definition knumber.h:90
T value_type
Definition knumber.h:51
#define KDWINUTILS_BEGIN_NAMESPACE
Definition kdwinutils_global.h:27
#define KDWINUTILS_END_NAMESPACE
Definition kdwinutils_global.h:28