Added route drawing to route dialog
[speedfreak] / Client / routedialog.cpp
1 /*
2  * RouteDialog class
3  *
4  * @author     Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
5  * @copyright  (c) 2010 Speed Freak team
6  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
7  */
8
9 #include "routedialog.h"
10 #include "ui_routedialog.h"
11 #include <QPainter>
12 #include <QList>
13 #include <QVector3D>
14
15 int left = 50, top = 50, right = 350, bottom = 200;  // Limits in screen coordinates
16 qreal xmax, xmin, ymin, ymax;                        // Limits in world coordinates
17
18 QList<QVector3D> vertexList;    // Vertecies of route
19
20 RouteDialog::RouteDialog(QWidget *parent) :
21     QDialog(parent),
22     ui(new Ui::RouteDialog)
23 {
24     ui->setupUi(this);
25     xmin =0.0; xmax=100.0; ymin = 0.0; ymax = 20.0;
26
27    vertexList.append(QVector3D(40.02, 10.02, 10.02));
28    vertexList.append(QVector3D(50.01,  5.01, 10));
29    vertexList.append(QVector3D(69.98,  4.98, 10));
30    vertexList.append(QVector3D(80.02,  9.98, 10));
31    vertexList.append(QVector3D(70.01, 15.01, 10));
32    vertexList.append(QVector3D(49.99, 14.97, 10));
33    vertexList.append(QVector3D(40.01, 10.01, 10.02));
34 }
35
36 RouteDialog::~RouteDialog()
37 {
38     delete ui;
39 }
40
41 void RouteDialog::changeEvent(QEvent *e)
42 {
43     QDialog::changeEvent(e);
44     switch (e->type()) {
45     case QEvent::LanguageChange:
46         ui->retranslateUi(this);
47         break;
48     default:
49         break;
50     }
51 }
52
53 /**
54   * Draws route to the route dialog
55   * @param QPaintEvent
56  */
57 void RouteDialog::paintEvent(QPaintEvent *)
58 {
59     int i, maxi;
60     qreal x1, y1, x2, y2;
61     int scx1, scy1, scx2, scy2;
62
63         QPainter painter(this);
64
65         painter.setRenderHint(QPainter::Antialiasing, true);
66         painter.setPen(QPen((Qt::black),2));
67         painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
68
69         // Draw route window frsme
70         painter.drawLine(left,top,right,top);
71         painter.drawLine(right,top,right,bottom);
72         painter.drawLine(left,top,left,bottom);
73         painter.drawLine(left,bottom,right,bottom);
74
75         maxi = vertexList.size();
76         for (i=0; i<maxi-1; i++)
77         {
78             x1 = vertexList.at(i).x();
79             y1 = vertexList.at(i).y();
80             x2 = vertexList.at(i+1).x();
81             y2 = vertexList.at(i+1).y();
82
83             scx1 = left + (x1-xmin)/(xmax-xmin)*(right-left);
84             scy1 = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
85             scx2 = left + (x2-xmin)/(xmax-xmin)*(right-left);
86             scy2 = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
87
88             painter.drawLine( scx1, scy1, scx2, scy2);
89         }
90 }
91
92 void RouteDialog::on_closePushButton_clicked()
93 {
94     close();
95 }