PageRenderTime 38ms CodeModel.GetById 14ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

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