Files @ a3a98f85e1c2
Branch filter:

Location: tempo-plotter/mainwindow.cpp - annotation

Hasan Yavuz ÖZDERYA
scroll text view to end
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
ce43e0348d45
ce43e0348d45
ce43e0348d45
ce43e0348d45
ce43e0348d45
ce43e0348d45
ce43e0348d45
ce43e0348d45
ce43e0348d45
ce43e0348d45
ce43e0348d45
ce43e0348d45
ce43e0348d45
ce43e0348d45
ce43e0348d45
ce43e0348d45
ce43e0348d45
ce43e0348d45
ce43e0348d45
58db5f6bf2b1
58db5f6bf2b1
508c8437347e
cb28b6958b0e
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
58db5f6bf2b1
8cac6c6afb36
8a94e68f9083
a3a98f85e1c2
90fb38350cfd
90fb38350cfd
70f83253cf6c
f918be7cd46f
f918be7cd46f
58db5f6bf2b1
6a726832c333
8cac6c6afb36
58db5f6bf2b1
5ea7c2e04353
5ea7c2e04353
5ea7c2e04353
5ea7c2e04353
5ea7c2e04353
58db5f6bf2b1
58db5f6bf2b1
6a311d722818
6a311d722818
58db5f6bf2b1
58db5f6bf2b1
6a311d722818
6a311d722818
6a311d722818
cb28b6958b0e
58db5f6bf2b1
58db5f6bf2b1
cb28b6958b0e
cb28b6958b0e
cb28b6958b0e
cb28b6958b0e
cb28b6958b0e
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
882842ce0980
882842ce0980
882842ce0980
6a311d722818
58db5f6bf2b1
58db5f6bf2b1
6a311d722818
6a311d722818
58db5f6bf2b1
a6e52579723b
a6e52579723b
a6e52579723b
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
44b87d87610b
44b87d87610b
44b87d87610b
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
a6f1f85f1eba
508c8437347e
649401566a84
649401566a84
649401566a84
649401566a84
641f94e963c4
641f94e963c4
b12c2ff3d038
b12c2ff3d038
90fb38350cfd
655c440be89b
90fb38350cfd
90fb38350cfd
655c440be89b
90fb38350cfd
90fb38350cfd
8932fd91959c
8932fd91959c
a6e52579723b
8932fd91959c
8932fd91959c
641f94e963c4
641f94e963c4
641f94e963c4
641f94e963c4
641f94e963c4
8932fd91959c
8932fd91959c
be167cc7f2fb
8932fd91959c
8932fd91959c
8932fd91959c
8932fd91959c
70f83253cf6c
8932fd91959c
8932fd91959c
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
abcdecae08b4
75952bda0a0e
75952bda0a0e
837bd67c8021
837bd67c8021
75952bda0a0e
75952bda0a0e
c27ec79b7b2c
c27ec79b7b2c
75952bda0a0e
8fd74552f317
8fd74552f317
8fd74552f317
4c9ac8ac3428
8fd74552f317
8fd74552f317
8fd74552f317
8fd74552f317
8fd74552f317
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
d4f23bf96cf5
d4f23bf96cf5
362c6d844fe3
362c6d844fe3
362c6d844fe3
362c6d844fe3
d4f23bf96cf5
d4f23bf96cf5
d4f23bf96cf5
d4f23bf96cf5
d4f23bf96cf5
8fd74552f317
58db5f6bf2b1
58db5f6bf2b1
58db5f6bf2b1
58db5f6bf2b1
8932fd91959c
8932fd91959c
8932fd91959c
8932fd91959c
8932fd91959c
fa6975789e02
fa6975789e02
fa6975789e02
fa6975789e02
58db5f6bf2b1
58db5f6bf2b1
58db5f6bf2b1
cb28b6958b0e
cb28b6958b0e
cb28b6958b0e
cb28b6958b0e
cb28b6958b0e
cb28b6958b0e
cb28b6958b0e
6a726832c333
6a726832c333
6a726832c333
e931abef3465
6a726832c333
cb28b6958b0e
cb28b6958b0e
58db5f6bf2b1
58db5f6bf2b1
abcdecae08b4
abcdecae08b4
abcdecae08b4
58db5f6bf2b1
58db5f6bf2b1
58db5f6bf2b1
58db5f6bf2b1
c6033ffac487
70f95e53d915
8932fd91959c
8932fd91959c
649401566a84
bcd6b30d01d1
bcd6b30d01d1
bcd6b30d01d1
bcd6b30d01d1
bcd6b30d01d1
bcd6b30d01d1
bcd6b30d01d1
bcd6b30d01d1
8932fd91959c
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
8932fd91959c
8932fd91959c
8932fd91959c
be167cc7f2fb
8932fd91959c
8932fd91959c
8932fd91959c
8932fd91959c
649401566a84
8932fd91959c
649401566a84
be167cc7f2fb
8932fd91959c
8932fd91959c
8932fd91959c
649401566a84
649401566a84
8932fd91959c
be167cc7f2fb
8932fd91959c
8932fd91959c
8932fd91959c
649401566a84
70f95e53d915
73abbe942246
73abbe942246
73abbe942246
73abbe942246
58db5f6bf2b1
f38042ba2eb0
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
aa93a4eacbb9
aa93a4eacbb9
aa93a4eacbb9
aa93a4eacbb9
aa93a4eacbb9
aa93a4eacbb9
aa93a4eacbb9
508c8437347e
91761de0142c
91761de0142c
91761de0142c
91761de0142c
91761de0142c
91761de0142c
508c8437347e
508c8437347e
be167cc7f2fb
508c8437347e
be167cc7f2fb
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
130806291698
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
b12c2ff3d038
b12c2ff3d038
797268305b37
797268305b37
797268305b37
797268305b37
797268305b37
da686c361be4
797268305b37
797268305b37
da686c361be4
6a311d722818
797268305b37
6a311d722818
797268305b37
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
da686c361be4
797268305b37
da686c361be4
797268305b37
797268305b37
b12c2ff3d038
b12c2ff3d038
bcd6b30d01d1
bcd6b30d01d1
bcd6b30d01d1
bcd6b30d01d1
bcd6b30d01d1
8932fd91959c
f38042ba2eb0
8932fd91959c
4ff060c076ab
4ff060c076ab
4ff060c076ab
4ff060c076ab
be167cc7f2fb
4ff060c076ab
8932fd91959c
4ff060c076ab
4ff060c076ab
4ff060c076ab
641f94e963c4
8932fd91959c
641f94e963c4
4ff060c076ab
4ff060c076ab
4ff060c076ab
4ff060c076ab
4ff060c076ab
4ff060c076ab
8932fd91959c
4ff060c076ab
4ff060c076ab
4ff060c076ab
4ff060c076ab
8932fd91959c
4ff060c076ab
4ff060c076ab
f38042ba2eb0
641f94e963c4
8932fd91959c
8932fd91959c
8fd74552f317
8fd74552f317
f38042ba2eb0
a6e52579723b
44b87d87610b
44b87d87610b
be167cc7f2fb
8932fd91959c
8932fd91959c
8932fd91959c
8932fd91959c
44b87d87610b
44b87d87610b
44b87d87610b
44b87d87610b
44b87d87610b
a6e52579723b
a6e52579723b
4ea8d600e383
a6e52579723b
a6e52579723b
a6e52579723b
a6e52579723b
a6e52579723b
a6e52579723b
be167cc7f2fb
8932fd91959c
8932fd91959c
8932fd91959c
a6e52579723b
a6e52579723b
a6e52579723b
a6e52579723b
4ea8d600e383
a6e52579723b
a6e52579723b
be167cc7f2fb
8932fd91959c
8932fd91959c
8932fd91959c
a6e52579723b
a6e52579723b
a6e52579723b
90fb38350cfd
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
be167cc7f2fb
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
70f83253cf6c
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
be167cc7f2fb
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
e65811fa0522
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
90fb38350cfd
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
a6f1f85f1eba
a6f1f85f1eba
a6f1f85f1eba
a6f1f85f1eba
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
6a311d722818
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
508c8437347e
6a311d722818
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
649401566a84
8cac6c6afb36
8a94e68f9083
8a94e68f9083
8a94e68f9083
8a94e68f9083
8a94e68f9083
8a94e68f9083
a3a98f85e1c2
a3a98f85e1c2
a3a98f85e1c2
8a94e68f9083
0c1920dac890
0c1920dac890
0c1920dac890
0c1920dac890
0c1920dac890
a3a98f85e1c2
a3a98f85e1c2
a3a98f85e1c2
a3a98f85e1c2
a3a98f85e1c2
a3a98f85e1c2
8a94e68f9083
8a94e68f9083
8cac6c6afb36
8cac6c6afb36
8cac6c6afb36
8cac6c6afb36
8cac6c6afb36
8cac6c6afb36
8cac6c6afb36
8cac6c6afb36
8cac6c6afb36
649401566a84
649401566a84
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
8fd74552f317
8fd74552f317
8fd74552f317
8fd74552f317
8fd74552f317
8fd74552f317
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
0471b369fdb1
abcdecae08b4
0471b369fdb1
0471b369fdb1
0471b369fdb1
0471b369fdb1
0471b369fdb1
0471b369fdb1
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
d4f23bf96cf5
d4f23bf96cf5
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
abcdecae08b4
d4f23bf96cf5
d4f23bf96cf5
abcdecae08b4
abcdecae08b4
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
2ceefb10b1c0
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
70f83253cf6c
2ceefb10b1c0
70f83253cf6c
70f83253cf6c
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
130806291698
4cc4bd170d6a
4cc4bd170d6a
4cc4bd170d6a
f918be7cd46f
f918be7cd46f
f918be7cd46f
f918be7cd46f
f918be7cd46f
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
ecd078ee6c19
f918be7cd46f
/*
  Copyright © 2015 Hasan Yavuz Özderya

  This file is part of serialplot.

  serialplot is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  serialplot is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QByteArray>
#include <QApplication>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QtDebug>
#include <QtEndian>
#include <QTextCursor>
#include <QScrollBar>
#include <qwt_plot.h>
#include <limits.h>
#include <cmath>
#include <iostream>

#include "utils.h"
#include "version.h"
#include "floatswap.h"

#if defined(Q_OS_WIN) && defined(QT_STATIC)
#include <QtPlugin>
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
#endif

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    portControl(&serialPort)
{
    ui->setupUi(this);
    ui->tabWidget->insertTab(0, &portControl, "Port");
    ui->tabWidget->setCurrentIndex(0);

    setupAboutDialog();

    // init UI signals

    // menu signals
    QObject::connect(ui->actionHelpAbout, &QAction::triggered,
              &aboutDialog, &QWidget::show);

    QObject::connect(ui->actionExportCsv, &QAction::triggered,
                     this, &MainWindow::onExportCsv);

    QObject::connect(ui->actionQuit, &QAction::triggered,
                     this, &MainWindow::close);

    QObject::connect(&portControl, &PortControl::portToggled,
                     this, &MainWindow::onPortToggled);

    QObject::connect(&portControl, &PortControl::skipByteRequested,
                     this, &MainWindow::skipByte);

    QObject::connect(ui->spNumOfSamples, SELECT<int>::OVERLOAD_OF(&QSpinBox::valueChanged),
                     this, &MainWindow::onNumOfSamplesChanged);

    QObject::connect(ui->cbAutoScale, &QCheckBox::toggled,
                     this, &MainWindow::onAutoScaleChecked);

    QObject::connect(ui->spYmin, SIGNAL(valueChanged(double)),
                     this, SLOT(onYScaleChanged()));

    QObject::connect(ui->spYmax, SIGNAL(valueChanged(double)),
                     this, SLOT(onYScaleChanged()));

    QObject::connect(ui->actionClear, SIGNAL(triggered(bool)),
                     this, SLOT(clearPlot()));

    // setup number of channels spinbox
    QObject::connect(ui->spNumOfChannels,
                     SELECT<int>::OVERLOAD_OF(&QSpinBox::valueChanged),
                     this, &MainWindow::onNumOfChannelsChanged);

    // setup number format buttons
    numberFormatButtons.addButton(ui->rbUint8,  NumberFormat_uint8);
    numberFormatButtons.addButton(ui->rbUint16, NumberFormat_uint16);
    numberFormatButtons.addButton(ui->rbUint32, NumberFormat_uint32);
    numberFormatButtons.addButton(ui->rbInt8,   NumberFormat_int8);
    numberFormatButtons.addButton(ui->rbInt16,  NumberFormat_int16);
    numberFormatButtons.addButton(ui->rbInt32,  NumberFormat_int32);
    numberFormatButtons.addButton(ui->rbFloat,  NumberFormat_float);
    numberFormatButtons.addButton(ui->rbASCII,  NumberFormat_ASCII);

    QObject::connect(&numberFormatButtons, SIGNAL(buttonToggled(int, bool)),
                     this, SLOT(onNumberFormatButtonToggled(int, bool)));

    // init port signals
    QObject::connect(&(this->serialPort), SIGNAL(error(QSerialPort::SerialPortError)),
                     this, SLOT(onPortError(QSerialPort::SerialPortError)));

    // set limits for axis limit boxes
    ui->spYmin->setRange((-1) * std::numeric_limits<double>::max(),
                         std::numeric_limits<double>::max());

    ui->spYmax->setRange((-1) * std::numeric_limits<double>::max(),
                         std::numeric_limits<double>::max());

    // init data arrays and plot

    numOfSamples = ui->spNumOfSamples->value();
    numOfChannels = 1;

    dataX.resize(numOfSamples);
    for (int i = 0; i < dataX.size(); i++)
    {
        dataX[i] = i;
    }

    // init channel data and curve list
    for (unsigned int i = 0; i < numOfChannels; i++)
    {
        channelsData.append(DataArray(numOfSamples, 0.0));
        curves.append(new QwtPlotCurve());
        curves[i]->setSamples(dataX, channelsData[i]);
        curves[i]->setPen(makeColor(i));
        curves[i]->attach(ui->plot);
    }

    // init number format
    if (numberFormatButtons.checkedId() >= 0)
    {
        selectNumberFormat((NumberFormat) numberFormatButtons.checkedId());
    }
    else
    {
        selectNumberFormat(NumberFormat_uint8);
    }

    // init text view
    ui->ptTextView->setEnabled(ui->cbEnableTextView->isChecked());
    // TODO: does this work in binary mode? (no new lines)
    ui->ptTextView->setMaximumBlockCount(10000);
    QObject::connect(ui->cbEnableTextView, &QCheckBox::toggled,
                     ui->ptTextView, &QWidget::setEnabled);
    QObject::connect(ui->pbClearTextView, &QPushButton::clicked,
                     ui->ptTextView, &QPlainTextEdit::clear);

    // Init sps (sample per second) counter
    sampleCount = 0;
    spsLabel.setText("0sps");
    spsLabel.setToolTip("samples per second (total of all channels)");
    ui->statusBar->addPermanentWidget(&spsLabel);
    spsTimer.start(SPS_UPDATE_TIMEOUT * 1000);
    QObject::connect(&spsTimer, &QTimer::timeout,
                     this, &MainWindow::spsTimerTimeout);

    // Init demo mode
    demoCount = 0;
    demoTimer.setInterval(100);
    QObject::connect(&demoTimer, &QTimer::timeout,
                     this, &MainWindow::demoTimerTimeout);
    QObject::connect(ui->actionDemoMode, &QAction::toggled,
                     this, &MainWindow::enableDemo);

    {   // init demo indicator
        QwtText demoText(" DEMO RUNNING ");  // looks better with spaces
        demoText.setColor(QColor("white"));
        demoText.setBackgroundBrush(Qt::darkRed);
        demoText.setBorderRadius(4);
        demoText.setRenderFlags(Qt::AlignLeft | Qt::AlignTop);
        demoIndicator.setText(demoText);
        demoIndicator.hide();
        demoIndicator.attach(ui->plot);
    }

}

MainWindow::~MainWindow()
{
    for (auto curve : curves)
    {
        delete curve;
    }

    if (serialPort.isOpen())
    {
        serialPort.close();
    }
    delete ui;
}

void MainWindow::setupAboutDialog()
{
    Ui_AboutDialog uiAboutDialog;
    uiAboutDialog.setupUi(&aboutDialog);

    QObject::connect(uiAboutDialog.pbAboutQt, &QPushButton::clicked,
                     [](){ QApplication::aboutQt();});

    QString aboutText = uiAboutDialog.lbAbout->text();
    aboutText.replace("$VERSION_STRING$", VERSION_STRING);
    aboutText.replace("$VERSION_REVISION$", VERSION_REVISION);
    uiAboutDialog.lbAbout->setText(aboutText);
}

void MainWindow::onPortToggled(bool open)
{
    // make sure demo mode is disabled
    if (open && isDemoRunning()) enableDemo(false);
    ui->actionDemoMode->setEnabled(!open);
}

void MainWindow::onDataReady()
{
    if (!ui->actionPause->isChecked())
    {
        // a package is a set of channel data like {CHAN0_SAMPLE, CHAN1_SAMPLE...}
        int packageSize = sampleSize * numOfChannels;
        int bytesAvailable = serialPort.bytesAvailable();

        if (bytesAvailable > 0 && skipByteRequested)
        {
            serialPort.read(1);
            skipByteRequested = false;
            bytesAvailable--;
        }

        if (bytesAvailable < packageSize)
        {
            return;
        }
        else
        {
            int numOfPackagesToRead =
                (bytesAvailable - (bytesAvailable % packageSize)) / packageSize;
            QVector<DataArray> channelSamples(numOfChannels);
            for (unsigned int ci = 0; ci < numOfChannels; ci++)
            {
                channelSamples[ci].resize(numOfPackagesToRead);
            }

            int i = 0;
            while(i < numOfPackagesToRead)
            {
                for (unsigned int ci = 0; ci < numOfChannels; ci++)
                {
                    channelSamples[ci].replace(i, (this->*readSample)());
                }
                i++;
            }

            for (unsigned int ci = 0; ci < numOfChannels; ci++)
            {
                addChannelData(ci, channelSamples[ci]);
            }
        }
    }
    else
    {
        serialPort.clear(QSerialPort::Input);
    }
}

void MainWindow::onDataReadyASCII()
{
    while(serialPort.canReadLine())
    {
        QByteArray line = serialPort.readLine();

        // discard data if paused
        if (ui->actionPause->isChecked())
        {
            return;
        }

        line = line.trimmed();

        if (ui->cbEnableTextView->isChecked())
        {
            ui->ptTextView->appendPlainText(line);
        }

        auto separatedValues = line.split(',');

        if (separatedValues.length() >= int(numOfChannels))
        {
            for (unsigned int ci = 0; ci < numOfChannels; ci++)
            {
                double channelSample = separatedValues[ci].toDouble();
                addChannelData(ci, DataArray({channelSample}));
            }
        }
        else // there is missing channel data
        {
            qWarning() << "Incoming data is missing data for some channels!";
            for (int ci = 0; ci < separatedValues.length(); ci++)
            {
                double channelSample = separatedValues[ci].toDouble();
                addChannelData(ci, DataArray({channelSample}));
            }
        }
    }
}

void MainWindow::onPortError(QSerialPort::SerialPortError error)
{
    switch(error)
    {
        case QSerialPort::NoError :
            break;
        case QSerialPort::ResourceError :
            qWarning() << "Port error: resource unavaliable; most likely device removed.";
            if (serialPort.isOpen())
            {
                qWarning() << "Closing port on resource error: " << serialPort.portName();
                portControl.togglePort();
            }
            portControl.loadPortList();
            break;
        case QSerialPort::DeviceNotFoundError:
            qCritical() << "Device doesn't exists: " << serialPort.portName();
            break;
        case QSerialPort::PermissionError:
            qCritical() << "Permission denied. Either you don't have \
required privileges or device is already opened by another process.";
            break;
        case QSerialPort::OpenError:
            qWarning() << "Device is already opened!";
            break;
        case QSerialPort::NotOpenError:
            qCritical() << "Device is not open!";
            break;
        case QSerialPort::ParityError:
            qCritical() << "Parity error detected.";
            break;
        case QSerialPort::FramingError:
            qCritical() << "Framing error detected.";
            break;
        case QSerialPort::BreakConditionError:
            qCritical() << "Break condition is detected.";
            break;
        case QSerialPort::WriteError:
            qCritical() << "An error occurred while writing data.";
            break;
        case QSerialPort::ReadError:
            qCritical() << "An error occurred while reading data.";
            break;
        case QSerialPort::UnsupportedOperationError:
            qCritical() << "Operation is not supported.";
            break;
        case QSerialPort::TimeoutError:
            qCritical() << "A timeout error occurred.";
            break;
        case QSerialPort::UnknownError:
            qCritical() << "Unknown error!";
            break;
        default:
            qCritical() << "Unhandled port error: " << error;
            break;
    }
}

void MainWindow::skipByte()
{
    skipByteRequested = true;
}

void MainWindow::addChannelData(unsigned int channel, DataArray data)
{
    DataArray* channelDataArray = &(channelsData[channel]);
    int offset = numOfSamples - data.size();

    if (offset < 0)
    {
        for (unsigned int i = 0; i < numOfSamples; i++)
        {
            (*channelDataArray)[i] = data[i - offset];
        }
    }
    else if (offset == 0)
    {
        (*channelDataArray) = data;
    }
    else
    {
        // shift old samples
        int shift = data.size();
        for (int i = 0; i < offset; i++)
        {
            (*channelDataArray)[i] = (*channelDataArray)[i + shift];
        }
        // place new samples
        for (int i = 0; i < data.size(); i++)
        {
            (*channelDataArray)[offset + i] = data[i];
        }
    }

    // update plot
    curves[channel]->setSamples(dataX, (*channelDataArray));
    ui->plot->replot(); // TODO: replot after all channel data updated

    sampleCount += data.size();
}

void MainWindow::clearPlot()
{
    for (unsigned int ci = 0; ci < numOfChannels; ci++)
    {
        channelsData[ci].fill(0.0);
        curves[ci]->setSamples(dataX, channelsData[ci]);
    }

    // update plot
    ui->plot->replot();
}

void MainWindow::onNumOfSamplesChanged(int value)
{
    unsigned int oldNum = this->numOfSamples;
    numOfSamples = value;

    // resize data arrays
    if (numOfSamples < oldNum)
    {
        dataX.resize(numOfSamples);
        for (unsigned int ci = 0; ci < numOfChannels; ci++)
        {
            channelsData[ci].remove(0, oldNum - numOfSamples);
        }
    }
    else if(numOfSamples > oldNum)
    {
        dataX.resize(numOfSamples);
        for (unsigned int i = oldNum; i < numOfSamples; i++)
        {
            dataX[i] = i;
            for (unsigned int ci = 0; ci < numOfChannels; ci++)
            {
                channelsData[ci].prepend(0);
            }
        }
    }
}

void MainWindow::onNumOfChannelsChanged(int value)
{
    unsigned int oldNum = this->numOfChannels;
    this->numOfChannels = value;

    if (numOfChannels > oldNum)
    {
        // add new channels
        for (unsigned int i = 0; i < numOfChannels - oldNum; i++)
        {
            channelsData.append(DataArray(numOfSamples, 0.0));
            curves.append(new QwtPlotCurve());
            curves.last()->setSamples(dataX, channelsData.last());
            curves.last()->setPen(makeColor(curves.length()-1));
            curves.last()->attach(ui->plot);
        }
    }
    else if(numOfChannels < oldNum)
    {
        // remove channels
        for (unsigned int i = 0; i < oldNum - numOfChannels; i++)
        {
            channelsData.removeLast();
            auto curve = curves.takeLast();
            curve->detach();
            delete curve;
        }
    }
}

void MainWindow::onAutoScaleChecked(bool checked)
{
    if (checked)
    {
        ui->plot->setAxisAutoScale(QwtPlot::yLeft);
        ui->lYmin->setEnabled(false);
        ui->lYmax->setEnabled(false);
        ui->spYmin->setEnabled(false);
        ui->spYmax->setEnabled(false);
    }
    else
    {
        ui->lYmin->setEnabled(true);
        ui->lYmax->setEnabled(true);
        ui->spYmin->setEnabled(true);
        ui->spYmax->setEnabled(true);

        ui->plot->setAxisScale(QwtPlot::yLeft, ui->spYmin->value(),
                               ui->spYmax->value());
    }
}

void MainWindow::onYScaleChanged()
{
    ui->plot->setAxisScale(QwtPlot::yLeft, ui->spYmin->value(),
                           ui->spYmax->value());
}

void MainWindow::onNumberFormatButtonToggled(int numberFormatId, bool checked)
{
    if (checked) selectNumberFormat((NumberFormat) numberFormatId);
}

void MainWindow::selectNumberFormat(NumberFormat numberFormatId)
{
    numberFormat = numberFormatId;

    switch(numberFormat)
    {
        case NumberFormat_uint8:
            sampleSize = 1;
            readSample = &MainWindow::readSampleAs<quint8>;
            break;
        case NumberFormat_int8:
            sampleSize = 1;
            readSample = &MainWindow::readSampleAs<qint8>;
            break;
        case NumberFormat_uint16:
            sampleSize = 2;
            readSample = &MainWindow::readSampleAs<quint16>;
            break;
        case NumberFormat_int16:
            sampleSize = 2;
            readSample = &MainWindow::readSampleAs<qint16>;
            break;
        case NumberFormat_uint32:
            sampleSize = 4;
            readSample = &MainWindow::readSampleAs<quint32>;
            break;
        case NumberFormat_int32:
            sampleSize = 4;
            readSample = &MainWindow::readSampleAs<qint32>;
            break;
        case NumberFormat_float:
            sampleSize = 4;
            readSample = &MainWindow::readSampleAs<float>;
            break;
        case NumberFormat_ASCII:
            sampleSize = 0;    // these two members should not be used
            readSample = NULL; // in this mode
            break;
    }

    if (numberFormat == NumberFormat_ASCII)
    {
        QObject::disconnect(&(this->serialPort), &QSerialPort::readyRead, 0, 0);
        QObject::connect(&(this->serialPort), &QSerialPort::readyRead,
                         this, &MainWindow::onDataReadyASCII);
        portControl.enableSkipByte();
    }
    else
    {
        QObject::disconnect(&(this->serialPort), &QSerialPort::readyRead, 0, 0);
        QObject::connect(&(this->serialPort), &QSerialPort::readyRead,
                         this, &MainWindow::onDataReady);
        portControl.enableSkipByte(false);
    }
}

template<typename T> double MainWindow::readSampleAs()
{
    T data;
    this->serialPort.read((char*) &data, sizeof(data));

    // print data in hex format
    if (ui->cbEnableTextView->isChecked())
    {
        QTextCursor cursor(ui->ptTextView->document());
        cursor.movePosition(QTextCursor::End);
        QByteArray ba((char*) &data, sizeof(data));
        QScrollBar* vbar = ui->ptTextView->verticalScrollBar();
        bool isEnd = (vbar->value() == vbar->maximum());

        cursor.insertText(ba.toHex() + " ");
        // `53` is selected for minimum width of the main window
        if (cursor.positionInBlock() >= 53)
        {
            cursor.insertText("\n");
        }

        // scroll if we were at the end of the display
        if (isEnd)
        {
            vbar->setValue(vbar->maximum());
        }
    }

    if (ui->rbLittleE->isChecked())
    {
        data = qFromLittleEndian(data);
    }
    else
    {
        data = qFromBigEndian(data);
    }

    return double(data);
}

bool MainWindow::isDemoRunning()
{
    return ui->actionDemoMode->isChecked();
}

void MainWindow::spsTimerTimeout()
{
    spsLabel.setText(QString::number(sampleCount/SPS_UPDATE_TIMEOUT) + "sps");
    sampleCount = 0;
}

void MainWindow::demoTimerTimeout()
{
    demoCount++;
    if (demoCount > 100) demoCount = 0;

    if (!ui->actionPause->isChecked())
    {
        for (unsigned int ci = 0; ci < numOfChannels; ci++)
        {
            DataArray data(1);
            data.replace(0, (ci + 1)*demoCount);
            addChannelData(ci, data);
        }
    }
}

void MainWindow::enableDemo(bool enabled)
{
    if (enabled)
    {
        if (!serialPort.isOpen())
        {
            demoTimer.start();
            ui->actionDemoMode->setChecked(true);
            demoIndicator.show();
            ui->plot->replot();
        }
        else
        {
            ui->actionDemoMode->setChecked(false);
        }
    }
    else
    {
        demoTimer.stop();
        ui->actionDemoMode->setChecked(false);
        demoIndicator.hide();
        ui->plot->replot();
    }
}

/*
  Below crude drawing demostrates how color selection occurs for
  given channel index

  0°                     <--Hue Value-->                           360°
  |* . o . + . o . * . o . + . o . * . o . + . o . * . o . + . o . |

  * -> 0-3
  + -> 4-7
  o -> 8-15
  . -> 16-31

 */

