modelpage.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #include "modelpage.h"
  2. /*----------------------------------ModelPage----------------------------------------------------------*/
  3. ModelPage::ModelPage(QWidget *parent) : QWidget(parent)
  4. {
  5. modelTable = new QTableView();
  6. itemModel = new QStandardItemModel();
  7. QHBoxLayout* searchLayout = new QHBoxLayout();
  8. QLabel *searchLable = new QLabel("搜索选择:");
  9. QComboBox* searchChose = new QComboBox();
  10. searchChose->addItem("模型id");
  11. searchChose->addItem("模型名称");
  12. searchChose->addItem("模型类型");
  13. QLineEdit* setSearch = new QLineEdit();
  14. QPushButton* confirmSearch = new QPushButton();
  15. confirmSearch->setText("确认");
  16. searchLayout->addWidget(searchLable);
  17. searchLayout->addWidget(searchChose);
  18. searchLayout->addWidget(setSearch);
  19. searchLayout->addWidget(confirmSearch);
  20. QHBoxLayout* optLayout = new QHBoxLayout();
  21. mPageInfo = new QLabel();
  22. mPageInfo->setText("");
  23. QSpacerItem* spacer1 = new QSpacerItem(0,30,QSizePolicy::Expanding,QSizePolicy::Fixed);
  24. QPushButton* upPage = new QPushButton();
  25. upPage->setText("上一页");
  26. QPushButton* nextPage = new QPushButton();
  27. nextPage->setText("下一页");
  28. QLabel* skipLabel = new QLabel();
  29. skipLabel->setText("跳转");
  30. mSetPage = new QLineEdit();
  31. QPushButton* confirmSkip = new QPushButton();
  32. confirmSkip->setText("确认");
  33. optLayout->addWidget(mPageInfo);
  34. optLayout->addItem(spacer1);
  35. optLayout->addWidget(upPage);
  36. optLayout->addWidget(nextPage);
  37. optLayout->addWidget(skipLabel);
  38. optLayout->addWidget(mSetPage);
  39. optLayout->addWidget(confirmSkip);
  40. QVBoxLayout* mainLayout = new QVBoxLayout(this);
  41. mainLayout->addLayout(searchLayout);
  42. mainLayout->addWidget(modelTable);
  43. mainLayout->addLayout(optLayout);
  44. /*
  45. {
  46. itemModel->setItem(0,0,new QStandardItem("1"));
  47. itemModel->setItem(0,1,new QStandardItem("扇叶模型"));
  48. itemModel->setItem(0,2,new QStandardItem("This is a model"));
  49. itemModel->setItem(0,3,new QStandardItem("vga"));
  50. QPushButton* edit1 = new QPushButton("编辑");
  51. edit1->setFixedSize(QSize(120,30));
  52. edit1->setStyleSheet("QPushButton{color:white;background-color:rgb(51,204,255);font-family:黑体;border-radius: 15px;}"
  53. "QPushButton:pressed{background-color:rgb(51,129,172)}");
  54. QPushButton* del1 = new QPushButton("删除");
  55. del1->setFixedSize(QSize(120,30));
  56. del1->setStyleSheet("QPushButton{color:white;background-color:rgb(51,204,255);font-family:黑体;border-radius: 15px;}"
  57. "QPushButton:pressed{background-color:rgb(51,129,172)}");
  58. QHBoxLayout *vLayout1 = new QHBoxLayout();
  59. QWidget * widget1 = new QWidget;
  60. vLayout1->addWidget(edit1);
  61. vLayout1->addWidget(del1);
  62. vLayout1->addSpacing(10);
  63. widget1->setLayout(vLayout1);
  64. modelTable->setIndexWidget(itemModel->index(0,4),widget1);
  65. itemModel->setItem(1,0,new QStandardItem("2"));
  66. itemModel->setItem(1,1,new QStandardItem("机翼模型"));
  67. itemModel->setItem(1,2,new QStandardItem("This is another model"));
  68. itemModel->setItem(1,3,new QStandardItem("txt"));
  69. QPushButton* edit = new QPushButton("编辑");
  70. edit->setFixedSize(QSize(120,30));
  71. edit->setStyleSheet("QPushButton{color:white;background-color:rgb(51,204,255);font-family:黑体;border-radius: 15px;}"
  72. "QPushButton:pressed{background-color:rgb(51,129,172)}");
  73. QPushButton* del = new QPushButton("删除");
  74. del->setFixedSize(QSize(120,30));
  75. del->setStyleSheet("QPushButton{color:white;background-color:rgb(51,204,255);font-family:黑体;border-radius: 15px;}"
  76. "QPushButton:pressed{background-color:rgb(51,129,172)}");
  77. QHBoxLayout *vLayout = new QHBoxLayout();
  78. QWidget * widget = new QWidget;
  79. vLayout->addWidget(edit);
  80. vLayout->addWidget(del);
  81. vLayout->addSpacing(10);
  82. widget->setLayout(vLayout);
  83. modelTable->setIndexWidget(itemModel->index(1,4),widget);
  84. }
  85. */
  86. mTotalCount = 0;
  87. mTotalPage = 0;
  88. mCurrPage = 0;
  89. mPerPageCnt = 9;
  90. // for(int i = 0 ; i < 20; i++)
  91. // {
  92. // ModelInfo* model = new ModelInfo;
  93. // model->id = "i";
  94. // model->name = "lisi" ;
  95. // model->des = "1";
  96. // model->path = "D";
  97. // model->type ="Gmash";
  98. // modelList.append(model);
  99. // }
  100. int num = modelList.size();
  101. cptPageData(num);
  102. int nStart = 0;
  103. int nEnd = 0;
  104. cptSubscripRange(nStart,nEnd);
  105. displayTable(nStart,nEnd);
  106. setPageInfo(mTotalPage,mTotalCount,mCurrPage);
  107. connect(confirmSkip,&QPushButton::clicked,this,&ModelPage::onSearchButtonClick);
  108. connect(upPage,&QPushButton::clicked,this,&ModelPage::onUpButtonClick);
  109. connect(nextPage,&QPushButton::clicked,this,&ModelPage::onNextButtonClick);
  110. }
  111. void ModelPage::displayTable(int nStart, int nEnd)
  112. {
  113. int columenCnt;
  114. if( modelList.size() == 0)
  115. {
  116. columenCnt = 0;
  117. }
  118. else
  119. {
  120. columenCnt = nEnd - nStart + 1 ;
  121. }
  122. QList<ModelInfo*> tv = modelList.mid(nStart,columenCnt );
  123. // qDebug()<<nStart<<" " << nEnd << " " << columenCnt;
  124. if(tv.size() != columenCnt )
  125. {
  126. // qDebug()<<"分页数据异常";
  127. return;
  128. }
  129. itemModel->clear();
  130. modelTable->setModel(itemModel);
  131. QStringList tableHead;
  132. tableHead<<"模型ID"<<"模型名称"<<"模型描述"<<"模型路径"<<"模型类型"<<"操作";
  133. itemModel->setHorizontalHeaderLabels(tableHead);
  134. QFont fontHeadr("微软雅黑",11);
  135. for(int i = 0 ; i < itemModel->columnCount(); i++)
  136. {
  137. QStandardItem *headerItem = itemModel->horizontalHeaderItem(i);
  138. headerItem->setFont(fontHeadr);
  139. headerItem->setForeground(QBrush(QColor(0,0,0)));
  140. }
  141. itemModel->setColumnCount(6);
  142. for(int i = 0; i < columenCnt ; i++)
  143. {
  144. itemModel->setItem(i,0,new QStandardItem(QString::fromStdString(tv.at(i)->id)));
  145. itemModel->setItem(i,1,new QStandardItem(QString::fromStdString(tv.at(i)->name)));
  146. itemModel->setItem(i,2,new QStandardItem(QString::fromStdString(tv.at(i)->des)));
  147. itemModel->setItem(i,3,new QStandardItem(QString::fromStdString(tv.at(i)->path)));
  148. itemModel->setItem(i,4,new QStandardItem(QString::fromStdString(tv.at(i)->type)));
  149. // int subscript = (mCurrPage -1)* mPerPageCnt + i;
  150. QPushButton* editButton = new QPushButton();
  151. editButton->setText("编辑");
  152. editButton->setProperty("edit",i);
  153. QPushButton* delButton = new QPushButton();
  154. delButton->setText("删除");
  155. delButton->setProperty("del",i);
  156. connect(delButton,&QPushButton::clicked,this,&ModelPage::onDelButtonClick);
  157. QHBoxLayout *vLayout = new QHBoxLayout();
  158. QWidget * buttonWidget = new QWidget;
  159. vLayout->addWidget(editButton);
  160. vLayout->addWidget(delButton);
  161. vLayout->addSpacing(10);
  162. buttonWidget->setLayout(vLayout);
  163. modelTable->setIndexWidget(itemModel->index(i,5),buttonWidget);
  164. }
  165. modelTable->verticalHeader()->hide();
  166. modelTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
  167. modelTable->setSelectionBehavior(QAbstractItemView::SelectRows);
  168. modelTable->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
  169. modelTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
  170. }
  171. void ModelPage::cptPageData(int count)
  172. {
  173. if(mPerPageCnt == 0)
  174. {
  175. return;
  176. }
  177. mTotalCount = count;
  178. mTotalPage = mTotalCount/mPerPageCnt + (mTotalCount % mPerPageCnt ? 1:0);
  179. mCurrPage = 1;
  180. }
  181. void ModelPage::cptSubscripRange(int &mStart,int &mEnd)
  182. {
  183. if(mPerPageCnt == 0 )
  184. {
  185. return;
  186. }
  187. if(mCurrPage < mTotalPage)
  188. {
  189. mStart = (mCurrPage - 1)* mPerPageCnt;
  190. mEnd = mCurrPage * mPerPageCnt - 1;
  191. }
  192. else if(mCurrPage == mTotalPage)
  193. {
  194. if(mTotalCount%mPerPageCnt == 0)
  195. {
  196. mStart = (mCurrPage -1)* mPerPageCnt;
  197. mEnd = mCurrPage*mPerPageCnt -1;
  198. }
  199. else
  200. {
  201. mStart = (mCurrPage -1)* mPerPageCnt;
  202. mEnd = (mCurrPage -1)*mPerPageCnt + mTotalCount% mPerPageCnt -1;
  203. }
  204. }
  205. }
  206. void ModelPage::setPageInfo(int i,int count,int curr)
  207. {
  208. mPageInfo->setText(QString("共计%1页,%2条记录,当前第%3页").arg(QString::number(i)).arg(QString::number(count)).arg(QString::number(curr)));
  209. }
  210. void ModelPage::onSearchButtonClick()
  211. {
  212. int target = mSetPage->text().toInt();
  213. if(target <= 0 || target > mTotalPage)
  214. {
  215. return;
  216. }
  217. mCurrPage = target;
  218. int mStart = 0;
  219. int mEnd = 0;
  220. cptSubscripRange(mStart,mEnd);
  221. displayTable(mStart,mEnd);
  222. setPageInfo(mTotalPage,mTotalCount,mCurrPage);
  223. }
  224. void ModelPage::onUpButtonClick()
  225. {
  226. if(mCurrPage - 1 <= 0)
  227. {
  228. return;
  229. }
  230. mCurrPage = mCurrPage - 1;
  231. int mStart = 0, mEnd = 0;
  232. cptSubscripRange(mStart,mEnd);
  233. displayTable(mStart,mEnd);
  234. setPageInfo(mTotalPage,mTotalCount,mCurrPage);
  235. }
  236. void ModelPage::onNextButtonClick()
  237. {
  238. if(mCurrPage + 1 > mTotalPage)
  239. {
  240. return;
  241. }
  242. mCurrPage = mCurrPage+1;
  243. int mStart = 0, mEnd = 0;
  244. cptSubscripRange(mStart,mEnd);
  245. displayTable(mStart,mEnd);
  246. setPageInfo(mTotalPage,mTotalCount,mCurrPage);
  247. }
  248. void ModelPage::onEditButtonClick()
  249. {
  250. }
  251. void ModelPage::onDelButtonClick()
  252. {
  253. QPushButton* pSender = qobject_cast<QPushButton*>(sender());
  254. int id = pSender->property("del").toInt();
  255. int subscript = (mCurrPage - 1)*mPerPageCnt + id;
  256. if(subscript > modelList.size())
  257. {
  258. return;
  259. }
  260. modelList.removeAt(subscript);
  261. cptPageData(modelList.size());
  262. int mStart = 0, mEnd = 0;
  263. cptSubscripRange(mStart,mEnd);
  264. displayTable(mStart,mEnd);
  265. setPageInfo(mTotalPage,mTotalCount,mCurrPage);
  266. }
  267. void ModelPage::onShowNews()
  268. {
  269. int num = modelList.size();
  270. cptPageData(num);
  271. int nStart = 0;
  272. int nEnd = 0;
  273. cptSubscripRange(nStart,nEnd);
  274. displayTable(nStart,nEnd);
  275. setPageInfo(mTotalPage,mTotalCount,mCurrPage);
  276. }
  277. /*----------------------------------------------------------ModelAdd-------------------------------------*/
  278. ModelAdd::ModelAdd(QDialog *parent) : QDialog(parent)
  279. {
  280. resize(500,500);
  281. setWindowTitle("导入模型");
  282. mModelInfo = new ModelInfo;
  283. QLabel* modelId = new QLabel("模型ID:");
  284. QLabel* modelName = new QLabel("模型名:");
  285. QLabel* modelDes = new QLabel("模型描述:");
  286. QLabel* modelPath = new QLabel("模型路径:");
  287. QLabel* modelType = new QLabel("模型类型:");
  288. setModelId = new QLineEdit;
  289. setModelName = new QLineEdit;
  290. setModelDes = new QLineEdit;
  291. setModelPath = new QLineEdit;
  292. setModelType = new QLineEdit;
  293. QPushButton* addButton = new QPushButton("添加");
  294. addButton->setFixedSize(QSize(120,30));
  295. addButton->setStyleSheet("QPushButton{color:white;background-color:rgb(51,204,255);font-family:黑体;border-radius: 15px;}"
  296. "QPushButton:pressed{background-color:rgb(51,129,172)}");
  297. QPushButton* cancelButton = new QPushButton("取消");
  298. cancelButton->setFixedSize(QSize(120,30));
  299. cancelButton->setStyleSheet("QPushButton{color:white;background-color:rgb(51,204,255);font-family:黑体;border-radius: 15px;}"
  300. "QPushButton:pressed{background-color:rgb(51,129,172)}");
  301. QGridLayout* textLayout = new QGridLayout;
  302. textLayout->addWidget(modelId,0,0);
  303. textLayout->addWidget(setModelId,0,1);
  304. textLayout->addWidget(modelName,1,0);
  305. textLayout->addWidget(setModelName,1,1);
  306. textLayout->addWidget(modelDes,2,0);
  307. textLayout->addWidget(setModelDes,2,1);
  308. textLayout->addWidget(modelPath,3,0);
  309. textLayout->addWidget(setModelPath,3,1);
  310. textLayout->addWidget(modelType,4,0);
  311. textLayout->addWidget(setModelType,4,1);
  312. QHBoxLayout* buttonLayout = new QHBoxLayout;
  313. buttonLayout->addWidget(addButton);
  314. buttonLayout->addWidget(cancelButton);
  315. QVBoxLayout* layout = new QVBoxLayout(this);
  316. layout->addLayout(textLayout);
  317. layout->addLayout(buttonLayout);
  318. connect(addButton,&QPushButton::clicked,this,[this](){
  319. mModelInfo->id = setModelId->text().toStdString();
  320. mModelInfo->name = setModelName->text().toStdString();
  321. mModelInfo->des = setModelDes->text().toStdString();
  322. mModelInfo->path = setModelPath->text().toStdString();
  323. mModelInfo->type = setModelType->text().toStdString();
  324. modelList.append(mModelInfo);
  325. emit addModel();
  326. emit sendModel();
  327. });
  328. connect(cancelButton,&QPushButton::clicked,this,[this](){
  329. close();
  330. });
  331. }