170 lines
4.8 KiB
C++
170 lines
4.8 KiB
C++
#include "WrapConfigs.h"
|
||
#include <QSplitter>
|
||
#include <QVBoxLayout>
|
||
#include <QMenu>
|
||
#include <QAction>
|
||
|
||
WrapConfigs::WrapConfigs(std::shared_ptr<TranslateBasic> _base, QWidget* p)
|
||
: QWidget(p), _bind_base(_base),
|
||
_alias_view(new QTableView(this)),
|
||
_alias_model(new QStandardItemModel(this)),
|
||
_configurations(new QStackedWidget(this))
|
||
{
|
||
auto layout = new QVBoxLayout(this);
|
||
auto split = new QSplitter(this);
|
||
layout->addWidget(split);
|
||
split->addWidget(_alias_view);
|
||
split->addWidget(_configurations);
|
||
|
||
_alias_view->setModel(_alias_model);
|
||
_alias_model->setHorizontalHeaderLabels(QStringList()
|
||
<< u8"UnitType" << u8"UnitAlias" << u8"Content");
|
||
|
||
aliasReload();
|
||
|
||
_alias_view->setContextMenuPolicy(Qt::CustomContextMenu);
|
||
_alias_view->setItemDelegateForColumn(1, new RuleSelectDelegate(_base));
|
||
connect(_alias_model, &QStandardItemModel::itemChanged, this, &WrapConfigs::dataChanged);
|
||
connect(_alias_view, &QTableView::customContextMenuRequested,
|
||
[=](const QPoint& pt) {
|
||
QMenu menu;
|
||
menu.addAction("Add Alias", this, &WrapConfigs::aliasAppend);
|
||
menu.addAction("Remove", this, &WrapConfigs::aliasRemove);
|
||
menu.exec(_alias_view->mapToGlobal(pt));
|
||
});
|
||
|
||
}
|
||
|
||
#include <QJsonDocument>
|
||
void WrapConfigs::aliasReload()
|
||
{
|
||
auto row_cnt = _alias_model->rowCount();
|
||
_alias_model->removeRows(0, row_cnt);
|
||
|
||
auto alias_keys = _bind_base->delegateAlias();
|
||
for (auto key_nm : alias_keys) {
|
||
QList<QStandardItem*> row;
|
||
|
||
auto unit_ins = _bind_base->operator[](key_nm);
|
||
row << new QStandardItem(key_nm);
|
||
row << new QStandardItem(unit_ins->unitType());
|
||
QJsonObject content;
|
||
unit_ins->saveTo(content);
|
||
auto bytes = QJsonDocument(content).toJson(QJsonDocument::Compact);
|
||
row << new QStandardItem(QString::fromUtf8(bytes));
|
||
|
||
switch (unit_ins->outType()) {
|
||
case DataType::LIST_COLLECTION:
|
||
case DataType::UNION_COMBINATE:
|
||
throw new BaseException(u8"Invalid Alias BaseType£¡");
|
||
case DataType::COMPLEX_RULESET:
|
||
for(auto it : row) it->setEditable(false);
|
||
break;
|
||
default:
|
||
row.last()->setEditable(false);
|
||
break;
|
||
}
|
||
_alias_model->appendRow(row);
|
||
}
|
||
|
||
_alias_view->resizeColumnsToContents();
|
||
}
|
||
|
||
#include <QInputDialog>
|
||
void WrapConfigs::aliasAppend()
|
||
{
|
||
auto name = QInputDialog::getText(this, "AliasName Input", "Name");
|
||
if (name == "")
|
||
return;
|
||
|
||
auto exists = _bind_base->delegateAlias();
|
||
while (exists.contains(name))
|
||
name += "#";
|
||
|
||
auto ins = _bind_base->defaultExtractUnit()->newDefault();
|
||
auto nins = std::dynamic_pointer_cast<ExtractImpl>(ins);
|
||
nins->setAlias(name);
|
||
|
||
_bind_base->addDelegate(nins);
|
||
aliasReload();
|
||
}
|
||
|
||
void WrapConfigs::aliasRemove()
|
||
{
|
||
auto index = _alias_view->currentIndex();
|
||
if (!index.isValid())
|
||
return;
|
||
|
||
auto origin_key = _bind_base->delegateAlias()[index.row()];
|
||
_bind_base->removeDelegate(origin_key);
|
||
aliasReload();
|
||
}
|
||
|
||
#include <QMessageBox>
|
||
void WrapConfigs::dataChanged(QStandardItem* cell)
|
||
{
|
||
switch (cell->column()) {
|
||
case 0: { // ÐÞ¸Äalias-name
|
||
auto cell_name = cell->text();
|
||
auto total_alias_set = _bind_base->delegateAlias();
|
||
auto origin_key = total_alias_set.at(cell->row());
|
||
auto appoint_unit = _bind_base->operator[](origin_key);
|
||
|
||
// alias-name Ãû³ÆÖظ´´¦Àí
|
||
if (total_alias_set.contains(cell_name)) {
|
||
QMessageBox::critical(this, "Alias Validate", "Alias Cant't Repeat.");
|
||
QSignalBlocker v(_alias_model);
|
||
cell->setText(origin_key);
|
||
return;
|
||
}
|
||
|
||
appoint_unit->setAlias(cell_name);
|
||
}break;
|
||
case 1: { // ÐÞ¸Äalias»ù´¡ÀàÐÍ
|
||
auto total_alias_set = _bind_base->delegateAlias();
|
||
auto origin_key = total_alias_set.at(cell->row());
|
||
|
||
auto new_type = cell->text();
|
||
auto new_ins = _bind_base->basicExtractors()[new_type]->newDefault();
|
||
auto new_delegate = std::dynamic_pointer_cast<ExtractImpl>(new_ins);
|
||
new_delegate->setAlias(origin_key);
|
||
_bind_base->replaceDelegate(origin_key, new_delegate);
|
||
}break;
|
||
default:
|
||
break;
|
||
}
|
||
|
||
aliasReload();
|
||
}
|
||
|
||
#include <QComboBox>
|
||
RuleSelectDelegate::RuleSelectDelegate(std::shared_ptr<TranslateBasic> ins)
|
||
:_kernel(ins) {
|
||
}
|
||
|
||
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->basicExtractors().keys();
|
||
std::sort(rule_names.begin(), rule_names.end());
|
||
|
||
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
|
||
{
|
||
auto rule_name = dynamic_cast<QComboBox*>(editor)->currentText();
|
||
model->setData(index, rule_name, Qt::EditRole);
|
||
}
|
||
|
||
void RuleSelectDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
||
{
|
||
editor->setGeometry(option.rect);
|
||
}
|