170 lines
5.2 KiB
C++
170 lines
5.2 KiB
C++
|
#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);
|
||
|
}
|