input.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "qt/input/input.hpp"
  2. QtInput::QtInput():QWidget(){
  3. data=new QtInputData;
  4. main_layout=new QVBoxLayout;
  5. button_layout=new QHBoxLayout;
  6. tab_widget=new QTabWidget;
  7. button_widget=new QWidget;
  8. button_save=new QPushButton("Save");
  9. button_cancel=new QPushButton("Cancel");
  10. time_bar=new QScrollBar(Qt::Horizontal);
  11. input_physics=new QtInputPhysics;
  12. input_time=new QtInputTime;
  13. input_geometry=new QtInputGeometry(data);
  14. input_initial_state=new QtInputInitialState(data);
  15. input_pump_tab=new QtInputPumpTab(data);
  16. input_clouds_tab=new QtInputCloudsTab(data);
  17. input_view=new QtInputView(data);
  18. //Tab
  19. tab_widget->addTab(input_physics,"Physics");
  20. tab_widget->addTab(input_time,"Time");
  21. tab_widget->addTab(input_geometry,"Geometry");
  22. tab_widget->addTab(input_initial_state,"Initial state");
  23. tab_widget->addTab(input_pump_tab,"Pumps");
  24. tab_widget->addTab(input_clouds_tab,"Clouds");
  25. //Buttons
  26. button_layout->addWidget(button_save);
  27. button_layout->addWidget(button_cancel);
  28. button_widget->setLayout(button_layout);
  29. //Main
  30. main_layout->addWidget(tab_widget);
  31. main_layout->addWidget(input_view,2);
  32. main_layout->addWidget(time_bar);
  33. main_layout->addWidget(button_widget);
  34. setLayout(main_layout);
  35. //Conectors
  36. connect(button_save,&QPushButton::clicked,this,&QtInput::save);
  37. connect(button_cancel,&QPushButton::clicked,this,&QtInput::cancel);
  38. connect(input_geometry,&QtInputGeometry::geometryChanged,this,&QtInput::updateGeometry);
  39. connect(input_physics,&QtInputPhysics::physicsChanged,this,&QtInput::updateInitialState);
  40. connect(input_view,&QtInputView::geometryChanged,this,&QtInput::updateGeometry);
  41. connect(tab_widget,&QTabWidget::currentChanged,this,&QtInput::changeTabIndex);
  42. connect(input_initial_state,&QtInputInitialState::initialStateChanged,this,&QtInput::updateInitialState);
  43. connect(time_bar,&QScrollBar::valueChanged,input_view,&QtInputView::setTime);
  44. connect(input_pump_tab,&QtInputPumpTab::sourcesChanged,this,&QtInput::updateSource);
  45. connect(input_clouds_tab,&QtInputCloudsTab::sourcesChanged,this,&QtInput::updateSource);
  46. connect(input_view,&QtInputView::timeChanged,this,&QtInput::updateSource);
  47. previous_index=-1;
  48. }
  49. QtInput::QtInput(QString filename):QtInput(){
  50. load(filename.toStdString());
  51. }
  52. bool
  53. QtInput::validate(){
  54. QWidget* widget=input_physics->validate();
  55. if(widget!=nullptr){
  56. QMessageBox msgBox;
  57. msgBox.setText("Incorrect physics entry");
  58. msgBox.exec();
  59. tab_widget->setCurrentWidget(input_physics);
  60. widget->setFocus();
  61. return false;
  62. }
  63. widget=input_time->validate();
  64. if(widget!=nullptr){
  65. QMessageBox msgBox;
  66. msgBox.setText("Incorrect time entry");
  67. msgBox.exec();
  68. tab_widget->setCurrentWidget(input_time);
  69. widget->setFocus();
  70. return false;
  71. }
  72. widget=input_geometry->validate();
  73. if(widget!=nullptr){
  74. QMessageBox msgBox;
  75. msgBox.setText("Incorrect geometry entry");
  76. msgBox.exec();
  77. tab_widget->setCurrentWidget(input_geometry);
  78. widget->setFocus();
  79. return false;
  80. }
  81. widget=input_initial_state->validate();
  82. if(widget!=nullptr){
  83. QMessageBox msgBox;
  84. msgBox.setText("Incorrect initial entry");
  85. msgBox.exec();
  86. tab_widget->setCurrentWidget(input_initial_state);
  87. widget->setFocus();
  88. return false;
  89. }
  90. return true;
  91. }
  92. void
  93. QtInput::save(){
  94. if(validate()){
  95. QString filename=QFileDialog::getSaveFileName(this,"Save input","inputs/","QT input file (*.input)");
  96. if(not filename.isEmpty()){
  97. if(filename.indexOf(".input")==-1){
  98. filename.append(".input");
  99. }
  100. save_input(filename.toStdString());
  101. }
  102. }
  103. }
  104. void
  105. QtInput::save_input(string filename){
  106. updateGeometry();
  107. fstream file;
  108. file.open(filename.c_str(),fstream::out|fstream::trunc|fstream::binary);
  109. input_physics->setPhysics();
  110. input_time->setTime();
  111. data->save(file);
  112. file.close();
  113. }
  114. void
  115. QtInput::cancel(){
  116. emit exit_signal();
  117. }
  118. void
  119. QtInput::load(string filename){
  120. fstream file;
  121. file.open(filename.c_str(),fstream::in|fstream::binary);
  122. data->load(file);
  123. input_physics->getPhysics();
  124. input_time->getTime();
  125. input_geometry->getGeometry();
  126. input_initial_state->getTanks();
  127. input_pump_tab->getPumps();
  128. input_clouds_tab->getClouds();
  129. file.close();
  130. }
  131. void
  132. QtInput::changeTabIndex(int index){
  133. if(not validate()) return;
  134. switch(index){
  135. case 2:
  136. input_view->setStatus(QtInputView::Geom);
  137. break;
  138. case 3:
  139. input_view->setStatus(QtInputView::Init);
  140. break;
  141. case 4:
  142. input_view->setStatus(QtInputView::Sources);
  143. break;
  144. default:
  145. input_view->setStatus(QtInputView::Other);
  146. break;
  147. }
  148. switch(previous_index){
  149. case 2:
  150. updateGeometry();
  151. break;
  152. case 3:
  153. updateInitialState();
  154. break;
  155. default:
  156. break;
  157. }
  158. previous_index=index;
  159. input_view->update();
  160. }
  161. void
  162. QtInput::updateGeometry(){
  163. data->updateGeometry();
  164. data->updateInitialState();
  165. input_view->update();
  166. }
  167. void
  168. QtInput::updateInitialState(){
  169. data->updateInitialState();
  170. input_view->update();
  171. }
  172. void
  173. QtInput::updateSource(){
  174. input_view->update();
  175. }
  176. QtInput::~QtInput(){
  177. delete data;
  178. }