1 /*
2 This file is part of the Okteta Kasten Framework, made within the KDE community.
3
4 SPDX-FileCopyrightText: 2011 Alex Richardson <alex.richardson@gmx.de>
5
6 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9 #include "uintdatainformation.hpp"
10
11 #include "../../structlogging.hpp"
12
13 // KF
14 #include <KLocalizedString>
15 // Qt
16 #include <QScriptValue>
17 #include <QLocale>
18
19 #include "../../../poddecoder/typeeditors/uintspinbox.hpp"
20 #include "structureviewpreferences.h"
21
22 template <typename T>
asScriptValue(T value,QScriptEngine * engine,ScriptHandlerInfo * handlerInfo)23 QScriptValue UIntDataInformationMethods<T>::asScriptValue(T value, QScriptEngine* engine, ScriptHandlerInfo* handlerInfo)
24 {
25 Q_UNUSED(engine);
26 Q_UNUSED(handlerInfo);
27 return {value};
28 }
29
30 template <>
asScriptValue(quint64 value,QScriptEngine * engine,ScriptHandlerInfo * handlerInfo)31 QScriptValue UIntDataInformationMethods<quint64>::asScriptValue(quint64 value, QScriptEngine* engine,
32 ScriptHandlerInfo* handlerInfo)
33 {
34 Q_UNUSED(engine);
35 Q_UNUSED(handlerInfo);
36 return {QString::number(value, 10)};
37 }
38
39 template <typename T>
staticValueString(T value,int base)40 QString UIntDataInformationMethods<T>::staticValueString(T value, int base)
41 {
42 QString num = QString::number(value, base);
43 if (base == 10) {
44 if (Kasten::StructureViewPreferences::localeAwareDecimalFormatting()) {
45 num = QLocale().toString(value);
46 }
47 } else {
48 // add one space every 8 chars
49 for (int i = 8; i < num.length(); i += 9) {
50 num.insert(num.length() - i, QLatin1Char(' '));
51 }
52 }
53 return PrimitiveDataInformation::basePrefix(base) + num;
54 }
55
56 template <typename T>
staticCreateEditWidget(QWidget * parent)57 inline QWidget* UIntDataInformationMethods<T>::staticCreateEditWidget(QWidget* parent)
58 {
59 auto* ret = new UIntSpinBox(parent, Kasten::StructureViewPreferences::unsignedDisplayBase());
60 ret->setMaximum(std::numeric_limits<T>::max());
61 return ret;
62 }
63
64 template <typename T>
staticDataFromWidget(const QWidget * w)65 inline QVariant UIntDataInformationMethods<T>::staticDataFromWidget(const QWidget* w)
66 {
67 const auto* spin = qobject_cast<const UIntSpinBox*> (w);
68 Q_CHECK_PTR(spin);
69 if (spin) {
70 return {spin->value()};
71 }
72 qCWarning(LOG_KASTEN_OKTETA_CONTROLLERS_STRUCTURES) << "could not cast widget";
73 return {};
74 }
75
76 template <typename T>
staticSetWidgetData(T value,QWidget * w)77 inline void UIntDataInformationMethods<T>::staticSetWidgetData(T value, QWidget* w)
78 {
79 auto* spin = qobject_cast<UIntSpinBox*> (w);
80 Q_CHECK_PTR(spin);
81 if (spin) {
82 spin->setValue(value);
83 }
84 }
85
86 // explicitly instantiate all valid classes (c++-faq-lite 35.12)
87 template class UIntDataInformationMethods<quint8>;
88 template class UIntDataInformationMethods<quint16>;
89 template class UIntDataInformationMethods<quint32>;
90 template class UIntDataInformationMethods<quint64>;
91