1 /*
2     This file is part of the Kasten Framework, made within the KDE community.
3 
4     SPDX-FileCopyrightText: 2008 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8 
9 #include "bytearraysourcecodestreamencoderconfigeditor.hpp"
10 
11 // lib
12 #include "bytearraytextstreamencoderpreview.hpp"
13 // KF
14 #include <KLocalizedString>
15 #include <KComboBox>
16 // Qt
17 #include <QSpinBox>
18 #include <QFormLayout>
19 #include <QCheckBox>
20 #include <QLineEdit>
21 
22 namespace Kasten {
23 
ByteArraySourceCodeStreamEncoderConfigEditor(ByteArraySourceCodeStreamEncoder * encoder,QWidget * parent)24 ByteArraySourceCodeStreamEncoderConfigEditor::ByteArraySourceCodeStreamEncoderConfigEditor(ByteArraySourceCodeStreamEncoder* encoder, QWidget* parent)
25     : AbstractModelStreamEncoderConfigEditor(parent)
26     , mEncoder(encoder)
27 {
28     mSettings = mEncoder->settings();
29 
30     auto* pageLayout = new QFormLayout(this);
31     pageLayout->setContentsMargins(0, 0, 0, 0);
32 
33     // variable name
34     const QString variableNameLabel =
35         i18nc("@label:textbox name of the created variable",
36               "Name of variable:");
37 
38     mVariableNameEdit = new QLineEdit(this);
39     mVariableNameEdit->setClearButtonEnabled(true);
40     mVariableNameEdit->setText(mSettings.variableName);
41     connect(mVariableNameEdit, &QLineEdit::textChanged, this, &ByteArraySourceCodeStreamEncoderConfigEditor::onSettingsChanged);
42     pageLayout->addRow(variableNameLabel, mVariableNameEdit);
43 
44     // items per line
45     const QString itemsPerLineLabel =
46         i18nc("@label:textbox to define after how many items the list is wrapped",
47               "Items per line:");
48 
49     mItemsPerLineEdit = new QSpinBox(this);
50     mItemsPerLineEdit->setMinimum(1);
51     mItemsPerLineEdit->setValue(mSettings.elementsPerLine);
52     connect(mItemsPerLineEdit, QOverload<int>::of(&QSpinBox::valueChanged),
53             this, &ByteArraySourceCodeStreamEncoderConfigEditor::onSettingsChanged);
54     pageLayout->addRow(itemsPerLineLabel, mItemsPerLineEdit);
55 
56     // data type
57     const QString dataTypeLabel =
58         i18nc("@label:listbox the type of the data: char, integer, etc.",
59               "Data type:");
60 
61     mDataTypeSelect = new KComboBox(this);
62     const char* const* dataTypeNames = mEncoder->dataTypeNames();
63     const int dataTypesCount = mEncoder->dataTypesCount();
64     QStringList dataTypeNameStrings;
65     dataTypeNameStrings.reserve(dataTypesCount);
66     for (int i = 0; i < dataTypesCount; ++i) {
67         dataTypeNameStrings << QLatin1String(dataTypeNames[i]);
68     }
69 
70     mDataTypeSelect->addItems(dataTypeNameStrings);
71     mDataTypeSelect->setCurrentIndex(static_cast<int>(mSettings.dataType));
72     connect(mDataTypeSelect, QOverload<int>::of(&KComboBox::activated),
73             this, &ByteArraySourceCodeStreamEncoderConfigEditor::onSettingsChanged);
74     pageLayout->addRow(dataTypeLabel, mDataTypeSelect);
75 
76     // unsigned as hexadezimal
77     const QString unsignedAsHexadecimalLabel =
78         i18nc("@option:check Encode the values in hexadecimal instead of decimal, "
79               "if the datatype has the property Unsigned",
80               "Unsigned as hexadecimal:");
81 
82     mUnsignedAsHexadecimalCheck = new QCheckBox(this);
83     mUnsignedAsHexadecimalCheck->setChecked(mSettings.unsignedAsHexadecimal);
84     connect(mUnsignedAsHexadecimalCheck, &QCheckBox::toggled, this, &ByteArraySourceCodeStreamEncoderConfigEditor::onSettingsChanged);
85     pageLayout->addRow(unsignedAsHexadecimalLabel, mUnsignedAsHexadecimalCheck);
86 }
87 
88 ByteArraySourceCodeStreamEncoderConfigEditor::~ByteArraySourceCodeStreamEncoderConfigEditor() = default;
89 
isValid() const90 bool ByteArraySourceCodeStreamEncoderConfigEditor::isValid() const
91 {
92     return true; // TODO: warn if not all selected bytes are used due to the data type length
93 }
94 
createPreviewView() const95 AbstractSelectionView* ByteArraySourceCodeStreamEncoderConfigEditor::createPreviewView() const
96 {
97     return new ByteArrayTextStreamEncoderPreview(mEncoder);
98 }
99 
onSettingsChanged()100 void ByteArraySourceCodeStreamEncoderConfigEditor::onSettingsChanged()
101 {
102     mSettings.variableName = mVariableNameEdit->text();
103     mSettings.elementsPerLine = mItemsPerLineEdit->value();
104     mSettings.dataType = static_cast<SourceCodeStreamEncoderSettings::PrimitiveDataType>(mDataTypeSelect->currentIndex());
105     mSettings.unsignedAsHexadecimal = mUnsignedAsHexadecimalCheck->isChecked();
106     mEncoder->setSettings(mSettings);
107 }
108 
109 }
110