QColor MainWindow::makeColor(unsigned int channelIndex)
{
    auto i = channelIndex;

    if (i < 4)
    {
        return QColor::fromHsv(360*i/4, 255, 230);
    }
    else
    {
        double p = floor(log2(i));
        double n = pow(2, p);
        i = i - n;
        return QColor::fromHsv(360*i/n + 360/pow(2,p+1), 255, 230);
    }
}

void MainWindow::onExportCsv()
{
    bool wasPaused = ui->actionPause->isChecked();
    ui->actionPause->setChecked(true); // pause plotting

    QString fileName = QFileDialog::getSaveFileName(this, tr("Export CSV File"));

    if (fileName.isNull())  // user canceled export
    {
        ui->actionPause->setChecked(wasPaused);
    }
    else
    {
        QFile file(fileName);
        if (file.open(QIODevice::WriteOnly | QIODevice::Text))
        {
            QTextStream fileStream(&file);

            for (unsigned int ci = 0; ci < numOfChannels; ci++)
            {
                fileStream << "Channel " << ci;
                if (ci != numOfChannels-1) fileStream << ",";
            }
            fileStream << '\n';

            for (unsigned int i = 0; i < numOfSamples; i++)
            {
                for (unsigned int ci = 0; ci < numOfChannels; ci++)
                {
                    fileStream << channelsData[ci][i];
                    if (ci != numOfChannels-1) fileStream << ",";
                }
                fileStream << '\n';
            }
        }
        else
        {
            qCritical() << "File open error during export: " << file.error();
        }
    }
}

void MainWindow::messageHandler(QtMsgType type,
                                const QMessageLogContext &context,
                                const QString &msg)
{
    QString logString;

    switch (type)
    {
        case QtDebugMsg:
            logString = "[Debug] " + msg;
            break;
        case QtWarningMsg:
            logString = "[Warning] " + msg;
            break;
        case QtCriticalMsg:
            logString = "[Error] " + msg;
            break;
        case QtFatalMsg:
            logString = "[Fatal] " + msg;
            break;
    }

    ui->ptLog->appendPlainText(logString);
    std::cerr << logString.toStdString() << std::endl;

    if (type != QtDebugMsg)
    {
        ui->statusBar->showMessage(msg, 5000);
    }
}