update 基本规则解析构建
This commit is contained in:
parent
e17fc39dc7
commit
862acd924c
|
@ -0,0 +1,169 @@
|
|||
#include "SequenceView.h"
|
||||
#include <QSpinBox>
|
||||
|
||||
|
||||
IntDelegate::IntDelegate(int min, int max)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QWidget* IntDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
||||
{
|
||||
return new QSpinBox(parent);
|
||||
}
|
||||
|
||||
void IntDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
|
||||
{
|
||||
dynamic_cast<QSpinBox*>(editor)->setValue(index.data(Qt::DisplayRole).toInt());
|
||||
}
|
||||
|
||||
void IntDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
|
||||
{
|
||||
auto value = dynamic_cast<QSpinBox*>(editor);
|
||||
model->setData(index, Qt::DisplayRole);
|
||||
}
|
||||
|
||||
void IntDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
||||
{
|
||||
editor->setGeometry(option.rect);
|
||||
}
|
||||
|
||||
#include "TranslateBasic.h"
|
||||
SequenceView::SequenceView(QWidget* p /*= nullptr*/)
|
||||
:QTableView(p),
|
||||
_seqs_model(new QStandardItemModel),
|
||||
base(std::make_shared<TranslateBasic>())
|
||||
{
|
||||
this->setModel(_seqs_model);
|
||||
_seqs_model->setHorizontalHeaderLabels(QStringList()
|
||||
<< tr(u8"Field Name") << tr(u8"Bytes Offset") << tr(u8"Bytes Count")
|
||||
<< tr(u8"Translate Rule") << tr(u8"Arguments"));
|
||||
|
||||
this->setItemDelegateForColumn(1, new IntDelegate(0, INT_MAX));
|
||||
this->setItemDelegateForColumn(2, new IntDelegate(0, INT_MAX));
|
||||
this->setItemDelegateForColumn(3, new RuleSelectDelegate(base, _rule_list));
|
||||
|
||||
this->setContextMenuPolicy(Qt::CustomContextMenu);
|
||||
connect(this, &QTableView::customContextMenuRequested,
|
||||
this, &SequenceView::customTranslateRuleEdit);
|
||||
}
|
||||
|
||||
#include <QMenu>
|
||||
#include <QJsonDocument>
|
||||
void SequenceView::customTranslateRuleEdit(const QPoint& pos)
|
||||
{
|
||||
QMenu immediate;
|
||||
immediate.addAction(u8"Add Unit", this, &SequenceView::addTranslateUnit);
|
||||
immediate.addAction(u8"Remove Unit", this, &SequenceView::removeTranslateUnit);
|
||||
immediate.exec(this->mapToGlobal(pos));
|
||||
}
|
||||
|
||||
void SequenceView::addTranslateUnit()
|
||||
{
|
||||
auto row_cnt = this->_seqs_model->rowCount();
|
||||
|
||||
QList<QStandardItem*> new_row;
|
||||
new_row << new QStandardItem(QString(u8"rule_%1").arg(row_cnt));
|
||||
new_row << new QStandardItem(u8"0");
|
||||
new_row << new QStandardItem(u8"1");
|
||||
new_row << new QStandardItem(base->defaultRule()->name());
|
||||
|
||||
auto curr_rule = base->defaultRule()->newDefault();
|
||||
this->_rule_list << std::static_pointer_cast<AbstractExtractor>(curr_rule);
|
||||
|
||||
auto hex_rule = std::static_pointer_cast<BytesAsHex>(curr_rule);
|
||||
hex_rule->setCountWithinParse(1);
|
||||
|
||||
QJsonObject obj;
|
||||
curr_rule->saveTo(obj);
|
||||
auto bytes = QJsonDocument(obj).toJson(QJsonDocument::Compact);
|
||||
auto json_doc = QString::fromUtf8(bytes);
|
||||
new_row << new QStandardItem(json_doc);
|
||||
|
||||
this->_seqs_model->appendRow(new_row);
|
||||
}
|
||||
|
||||
void SequenceView::removeTranslateUnit()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#include "TranslateBasic.h"
|
||||
TranslateBasic::TranslateBasic()
|
||||
{
|
||||
std::shared_ptr<ExtractUnit> u_ptr = std::make_shared<BytesAsBitCombine>();
|
||||
_extractor_types[u_ptr->name()] = u_ptr;
|
||||
|
||||
u_ptr = std::make_shared<BytesAsInteger>();
|
||||
_extractor_types[u_ptr->name()] = u_ptr;
|
||||
|
||||
u_ptr = std::make_shared<BytesAsString>();
|
||||
_extractor_types[u_ptr->name()] = u_ptr;
|
||||
|
||||
u_ptr = std::make_shared<BytesAsHex>();
|
||||
_extractor_types[u_ptr->name()] = u_ptr;
|
||||
this->_default_translate_rule = u_ptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<ExtractUnit> TranslateBasic::defaultRule() const
|
||||
{
|
||||
return _default_translate_rule;
|
||||
}
|
||||
|
||||
QHash<QString, std::shared_ptr<ExtractUnit>> TranslateBasic::extractorMap() const
|
||||
{
|
||||
return _extractor_types;
|
||||
}
|
||||
|
||||
RuleSelectDelegate::RuleSelectDelegate(std::shared_ptr<TranslateBasic> ins, QList<std::shared_ptr<ExtractUnit>>& rule)
|
||||
:_kernel(ins), rule_list(rule) {
|
||||
}
|
||||
|
||||
#include <QComboBox>
|
||||
QWidget* RuleSelectDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
||||
{
|
||||
return new QComboBox(parent);
|
||||
}
|
||||
|
||||
void RuleSelectDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
|
||||
{
|
||||
auto rule_names = this->_kernel->extractorMap().keys();
|
||||
std::sort(rule_names.begin(), rule_names.end());
|
||||
|
||||
dynamic_cast<QComboBox*>(editor)->addItems(rule_names);
|
||||
}
|
||||
|
||||
void RuleSelectDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
|
||||
{
|
||||
auto rule_name = dynamic_cast<QComboBox*>(editor)->currentText();
|
||||
model->setData(index, rule_name, Qt::EditRole);
|
||||
|
||||
auto rule_ins = _kernel->extractorMap()[rule_name];
|
||||
const_cast<RuleSelectDelegate*>(this)->rule_list.replace(index.row(), rule_ins);
|
||||
|
||||
auto offset_index = index.sibling(index.row(), 1);
|
||||
auto offset_number = offset_index.data(Qt::DisplayRole).toInt();
|
||||
auto count_index = index.sibling(index.row(), 2);
|
||||
auto count_number = count_index.data(Qt::DisplayRole).toInt();
|
||||
auto rule_insv = std::dynamic_pointer_cast<AbstractExtractor>(rule_ins);
|
||||
|
||||
if (rule_insv) {
|
||||
rule_insv->setOffsetFromPrevious(offset_number);
|
||||
rule_insv->setCountWithinParse(count_number);
|
||||
}
|
||||
|
||||
model->setData(offset_index, rule_ins->offsetFromPrevious(), Qt::EditRole);
|
||||
model->setData(count_index, rule_ins->countWithinParse(), Qt::EditRole);
|
||||
|
||||
auto param_index = index.sibling(index.row(), 4);
|
||||
QJsonObject obj;
|
||||
rule_insv->saveTo(obj);
|
||||
auto bytes = QJsonDocument(obj).toJson(QJsonDocument::Compact);
|
||||
|
||||
model->setData(param_index, QString::fromUtf8(bytes));
|
||||
}
|
||||
|
||||
void RuleSelectDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
||||
{
|
||||
editor->setGeometry(option.rect);
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
#pragma once
|
||||
#include <QWidget>
|
||||
#include <QTableView>
|
||||
#include <QStandardItemModel>
|
||||
#include <QStyledItemDelegate>
|
||||
#include <QItemDelegate>
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
class IntDelegate : public QStyledItemDelegate {
|
||||
public:
|
||||
IntDelegate(int min, int max);
|
||||
|
||||
virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
|
||||
virtual void setEditorData(QWidget* editor, const QModelIndex& index) const override;
|
||||
virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override;
|
||||
virtual void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
|
||||
};
|
||||
|
||||
#include <QHash>
|
||||
#include <memory>
|
||||
class ExtractUnit;
|
||||
class TranslateBasic {
|
||||
private:
|
||||
QHash<QString, std::shared_ptr<ExtractUnit>> _extractor_types;
|
||||
std::shared_ptr<ExtractUnit> _default_translate_rule;
|
||||
|
||||
public:
|
||||
TranslateBasic();
|
||||
|
||||
std::shared_ptr<ExtractUnit> defaultRule() const;
|
||||
QHash<QString, std::shared_ptr<ExtractUnit>> extractorMap() const;
|
||||
|
||||
};
|
||||
|
||||
class RuleSelectDelegate : public QStyledItemDelegate {
|
||||
private:
|
||||
std::shared_ptr<TranslateBasic> _kernel;
|
||||
QList<std::shared_ptr<ExtractUnit>> &rule_list;
|
||||
|
||||
public:
|
||||
RuleSelectDelegate(std::shared_ptr<TranslateBasic> ins, QList<std::shared_ptr<ExtractUnit>> &rule);
|
||||
|
||||
virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
|
||||
virtual void setEditorData(QWidget* editor, const QModelIndex& index) const override;
|
||||
virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override;
|
||||
virtual void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
|
||||
};
|
||||
|
||||
class SequenceView : public QTableView {
|
||||
private:
|
||||
QStandardItemModel* const _seqs_model;
|
||||
std::shared_ptr<TranslateBasic> base = nullptr;
|
||||
QList<std::shared_ptr<ExtractUnit>> _rule_list;
|
||||
|
||||
public:
|
||||
SequenceView(QWidget* p = nullptr);
|
||||
|
||||
void customTranslateRuleEdit(const QPoint& pos);
|
||||
void addTranslateUnit();
|
||||
void removeTranslateUnit();
|
||||
};
|
||||
|
|
@ -277,11 +277,11 @@ void AbstractValueConvert::loadFrom(const QJsonObject& obj)
|
|||
{
|
||||
auto int_in_type = (int)_in_type;
|
||||
INT32_PEAK(int_in_type);
|
||||
_in_type = (DataType) int_in_type;
|
||||
_in_type = (DataType)int_in_type;
|
||||
|
||||
auto int_out_type = (int)_out_type;
|
||||
INT32_PEAK(int_out_type);
|
||||
_out_type = (DataType) int_out_type;
|
||||
_out_type = (DataType)int_out_type;
|
||||
}
|
||||
|
||||
void AbstractValueConvert::saveTo(QJsonObject& obj) const
|
||||
|
@ -292,3 +292,32 @@ void AbstractValueConvert::saveTo(QJsonObject& obj) const
|
|||
auto int_out_type = (int)_out_type;
|
||||
INT32_SAVE(int_out_type);
|
||||
}
|
||||
|
||||
DoubleWithLSB::DoubleWithLSB()
|
||||
:AbstractValueConvert(NAME(DoubleWithLSB), DataType::Int64, DataType::Dbl64),
|
||||
_lsb_value(0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void DoubleWithLSB::setLSB(double lsbv)
|
||||
{
|
||||
this->_lsb_value = lsbv;
|
||||
}
|
||||
|
||||
double DoubleWithLSB::getLSB() const
|
||||
{
|
||||
return this->_lsb_value;
|
||||
}
|
||||
|
||||
QVariant DoubleWithLSB::convert(const QVariant& value) const
|
||||
{
|
||||
if (value.canConvert<int64_t>())
|
||||
return value.toLongLong() * this->_lsb_value;
|
||||
return value.toULongLong() * this->_lsb_value;
|
||||
}
|
||||
|
||||
std::shared_ptr<Serializable> DoubleWithLSB::newDefault() const
|
||||
{
|
||||
return std::make_shared<DoubleWithLSB>();
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ public:
|
|||
enum class DataType {
|
||||
ValueSequenceString,
|
||||
TextString,
|
||||
Int64,
|
||||
Int64, // 64λÕûÐλòÎÞ·ûºÅÕûÐÎ
|
||||
Flt32,
|
||||
Dbl64,
|
||||
};
|
||||
|
@ -222,3 +222,18 @@ public:
|
|||
void loadFrom(const QJsonObject& obj) override;
|
||||
void saveTo(QJsonObject& obj) const override;
|
||||
};
|
||||
|
||||
|
||||
class DoubleWithLSB : public AbstractValueConvert {
|
||||
private:
|
||||
double _lsb_value;
|
||||
|
||||
public:
|
||||
DoubleWithLSB();
|
||||
|
||||
void setLSB(double lsbv);
|
||||
double getLSB() const;
|
||||
|
||||
QVariant convert(const QVariant& value) const override;
|
||||
std::shared_ptr<Serializable> newDefault() const override;
|
||||
};
|
|
@ -92,6 +92,7 @@
|
|||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="SequenceView.cpp" />
|
||||
<ClCompile Include="TranslateBasic.cpp" />
|
||||
<QtRcc Include="TranslateUI.qrc" />
|
||||
<QtUic Include="TranslateUI.ui" />
|
||||
|
@ -103,6 +104,7 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="ParseUntility.h" />
|
||||
<ClInclude Include="SequenceView.h" />
|
||||
<ClInclude Include="StructView.h" />
|
||||
<ClInclude Include="TranslateBasic.h" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -49,6 +49,9 @@
|
|||
<ClCompile Include="TranslateBasic.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="SequenceView.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="StructView.h">
|
||||
|
@ -60,6 +63,9 @@
|
|||
<ClInclude Include="TranslateBasic.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="SequenceView.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="ReadMe.md" />
|
||||
|
|
|
@ -3,16 +3,15 @@
|
|||
#include "StructView.h"
|
||||
#include <QDebug>
|
||||
#include "TranslateBasic.h"
|
||||
#include <cmath>
|
||||
#include "SequenceView.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
QByteArray vbuf;
|
||||
uint value = -122;
|
||||
vbuf.append((char*)&value, 4);
|
||||
BytesAsInteger u;
|
||||
qDebug() << u.parse(vbuf);
|
||||
SequenceView v;
|
||||
v.show();
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue