Warning, file /utilities/okteta/kasten/gui/liboktetawidgets/bytearraycombobox_p.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2009, 2011 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "bytearraycombobox_p.hpp"
0010 #include "bytearraycombobox.hpp"
0011 
0012 // KF
0013 #include <KLocalizedString>
0014 // Qt
0015 #include <QHBoxLayout>
0016 #include <QLineEdit>
0017 #include <QAbstractItemView>
0018 
0019 namespace Okteta {
0020 
0021 const QStringList& formatNames()
0022 {
0023     static QStringList list = QStringList {
0024 //         i18nc("@item:inlistbox guessing the coding of the bytes by the input",       "Auto"),
0025         i18nc("@item:inlistbox coding of the bytes as values in the hexadecimal format", "Hex"),
0026         i18nc("@item:inlistbox coding of the bytes as values in the decimal format",     "Dec"),
0027         i18nc("@item:inlistbox coding of the bytes as values in the octal format",       "Oct"),
0028         i18nc("@item:inlistbox coding of the bytes as values in the binary format",      "Bin"),
0029         i18nc("@item:inlistbox coding of the bytes as characters with the values",       "Char"),
0030         i18nc("@item:inlistbox coding of the bytes as UTF-8 characters with the values", "UTF-8"),
0031     };
0032     return list;
0033 }
0034 
0035 void ByteArrayComboBoxPrivate::init()
0036 {
0037     Q_Q(ByteArrayComboBox);
0038 
0039     auto* baseLayout = new QHBoxLayout(q);
0040     baseLayout->setContentsMargins(0, 0, 0, 0);
0041     baseLayout->setSpacing(0);
0042 
0043     mFormatComboBox = new KComboBox(q);
0044     mFormatComboBox->addItems(formatNames());
0045     QObject::connect(mFormatComboBox, QOverload<int>::of(&QComboBox::activated),
0046                      q, [&](int index) { onFormatChanged(index); });
0047 
0048     mValueComboBox = new KComboBox(q);
0049     mValueComboBox->setEditable(true);
0050     mValueComboBox->setMaxCount(10);
0051     mValueComboBox->setInsertPolicy(QComboBox::NoInsert);
0052     mValueComboBox->setDuplicatesEnabled(false);
0053     q->setFocusProxy(mValueComboBox);
0054     QObject::connect(mValueComboBox->lineEdit(), &QLineEdit::textEdited,
0055                      q, [&](const QString& text) { onValueEdited(text); });
0056     QAbstractItemView* formatComboBoxListView = mFormatComboBox->view();
0057     QObject::connect(formatComboBoxListView, &QAbstractItemView::activated,
0058                      mValueComboBox, QOverload<>::of(&KComboBox::setFocus));
0059     // TODO: is a workaround for Qt 4.5.1 which doesn't emit activated() for mouse clicks
0060     QObject::connect(formatComboBoxListView, &QAbstractItemView::pressed,
0061                      mValueComboBox, QOverload<>::of(&KComboBox::setFocus));
0062     mValidator = new ByteArrayValidator(mValueComboBox);
0063     const ByteArrayValidator::Coding coding =
0064         static_cast<ByteArrayValidator::Coding>(mFormatComboBox->currentIndex());
0065     mValidator->setCodec(coding);
0066 
0067     mValueComboBox->setValidator(mValidator);
0068     QObject::connect(mValueComboBox, QOverload<int>::of(&QComboBox::activated),
0069                      q, [&](int index) { onValueActivated(index); });
0070 
0071     baseLayout->addWidget(mFormatComboBox);
0072     baseLayout->addWidget(mValueComboBox, 1);
0073     QWidget::setTabOrder(mFormatComboBox, mValueComboBox);
0074 }
0075 
0076 void ByteArrayComboBoxPrivate::setByteArray(const QByteArray& byteArray)
0077 {
0078     mValueComboBox->setEditText(mValidator->toString(byteArray));
0079 }
0080 
0081 void ByteArrayComboBoxPrivate::setFormat(ByteArrayComboBox::Coding codecId)
0082 {
0083     if (codecId == mFormatComboBox->currentIndex()) {
0084         return;
0085     }
0086 
0087     mFormatComboBox->setCurrentIndex(codecId);
0088     onFormatChanged(codecId);
0089 }
0090 
0091 void ByteArrayComboBoxPrivate::setCharCodec(const QString& charCodecName)
0092 {
0093     const bool isChar8Visible = (mFormatComboBox->currentIndex() == ByteArrayValidator::CharCoding);
0094 
0095     // update the char string if shown
0096     QByteArray currentByteArray;
0097     if (isChar8Visible) {
0098         const QString currentChar8String = mValueComboBox->currentText();
0099         currentByteArray = mValidator->toByteArray(currentChar8String);
0100     }
0101 
0102     mValidator->setCharCodec(charCodecName);
0103 
0104     if (isChar8Visible) {
0105         const QString char8String = mValidator->toString(currentByteArray);
0106         mValueComboBox->setEditText(char8String);
0107     }
0108 }
0109 
0110 void ByteArrayComboBoxPrivate::setMaxLength(int maxLength)
0111 {
0112     const int oldMaxLength = mValidator->maxLength();
0113     if (oldMaxLength == maxLength) {
0114         return;
0115     }
0116 
0117     mValidator->setMaxLength(maxLength);
0118 
0119     if (oldMaxLength > maxLength) {
0120         QString currentText = mValueComboBox->currentText();
0121         int dummyPos;
0122         mValidator->validate(currentText, dummyPos);
0123         mValueComboBox->setEditText(currentText);
0124     }
0125 }
0126 
0127 void ByteArrayComboBoxPrivate::setMinLength(int minLength)
0128 {
0129     const int oldMinLength = mValidator->minLength();
0130     if (oldMinLength == minLength) {
0131         return;
0132     }
0133 
0134     mValidator->setMinLength(minLength);
0135 
0136     if (oldMinLength < minLength) {
0137         QString currentText = mValueComboBox->currentText();
0138         int dummyPos;
0139         mValidator->validate(currentText, dummyPos);
0140         mValueComboBox->setEditText(currentText);
0141     }
0142 }
0143 
0144 void ByteArrayComboBoxPrivate::rememberCurrentByteArray()
0145 {
0146     mValueComboBox->insertItem(-1, mValueComboBox->currentText(), mFormatComboBox->currentIndex());
0147 }
0148 
0149 int ByteArrayComboBoxPrivate::maxLength() const
0150 {
0151     return mValidator->maxLength();
0152 }
0153 
0154 int ByteArrayComboBoxPrivate::minLength() const
0155 {
0156     return mValidator->minLength();
0157 }
0158 
0159 void ByteArrayComboBoxPrivate::onFormatChanged(int index)
0160 {
0161     Q_Q(ByteArrayComboBox);
0162 
0163     const QString currentValueText = mValueComboBox->currentText();
0164     const bool isCurrentValueTextEmpty = currentValueText.isEmpty();
0165     const QByteArray byteArray = isCurrentValueTextEmpty ? QByteArray() : mValidator->toByteArray(currentValueText);
0166 
0167     mValidator->setCodec(static_cast<ByteArrayValidator::Coding>(index));
0168 
0169     if (!isCurrentValueTextEmpty) {
0170         const QString convertedValueText = mValidator->toString(byteArray);
0171         mValueComboBox->setEditText(convertedValueText);
0172     }
0173 
0174     emit q->formatChanged(index);
0175 }
0176 
0177 void ByteArrayComboBoxPrivate::onValueEdited(const QString& value)
0178 {
0179     Q_Q(ByteArrayComboBox);
0180 
0181     const QByteArray byteArray = mValidator->toByteArray(value);
0182 
0183     emit q->byteArrayChanged(byteArray);
0184 }
0185 
0186 void ByteArrayComboBoxPrivate::onValueActivated(int index)
0187 {
0188     Q_Q(ByteArrayComboBox);
0189 
0190     if (index != -1) {
0191         const int oldFormatIndex = mFormatComboBox->currentIndex();
0192         const int itemFormatIndex = mValueComboBox->itemData(index).toInt();
0193         const bool isOtherFormat = (oldFormatIndex != itemFormatIndex);
0194 
0195         if (isOtherFormat) {
0196             mFormatComboBox->setCurrentIndex(itemFormatIndex);
0197             mValidator->setCodec(static_cast<ByteArrayValidator::Coding>(itemFormatIndex));
0198 
0199         }
0200         const QString currentValueText = mValueComboBox->currentText();
0201         const QByteArray byteArray = mValidator->toByteArray(currentValueText);
0202 
0203         emit q->byteArrayChanged(byteArray);
0204         if (isOtherFormat) {
0205             emit q->formatChanged(itemFormatIndex);
0206         }
0207     }
0208 }
0209 
0210 }