|
| 1 | +#include <QRegularExpressionValidator> |
| 2 | +#include <QMimeData> |
| 3 | +#include "formatutils.h" |
| 4 | +#include "bytelisttextedit.h" |
| 5 | + |
| 6 | +/// |
| 7 | +/// \brief ByteListTextEdit::ByteListTextEdit |
| 8 | +/// \param parent |
| 9 | +/// |
| 10 | +ByteListTextEdit::ByteListTextEdit(QWidget* parent) |
| 11 | + :QPlainTextEdit(parent) |
| 12 | + ,_separator(' ') |
| 13 | + ,_validator(nullptr) |
| 14 | +{ |
| 15 | + setInputMode(DecMode); |
| 16 | + connect(this, &QPlainTextEdit::textChanged, this, &ByteListTextEdit::on_textChanged); |
| 17 | +} |
| 18 | + |
| 19 | +/// |
| 20 | +/// \brief ByteListTextEdit::ByteListTextEdit |
| 21 | +/// \param mode |
| 22 | +/// \param parent |
| 23 | +/// |
| 24 | +ByteListTextEdit::ByteListTextEdit(InputMode mode, QWidget *parent) |
| 25 | + :QPlainTextEdit(parent) |
| 26 | + ,_separator(' ') |
| 27 | + ,_validator(nullptr) |
| 28 | +{ |
| 29 | + setInputMode(mode); |
| 30 | + connect(this, &QPlainTextEdit::textChanged, this, &ByteListTextEdit::on_textChanged); |
| 31 | +} |
| 32 | + |
| 33 | +/// |
| 34 | +/// \brief ByteListTextEdit::value |
| 35 | +/// \return |
| 36 | +/// |
| 37 | +QByteArray ByteListTextEdit::value() const |
| 38 | +{ |
| 39 | + return _value; |
| 40 | +} |
| 41 | + |
| 42 | +/// |
| 43 | +/// \brief ByteListTextEdit::setValue |
| 44 | +/// \param value |
| 45 | +/// |
| 46 | +void ByteListTextEdit::setValue(const QByteArray& value) |
| 47 | +{ |
| 48 | + switch(_inputMode) |
| 49 | + { |
| 50 | + case DecMode: |
| 51 | + { |
| 52 | + const auto text = formatByteArray(DataDisplayMode::Decimal, value); |
| 53 | + if(text != toPlainText()) |
| 54 | + setPlainText(formatByteArray(DataDisplayMode::Decimal, value)); |
| 55 | + } |
| 56 | + break; |
| 57 | + |
| 58 | + case HexMode: |
| 59 | + { |
| 60 | + const auto text = formatByteArray(DataDisplayMode::Hex, value); |
| 61 | + if(text != toPlainText()) |
| 62 | + setPlainText(formatByteArray(DataDisplayMode::Hex, value)); |
| 63 | + } |
| 64 | + break; |
| 65 | + } |
| 66 | + |
| 67 | + if(value != _value) |
| 68 | + { |
| 69 | + _value = value; |
| 70 | + emit valueChanged(_value); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/// |
| 75 | +/// \brief ByteListTextEdit::inputMode |
| 76 | +/// \return |
| 77 | +/// |
| 78 | +ByteListTextEdit::InputMode ByteListTextEdit::inputMode() const |
| 79 | +{ |
| 80 | + return _inputMode; |
| 81 | +} |
| 82 | + |
| 83 | +/// |
| 84 | +/// \brief ByteListTextEdit::setInputMode |
| 85 | +/// \param mode |
| 86 | +/// |
| 87 | +void ByteListTextEdit::setInputMode(InputMode mode) |
| 88 | +{ |
| 89 | + _inputMode = mode; |
| 90 | + |
| 91 | + if(_validator) |
| 92 | + { |
| 93 | + delete _validator; |
| 94 | + _validator = nullptr; |
| 95 | + } |
| 96 | + |
| 97 | + const auto sep = (_separator == ' ')? "\\s" : QString(_separator); |
| 98 | + switch(_inputMode) |
| 99 | + { |
| 100 | + case DecMode: |
| 101 | + _validator =new QRegularExpressionValidator(QRegularExpression("(?:[0-9]{1,2}[" + sep + "]{0,1})*"), this); |
| 102 | + break; |
| 103 | + |
| 104 | + case HexMode: |
| 105 | + _validator = new QRegularExpressionValidator(QRegularExpression("(?:[0-9a-fA-F]{1,2}[" + sep + "]{0,1})*"), this); |
| 106 | + break; |
| 107 | + } |
| 108 | + |
| 109 | + setValue(_value); |
| 110 | +} |
| 111 | + |
| 112 | +/// |
| 113 | +/// \brief ByteListTextEdit::text |
| 114 | +/// \return |
| 115 | +/// |
| 116 | +QString ByteListTextEdit::text() const |
| 117 | +{ |
| 118 | + return toPlainText(); |
| 119 | +} |
| 120 | + |
| 121 | +/// |
| 122 | +/// \brief ByteListTextEdit::setText |
| 123 | +/// \param text |
| 124 | +/// |
| 125 | +void ByteListTextEdit::setText(const QString& text) |
| 126 | +{ |
| 127 | + setPlainText(text); |
| 128 | + updateValue(); |
| 129 | +} |
| 130 | + |
| 131 | +/// |
| 132 | +/// \brief ByteListTextEdit::focusOutEvent |
| 133 | +/// \param event |
| 134 | +/// |
| 135 | +void ByteListTextEdit::focusOutEvent(QFocusEvent* e) |
| 136 | +{ |
| 137 | + updateValue(); |
| 138 | + QPlainTextEdit::focusOutEvent(e); |
| 139 | +} |
| 140 | + |
| 141 | +/// |
| 142 | +/// \brief ByteListTextEdit::keyPressEvent |
| 143 | +/// \param e |
| 144 | +/// |
| 145 | +void ByteListTextEdit::keyPressEvent(QKeyEvent *e) |
| 146 | +{ |
| 147 | + if(!_validator) |
| 148 | + { |
| 149 | + QPlainTextEdit::keyPressEvent(e); |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + if(e->key() == Qt::Key_Enter || |
| 154 | + e->key() == Qt::Key_Return) |
| 155 | + { |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + int pos = 0; |
| 160 | + auto text = toPlainText() + e->text(); |
| 161 | + const auto state = _validator->validate(text, pos); |
| 162 | + |
| 163 | + if(state == QValidator::Acceptable || |
| 164 | + e->key() == Qt::Key_Backspace || |
| 165 | + e->key() == Qt::Key_Delete || |
| 166 | + e->key() == Qt::Key_Space || |
| 167 | + e->matches(QKeySequence::Cut) || |
| 168 | + e->matches(QKeySequence::Copy) || |
| 169 | + e->matches(QKeySequence::Paste) || |
| 170 | + e->matches(QKeySequence::Undo) || |
| 171 | + e->matches(QKeySequence::Redo) || |
| 172 | + e->matches(QKeySequence::SelectAll)) |
| 173 | + { |
| 174 | + QPlainTextEdit::keyPressEvent(e); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +/// |
| 179 | +/// \brief ByteListTextEdit::canInsertFromMimeData |
| 180 | +/// \param source |
| 181 | +/// \return |
| 182 | +/// |
| 183 | +bool ByteListTextEdit::canInsertFromMimeData(const QMimeData* source) const |
| 184 | +{ |
| 185 | + int pos = 0; |
| 186 | + auto text = source->text().trimmed(); |
| 187 | + const auto state = _validator->validate(text, pos); |
| 188 | + |
| 189 | + return state == QValidator::Acceptable; |
| 190 | +} |
| 191 | + |
| 192 | +/// |
| 193 | +/// \brief ByteListTextEdit::insertFromMimeData |
| 194 | +/// \param source |
| 195 | +/// |
| 196 | +void ByteListTextEdit::insertFromMimeData(const QMimeData* source) |
| 197 | +{ |
| 198 | + int pos = 0; |
| 199 | + auto text = source->text().trimmed(); |
| 200 | + const auto state = _validator->validate(text, pos); |
| 201 | + |
| 202 | + if(state == QValidator::Acceptable) |
| 203 | + { |
| 204 | + QPlainTextEdit::insertFromMimeData(source); |
| 205 | + updateValue(); |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +/// |
| 210 | +/// \brief ByteListTextEdit::on_textChanged |
| 211 | +/// |
| 212 | +void ByteListTextEdit::on_textChanged() |
| 213 | +{ |
| 214 | + QByteArray value; |
| 215 | + switch(_inputMode) |
| 216 | + { |
| 217 | + case DecMode: |
| 218 | + { |
| 219 | + for(auto&& s : text().split(_separator)) |
| 220 | + { |
| 221 | + bool ok; |
| 222 | + const quint8 v = s.trimmed().toUInt(&ok); |
| 223 | + if(ok) value.push_back(v); |
| 224 | + } |
| 225 | + } |
| 226 | + break; |
| 227 | + |
| 228 | + case HexMode: |
| 229 | + { |
| 230 | + for(auto&& s : text().split(_separator)) |
| 231 | + { |
| 232 | + bool ok; |
| 233 | + const quint8 v = s.trimmed().toUInt(&ok, 16); |
| 234 | + if(ok) value.push_back(v); |
| 235 | + } |
| 236 | + } |
| 237 | + break; |
| 238 | + } |
| 239 | + |
| 240 | + if(value != _value) |
| 241 | + { |
| 242 | + _value = value; |
| 243 | + emit valueChanged(_value); |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | +/// |
| 248 | +/// \brief ByteListTextEdit::updateValue |
| 249 | +/// |
| 250 | +void ByteListTextEdit::updateValue() |
| 251 | +{ |
| 252 | + QByteArray value; |
| 253 | + switch(_inputMode) |
| 254 | + { |
| 255 | + case DecMode: |
| 256 | + { |
| 257 | + for(auto&& s : text().split(_separator)) |
| 258 | + { |
| 259 | + bool ok; |
| 260 | + const quint8 v = s.trimmed().toUInt(&ok); |
| 261 | + if(ok) value.push_back(v); |
| 262 | + } |
| 263 | + |
| 264 | + if(!value.isEmpty()) setValue(value); |
| 265 | + else setValue(_value); |
| 266 | + } |
| 267 | + break; |
| 268 | + |
| 269 | + case HexMode: |
| 270 | + { |
| 271 | + for(auto&& s : text().split(_separator)) |
| 272 | + { |
| 273 | + bool ok; |
| 274 | + const quint8 v = s.trimmed().toUInt(&ok, 16); |
| 275 | + if(ok) value.push_back(v); |
| 276 | + } |
| 277 | + |
| 278 | + if(!value.isEmpty()) setValue(value); |
| 279 | + else setValue(_value); |
| 280 | + } |
| 281 | + break; |
| 282 | + } |
| 283 | +} |
0 commit comments