/mainwindow.cpp

http://nscow.googlecode.com/ · C++ · 202 lines · 169 code · 33 blank · 0 comment · 9 complexity · 0399383b36e0621be18e24003326e19f MD5 · raw file

  1. #include <QDesktopWidget>
  2. #include <QCursor>
  3. #include <QMessageBox>
  4. #include "mainwindow.h"
  5. #include "ui_mainwindow.h"
  6. MainWindow::MainWindow(QWidget *parent) :
  7. QMainWindow(parent),
  8. ui(new Ui::MainWindow)
  9. {
  10. ui->setupUi(this);
  11. thread = new RenderThread();
  12. QDesktopWidget desktop;
  13. QRect space = desktop.availableGeometry();
  14. QPixmap tempPixmap(1,1);
  15. tempPixmap.fill(qRgb(0,0,0));
  16. tempPixmap = tempPixmap.scaled(0.75*space.width(), 0.75*space.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
  17. ui->drawBox->setPixmap(tempPixmap);
  18. connect(ui->stopButton, SIGNAL(clicked()), thread, SLOT(stopProcess()));
  19. connect(thread, SIGNAL(finished()), this, SLOT(resetUi()));
  20. connect(thread, SIGNAL(sendImage(QImage)), this, SLOT(updatePixmap(QImage)));
  21. connect(ui->outputBox, SIGNAL(currentIndexChanged(int)), thread,SLOT(setOutput(int)));
  22. connect(ui->drawBox, SIGNAL(clickAt(double,double)), thread, SLOT(startForce(double,double)));
  23. connect(ui->drawBox, SIGNAL(dragTo(double,double)), thread, SLOT(moreForce(double,double)));
  24. ui->nuSlider->setValue(2);
  25. ui->dtSlider->setValue(2);
  26. ui->dxSlider->setValue(2);
  27. ui->eSlider->setValue(2);
  28. ui->rToolSlider->setValue(2);
  29. ui->magToolSlider->setValue(2);
  30. ui->dcSlider->setValue(2);
  31. ui->nuSlider->setValue(1*10);
  32. ui->dtSlider->setValue(1*100);
  33. ui->dxSlider->setValue(1*100);
  34. ui->dcSlider->setValue(1*10);
  35. ui->rToolSlider->setValue(1);
  36. ui->magToolSlider->setValue(1);
  37. ui->eSlider->setValue(0*10);
  38. ui->drawBox->setCursor(QCursor(QPixmap(":/img/Logo_physics.png").scaled(32,32)));
  39. }
  40. MainWindow::~MainWindow()
  41. {
  42. delete thread;
  43. delete ui;
  44. }
  45. void MainWindow::changeEvent(QEvent *e)
  46. {
  47. QMainWindow::changeEvent(e);
  48. switch (e->type()) {
  49. case QEvent::LanguageChange:
  50. ui->retranslateUi(this);
  51. break;
  52. default:
  53. break;
  54. }
  55. }
  56. void MainWindow::run() {
  57. ui->runButton->setEnabled(false);
  58. ui->stopButton->setEnabled(true);
  59. ui->xdimValue->setEnabled(false);
  60. ui->ydimValue->setEnabled(false);
  61. bool xokay, yokay;
  62. int xdim = ui->xdimValue->text().toInt(&xokay);
  63. int ydim = ui->ydimValue->text().toInt(&yokay);
  64. if (xokay && yokay) {
  65. thread->startSim(xdim,ydim);
  66. } else {
  67. ui->xdimValue->setText(QString("50"));
  68. ui->ydimValue->setText(QString("50"));
  69. thread->startSim(50,50);
  70. }
  71. }
  72. void MainWindow::updatePixmap(const QImage &image)
  73. {
  74. if(!image.isNull()){
  75. QPixmap pixmap = QPixmap::fromImage(image);
  76. QDesktopWidget desktop;
  77. QRect space = desktop.availableGeometry();
  78. pixmap = pixmap.scaled(0.75*space.width(), 0.75*space.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
  79. ui->drawBox->setPixmap(pixmap);
  80. }
  81. }
  82. void MainWindow::resetUi()
  83. {
  84. ui->runButton->setEnabled(true);
  85. ui->stopButton->setEnabled(false);
  86. ui->xdimValue->setEnabled(true);
  87. ui->ydimValue->setEnabled(true);
  88. }
  89. void MainWindow::on_actionAbout_triggered() {
  90. QMessageBox::about(this,tr("Purple Cow"),
  91. tr("Navier-Stokes's Cow is a simple fluid dynamics simulator. It assumes constant density and no-slip boundary conditions.\n\n"
  92. "The purple cow was written by William Olson Copyright 2010 and released under the GNU GPL."));
  93. }
  94. void MainWindow::on_nuSlider_valueChanged(int val) {
  95. ui->nuValue->setText(QString::number(val/10.0));
  96. thread->setNu(val/10.0);
  97. }
  98. void MainWindow::on_dtSlider_valueChanged(int val) {
  99. ui->dtValue->setText(QString::number(val/100.0));
  100. thread->setDt(val/100.0);
  101. }
  102. void MainWindow::on_dxSlider_valueChanged(int val) {
  103. ui->dxValue->setText(QString::number(val/100.0));
  104. thread->setDx(val/100.0);
  105. }
  106. void MainWindow::on_eSlider_valueChanged(int val) {
  107. ui->eValue->setText(QString::number(val/100.0));
  108. thread->setE(val/100.0);
  109. }
  110. void MainWindow::on_rToolSlider_valueChanged(int val) {
  111. ui->rToolValue->setText(QString::number(val));
  112. thread->setRTool(val);
  113. }
  114. void MainWindow::on_magToolSlider_valueChanged(int val) {
  115. ui->magToolValue->setText(QString::number(val));
  116. thread->setMagTool(val);
  117. }
  118. void MainWindow::on_dcSlider_valueChanged(int val) {
  119. ui->dcValue->setText(QString::number(val/10.0));
  120. thread->setDC(val/10.0);
  121. }
  122. void MainWindow::on_actionAdd_Force_triggered(bool isChecked) {
  123. if(isChecked) {
  124. resetTools();
  125. ui->drawBox->setCursor(QCursor(QPixmap(":/img/Logo_physics.png").scaled(32,32)));
  126. connect(ui->drawBox, SIGNAL(clickAt(double,double)), thread, SLOT(startForce(double,double)));
  127. connect(ui->drawBox, SIGNAL(dragTo(double,double)), thread, SLOT(moreForce(double,double)));
  128. }
  129. ui->actionAdd_Force->setChecked(true);
  130. }
  131. void MainWindow::on_actionAdd_Dye_triggered(bool isChecked) {
  132. if(isChecked) {
  133. resetTools();
  134. ui->drawBox->setCursor(QCursor(QPixmap(":/img/Beakerblue.png").scaled(32,32)));
  135. connect(ui->drawBox, SIGNAL(clickAt(double,double)), thread, SLOT(addDye(double,double)));
  136. connect(ui->drawBox, SIGNAL(dragTo(double,double)), thread, SLOT(addDye(double,double)));
  137. connect(ui->drawBox, SIGNAL(releaseAt(double,double)), thread, SLOT(stopAction(double,double)));
  138. }
  139. ui->actionAdd_Dye->setChecked(true);
  140. }
  141. void MainWindow::on_actionOutward_Force_triggered(bool isChecked) {
  142. if(isChecked) {
  143. resetTools();
  144. ui->drawBox->setCursor(QCursor(QPixmap(":/img/Arrow_out.png").scaled(32,32)));
  145. connect(ui->drawBox, SIGNAL(clickAt(double,double)), thread, SLOT(addOutForce(double,double)));
  146. connect(ui->drawBox, SIGNAL(dragTo(double,double)), thread, SLOT(addOutForce(double,double)));
  147. connect(ui->drawBox, SIGNAL(releaseAt(double,double)), thread, SLOT(stopAction(double,double)));
  148. }
  149. ui->actionOutward_Force->setChecked(true);
  150. }
  151. void MainWindow::on_actionInward_Force_triggered(bool isChecked) {
  152. if(isChecked) {
  153. resetTools();
  154. ui->drawBox->setCursor(QCursor(QPixmap(":/img/Arrow_in.png").scaled(32,32)));
  155. connect(ui->drawBox, SIGNAL(clickAt(double,double)), thread, SLOT(addInForce(double,double)));
  156. connect(ui->drawBox, SIGNAL(dragTo(double,double)), thread, SLOT(addInForce(double,double)));
  157. connect(ui->drawBox, SIGNAL(releaseAt(double,double)), thread, SLOT(stopAction(double,double)));
  158. }
  159. ui->actionInward_Force->setChecked(true);
  160. }
  161. void MainWindow::resetTools() {
  162. ui->actionInward_Force->setChecked(false);
  163. ui->actionAdd_Force->setChecked(false);
  164. ui->actionAdd_Dye->setChecked(false);
  165. ui->actionOutward_Force->setChecked(false);
  166. disconnect(ui->drawBox,SIGNAL(clickAt(double,double)),0,0);
  167. disconnect(ui->drawBox,SIGNAL(dragTo(double,double)),0,0);
  168. disconnect(ui->drawBox,SIGNAL(releaseAt(double,double)),0,0);
  169. }