added all files
[ffqwlibrary] / libffqw-demo-1.0 / sources / mainwindow.cpp
1 /*
2           GNU GENERAL PUBLIC LICENSE
3                        Version 3, 29 June 2007
4
5  Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
6  Everyone is permitted to copy and distribute verbatim copies
7  of this license document, but changing it is not allowed.
8
9                             Preamble
10
11   The GNU General Public License is a free, copyleft license for
12 software and other kinds of works.
13
14   The licenses for most software and other practical works are designed
15 to take away your freedom to share and change the works.  By contrast,
16 the GNU General Public License is intended to guarantee your freedom to
17 share and change all versions of a program--to make sure it remains free
18 software for all its users.  We, the Free Software Foundation, use the
19 GNU General Public License for most of our software; it applies also to
20 any other work released this way by its authors.  You can apply it to
21 your programs, too.
22
23   When we speak of free software, we are referring to freedom, not
24 price.  Our General Public Licenses are designed to make sure that you
25 have the freedom to distribute copies of free software (and charge for
26 them if you wish), that you receive source code or can get it if you
27 want it, that you can change the software or use pieces of it in new
28 free programs, and that you know you can do these things.
29
30   To protect your rights, we need to prevent others from denying you
31 these rights or asking you to surrender the rights.  Therefore, you have
32 certain responsibilities if you distribute copies of the software, or if
33 you modify it: responsibilities to respect the freedom of others.
34
35   For example, if you distribute copies of such a program, whether
36 gratis or for a fee, you must pass on to the recipients the same
37 freedoms that you received.  You must make sure that they, too, receive
38 or can get the source code.  And you must show them these terms so they
39 know their rights.
40
41   Developers that use the GNU GPL protect your rights with two steps:
42 (1) assert copyright on the software, and (2) offer you this License
43 giving you legal permission to copy, distribute and/or modify it.
44
45   For the developers' and authors' protection, the GPL clearly explains
46 that there is no warranty for this free software.  For both users' and
47 authors' sake, the GPL requires that modified versions be marked as
48 changed, so that their problems will not be attributed erroneously to
49 authors of previous versions.
50
51   Some devices are designed to deny users access to install or run
52 modified versions of the software inside them, although the manufacturer
53 can do so.  This is fundamentally incompatible with the aim of
54 protecting users' freedom to change the software.  The systematic
55 pattern of such abuse occurs in the area of products for individuals to
56 use, which is precisely where it is most unacceptable.  Therefore, we
57 have designed this version of the GPL to prohibit the practice for those
58 products.  If such problems arise substantially in other domains, we
59 stand ready to extend this provision to those domains in future versions
60 of the GPL, as needed to protect the freedom of users.
61
62   Finally, every program is threatened constantly by software patents.
63 States should not allow patents to restrict development and use of
64 software on general-purpose computers, but in those that do, we wish to
65 avoid the special danger that patents applied to a free program could
66 make it effectively proprietary.  To prevent this, the GPL assures that
67 patents cannot be used to render the program non-free.
68
69   The precise terms and conditions for copying, distribution and
70 modification follow.
71
72 http://www.gnu.org/licenses/gpl-3.0.txt
73 */
74 /**
75  * @file mainwindow.cpp
76  * @brief Implementation of mainwindow class.
77  *
78  * @author ComArch S.A.
79  * @date 2009.09.03
80  * @version 1.0
81  */
82
83 #include "mainwindow.h"
84
85
86 /**
87  *Function to generate points used in the library demonstration graph.
88  */
89 float fun01(float x)
90 {
91         return (sin(x/5)*10 + qAbs(x)) - 50;
92 }
93
94 /**
95  *Function to generate points used in the library demonstration graph.
96  */
97 float fun02(float x)
98 {
99         if(x==0)
100                 x=0.000001;
101         return sin(x/4)/ (x/100);
102 }
103
104 /**
105  *Function to generate points used in the library demonstration graph.
106  */
107 float fun03(float x)
108 {
109         return (x*x)/300;
110 }
111
112 /**
113  *Function to generate points used in the library demonstration graph.
114  */
115 float fun04(float x)
116 {
117         return (x*x*x)/1000;
118 }
119
120 /**
121  *Function to generate points used in the library demonstration graph.
122  */
123 float fun05(float x)
124 {
125         return x;
126 }
127
128 /**
129  *Function to generate points used in the library demonstration graph.
130  */
131 float fun06(float x)
132 {
133         if(x<-50)
134                 return -50;
135         else if(x>=-50 && x<=50)
136                 return x - 20;
137         else if(x>50)
138                 return 50;
139 }
140
141 /**
142  *Main function to show FFQW library features.
143  */
144 MainWindow::MainWindow(QWidget *parent) :
145         QMainWindow(parent)
146 {
147         QWidget* centralWidget = new QWidget;
148         setCentralWidget(centralWidget);
149         resize(800, 480);
150
151         chartTab = new QWidget;
152
153
154         chart = new FFChart;
155
156         functions = new FFStringComboBox;
157         combo = new FFColorComboBox;
158         line = new FFLineComboBox;
159
160         QVBoxLayout* chartLayout = new QVBoxLayout;
161         chartLayout->addWidget(chart);
162         chartLayout->addWidget(functions);
163
164         centralWidget->setLayout(chartLayout);
165
166         fun.append("Set 01 - sinusoids");
167         fun.append("Set 02 - parabolic");
168         fun.append("Set 03 - linears");
169         functions->addItems(fun);
170         functions->setTitle("Choose a functions set");
171
172         setFocusPolicy(Qt::ClickFocus);
173         chart->setFocusPolicy(Qt::ClickFocus);
174         init();
175
176         chart->installEventFilter(this);
177
178         leftKeySw = false;
179         rightKeySw = false;
180         upKeySw = false;
181         downKeySw = false;
182
183         connect(functions,SIGNAL(activated(int)),this,SLOT(changeFunctions(int)));
184         functions->setCurrentItem(0);
185 }
186
187 MainWindow::~MainWindow()
188 {
189
190 }
191
192 /**
193  * Initiate an object of demo application. Set all needed fields.
194  */
195 void MainWindow::init()
196 {
197         setStyleSheet(  "background: black;"
198                         "padding: 0px;"
199                         "spacing: 0px;"
200                         "border: 0px;");
201
202         chartTab->setStyleSheet(  "background: black;"
203                                "padding: 0px;"
204                                "spacing: 0px;"
205                                "border: 0px;");
206         {
207         // Sinusoids
208         for(float i=-150;i<150;i+=1)
209         {
210                 series01.append(QPointF(i,fun01(i)));
211         }
212
213         for(float i=-150;i<150;i+=1)
214         {
215                 series02.append(QPointF(i,fun02(i)));
216         }
217
218         // Linears
219         for(float i = -250; i < 250; i += 1)
220         {
221                 series03.append(QPointF(i, fun03(i)));
222         }
223
224         for(float i = -150; i < 150; i += 1)
225         {
226                 series04.append(QPointF(i, fun04(i)));
227         }
228
229         // Linears
230         series05.append(QPointF(-150,-150));
231         series05.append(QPointF(150,150));
232
233         series06.append(QPointF(-150,-150));
234         series06.append(QPointF(-50,-150));
235         series06.append(QPointF(50,150));
236         series06.append(QPointF(150,150));
237
238         series07.append(QPointF(-200,20));
239         series07.append(QPointF(-80,20));
240         series07.append(QPointF(-70,-80));
241         series07.append(QPointF(-60,120));
242         series07.append(QPointF(-50,20));
243         series07.append(QPointF( 70,20));
244         series07.append(QPointF( 80,-60));
245         series07.append(QPointF( 90,110));
246         series07.append(QPointF(100,20));
247         series07.append(QPointF(200,20));
248
249         series08.append(QPointF(-200,-200));
250         series08.append(QPointF( 200,-200));
251
252         series09.append(QPointF(-200,-180));
253         series09.append(QPointF(200,-180));
254
255         series10.append(QPointF(-200,-160));
256         series10.append(QPointF(200,-160));
257
258         series11.append(QPointF(-200,-140));
259         series11.append(QPointF(200,-140));
260         }
261         changeFunctions(1);
262 }
263
264 /**
265  * Change set of function to display in gprah to another.
266  */
267  void MainWindow::changeFunctions(int funNum)
268 {
269         if(funNum == 0)
270         {
271                 chart->removeSeries();
272                 chart->addSeries(new FFChartSeries(series01,QPen(QBrush(QColor(255,255,0)),2),"SRP lev 2008"));
273                 chart->addSeries(new FFChartSeries(series02,QPen(QBrush(QColor(255,0,255)),2),"SRP standardized lev 2008"));
274         }
275         else if(funNum == 1)
276         {
277                 chart->removeSeries();
278                 chart->addSeries(new FFChartSeries(series03,QPen(QBrush(QColor(0,0,255)),2),"Swimming Performance"));
279                 chart->addSeries(new FFChartSeries(series04,QPen(QBrush(QColor(0,255,0)),2),"Running Performance"));
280         }
281         else if(funNum == 2)
282         {
283                 chart->removeSeries();
284                 chart->addSeries(new FFChartSeries(series05,QPen(QBrush(QColor(  0,255,  0)),2),"FFQW Progress"));
285                 chart->addSeries(new FFChartSeries(series06,QPen(QBrush(QColor(255,  0,  0)),2),"Standardized Reverse Progress"));
286                 chart->addSeries(new FFChartSeries(series07,QPen(QBrush(QColor(255,255,  0)),2),"Standardized Tech Progress"));
287                 chart->addSeries(new FFChartSeries(series08,QPen(QBrush(QColor(255,255,255)),2),"EKG"));
288                 chart->addSeries(new FFChartSeries(series09,QPen(QBrush(QColor(  0,255,255)),2),"PKB Lev"));
289                 chart->addSeries(new FFChartSeries(series10,QPen(QBrush(QColor(  0,  0,255)),2),"YETI Lev"));
290                 chart->addSeries(new FFChartSeries(series11,QPen(QBrush(QColor(150,150,150)),2),"Mot Ratio"));
291         }
292 }
293
294 /**
295  * Support key press events in demo application.
296  */
297  void MainWindow::keyPressEvent(QKeyEvent* event)
298  {
299         switch ( event->key () )
300         {
301                 case Qt::Key_F5:        //HOME
302                         break;
303                 case Qt::Key_F6:        //FULL_SCREEN
304                         break;
305                 case Qt::Key_F7:        //+
306                         chart->zoomIn(0.2);
307                         break;
308                 case Qt::Key_F8:        //-
309                         chart->zoomOut(0.2);
310                         break;
311                 case Qt::Key_F4:        //MENU
312                         break;
313                 case Qt::Key_Left:      //LEFT
314                         leftKeySw=true;
315                         if(upKeySw)
316                         {
317                                 chart->moveBy(QPoint(10,10));
318                                 break;
319                         }
320                         if(downKeySw)
321                         {
322                                 chart->moveBy(QPoint(10,-10));
323                                 break;
324                         }
325                         chart->moveBy(QPoint(10,0));
326                         break;
327                 case Qt::Key_Right:     //RIGHT
328                         rightKeySw=true;
329                         if(upKeySw)
330                         {
331                                 chart->moveBy(QPoint(-10, 10));
332                                 break;
333                         }
334                         if(downKeySw)
335                         {
336                                 chart->moveBy(QPoint(-10, -10));
337                                 break;
338                         }
339                         chart->moveBy(QPoint(- 10,0));
340                         break;
341                 case Qt::Key_Up:        //UP
342                         upKeySw=true;
343                         if(leftKeySw)
344                         {
345                                 chart->moveBy(QPoint(10, 10));
346                                 break;
347                         }
348                         if(rightKeySw)
349                         {
350                                 chart->moveBy(QPoint(-10, 10));
351                                 break;
352                         }
353                         chart->moveBy(QPoint(0,10));
354                         break;
355                 case Qt::Key_Down:      //DOWN
356                         downKeySw=true;
357                         if(leftKeySw)
358                         {
359                                 chart->moveBy(QPoint(10, -10));
360                                 break;
361                         }
362                         if(rightKeySw)
363                         {
364                                 chart->moveBy(QPoint(-10, -10));
365                                 break;
366                         }
367                         chart->moveBy(QPoint(0,-10));
368                         break;
369                 case Qt::Key_Return:    //ENTER
370                         break;
371                 case Qt::Key_Escape:    //BACK
372                         break;
373                 default:
374                         event->ignore ();
375         }
376  }
377
378 /**
379  * Support key release events in demo application.
380  */
381  void MainWindow::keyReleaseEvent(QKeyEvent* event)
382  {
383         switch(event->key())
384         {
385                 case Qt::Key_Left: //LEFT
386                         leftKeySw = false;
387                         break;
388                 case Qt::Key_Right: //RIGHT
389                         rightKeySw = false;
390                         break;
391                 case Qt::Key_Up: //UP
392                         upKeySw = false;
393                         break;
394                 case Qt::Key_Down: //DOWN
395                         downKeySw = false;
396                         break;
397                 default:
398                         event->ignore();
399         }
400  }
401
402 /**
403  * Reimplement standard eventFilter.
404  */
405 bool MainWindow::eventFilter(QObject *object, QEvent *event)
406 {
407         Q_UNUSED(object);
408         if(event->type() == QEvent::KeyPress)
409         {
410                 QKeyEvent *keyEvent = static_cast<QKeyEvent*> (event);
411                 keyPressEvent(keyEvent);
412                 return true;
413         }
414         else
415         {
416                 return false;
417         }
418 }