MultipleWidget.cpp.template 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "%1.h"
  2. #include "MessageException.h"
  3. #include "%2.h"
  4. #include "Application.h"
  5. #include <QMessageBox>
  6. #if _MSC_VER >= 1600
  7. #pragma execution_character_set("utf-8")
  8. #endif
  9. %1::%1(QWidget *parent)
  10. : QWidget(parent)
  11. , m_PageIndex(0)
  12. {
  13. ui = new Ui::%1();
  14. ui->setupUi(this);
  15. init();
  16. doQuery();
  17. }
  18. %1::~%1()
  19. {
  20. delete ui;
  21. }
  22. void %1::init()
  23. {
  24. initTableView();
  25. initModel();
  26. initService();
  27. initPagination();
  28. initFormBuilder();
  29. }
  30. void %1::initTableView()
  31. {
  32. ui->tvData->setMultipleSelect(true);
  33. }
  34. void %1::initModel()
  35. {
  36. m_Model = new %3(this);
  37. m_Model->setMultiSelect(true);
  38. ui->tvData->setModel(m_Model);
  39. }
  40. void %1::initService()
  41. {
  42. m_Service = new %4(this);
  43. }
  44. void %1::initFormBuilder()
  45. {
  46. m_FormBuilder = new FormBuilder(this);
  47. }
  48. void %1::initPagination()
  49. {
  50. ui->pagination->setPageCount(0);
  51. ui->pagination->setRecordCount(0);
  52. connect(ui->pagination, &Pagination::selectPage, [=](int value) {
  53. doQuery(value);
  54. });
  55. }
  56. QList<%2_ptr> %1::convertToList(qx::IxCollection *collection)
  57. {
  58. QList<%2_ptr> objs;
  59. for (size_t i = 0; i < collection->_count(); i++)
  60. {
  61. objs << collection->_get<%2_ptr>(i);
  62. }
  63. return objs;
  64. }
  65. void %1::doQuery(int pageIndex /*= 0*/)
  66. {
  67. QString name = ui->leQName->text();
  68. try
  69. {
  70. m_Model->fetchByName(name, pageIndex);
  71. ui->pagination->setRecordCount(m_Model->getTotalCount());
  72. ui->pagination->setPageCount(m_Model->getPageCount());
  73. }
  74. catch (MessageException &e)
  75. {
  76. QMessageBox::critical(this, "错误", e.getMessage());
  77. }
  78. }
  79. void %1::onQuery()
  80. {
  81. doQuery();
  82. }
  83. void %1::onReset()
  84. {
  85. ui->leQName->clear();
  86. doQuery();
  87. }
  88. void %1::onAdd()
  89. {
  90. QWidget *widget = m_FormBuilder->build<%2>("添加%5");
  91. m_FormBuilder->clear();
  92. int result = Application::instance()->showModalDialog(widget, this);
  93. if (result == QDialog::Accepted)
  94. {
  95. %2_ptr obj = m_FormBuilder->getValue<%2>();
  96. try
  97. {
  98. m_Service->add(obj);
  99. }
  100. catch (MessageException &e)
  101. {
  102. QMessageBox::critical(this, "错误", e.getMessage());
  103. return;
  104. }
  105. doQuery();
  106. }
  107. }
  108. void %1::onEdit()
  109. {
  110. QModelIndex index = ui->tvData->currentIndex();
  111. if (!index.isValid())
  112. {
  113. QMessageBox::information(this, "提示", "请选中要修改的数据!");
  114. return;
  115. }
  116. %2_ptr oldObj = m_Model->getByRow(index.row());
  117. QWidget *widget = m_FormBuilder->build<%2>("修改%5");
  118. m_FormBuilder->clear();
  119. m_FormBuilder->fill(oldObj);
  120. int result = Application::instance()->showModalDialog(widget, this);
  121. if (result == QDialog::Accepted)
  122. {
  123. try{
  124. %2_ptr newObj = m_FormBuilder->getValue<%2>();
  125. newObj->setID(oldObj->getID());
  126. m_Service->updateById(newObj);
  127. }
  128. catch (MessageException &e){
  129. QMessageBox::critical(this, "错误", e.getMessage());
  130. return;
  131. }
  132. doQuery();
  133. }
  134. }
  135. void %1::onRemove()
  136. {
  137. qx::IxCollection * collection = ui->tvData->getSelectedRecord();
  138. if (collection->_count() <= 0)
  139. {
  140. QMessageBox::information(this, "提示", "请选中要删除的数据!");
  141. return;
  142. }
  143. QMessageBox::StandardButton msgBox;
  144. msgBox = QMessageBox::question(this, "提示", "确定删除选中的数据吗?", QMessageBox::Yes | QMessageBox::No);
  145. if (msgBox == QMessageBox::No)
  146. return;
  147. try
  148. {
  149. m_Service->deleteById(convertToList(collection));
  150. m_Model->clear();
  151. doQuery();
  152. }
  153. catch (MessageException &e)
  154. {
  155. QMessageBox::critical(this, "错误", e.getMessage());
  156. }
  157. }