From 6027701a360cf3b5fe24d2380ac84a9f4f67db8e Mon Sep 17 00:00:00 2001 From: codeboss <2422523675@qq.com> Date: Thu, 31 Jul 2025 00:39:26 +0800 Subject: [PATCH] u --- TranslateUI/SequenceView.cpp | 13 ++- TranslateUI/StructuralRuleView.cpp | 35 +------ TranslateUI/StructuralRuleView.h | 16 --- TranslateUI/TranslateBasic.cpp | 127 +++++++++++------------- TranslateUI/TranslateBasic.h | 49 +++++---- TranslateUI/TranslateUI.vcxproj | 2 +- TranslateUI/TranslateUI.vcxproj.filters | 12 +-- 7 files changed, 107 insertions(+), 147 deletions(-) diff --git a/TranslateUI/SequenceView.cpp b/TranslateUI/SequenceView.cpp index dc186d9..34eb505 100644 --- a/TranslateUI/SequenceView.cpp +++ b/TranslateUI/SequenceView.cpp @@ -45,12 +45,12 @@ void SequenceRulesView::currentRuleRefresh(const QModelIndex& idx) if (rule_insv) { auto offset_number = offset_index.data(Qt::DisplayRole).toInt(); auto count_number = count_index.data(Qt::DisplayRole).toInt(); - rule_insv->setOffsetFromPrevious(offset_number); - rule_insv->setCountWithinParse(count_number); + rule_insv->setOffsetSpan(offset_number); + rule_insv->setCountWithin(count_number); } - _sequence_model->setData(offset_index, new_inst->offsetFromPrevious(), Qt::EditRole); - _sequence_model->setData(count_index, new_inst->countWithinParse(), Qt::EditRole); + _sequence_model->setData(offset_index, new_inst->offsetSpan(), Qt::EditRole); + _sequence_model->setData(count_index, new_inst->countWithin(), Qt::EditRole); auto param_index = idx.sibling(idx.row(), 4); QJsonObject obj; @@ -124,7 +124,7 @@ void SequenceRulesView::addTranslateUnit() this->_rule_sequence << std::static_pointer_cast(curr_rule); auto hex_rule = std::static_pointer_cast(curr_rule); - hex_rule->setCountWithinParse(1); + hex_rule->setCountWithin(1); QJsonObject obj; curr_rule->saveTo(obj); @@ -155,6 +155,9 @@ TranslateBasic::TranslateBasic() u_ptr = std::make_shared(); _extractor_types[u_ptr->name()] = u_ptr; + u_ptr = std::make_shared(); + _extractor_types[u_ptr->name()] = u_ptr; + u_ptr = std::make_shared(); _extractor_types[u_ptr->name()] = u_ptr; diff --git a/TranslateUI/StructuralRuleView.cpp b/TranslateUI/StructuralRuleView.cpp index 1882aeb..1bae0cd 100644 --- a/TranslateUI/StructuralRuleView.cpp +++ b/TranslateUI/StructuralRuleView.cpp @@ -21,20 +21,18 @@ StructuralRuleView::StructuralRuleView(QWidget* p /*= nullptr*/) split->addWidget(_configs_stack); _configs_stack->addWidget(new EmptyConfiguration(this)); - auto int_config = new IntegerConfiguration(this); - _configs_stack->addWidget(int_config); - connect(int_config, &IntegerConfiguration::ruleHasBeenChanged, - _sequence_view, &SequenceRulesView::currentRuleRefresh); + //connect(int_config, &IntegerConfiguration::ruleHasBeenChanged, + // _sequence_view, &SequenceRulesView::currentRuleRefresh); connect(_sequence_view, &SequenceRulesView::currentRuleChanged, [=](std::shared_ptr u, const QModelIndex& i) { - if (u->name() == extract::BytesAsInteger().name()) { + /*if (u->name() == extract::BytesAsInteger().name()) { _configs_stack->setCurrentIndex(1); int_config->setCurrent(i, u); } - else { + else */{ _configs_stack->setCurrentIndex(0); } }); @@ -46,27 +44,4 @@ EmptyConfiguration::EmptyConfiguration(QWidget* p/*=nullptr*/) auto label = new QLabel(u8"当前规则无需配置", this); label->setAlignment(Qt::AlignCenter); layout->addWidget(label); -} - -IntegerConfiguration::IntegerConfiguration(QWidget* p) - :_unsigned_mark(new QCheckBox(this)) -{ - auto layout = new QGridLayout(this); - layout->addWidget(new QLabel(u8"无符号?:"), 0, 0); - layout->addWidget(_unsigned_mark, 0, 1, 1, 2); - layout->setRowStretch(2, 1); - - connect(_unsigned_mark, &QCheckBox::stateChanged, [=](){ - auto checkmark = _unsigned_mark->isChecked(); - _current_extractor->setUnsignedMark(checkmark); - emit this->ruleHasBeenChanged(_target_modelindex); - }); -} - -void IntegerConfiguration::setCurrent(const QModelIndex& idx, std::shared_ptr u) -{ - this->_target_modelindex = idx; - _current_extractor = std::dynamic_pointer_cast(u); - - this->_unsigned_mark->setChecked(_current_extractor->unsignedMark()); -} +} \ No newline at end of file diff --git a/TranslateUI/StructuralRuleView.h b/TranslateUI/StructuralRuleView.h index 74f6d31..0448329 100644 --- a/TranslateUI/StructuralRuleView.h +++ b/TranslateUI/StructuralRuleView.h @@ -17,22 +17,6 @@ public: EmptyConfiguration(QWidget* p = nullptr); }; -namespace extract { class BytesAsInteger; } -class IntegerConfiguration : public QWidget { - Q_OBJECT -private: - std::shared_ptr _current_extractor = nullptr; - QModelIndex _target_modelindex; - QCheckBox* const _unsigned_mark; - -signals: - void ruleHasBeenChanged(const QModelIndex& idx); - -public: - IntegerConfiguration(QWidget* p = nullptr); - - void setCurrent(const QModelIndex& idx, std::shared_ptr u); -}; class StructuralRuleView : public QWidget diff --git a/TranslateUI/TranslateBasic.cpp b/TranslateUI/TranslateBasic.cpp index b2502f9..0dee80d 100644 --- a/TranslateUI/TranslateBasic.cpp +++ b/TranslateUI/TranslateBasic.cpp @@ -7,13 +7,13 @@ AbstractExtractor::AbstractExtractor(const QString& name, DataType data) :_name_store(name), _type_store(data), _byte_offset(0), _byte_count(0) { } -bool AbstractExtractor::setOffsetFromPrevious(int bytes) +bool AbstractExtractor::setOffsetSpan(int bytes) { this->_byte_offset = bytes; return true; } -bool AbstractExtractor::setCountWithinParse(int bytes) +bool AbstractExtractor::setCountWithin(int bytes) { this->_byte_count = bytes; return true; @@ -29,12 +29,12 @@ DataType AbstractExtractor::outType() const return _type_store; } -int AbstractExtractor::offsetFromPrevious() const +int AbstractExtractor::offsetSpan() const { return _byte_offset; } -int AbstractExtractor::countWithinParse() const +int AbstractExtractor::countWithin() const { return _byte_count; } @@ -52,7 +52,7 @@ void AbstractExtractor::saveTo(QJsonObject& obj) const } BytesAsHex::BytesAsHex() - : AbstractExtractor(NAME(BytesAsHex), DataType::ValueSequenceString) { + : AbstractExtractor(NAME(BytesAsHex), DataType::TextString) { } QVariant BytesAsHex::parse(const QByteArray& bytes) const @@ -61,7 +61,7 @@ QVariant BytesAsHex::parse(const QByteArray& bytes) const for (auto char_v : bytes) { result += QString("%1").arg(char_v, 2, 16, QChar('0')); } - return result + "H"; + return result; } std::shared_ptr BytesAsHex::newDefault() const @@ -77,7 +77,7 @@ BytesAsBitCombine::BytesAsBitCombine() bool BytesAsBitCombine::setSwitchOption(int bit_index, const QString& keyword) { - if (bit_index >= 0 && bit_index <= this->countWithinParse() * 8) { + if (bit_index >= 0 && bit_index <= this->countWithin() * 8) { _switch_options[bit_index] = keyword; return true; } @@ -141,76 +141,44 @@ std::shared_ptr BytesAsBitCombine::newDefault() const return std::make_shared(); } -void BytesAsInteger::loadFrom(const QJsonObject& obj) -{ - AbstractExtractor::loadFrom(obj); - INT32_PEAK(unsigned_mark); -} - -void BytesAsInteger::saveTo(QJsonObject& obj) const -{ - AbstractExtractor::saveTo(obj); - INT32_SAVE(unsigned_mark); -} - std::shared_ptr BytesAsInteger::newDefault() const { return std::make_shared(); } BytesAsInteger::BytesAsInteger() - :AbstractExtractor(NAME(BytesAsInteger), DataType::Int64) { + :AbstractExtractor(NAME(BytesAsInteger), DataType::Integer) { } -void BytesAsInteger::setUnsignedMark(bool ste) -{ - this->unsigned_mark = ste; -} - -bool BytesAsInteger::unsignedMark() const -{ - return unsigned_mark; -} - -bool BytesAsInteger::setCountWithinParse(int bytes) +bool BytesAsInteger::setCountWithin(int bytes) { bytes = std::min(8, std::max(bytes, 0)); - return AbstractExtractor::setCountWithinParse(bytes); + return AbstractExtractor::setCountWithin(bytes); } QVariant BytesAsInteger::parse(const QByteArray& bytes) const { - if (unsigned_mark) { - unsigned long long value = 0; - for (auto vidx = bytes.size() - 1; vidx >= 0; vidx--) { - auto vchar = bytes.at(vidx); - value = value << 8; - value += (uchar)vchar; - } - return value; + auto last = bytes[bytes.size() - 1]; + auto mark = last == 0 ? 0 : last / std::abs(last); + + auto xbuffer = bytes; + if (mark >= 0) { + xbuffer.append(8 - bytes.size(), 0); } else { - auto last = bytes[bytes.size() - 1]; - auto mark = last == 0 ? 0 : last / std::abs(last); - - auto xbuffer = bytes; - if (mark >= 0) { - xbuffer.append(8 - bytes.size(), 0); - } - else { - xbuffer.append(8 - bytes.size(), 0xff); - } - - unsigned long long value = 0; - for (auto vidx = xbuffer.size() - 1; vidx >= 0; vidx--) { - auto vchar = xbuffer.at(vidx); - value = value << 8; - value += (uchar)vchar; - } - return *((long long*)(&value)); + xbuffer.append(8 - bytes.size(), 0xff); } + + unsigned long long value = 0; + for (auto vidx = xbuffer.size() - 1; vidx >= 0; vidx--) { + auto vchar = xbuffer.at(vidx); + value = value << 8; + value += (uchar)vchar; + } + return *((long long*)(&value)); } + #include BytesAsString::BytesAsString() @@ -295,11 +263,8 @@ void AbstractValueConvert::saveTo(QJsonObject& obj) const } DoubleWithLSB::DoubleWithLSB() - :AbstractValueConvert(NAME(DoubleWithLSB), DataType::Int64, DataType::Dbl64), - _lsb_value(0) -{ - -} + :AbstractValueConvert(NAME(DoubleWithLSB), DataType::Integer, DataType::Dbl64), + _lsb_value(1) {} void DoubleWithLSB::setLSB(double lsbv) { @@ -333,9 +298,9 @@ std::shared_ptr BytesAsFloat::newDefault() const return std::make_shared(); } -bool BytesAsFloat::setCountWithinParse(int bytes) +bool BytesAsFloat::setCountWithin(int bytes) { - AbstractExtractor::setCountWithinParse(4); + AbstractExtractor::setCountWithin(4); return true; } @@ -355,9 +320,9 @@ QVariant BytesAsDouble::parse(const QByteArray& bytes) const return *((double*)bytes.data()); } -bool BytesAsDouble::setCountWithinParse(int bytes) +bool BytesAsDouble::setCountWithin(int bytes) { - AbstractExtractor::setCountWithinParse(8); + AbstractExtractor::setCountWithin(8); return true; } @@ -366,3 +331,31 @@ BytesAsDouble::BytesAsDouble() { } + +BytesAsUnsigned::BytesAsUnsigned() +:AbstractExtractor(NAME(BytesAsUnsigned), DataType::Unsigned) +{ + +} + +bool BytesAsUnsigned::setCountWithin(int bytes) +{ + auto count = std::max(1, std::min(8, bytes)); + return AbstractExtractor::setCountWithin(count); +} + +QVariant BytesAsUnsigned::parse(const QByteArray& bytes) const +{ + unsigned long long value = 0; + for (auto vidx = bytes.size() - 1; vidx >= 0; vidx--) { + auto vchar = bytes.at(vidx); + value = value << 8; + value += (uchar)vchar; + } + return value; +} + +std::shared_ptr BytesAsUnsigned::newDefault() const +{ + return std::make_shared(); +} diff --git a/TranslateUI/TranslateBasic.h b/TranslateUI/TranslateBasic.h index 98d6852..7e832ca 100644 --- a/TranslateUI/TranslateBasic.h +++ b/TranslateUI/TranslateBasic.h @@ -27,20 +27,19 @@ public: virtual std::shared_ptr newDefault() const = 0; }; + + /// /// 内部使用的数据类型 /// enum class DataType { - ValueSequenceString, TextString, - Int64, // 64位整形或无符号整形 + Integer, // 整形 + Unsigned,// 无符号整形 Flt32, Dbl64, }; - - - /// /// 翻译单元抽象接口 /// @@ -64,12 +63,12 @@ public: /// 从上一个单元字节偏移字节数量 /// /// - virtual int offsetFromPrevious() const = 0; + virtual int offsetSpan() const = 0; /// /// 解析所用的字节数量 /// /// - virtual int countWithinParse() const = 0; + virtual int countWithin() const = 0; /// /// 执行解析过程 @@ -92,18 +91,18 @@ namespace extract { /// 设置偏移字节数量 /// /// - virtual bool setOffsetFromPrevious(int bytes); + virtual bool setOffsetSpan(int bytes); /// /// 设置解析字节数量 /// /// - virtual bool setCountWithinParse(int bytes); + virtual bool setCountWithin(int bytes); // ExtractUnit =========================== QString name() const override; virtual DataType outType() const; - int offsetFromPrevious() const override; - int countWithinParse() const override; + int offsetSpan() const override; + int countWithin() const override; // Serializable ============================== void loadFrom(const QJsonObject& obj) override; @@ -156,22 +155,28 @@ namespace extract { /// 转换源数据为整形 /// class BytesAsInteger : public AbstractExtractor { - private: - bool unsigned_mark = false; - public: BytesAsInteger(); - void setUnsignedMark(bool ste); - bool unsignedMark() const; - // ExtractUnit ============================ - bool setCountWithinParse(int bytes) override; + bool setCountWithin(int bytes) override; + QVariant parse(const QByteArray& bytes) const override; + + // Serializable ============================== + std::shared_ptr newDefault() const override; + }; + /// + /// 转换源数据为整形 + /// + class BytesAsUnsigned : public AbstractExtractor { + public: + BytesAsUnsigned(); + + // ExtractUnit ============================ + bool setCountWithin(int bytes) override; QVariant parse(const QByteArray& bytes) const override; // Serializable ============================== - void loadFrom(const QJsonObject& obj) override; - void saveTo(QJsonObject& obj) const override; std::shared_ptr newDefault() const override; }; @@ -205,7 +210,7 @@ namespace extract { /// 设置解析字节数量 /// /// - virtual bool setCountWithinParse(int bytes) override; + virtual bool setCountWithin(int bytes) override; QVariant parse(const QByteArray& bytes) const override; std::shared_ptr newDefault() const override; @@ -219,7 +224,7 @@ namespace extract { /// 设置解析字节数量 /// /// - virtual bool setCountWithinParse(int bytes) override; + virtual bool setCountWithin(int bytes) override; QVariant parse(const QByteArray& bytes) const override; std::shared_ptr newDefault() const override; diff --git a/TranslateUI/TranslateUI.vcxproj b/TranslateUI/TranslateUI.vcxproj index 709f852..ded359c 100644 --- a/TranslateUI/TranslateUI.vcxproj +++ b/TranslateUI/TranslateUI.vcxproj @@ -105,7 +105,7 @@ - + diff --git a/TranslateUI/TranslateUI.vcxproj.filters b/TranslateUI/TranslateUI.vcxproj.filters index 8d1118e..c15eff0 100644 --- a/TranslateUI/TranslateUI.vcxproj.filters +++ b/TranslateUI/TranslateUI.vcxproj.filters @@ -40,9 +40,6 @@ Source Files - - Source Files - Source Files @@ -52,17 +49,20 @@ Source Files + + Source Files + - - Header Files - Header Files Header Files + + Header Files +