整理代码
This commit is contained in:
parent
862acd924c
commit
3f8d4b9614
|
@ -1,10 +1,10 @@
|
|||
#include "SequenceView.h"
|
||||
#include <QSpinBox>
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
|
||||
IntDelegate::IntDelegate(int min, int max)
|
||||
{
|
||||
|
||||
: _min_value(min), _max_value(max) {
|
||||
}
|
||||
|
||||
QWidget* IntDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
||||
|
@ -14,13 +14,17 @@ QWidget* IntDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem&
|
|||
|
||||
void IntDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
|
||||
{
|
||||
dynamic_cast<QSpinBox*>(editor)->setValue(index.data(Qt::DisplayRole).toInt());
|
||||
auto spin = dynamic_cast<QSpinBox*>(editor);
|
||||
spin->setRange(_min_value, _max_value);
|
||||
spin->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);
|
||||
model->setData(index, value->value(), Qt::EditRole);
|
||||
|
||||
emit this->valueChanged(index);
|
||||
}
|
||||
|
||||
void IntDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
||||
|
@ -29,6 +33,34 @@ void IntDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewIt
|
|||
}
|
||||
|
||||
#include "TranslateBasic.h"
|
||||
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));
|
||||
}
|
||||
|
||||
SequenceView::SequenceView(QWidget* p /*= nullptr*/)
|
||||
:QTableView(p),
|
||||
_seqs_model(new QStandardItemModel),
|
||||
|
@ -39,13 +71,16 @@ SequenceView::SequenceView(QWidget* p /*= nullptr*/)
|
|||
<< 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));
|
||||
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);
|
||||
|
||||
this->setContextMenuPolicy(Qt::CustomContextMenu);
|
||||
connect(this, &QTableView::customContextMenuRequested,
|
||||
this, &SequenceView::customTranslateRuleEdit);
|
||||
connect(this, &QTableView::customContextMenuRequested, this, &SequenceView::customTranslateRuleEdit);
|
||||
connect(rule_delegate, &RuleSelectDelegate::dataChanged, this, &SequenceView::tidyRuleAt);
|
||||
connect(int_delegate, &IntDelegate::valueChanged, this, &SequenceView::tidyRuleAt);
|
||||
}
|
||||
|
||||
#include <QMenu>
|
||||
|
@ -110,13 +145,13 @@ std::shared_ptr<ExtractUnit> TranslateBasic::defaultRule() const
|
|||
return _default_translate_rule;
|
||||
}
|
||||
|
||||
QHash<QString, std::shared_ptr<ExtractUnit>> TranslateBasic::extractorMap() const
|
||||
QHash<QString, std::shared_ptr<ExtractUnit>> TranslateBasic::extactors() const
|
||||
{
|
||||
return _extractor_types;
|
||||
}
|
||||
|
||||
RuleSelectDelegate::RuleSelectDelegate(std::shared_ptr<TranslateBasic> ins, QList<std::shared_ptr<ExtractUnit>>& rule)
|
||||
:_kernel(ins), rule_list(rule) {
|
||||
RuleSelectDelegate::RuleSelectDelegate(std::shared_ptr<TranslateBasic> ins)
|
||||
:_kernel(ins) {
|
||||
}
|
||||
|
||||
#include <QComboBox>
|
||||
|
@ -127,10 +162,12 @@ QWidget* RuleSelectDelegate::createEditor(QWidget* parent, const QStyleOptionVie
|
|||
|
||||
void RuleSelectDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
|
||||
{
|
||||
auto rule_names = this->_kernel->extractorMap().keys();
|
||||
auto rule_names = this->_kernel->extactors().keys();
|
||||
std::sort(rule_names.begin(), rule_names.end());
|
||||
|
||||
dynamic_cast<QComboBox*>(editor)->addItems(rule_names);
|
||||
auto combo = dynamic_cast<QComboBox*>(editor);
|
||||
combo->addItems(rule_names);
|
||||
combo->setCurrentText(index.data(Qt::DisplayRole).toString());
|
||||
}
|
||||
|
||||
void RuleSelectDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
|
||||
|
@ -138,29 +175,7 @@ void RuleSelectDelegate::setModelData(QWidget* editor, QAbstractItemModel* model
|
|||
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));
|
||||
emit this->dataChanged(index);
|
||||
}
|
||||
|
||||
void RuleSelectDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
||||
|
|
|
@ -9,6 +9,13 @@
|
|||
///
|
||||
/// </summary>
|
||||
class IntDelegate : public QStyledItemDelegate {
|
||||
Q_OBJECT
|
||||
private:
|
||||
int _min_value, _max_value;
|
||||
|
||||
signals:
|
||||
void valueChanged(const QModelIndex &idx) const;
|
||||
|
||||
public:
|
||||
IntDelegate(int min, int max);
|
||||
|
||||
|
@ -30,17 +37,20 @@ public:
|
|||
TranslateBasic();
|
||||
|
||||
std::shared_ptr<ExtractUnit> defaultRule() const;
|
||||
QHash<QString, std::shared_ptr<ExtractUnit>> extractorMap() const;
|
||||
QHash<QString, std::shared_ptr<ExtractUnit>> extactors() const;
|
||||
|
||||
};
|
||||
|
||||
class RuleSelectDelegate : public QStyledItemDelegate {
|
||||
Q_OBJECT
|
||||
private:
|
||||
std::shared_ptr<TranslateBasic> _kernel;
|
||||
QList<std::shared_ptr<ExtractUnit>> &rule_list;
|
||||
|
||||
signals:
|
||||
void dataChanged(const QModelIndex &idx) const;
|
||||
|
||||
public:
|
||||
RuleSelectDelegate(std::shared_ptr<TranslateBasic> ins, QList<std::shared_ptr<ExtractUnit>> &rule);
|
||||
RuleSelectDelegate(std::shared_ptr<TranslateBasic> ins);
|
||||
|
||||
virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
|
||||
virtual void setEditorData(QWidget* editor, const QModelIndex& index) const override;
|
||||
|
@ -54,6 +64,9 @@ private:
|
|||
std::shared_ptr<TranslateBasic> base = nullptr;
|
||||
QList<std::shared_ptr<ExtractUnit>> _rule_list;
|
||||
|
||||
public:
|
||||
void tidyRuleAt(const QModelIndex& idx_rule);
|
||||
|
||||
public:
|
||||
SequenceView(QWidget* p = nullptr);
|
||||
|
||||
|
|
|
@ -104,7 +104,7 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="ParseUntility.h" />
|
||||
<ClInclude Include="SequenceView.h" />
|
||||
<QtMoc Include="SequenceView.h" />
|
||||
<ClInclude Include="StructView.h" />
|
||||
<ClInclude Include="TranslateBasic.h" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -63,11 +63,13 @@
|
|||
<ClInclude Include="TranslateBasic.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="SequenceView.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="ReadMe.md" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<QtMoc Include="SequenceView.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</QtMoc>
|
||||
</ItemGroup>
|
||||
</Project>
|
Loading…
Reference in New Issue