File indexing completed on 2024-07-07 05:34:58

0001 /*
0002     This file is part of the Okteta Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2010 Alex Richardson <alex.richardson@gmx.de>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "signedbitfielddatainformation.hpp"
0010 
0011 #include <KLocalizedString>
0012 
0013 #include <QScriptValue>
0014 
0015 #include <view/poddecoder/typeeditors/sintspinbox.hpp>
0016 #include "../sintdatainformation.hpp"
0017 
0018 QString SignedBitfieldDataInformation::valueStringImpl() const
0019 {
0020     Q_ASSERT(mWasAbleToRead);
0021     return SIntDataInformationMethods<qint64>::staticValueString(mValue.value<qint64>());
0022 }
0023 
0024 QVariant SignedBitfieldDataInformation::valueToQVariant() const
0025 {
0026     Q_ASSERT(mWasAbleToRead);
0027     return SIntDataInformationMethods<qint64>::staticToQVariant(mValue.value<qint64>());
0028 }
0029 
0030 QString SignedBitfieldDataInformation::valueToQString(AllPrimitiveTypes value) const
0031 {
0032     return SIntDataInformationMethods<qint64>::staticValueString(value.value<qint64>());
0033 }
0034 
0035 QVariant SignedBitfieldDataInformation::valueToQVariant(AllPrimitiveTypes value) const
0036 {
0037     return SIntDataInformationMethods<qint64>::staticToQVariant(value.value<qint64>());
0038 }
0039 
0040 QWidget* SignedBitfieldDataInformation::createEditWidget(QWidget* parent) const
0041 {
0042     auto* ret = new SIntSpinBox(parent);
0043     ret->setBase(Kasten::StructureViewPreferences::signedDisplayBase());
0044     ret->setRange(~mask(), mask() >> 1); // mask is unsigned, so shift will do the right thing
0045     return ret;
0046 }
0047 
0048 QVariant SignedBitfieldDataInformation::dataFromWidget(const QWidget* w) const
0049 {
0050     const auto* spin = qobject_cast<const SIntSpinBox*> (w);
0051     if (spin) {
0052         return spin->value();
0053     }
0054 
0055     return {};
0056 }
0057 
0058 void SignedBitfieldDataInformation::setWidgetData(QWidget* w) const
0059 {
0060     auto* spin = qobject_cast<SIntSpinBox*> (w);
0061     if (spin) {
0062         spin->setValue(mValue.value<qint64>());
0063     }
0064 }
0065 
0066 QScriptValue SignedBitfieldDataInformation::valueAsQScriptValue() const
0067 {
0068     if (width() <= 32) {
0069         return qint32(mValue.value<qint32>());  // 32 bit or less -> can be put in as value
0070     }
0071     // have to save it as string since 64 bit values are not supported
0072     return QString::number(mValue.value<qint64>());
0073 }
0074 
0075 void SignedBitfieldDataInformation::setValue(AllPrimitiveTypes newVal)
0076 {
0077     // check that values are not too large
0078     Q_ASSERT((newVal.value<qint64>() < 0 && (newVal.value<qint64>() | ~mask()) == newVal.value<quint64>())
0079              || (newVal.value<quint64>() & mask()) == newVal.value<quint64>());
0080     mValue = newVal.value<quint64>() & mask();
0081     // check if MSB is set -> negative -> sign extend
0082     if (newVal.value<quint64>() & (quint64(1) << (width() - 1))) {
0083         mValue = mValue.value<quint64>() | (~mask());
0084     }
0085 }
0086 
0087 AllPrimitiveTypes SignedBitfieldDataInformation::fromVariant(const QVariant& variant, bool* ok) const
0088 {
0089     return AllPrimitiveTypes(variant.toLongLong(ok));
0090 }
0091 
0092 QString SignedBitfieldDataInformation::typeNameImpl() const
0093 {
0094     return i18ncp("Data type", "signed bitfield (%1 bit wide)", "signed bitfield (%1 bits wide)",
0095                   width());
0096 }
0097 
0098 AbstractBitfieldDataInformation::Type SignedBitfieldDataInformation::bitfieldType() const
0099 {
0100     return AbstractBitfieldDataInformation::Type::Signed;
0101 }