1 /*
2     This file is part of the Okteta Kasten Framework, made within the KDE community.
3 
4     SPDX-FileCopyrightText: 2009 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 "structureviewsettingswidget.hpp"
10 
11 #include "structureviewpreferences.h"
12 #include "../structlogging.hpp"
13 
14 #include <KLocalizedString>
15 
StructureViewSettingsWidget()16 StructureViewSettingsWidget::StructureViewSettingsWidget()
17 {
18     ui.setupUi(this);
19     ui.combo_CharDisplayBase->setEnabled(ui.kcfg_ShowCharNumericalValue->isChecked());
20 
21     // no need for a hidden spinbox with byteOrder, since it is a simple sequential enum
22 
23     // setup the hidden spin boxes for the display bases
24     // these are needed since KConfigXT always uses the combo box index as the value
25     // we want the UserData of the current index instead
26     // maybe there is a nicer solution, but this works
27     ui.kcfg_CharDisplayBase->setValue(Kasten::StructureViewPreferences::charDisplayBase());
28     ui.kcfg_CharDisplayBase->setHidden(true);
29     ui.kcfg_SignedDisplayBase->setValue(Kasten::StructureViewPreferences::signedDisplayBase());
30     ui.kcfg_SignedDisplayBase->setHidden(true);
31     ui.kcfg_UnsignedDisplayBase->setValue(Kasten::StructureViewPreferences::unsignedDisplayBase());
32     ui.kcfg_UnsignedDisplayBase->setHidden(true);
33     setupBasesCombo(ui.combo_SignedDisplayBase, Kasten::StructureViewPreferences::self()->signedDisplayBaseItem(),
34                     Kasten::StructureViewPreferences::signedDisplayBase(), SLOT(setSignedDisplay(int)));
35     setupBasesCombo(ui.combo_UnsignedDisplayBase, Kasten::StructureViewPreferences::self()->unsignedDisplayBaseItem(),
36                     Kasten::StructureViewPreferences::unsignedDisplayBase(), SLOT(setUnsignedDisplay(int)));
37     setupBasesCombo(ui.combo_CharDisplayBase, Kasten::StructureViewPreferences::self()->charDisplayBaseItem(),
38                     Kasten::StructureViewPreferences::charDisplayBase(), SLOT(setCharDisplay(int)));
39 }
40 
41 StructureViewSettingsWidget::~StructureViewSettingsWidget() = default;
42 
setupBasesCombo(QComboBox * box,KConfigSkeletonItem * configItem,int currentValue,const char * slot)43 void StructureViewSettingsWidget::setupBasesCombo(QComboBox* box, KConfigSkeletonItem* configItem,
44                                                   int currentValue, const char* slot)
45 {
46     Q_ASSERT(box->count() == 0);
47     Q_ASSERT(currentValue == 2 || currentValue == 8 || currentValue == 10 || currentValue == 16);
48     qCDebug(LOG_KASTEN_OKTETA_CONTROLLERS_STRUCTURES) << "current value:" << configItem->property() << "vs" << currentValue;
49     box->addItem(i18nc("@item:inlistbox", "Binary"), 2);
50     box->addItem(i18nc("@item:inlistbox", "Octal"), 8);
51     box->addItem(i18nc("@item:inlistbox", "Decimal"), 10);
52     box->addItem(i18nc("@item:inlistbox", "Hexadecimal"), 16);
53 
54     box->setCurrentIndex(currentValue == 2 ? 0 : (currentValue == 8 ? 1 : (currentValue == 16 ? 3 : 2)));
55 
56     box->setToolTip(configItem->toolTip());
57     connect(box, SIGNAL(activated(int)), this, slot);
58 }
59 
handleMapping(int index,QComboBox * box,QSpinBox * spin)60 void StructureViewSettingsWidget::handleMapping(int index, QComboBox* box, QSpinBox* spin)
61 {
62     QVariant currentValue = box->itemData(index);
63     qCDebug(LOG_KASTEN_OKTETA_CONTROLLERS_STRUCTURES) << "box changed to " << index << "value = " << currentValue;
64     if (spin->value() != currentValue.toInt()) {
65         spin->setValue(currentValue.toInt());
66     }
67 }
68 
setCharDisplay(int index)69 void StructureViewSettingsWidget::setCharDisplay(int index)
70 {
71     qCDebug(LOG_KASTEN_OKTETA_CONTROLLERS_STRUCTURES) << "byteOrder changed to " << index;
72     handleMapping(index, ui.combo_CharDisplayBase, ui.kcfg_CharDisplayBase);
73 }
74 
setSignedDisplay(int index)75 void StructureViewSettingsWidget::setSignedDisplay(int index)
76 {
77     qCDebug(LOG_KASTEN_OKTETA_CONTROLLERS_STRUCTURES) << "byteOrder changed to " << index;
78     handleMapping(index, ui.combo_SignedDisplayBase, ui.kcfg_SignedDisplayBase);
79 }
80 
setUnsignedDisplay(int index)81 void StructureViewSettingsWidget::setUnsignedDisplay(int index)
82 {
83     qCDebug(LOG_KASTEN_OKTETA_CONTROLLERS_STRUCTURES) << "byteOrder changed to " << index;
84     handleMapping(index, ui.combo_UnsignedDisplayBase, ui.kcfg_UnsignedDisplayBase);
85 }
86