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 "boolbitfielddatainformation.hpp"
0010 
0011 #include <KLocalizedString>
0012 #include <KComboBox>
0013 
0014 #include <QScriptValue>
0015 
0016 #include "../booldatainformation.hpp"
0017 #include "../../../../poddecoder/typeeditors/uintspinbox.hpp"
0018 
0019 QString BoolBitfieldDataInformation::valueStringImpl() const
0020 {
0021     Q_ASSERT(mWasAbleToRead);
0022     return BoolDataInformationMethods<quint64>::staticValueString(mValue.value<quint64>());
0023 }
0024 
0025 QVariant BoolBitfieldDataInformation::valueToQVariant() const
0026 {
0027     Q_ASSERT(mWasAbleToRead);
0028     return BoolDataInformationMethods<quint64>::staticToQVariant(mValue.value<quint64>());
0029 }
0030 
0031 QString BoolBitfieldDataInformation::valueToQString(AllPrimitiveTypes value) const
0032 {
0033     return BoolDataInformationMethods<quint64>::staticValueString(value.value<quint64>());
0034 }
0035 
0036 QVariant BoolBitfieldDataInformation::valueToQVariant(AllPrimitiveTypes value) const
0037 {
0038     return BoolDataInformationMethods<quint64>::staticToQVariant(value.value<quint64>());
0039 }
0040 
0041 QWidget* BoolBitfieldDataInformation::createEditWidget(QWidget* parent) const
0042 {
0043     if (width() == 1) {
0044         // just a simple combobox
0045         auto* box = new KComboBox(false, parent);
0046         box->addItem(i18nc("boolean value", "false"));
0047         box->addItem(i18nc("boolean value", "true"));
0048         return box;
0049     }
0050     auto* ret = new UIntSpinBox(parent);
0051     ret->setBase(Kasten::StructureViewPreferences::unsignedDisplayBase());
0052     ret->setMaximum(mask());
0053     return ret;
0054 }
0055 
0056 QVariant BoolBitfieldDataInformation::dataFromWidget(const QWidget* w) const
0057 {
0058     if (width() == 1) {
0059         const auto* box = qobject_cast<const KComboBox*>(w);
0060         Q_CHECK_PTR(box);
0061         return box->currentIndex();
0062     }
0063     const auto* spin = qobject_cast<const UIntSpinBox*>(w);
0064     Q_CHECK_PTR(spin);
0065     if (spin) {
0066         return spin->value();
0067     }
0068     return {};
0069 }
0070 
0071 void BoolBitfieldDataInformation::setWidgetData(QWidget* w) const
0072 {
0073     if (width() == 1) {
0074         auto* box = qobject_cast<KComboBox*>(w);
0075         Q_CHECK_PTR(box);
0076         box->setCurrentIndex((mValue.value<quint64>() == 0) ? 0 : 1);
0077         return;
0078     }
0079     auto* spin = qobject_cast<UIntSpinBox*> (w);
0080     if (spin) {
0081         spin->setValue(mValue.value<quint64>());
0082     }
0083 }
0084 
0085 QScriptValue BoolBitfieldDataInformation::valueAsQScriptValue() const
0086 {
0087     return (mValue.value<quint64>()) != 0;
0088 }
0089 
0090 QString BoolBitfieldDataInformation::typeNameImpl() const
0091 {
0092     return i18ncp("Data type", "boolean bitfield (%1 bit wide)", "boolean bitfield (%1 bits wide)",
0093                   width());
0094 }
0095 
0096 AbstractBitfieldDataInformation::Type BoolBitfieldDataInformation::bitfieldType() const
0097 {
0098     return AbstractBitfieldDataInformation::Type::Boolean;
0099 }