Warning, file /utilities/okteta/kasten/gui/liboktetawidgets/addresscombobox_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 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 "addresscombobox_p.hpp" 0010 #include "addresscombobox.hpp" 0011 0012 // KF 0013 #include <KLocalizedString> 0014 // Qt 0015 #include <QHBoxLayout> 0016 #include <QLineEdit> 0017 #include <QAbstractItemView> 0018 0019 /* 0020 Problem: what to do if the format is changed for which the current string is not valid? 0021 Solution: we always convert the string to the new format, so there is never such a situation 0022 */ 0023 0024 namespace Okteta { 0025 0026 static const QStringList& formatStrings() 0027 { 0028 static QStringList list = QStringList { 0029 // i18nc("@item:inlistbox guessing the format of the address by the input", "Auto"), 0030 i18nc("@item:inlistbox coding of offset in the hexadecimal format", "Hex"), 0031 i18nc("@item:inlistbox coding of offset in the decimal format", "Dec"), 0032 i18nc("@item:inlistbox coding of offset in the expression format", "Expr"), 0033 }; 0034 return list; 0035 } 0036 0037 void AddressComboBoxPrivate::init() 0038 { 0039 Q_Q(AddressComboBox); 0040 0041 auto* baseLayout = new QHBoxLayout(q); 0042 baseLayout->setContentsMargins(0, 0, 0, 0); 0043 baseLayout->setSpacing(0); 0044 0045 mFormatComboBox = new KComboBox(q); 0046 mFormatComboBox->addItems(formatStrings()); 0047 QObject::connect(mFormatComboBox, QOverload<int>::of(&QComboBox::activated), 0048 q, [&](int index) { onFormatChanged(index); }); 0049 0050 mValueComboBox = new KComboBox(q); 0051 mValueComboBox->setEditable(true); 0052 mValueComboBox->setMaxCount(10); 0053 mValueComboBox->setInsertPolicy(QComboBox::NoInsert); 0054 mValueComboBox->setDuplicatesEnabled(false); 0055 q->setFocusProxy(mValueComboBox); 0056 QObject::connect(mValueComboBox->lineEdit(), &QLineEdit::textEdited, 0057 q, [&](const QString& text) { onValueEdited(text); }); 0058 QAbstractItemView* formatComboBoxListView = mFormatComboBox->view(); 0059 QObject::connect(formatComboBoxListView, &QAbstractItemView::activated, 0060 mValueComboBox, QOverload<>::of(&KComboBox::setFocus)); 0061 // TODO: is a workaround for Qt 4.5.1 which doesn't emit activated() for mouse clicks 0062 QObject::connect(formatComboBoxListView, &QAbstractItemView::pressed, 0063 mValueComboBox, QOverload<>::of(&KComboBox::setFocus)); 0064 mValidator = new AddressValidator(mValueComboBox, AddressValidator::HexadecimalCoding); 0065 const AddressValidator::Coding coding = 0066 static_cast<AddressValidator::Coding>(mFormatComboBox->currentIndex()); 0067 mValidator->setCodec(coding); 0068 mValueComboBox->setValidator(mValidator); 0069 QObject::connect(mValueComboBox, QOverload<int>::of(&QComboBox::activated), 0070 q, [&](int index) { onValueActivated(index); }); 0071 baseLayout->addWidget(mFormatComboBox); 0072 baseLayout->addWidget(mValueComboBox, 1); 0073 QWidget::setTabOrder(mFormatComboBox, mValueComboBox); 0074 } 0075 0076 void AddressComboBoxPrivate::setFormat(AddressComboBox::Coding codingId) 0077 { 0078 if (codingId == mFormatComboBox->currentIndex()) { 0079 return; 0080 } 0081 0082 mFormatComboBox->setCurrentIndex(codingId); 0083 onFormatChanged(codingId); 0084 } 0085 0086 void AddressComboBoxPrivate::rememberCurrentAddress() 0087 { 0088 // don't insert same value multiple times in a row 0089 if (mValueComboBox->itemText(0) != mValueComboBox->currentText()) { 0090 mValueComboBox->insertItem(-1, mValueComboBox->currentText(), mFormatComboBox->currentIndex()); 0091 } 0092 } 0093 0094 void AddressComboBoxPrivate::onFormatChanged(int formatIndex) 0095 { 0096 Q_Q(AddressComboBox); 0097 0098 const QString currentValueText = mValueComboBox->currentText(); 0099 const bool isCurrentValueTextEmpty = currentValueText.isEmpty(); 0100 0101 AddressValidator::AddressType addressType; 0102 Address address; 0103 if (isCurrentValueTextEmpty) { 0104 address = -1; 0105 addressType = mAddressType; 0106 } else { 0107 address = mValidator->toAddress(currentValueText, &addressType); 0108 } 0109 0110 mValidator->setCodec(static_cast<AddressValidator::Coding>(formatIndex)); 0111 0112 if (!isCurrentValueTextEmpty) { 0113 const QString convertedValueText = mValidator->toString(address, addressType); 0114 mValueComboBox->setEditText(convertedValueText); 0115 } 0116 0117 if (mAddressType != addressType) { 0118 mAddressType = addressType; 0119 emit q->addressTypeChanged(mAddressType); 0120 } 0121 emit q->formatChanged(formatIndex); 0122 } 0123 0124 void AddressComboBoxPrivate::onValueEdited(const QString& value) 0125 { 0126 Q_Q(AddressComboBox); 0127 0128 AddressValidator::AddressType addressType; 0129 const Address address = mValidator->toAddress(value, &addressType); 0130 0131 if (mAddressType != addressType) { 0132 mAddressType = addressType; 0133 emit q->addressTypeChanged(mAddressType); 0134 } 0135 emit q->addressChanged(address); 0136 } 0137 0138 void AddressComboBoxPrivate::onValueActivated(int index) 0139 { 0140 Q_Q(AddressComboBox); 0141 0142 if (index != -1) { 0143 const int oldFormatIndex = mFormatComboBox->currentIndex(); 0144 const int itemFormatIndex = mValueComboBox->itemData(index).toInt(); 0145 0146 const bool isOtherFormat = (oldFormatIndex != itemFormatIndex); 0147 if (isOtherFormat) { 0148 mFormatComboBox->setCurrentIndex(itemFormatIndex); 0149 mValidator->setCodec(static_cast<AddressValidator::Coding>(itemFormatIndex)); 0150 0151 } 0152 const QString currentValueText = mValueComboBox->currentText(); 0153 0154 AddressValidator::AddressType addressType; 0155 const Address address = mValidator->toAddress(currentValueText, &addressType); 0156 0157 if (mAddressType != addressType) { 0158 mAddressType = addressType; 0159 emit q->addressTypeChanged(mAddressType); 0160 } 0161 emit q->addressChanged(address); 0162 if (isOtherFormat) { 0163 emit q->formatChanged(itemFormatIndex); 0164 } 0165 } 0166 } 0167 0168 }