X-Git-Url: http://git.maemo.org/git/?p=speedfreak;a=blobdiff_plain;f=Client%2Froutedialog.cpp;h=53efe88684db92f54379abae8f4bb9b46266a4cb;hp=ff55de27ada8ffb497cc8a4051222af65c2ae012;hb=16f76b90231274365c45d9efa6d662a819ac525d;hpb=1e6de127ae8f82b42b9745732877eb94a940efbf diff --git a/Client/routedialog.cpp b/Client/routedialog.cpp index ff55de2..53efe88 100644 --- a/Client/routedialog.cpp +++ b/Client/routedialog.cpp @@ -1,11 +1,13 @@ /* * RouteDialog class * - * @author Olavi Pulkkinen - * @copyright (c) 2010 Speed Freak team - * @license http://opensource.org/licenses/gpl-license.php GNU Public License + * @author Olavi Pulkkinen + * @author Toni Jussila + * @copyright (c) 2010 Speed Freak team + * @license http://opensource.org/licenses/gpl-license.php GNU Public License */ +#include "routesavedialog.h" #include "routedialog.h" #include "ui_routedialog.h" #include "usersettings.h" @@ -16,8 +18,9 @@ #include #include #include +#include -/* +/** * Vector class. * In starting Qt 4.6 there is QVector3D. * Later (updating Qt version) this class can be removed. @@ -62,7 +65,9 @@ public: }; }; - +/** + * Viewing class. + */ class Viewing { Vector atPoint, fromPoint, up, a1, a2, a3; @@ -90,7 +95,8 @@ public: void setEye() { double amarkmag, tempmag; - Vector temp, dist; + Vector temp; + Vector dist; dval = cos(angle/2.0)/sin(angle/2.0); dist = atPoint-fromPoint; @@ -140,7 +146,8 @@ void setFromPoint( Viewing *v); void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2 ); #define R 6378.140 // The radius of the earth by kilometers -/* + +/** * count distance of two points (defined by longitude & latitude) * on the surface of the earth. */ @@ -163,33 +170,51 @@ qreal countDistance(Vector *p1, Vector *p2) return R*c; // Return distance in kilometers } -RouteDialog::RouteDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::RouteDialog) +/** + * Constructor of this class. + */ +RouteDialog::RouteDialog(RouteSaveDialog *parent) : + QDialog(parent), ui(new Ui::RouteDialog) { + qDebug() << "__RouteDialog"; ui->setupUi(this); + this->setWindowTitle("Route"); left = 5; top = 5; right = 495; bottom = 295; // Limits in screen coordinates - //Button settings + helpRoutingDialog = NULL; + + // Button settings ui->sendPushButton->setAutoFillBackground(true); ui->sendPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)"); - ui->newPushButton->setAutoFillBackground(true); - ui->newPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)"); - // Send rout to server button disable/enable. - ui->sendPushButton->setEnabled(false); - if (loginSaved()) - { - ui->sendPushButton->setEnabled(true); - } + // Clear labels + ui->labelInfoToUser->setText(""); + ui->speedValueLabel->setText(""); + ui->avgSpeedValueLabel->setText(""); + + // Check login + checkLogin(); + + // Set average speed + QString average; + ui->avgSpeedValueLabel->setText(average.sprintf("%.1f", parent->getAverageSpeed()) + " km/h"); + ui->distanceValueLabel->setText(parent->getDistanceTraveled() + " km"); } +/** + * Destructor of this class. + */ RouteDialog::~RouteDialog() { - delete ui; + qDebug() << "__~RouteDialog"; + if(ui) + delete ui; } +/** + * + */ void RouteDialog::changeEvent(QEvent *e) { QDialog::changeEvent(e); @@ -201,31 +226,58 @@ void RouteDialog::changeEvent(QEvent *e) break; } } + +/** + * Get left. + * + * @return int left + */ int RouteDialog::getLeft() { return left; } + +/** + * Get right. + * + * @return int right + */ int RouteDialog::getRight() { return right; } + +/** + * Get top. + * + * @return int top + */ int RouteDialog::getTop() { return top; } + +/** + * Get bottom. + * + * @return int bottom + */ int RouteDialog::getBottom() { return bottom; } -void drawFlag( RouteDialog *rD, QPainter *p, int x, int y) +/** + * draw flag + * + * @param RouteDialog* + * @param QPainter* + * @param int x + * @param int y + * @param QString startFinish + */ +void drawFlag( RouteDialog *rD, QPainter *p, int x, int y, QString startFinish) { - /*QPolygon pg; - - pg.setPoint(0,x, y-25); - pg.setPoint(1,x+10,y-20); - pg.setPoint(2,x, y-15); - pg.setPoint(3,x,y-20);*/ if (y> (rD->getTop() + 25)) { // Upside @@ -236,6 +288,9 @@ void drawFlag( RouteDialog *rD, QPainter *p, int x, int y) p->drawLine( x, y-25, x+10, y-20); p->drawLine( x+10, y-20, x, y-15); p->drawLine( x, y-15, x, y-25); + + // Draw start or finish + p->drawText(x+10, y, startFinish); } else { @@ -243,8 +298,10 @@ void drawFlag( RouteDialog *rD, QPainter *p, int x, int y) p->drawLine( x, y-25, x-10, y-20); p->drawLine( x-10, y-20, x, y-15); p->drawLine( x, y-15, x, y-25); - } + // Draw start or finish + p->drawText(x+10, y, startFinish); + } } else if (y <= (rD->getTop() + 25)) { @@ -256,6 +313,9 @@ void drawFlag( RouteDialog *rD, QPainter *p, int x, int y) p->drawLine( x, y+25, x+10, y+20); p->drawLine( x+10, y+20, x, y+15); p->drawLine( x, y+15, x, y+25); + + // Draw start or finish + p->drawText(x+10, y+15, startFinish); } else { @@ -263,27 +323,35 @@ void drawFlag( RouteDialog *rD, QPainter *p, int x, int y) p->drawLine( x, y+25, x-10, y+20); p->drawLine( x-10, y+20, x, y+15); p->drawLine( x, y+15, x, y+25); + + // Draw start or finish + p->drawText(x+10, y+15, startFinish); } } - //p->drawPolygon(); - // p->drawPolygon( pg,Qt::OddEvenFill); - //p->drawPolyline( &pg); - //p->drawPoints( pg); } /** * Draws route to the route dialog. * Type 0 is 2d viewing and type 1 is for 3d viewing - * @param QPaintEvent + * + * @param QPaintEvent* */ /* */ void RouteDialog::paintEvent(QPaintEvent *) { + // Check login + checkLogin(); + int type = 0; // 0 for 2d, 1 for 3d - int startx, starty; // Starting point of the route - int i, maxi; + int startx = 0; // Starting x point of the route + int starty = 0; // Starting y point of the route + int i = 0; + int maxi = 0; qreal x1, y1, x2, y2; - int x1Screen, y1Screen, x2Screen, y2Screen; + int x1Screen = 0; + int y1Screen = 0; + int x2Screen = 0; + int y2Screen = 0; Vector v1, v2; QPainter painter(this); @@ -291,12 +359,6 @@ void RouteDialog::paintEvent(QPaintEvent *) painter.setPen(QPen((Qt::white),2)); painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern)); - // Draw route window frame - /*painter.drawLine(left,top,right,top); - painter.drawLine(right,top,right,bottom); - painter.drawLine(left,top,left,bottom); - painter.drawLine(left,bottom,right,bottom);*/ - maxi = vertexList.size(); for (i=0; imaxSpeedLabel->geometry().x()-15, ui->maxSpeedLabel->geometry().y()+15, 10, 10); QString jono; - jono = QString("%1 m/s").arg(maxv); + //jono = QString("%1 km/h").arg(maxv); + jono.sprintf("%.1f km/h", maxv); // Show only 1 decimal ui->speedValueLabel->setText(jono); } } -bool RouteDialog::readRouteFromFile( QString &routeFile) - { +/** + * read route from file + * + * @param QString route file + */ +bool RouteDialog::readRouteFromFile( QString &routeFile ) +{ + QString rFile = routeFile; + fileName = routeFile; Vector temp; QString rivi; QFile file; + int progressbarInt = 5; + int count; - QString fileName = QFileDialog::getOpenFileName(this, - tr("Read Route"), "./", tr("Route Files (*.txt)")); - - file.setFileName( fileName); + file.setFileName( rFile); if (!file.open(QIODevice::ReadOnly)) { - QMessageBox::about(0, "Error", "File not found"); + QMessageBox::about(0, "Error", "Empty file"); return false; } - + emit progressbar(progressbarInt); vertexList.clear(); + while(!file.atEnd()) { + bool allRead; + QString astr1, astr2, astr3, astr4; QString str1, str2, str3, str4; rivi = file.readLine(); - - str1 = rivi.section(" ", 0, 0); - if (str1.compare("Start:") != 0 && str1.compare("Stop:") != 0) + allRead = false; + count = 0; + while( !allRead) { - str1 = rivi.section(" ", 2, 2); // latitude y-value - str2 = rivi.section(" ", 4, 4); // longitude x-value - str3 = rivi.section(" ", 6, 6); // altitude z-value - str4 = rivi.section(" ", 8, 8); // speed m/s - //QString str = QString("la: %1 lo: %2 al: %3").arg(str1).arg(str2).arg(str3); - //QMessageBox::about(0, "LUKEE", str); - - if (str1.length() > 0) + astr1 = rivi.section(" ", count*4+1, count*4+1); // latitude="" + astr2 = rivi.section(" ", count*4+2, count*4+2); // longitude="" + astr3 = rivi.section(" ", count*4+3, count*4+3); // altitude="" + astr4 = rivi.section(" ", count*4+4, count*4+4); // speed="" + { double x, y, z, v; - x = str2.toDouble(); - y = str1.toDouble(); - z = str3.toDouble(); - v = str4.toDouble(); - temp.setX( x); // Longitude - temp.setY( y); // Latitude - temp.setZ( z); // altitude - temp.setV( v); - - vertexList.append(temp); + str1 = astr1.section('"',1,1); + str2 = astr2.section('"',1,1); + str3 = astr3.section('"',1,1); + str4 = astr4.section('"',1,1); + + if (str1.length() > 0) + { + x = str2.toDouble();// latitude y-value + y = str1.toDouble();// longitude x-value + z = str3.toDouble();// altitude z-value + v = str4.toDouble();// speed km/h + temp.setX( x); // Longitude + temp.setY( y); // Latitude + temp.setZ( z); // altitude + temp.setV( v); + + vertexList.append(temp); + count++; + } + else + { + allRead = true; + } } + if(progressbarInt < 50) + progressbarInt++; + emit progressbar(progressbarInt); } } - file.close(); - /******** in 3d use only */ - a = 400/2.; - b = 1 - a*(-1); - c = -300/2.; - d = 300 - c*(-1); - //angle = toradians(60); - - view3d.setUp( 1.0, 0.0, 0.0); - view3d.setAngle(toradians(60)); - setAtPoint( &view3d); - xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!! - setFromPoint( &view3d); - view3d.setEye(); - /****** end of 3d *****/ - - /* - //Testing distance counting - Vector a1, a2; - qreal dist; - //a1.setX( xmin); a1.setY( ymin); - //a2.setX( xmax); a2.setY( ymax); - a1.setX( 25.483); a1.setY( 65.017); // Oulu - a2.setX( 27.767); a2.setY( 64.283); // Kajaani - dist = countDistance( &a1, &a2); - QString str = QString("Min & Max datan välimatka %1").arg(dist); - QMessageBox::about( 0, "Testi", str); - */ - - return true; - } + if(count == 1) // Check is file empty. + { + progressbarInt = 50; + emit progressbar(progressbarInt); + qDebug() << "count: " + QString::number(count); + /******** in 3d use only */ + a = 400/2.; + b = 1 - a*(-1); + c = -300/2.; + d = 300 - c*(-1); + emit progressbar(60); + //angle = toradians(60); + view3d.setUp( 1.0, 0.0, 0.0); + emit progressbar(70); + view3d.setAngle(toradians(60)); + emit progressbar(80); + setAtPoint( &view3d); // If file is empty software crash here. + emit progressbar(90); + xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!! + emit progressbar(95); + setFromPoint( &view3d); + emit progressbar(98); + view3d.setEye(); + emit progressbar(99); + /****** end of 3d *****/ + /* + //Testing distance counting + Vector a1, a2; + qreal dist; + //a1.setX( xmin); a1.setY( ymin); + //a2.setX( xmax); a2.setY( ymax); + a1.setX( 25.483); a1.setY( 65.017); // Oulu + a2.setX( 27.767); a2.setY( 64.283); // Kajaani + dist = countDistance( &a1, &a2); + QString str = QString("Min & Max datan välimatka %1").arg(dist); + QMessageBox::about( 0, "Testi", str); + */ + emit progressbar(100); + return true; + } + else // Probably file is empty. + { + QMessageBox::about(0, "Error", "Empty file"); + return false; + } +} -/* +/** * Find out data range for x-, y- and z-coordinates */ void dataMinMax( void) @@ -504,7 +606,7 @@ void dataMinMax( void) //QMessageBox::about(0,"Tark", jono); } -/* +/** * Setting the point where the viewed object is. In the middle of datapoints. */ void setAtPoint( Viewing *v) @@ -528,7 +630,7 @@ void setAtPoint( Viewing *v) * */ } -/* +/** * Setting the point where the object is viewed by eye. */ void setFromPoint( Viewing *v) @@ -555,31 +657,39 @@ void setFromPoint( Viewing *v) * */ } - #define NOEDGE 0x00 #define LEFTEDGE 0x01 #define RIGHTEDGE 0x02 #define BOTTOMEDGE 0x04 #define TOPEDGE 0x08 -/* + +/** * Returns a code specifying which edge in the viewing pyramid was crossed. * There may be more than one. + * + * @param qreal x + * @param qreal y + * @param qreal z + * @return int */ int code( qreal x, qreal y, qreal z) { int c; - c = NOEDGE; if (x<-z) c |= LEFTEDGE; if (x>z) c |= RIGHTEDGE; if (y<-z) c |= BOTTOMEDGE; if (y>z) c |= TOPEDGE; - return c; } -/* +/** * Converts clipped world coordinates to screen coordinates. + * + * @param qreal xWorld + * @param qreal yWorld + * @param int *xScreen + * @param int *yScreen */ void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen) { @@ -587,14 +697,30 @@ void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen) *yScreen = (int) (c*yWorld+d); } -/* +/** * Clips the line segment in three-dimensional coordinates to the * viewing pyramid. + * + * @param qreal x1 + * @param qreal y1 + * @param qreal z1 + * @param qreal x2 + * @param qreal y2 + * @param qreal z2 + * @param int *xscreen1 + * @param int *yscreen1 + * @param int *xscreen2 + * @param int *yscreen2 */ void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2) { - int c,c1,c2; - qreal x,y,z,t; + int c = 0; + int c1 = 0; + int c2 = 0; + qreal x = 0; + qreal y = 0; + qreal z = 0; + qreal t = 0; c1 = code(x1,y1,z1); c2 = code(x2,y2,z2); @@ -662,9 +788,17 @@ void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xs //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2); } -/* +/** * Transform the segment connecting the two vectors into the viewing plane. * clip3d() clips the line if needed. + * + * @param Viewing *v + * @param Vector *v1 + * @param Vector *v2 + * @param int *xscreen1 + * @param int *yscreen1 + * @param int *xscreen2 + * @param int *yscreen2 */ void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2) @@ -687,12 +821,101 @@ void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscre clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 ); } -void RouteDialog::on_newPushButton_clicked() +/** + * This slot function is called when ever send push button clicked. + */ +void RouteDialog::on_sendPushButton_clicked() { - close(); // go back to previous dialog + ui->sendPushButton->setEnabled(false); + + // Check default file dir. + if(!QDir("/home/user/MyDocs/speedfreak/route").exists()) + { + QDir().mkdir("/home/user/MyDocs/speedfreak/route"); + } + + // If file name is empty set it to default. + if(fileName == "") + fileName = "/home/user/MyDocs/speedfreak/route/"; + + if(fileName == "/home/user/MyDocs/speedfreak/route/routetemp.xml") + fileName = "/home/user/MyDocs/speedfreak/route/route.xml"; + + qDebug() << "__FileName: " + fileName; + QString newFileName = QFileDialog::getSaveFileName(this, tr("Save Route"), fileName); + int server = QMessageBox::question(this, "Save route to server?", "", 4,3); + + if(server == 3) // Yes button + { + qDebug() << "__Save to server: yes"; + emit sendroute(newFileName,1); // Save route to server and phone. + } + else if(server == 4) // No button + { + qDebug() << "__Save to server: no"; + + if(fileName != "") + emit sendroute(newFileName,0); // Save route to phone. + } } -void RouteDialog::on_sendPushButton_clicked() +/** + * This function is set info text to user. + * + * @param QString info text + */ +void RouteDialog::setLabelInfoToUser(QString infoText) +{ + this->ui->labelInfoToUser->setText(infoText); +} + +/** + * This function enable send server button. + */ +void RouteDialog::setSendServerButtonEnabled() +{ + ui->sendPushButton->setEnabled(true); +} + +/** + * This function check login and set send route to server button disabled/enabled. + */ +void RouteDialog::checkLogin() { - // Send route points file to server + if (loginSaved()) + { + ui->sendPushButton->setEnabled(true); + ui->labelInfoToUser->setText(""); + } + else + { + ui->sendPushButton->setEnabled(false); + ui->labelInfoToUser->setText("You're not logged! Please register or log in."); + } +} + +/** + * This slot function called when ever info button clicked. + */ +void RouteDialog::on_pushButtonInfo_clicked() +{ + if(!helpRoutingDialog) + { + helpRoutingDialog = new HelpRoutingDialog; + } + connect(helpRoutingDialog, SIGNAL(rejected()), this, SLOT(killHelpDialog())); + helpRoutingDialog->show(); +} + +/** + * This slot function called when ever dialog rejected. + */ +void RouteDialog::killHelpDialog() +{ + if(helpRoutingDialog) + { + qDebug() << "__Route kill: helpRoutingDialog"; + delete helpRoutingDialog; + helpRoutingDialog = NULL; + } }