2025-07-17 16:17:39 +00:00
|
|
|
#include "SequenceView.h"
|
|
|
|
#include <QSpinBox>
|
2025-07-18 14:41:36 +00:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
2025-07-17 16:17:39 +00:00
|
|
|
|
|
|
|
IntDelegate::IntDelegate(int min, int max)
|
2025-07-18 14:41:36 +00:00
|
|
|
: _min_value(min), _max_value(max) {
|
2025-07-17 16:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2025-07-18 14:41:36 +00:00
|
|
|
auto spin = dynamic_cast<QSpinBox*>(editor);
|
|
|
|
spin->setRange(_min_value, _max_value);
|
|
|
|
spin->setValue(index.data(Qt::DisplayRole).toInt());
|
2025-07-17 16:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IntDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
auto value = dynamic_cast<QSpinBox*>(editor);
|
2025-07-18 14:41:36 +00:00
|
|
|
model->setData(index, value->value(), Qt::EditRole);
|
|
|
|
|
|
|
|
emit this->valueChanged(index);
|
2025-07-17 16:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IntDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
editor->setGeometry(option.rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "TranslateBasic.h"
|
2025-07-18 14:41:36 +00:00
|
|
|
void SequenceView::tidyRuleAt(const QModelIndex& idx)
|
|
|
|
{
|
|
|
|
auto rule_idx = idx.sibling(idx.row(), 3);
|
|
|
|
auto rule_nm = base->extactors()[rule_idx.data(Qt::DisplayRole).toString()];
|
|
|
|
auto new_inst = std::dynamic_pointer_cast<ExtractUnit>(rule_nm->newDefault());
|
|
|
|
_rule_list.replace(idx.row(), new_inst);
|
|
|
|
|
|
|
|
auto offset_index = idx.sibling(idx.row(), 1);
|
|
|
|
auto offset_number = offset_index.data(Qt::DisplayRole).toInt();
|
|
|
|
auto count_index = idx.sibling(idx.row(), 2);
|
|
|
|
auto count_number = count_index.data(Qt::DisplayRole).toInt();
|
|
|
|
auto rule_insv = std::dynamic_pointer_cast<AbstractExtractor>(new_inst);
|
|
|
|
|
|
|
|
if (rule_insv) {
|
|
|
|
rule_insv->setOffsetFromPrevious(offset_number);
|
|
|
|
rule_insv->setCountWithinParse(count_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
_seqs_model->setData(offset_index, new_inst->offsetFromPrevious(), Qt::EditRole);
|
|
|
|
_seqs_model->setData(count_index, new_inst->countWithinParse(), Qt::EditRole);
|
|
|
|
|
|
|
|
auto param_index = idx.sibling(idx.row(), 4);
|
|
|
|
QJsonObject obj;
|
|
|
|
rule_insv->saveTo(obj);
|
|
|
|
auto bytes = QJsonDocument(obj).toJson(QJsonDocument::Compact);
|
|
|
|
_seqs_model->setData(param_index, QString::fromUtf8(bytes));
|
|
|
|
}
|
|
|
|
|
2025-07-17 16:17:39 +00:00
|
|
|
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"));
|
|
|
|
|
2025-07-18 14:41:36 +00:00
|
|
|
auto int_delegate = new IntDelegate(0, INT_MAX);
|
|
|
|
this->setItemDelegateForColumn(1, int_delegate);
|
|
|
|
this->setItemDelegateForColumn(2, int_delegate);
|
|
|
|
auto rule_delegate = new RuleSelectDelegate(base);
|
|
|
|
this->setItemDelegateForColumn(3, rule_delegate);
|
2025-07-17 16:17:39 +00:00
|
|
|
|
|
|
|
this->setContextMenuPolicy(Qt::CustomContextMenu);
|
2025-07-18 14:41:36 +00:00
|
|
|
connect(this, &QTableView::customContextMenuRequested, this, &SequenceView::customTranslateRuleEdit);
|
|
|
|
connect(rule_delegate, &RuleSelectDelegate::dataChanged, this, &SequenceView::tidyRuleAt);
|
|
|
|
connect(int_delegate, &IntDelegate::valueChanged, this, &SequenceView::tidyRuleAt);
|
2025-07-17 16:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2025-07-18 14:41:36 +00:00
|
|
|
QHash<QString, std::shared_ptr<ExtractUnit>> TranslateBasic::extactors() const
|
2025-07-17 16:17:39 +00:00
|
|
|
{
|
|
|
|
return _extractor_types;
|
|
|
|
}
|
|
|
|
|
2025-07-18 14:41:36 +00:00
|
|
|
RuleSelectDelegate::RuleSelectDelegate(std::shared_ptr<TranslateBasic> ins)
|
|
|
|
:_kernel(ins) {
|
2025-07-17 16:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
{
|
2025-07-18 14:41:36 +00:00
|
|
|
auto rule_names = this->_kernel->extactors().keys();
|
2025-07-17 16:17:39 +00:00
|
|
|
std::sort(rule_names.begin(), rule_names.end());
|
|
|
|
|
2025-07-18 14:41:36 +00:00
|
|
|
auto combo = dynamic_cast<QComboBox*>(editor);
|
|
|
|
combo->addItems(rule_names);
|
|
|
|
combo->setCurrentText(index.data(Qt::DisplayRole).toString());
|
2025-07-17 16:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2025-07-18 14:41:36 +00:00
|
|
|
emit this->dataChanged(index);
|
2025-07-17 16:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RuleSelectDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
editor->setGeometry(option.rect);
|
|
|
|
}
|