#include "%1.h" #include "MessageException.h" #include "%2.h" #include "Application.h" #include #if _MSC_VER >= 1600 #pragma execution_character_set("utf-8") #endif %1::%1(QWidget *parent) : QWidget(parent) , m_PageIndex(0) { ui = new Ui::%1(); ui->setupUi(this); init(); doQuery(); } %1::~%1() { delete ui; } void %1::init() { initTableView(); initModel(); initService(); initPagination(); initFormBuilder(); } void %1::initTableView() { ui->tvData->setMultipleSelect(true); } void %1::initModel() { m_Model = new %3(this); m_Model->setMultiSelect(true); ui->tvData->setModel(m_Model); } void %1::initService() { m_Service = new %4(this); } void %1::initFormBuilder() { m_FormBuilder = new FormBuilder(this); } void %1::initPagination() { ui->pagination->setPageCount(0); ui->pagination->setRecordCount(0); connect(ui->pagination, &Pagination::selectPage, [=](int value) { doQuery(value); }); } QList<%2_ptr> %1::convertToList(qx::IxCollection *collection) { QList<%2_ptr> objs; for (size_t i = 0; i < collection->_count(); i++) { objs << collection->_get<%2_ptr>(i); } return objs; } void %1::doQuery(int pageIndex /*= 0*/) { QString name = ui->leQName->text(); try { m_Model->fetchByName(name, pageIndex); ui->pagination->setRecordCount(m_Model->getTotalCount()); ui->pagination->setPageCount(m_Model->getPageCount()); } catch (MessageException &e) { QMessageBox::critical(this, "错误", e.getMessage()); } } void %1::onQuery() { doQuery(); } void %1::onReset() { ui->leQName->clear(); doQuery(); } void %1::onAdd() { QWidget *widget = m_FormBuilder->build<%2>("添加%5"); m_FormBuilder->clear(); int result = Application::instance()->showModalDialog(widget, this); if (result == QDialog::Accepted) { %2_ptr obj = m_FormBuilder->getValue<%2>(); try { m_Service->add(obj); } catch (MessageException &e) { QMessageBox::critical(this, "错误", e.getMessage()); return; } doQuery(); } } void %1::onEdit() { QModelIndex index = ui->tvData->currentIndex(); if (!index.isValid()) { QMessageBox::information(this, "提示", "请选中要修改的数据!"); return; } %2_ptr oldObj = m_Model->getByRow(index.row()); QWidget *widget = m_FormBuilder->build<%2>("修改%5"); m_FormBuilder->clear(); m_FormBuilder->fill(oldObj); int result = Application::instance()->showModalDialog(widget, this); if (result == QDialog::Accepted) { try{ %2_ptr newObj = m_FormBuilder->getValue<%2>(); newObj->setID(oldObj->getID()); m_Service->updateById(newObj); } catch (MessageException &e){ QMessageBox::critical(this, "错误", e.getMessage()); return; } doQuery(); } } void %1::onRemove() { qx::IxCollection * collection = ui->tvData->getSelectedRecord(); if (collection->_count() <= 0) { QMessageBox::information(this, "提示", "请选中要删除的数据!"); return; } QMessageBox::StandardButton msgBox; msgBox = QMessageBox::question(this, "提示", "确定删除选中的数据吗?", QMessageBox::Yes | QMessageBox::No); if (msgBox == QMessageBox::No) return; try { m_Service->deleteById(convertToList(collection)); m_Model->clear(); doQuery(); } catch (MessageException &e) { QMessageBox::critical(this, "错误", e.getMessage()); } }