150 lines
4.5 KiB
C++
150 lines
4.5 KiB
C++
#include "TranslateUI.h"
|
||
#include "StructuralRuleView.h"
|
||
#include <QSplitter>
|
||
#include <QTreeView>
|
||
#include <QTabWidget>
|
||
#include <QMenu>
|
||
#include <QInputDialog>
|
||
#include <QMessageBox>
|
||
|
||
TranslateUI::TranslateUI(QWidget* parent)
|
||
: QMainWindow(parent),
|
||
_tab_widget(new QTabWidget(this)),
|
||
_ruleset_navi(new QTreeView(this)),
|
||
_solution_model(new QStandardItemModel(this))
|
||
{
|
||
_core._translate_core = std::make_shared<TranslateBasic>();
|
||
_core._view_mgr = new ViewManager(_core._translate_core, this);
|
||
|
||
|
||
|
||
auto splitter = new QSplitter(Qt::Horizontal, this);
|
||
this->setCentralWidget(splitter);
|
||
splitter->addWidget(_ruleset_navi);
|
||
_ruleset_navi->setModel(_solution_model);
|
||
_ruleset_navi->setHeaderHidden(true);
|
||
_ruleset_navi->setContextMenuPolicy(Qt::CustomContextMenu);
|
||
connect(_ruleset_navi, &QWidget::customContextMenuRequested, [=](const QPoint& pos) {
|
||
QMenu m;
|
||
m.addAction(u8"Add Rule", this, &TranslateUI::addCustomRule);
|
||
m.addSeparator();
|
||
m.addAction(u8"Open ExtractView", this, &TranslateUI::openExtractView);
|
||
m.exec(this->mapToGlobal(pos));
|
||
});
|
||
|
||
|
||
|
||
|
||
splitter->addWidget(_tab_widget);
|
||
_tab_widget->setTabsClosable(true);
|
||
connect(_tab_widget, &QTabWidget::tabCloseRequested, _core._view_mgr, &ViewManager::closeAt);
|
||
connect(_core._view_mgr, &ViewManager::appendRequest, _tab_widget, QOverload<QWidget*, const QString&>::of(&QTabWidget::addTab));
|
||
connect(_core._view_mgr, &ViewManager::removeRequest, _tab_widget, &QTabWidget::removeTab);
|
||
connect(_core._view_mgr, &ViewManager::activeRequest, _tab_widget, &QTabWidget::setCurrentIndex);
|
||
|
||
this->present_solution_customs(_core._translate_core, _solution_model);
|
||
splitter->setStretchFactor(1, 1);
|
||
}
|
||
|
||
TranslateUI::~TranslateUI()
|
||
{
|
||
}
|
||
|
||
void TranslateUI::addCustomRule()
|
||
{
|
||
auto typeAlias = QInputDialog::getText(this, u8"Please Enter Rule Name", "Rule:");
|
||
if (typeAlias == "")
|
||
return;
|
||
|
||
auto names = this->_core._translate_core->customDelegates().keys();
|
||
if (names.contains(typeAlias)) {
|
||
QMessageBox::critical(this, u8"Enter Validate", u8"RuleName Can't Repeat.");
|
||
return;
|
||
}
|
||
|
||
auto new_rule = std::make_shared<extract::AsRuleSet>();
|
||
new_rule->setAlias(typeAlias);
|
||
_core._translate_core->setCustomRule(typeAlias, new_rule);
|
||
|
||
this->present_solution_customs(_core._translate_core, _solution_model);
|
||
}
|
||
|
||
void TranslateUI::openExtractView()
|
||
{
|
||
auto index = this->_ruleset_navi->currentIndex();
|
||
if (!index.isValid())
|
||
return;
|
||
|
||
auto typeAlias = this->_solution_model->data(index, Qt::DisplayRole).toString();
|
||
this->_core._view_mgr->open(typeAlias, ViewType::ExtractConfiguration, this);
|
||
}
|
||
|
||
void TranslateUI::present_solution_customs(std::shared_ptr<TranslateBasic> ins, QStandardItemModel* model) const
|
||
{
|
||
model->removeRows(0, model->rowCount());
|
||
|
||
auto ruleset = ins->customDelegates();
|
||
for (auto rule_name : ruleset.keys()) {
|
||
auto cell = new QStandardItem(rule_name);
|
||
cell->setEditable(false);
|
||
model->appendRow(cell);
|
||
}
|
||
}
|
||
|
||
ViewManager::ViewManager(std::shared_ptr<TranslateBasic> base, QObject* p /*= nullptr*/)
|
||
:QObject(p), _translate_core(base)
|
||
{
|
||
|
||
}
|
||
|
||
void ViewManager::open(const QString& rule_name, ViewType v_type, QWidget* p)
|
||
{
|
||
for (auto vidx = 0; vidx < content_pages.size(); vidx++) {
|
||
auto v = content_pages.at(vidx);
|
||
if (std::get<0>(v)->aliasName() == rule_name && std::get<2>(v) == v_type) {
|
||
emit this->activeRequest(vidx, rule_name);
|
||
return;
|
||
}
|
||
}
|
||
assert(u8"RuleName must be valid." && _translate_core->customDelegates().contains(rule_name));
|
||
|
||
auto appoint = _translate_core->customDelegates()[rule_name];
|
||
//auto present = new ExtractRuleView(_translate_core, appoint, p);
|
||
//content_pages << std::make_tuple(appoint, present, v_type);
|
||
//emit this->appendRequest(present, rule_name);
|
||
//emit this->activeRequest(content_pages.size() - 1, rule_name);
|
||
}
|
||
|
||
void ViewManager::closeAt(int index)
|
||
{
|
||
assert(u8"index<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч" && (index < content_pages.size()));
|
||
emit this->removeRequest(index);
|
||
|
||
auto current = this->content_pages.at(index);
|
||
std::get<1>(current)->deleteLater();
|
||
content_pages.removeAt(index);
|
||
}
|
||
|
||
void ViewManager::active(const QString& rule_name, ViewType v_type)
|
||
{
|
||
for (auto vidx = 0; vidx < content_pages.size(); vidx++) {
|
||
auto v = content_pages.at(vidx);
|
||
if (std::get<0>(v)->aliasName() == rule_name && std::get<2>(v) == v_type) {
|
||
emit this->activeRequest(vidx, rule_name);
|
||
return;
|
||
}
|
||
}
|
||
|
||
assert(u8"RuleName<EFBFBD><EFBFBD>V_Type<EFBFBD><EFBFBD><EFBFBD>ϱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч" && 0);
|
||
}
|
||
|
||
void ViewManager::purge(const QString& rule_name)
|
||
{
|
||
for (auto vidx = 0; vidx < content_pages.size(); vidx++) {
|
||
auto v = content_pages.at(vidx);
|
||
if (std::get<0>(v)->aliasName() == rule_name) {
|
||
this->closeAt(vidx--);
|
||
}
|
||
}
|
||
}
|