forked from cculianu/SpikeGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileViewerWindow.cpp
1665 lines (1449 loc) · 53.3 KB
/
FileViewerWindow.cpp
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* FileViewerWindow.cpp
* SpikeGL
*
* Created by calin on 10/26/10.
* Copyright 2010 Calin Culianu <[email protected]>. All rights reserved.
*
*/
#include "FileViewerWindow.h"
#include "GLGraph.h"
#include "MainApp.h"
#include "ConsoleWindow.h"
#include <QFileInfo>
#include <QFile>
#include <QScrollArea>
#include <limits.h>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QSlider>
#include <QSpinBox>
#include <QDoubleSpinBox>
#include <QToolBar>
#include <QMenuBar>
#include <QStatusBar>
#include "close_but_16px.xpm"
#include <QPixmap>
#include <QTimer>
#include <QCursor>
#include <QResizeEvent>
#include <QScrollBar>
#include <QAction>
#include <QMenu>
#include "ExportDialogController.h"
#include <QProgressDialog>
#include <QTextStream>
#include <QMessageBox>
#include <QFrame>
#include <QCheckBox>
#include <QPushButton>
#include "HPFilter.h"
#include "ClickableLabel.h"
#include <QKeyEvent>
#include "ui_FVW_OptionsDialog.h"
#include <QDialog>
#include <QComboBox>
#include <stdio.h>
#include "ui_FVW_Readme.h"
const QString FileViewerWindow::viewModeNames[] = {
"Tiled", "Stacked", "Stacked Large", "Stacked Huge"
};
const QString FileViewerWindow::colorSchemeNames[] = {
"Ice", "Fire", "Green", "BlackWhite", "Classic"
};
class TaggableLabel : public QLabel
{
public:
TaggableLabel(QWidget *parent, Qt::WindowFlags f = 0) : QLabel(parent, f), tagPtr(0) {}
void setTag(void *ptr) { tagPtr = ptr; }
void *tag() const { return tagPtr; }
private:
mutable void *tagPtr;
};
FileViewerWindow::FileViewerWindow()
: QMainWindow(0), pscale(1), mouseOverT(-1.), mouseOverV(0), mouseOverGNum(-1), mouseButtonIsDown(false), dontKillSelection(false), hpfilter(0), arrowKeyFactor(.1), pgKeyFactor(.5), n_graphs_pg(8), curr_graph_page(0), showReadme(true)
{
readmeDlg = 0; readme=0;
QWidget *cw = new QWidget(this);
QVBoxLayout *l = new QVBoxLayout(cw);
setCentralWidget(cw);
scrollArea = new QScrollArea(cw);
scrollArea->installEventFilter(this);
graphParent = new QWidget(scrollArea);
scrollArea->setWidget(graphParent);
l->addWidget(scrollArea, 1);
// all channels hidden label
allChannelsHiddenLbl = new QLabel("<b><i><font size=+1 color=#6666cc>All graphs on this page are hidden. Use the \"channels\" menu to unhide.</font></i></b>", cw);
l->addWidget(allChannelsHiddenLbl,0);
allChannelsHiddenLbl->setHidden(true);
// bottom slider
QWidget *w = new QWidget(cw);
QHBoxLayout *hl = new QHBoxLayout(w);
maximizedLbl = new QLabel("<b><i>MAXIMIZED (right-click to restore)</i></b>", w);
hl->addWidget(maximizedLbl);
maximizedLbl->setHidden(true);
pageCB = new QComboBox(w);
hl->addWidget(pageCB);
QLabel *lbl = pgLbl = new QLabel("Graphs/Page", w);
lbl->setToolTip("The number of graphs to display on-screen at a time. Page through graphs using the 'tabwidget' control.");
hl->addWidget(lbl);
graphPgSz = new QSpinBox(w);
graphPgSz->setMinimum(4);
graphPgSz->setMaximum(256);
graphPgSz->setToolTip("The number of graphs to display on-screen at a time. Page through graphs using the 'tabwidget' control.");
hl->addWidget(graphPgSz);
Connect(graphPgSz, SIGNAL(valueChanged(int)), this, SLOT(graphsPerPageChanged(int)));
lbl = new QLabel("File position: ", w);
hl->addWidget(lbl);
lbl = new QLabel("scans", w);
posScansSB = new QSpinBox(w);
hl->addWidget(lbl, 0, Qt::AlignRight);
hl->addWidget(posScansSB, 0, Qt::AlignLeft);
lbl = totScansLbl = new QLabel("", w);
hl->addWidget(lbl, 0, Qt::AlignLeft);
hl->addSpacing(25);
lbl = new QLabel("secs", w);
posSecsSB = new QDoubleSpinBox(w);
posSecsSB->setDecimals(3);
posSecsSB->setSingleStep(0.001);
hl->addWidget(lbl, 0, Qt::AlignRight);
hl->addWidget(posSecsSB, 0, Qt::AlignLeft);
lbl = totSecsLbl = new QLabel("", w);
hl->addWidget(lbl, 0, Qt::AlignLeft);
hl->addSpacing(10);
posSlider = new QSlider(Qt::Horizontal, w);
posSlider->setMinimum(0);
posSlider->setMaximum(1000);
hl->addWidget(posSlider, 1);
Connect(posSlider, SIGNAL(valueChanged(int)), this, SLOT(setFilePos(int)));
Connect(posScansSB, SIGNAL(valueChanged(int)), this, SLOT(setFilePos(int)));
Connect(posSecsSB, SIGNAL(valueChanged(double)), this, SLOT(setFilePosSecs(double)));
toolBar = addToolBar("Graph Controls");
lbl = new QLabel("Graph:", toolBar);
toolBar->addWidget(lbl);
lbl = graphNameLbl = new QLabel("<font size=-1>0 Ch. 0</font>", toolBar);
toolBar->addWidget(lbl);
toolBar->addSeparator();
lbl = new QLabel("<font size=-1>X-Scale (secs):</font>", toolBar);
toolBar->addWidget(lbl);
xScaleSB = new QDoubleSpinBox(toolBar);
xScaleSB->setDecimals(4);
xScaleSB->setRange(.0001, 1e6);
xScaleSB->setSingleStep(.25);
toolBar->addWidget(xScaleSB);
lbl = new QLabel("<font size=-1>Y-Scale (factor):</font>", toolBar);
toolBar->addWidget(lbl);
yScaleSB = new QDoubleSpinBox(toolBar);
yScaleSB->setRange(.01, 100.0);
yScaleSB->setSingleStep(0.25);
toolBar->addWidget(yScaleSB);
Connect(xScaleSB, SIGNAL(valueChanged(double)), this, SLOT(setXScaleSecs(double)));
Connect(yScaleSB, SIGNAL(valueChanged(double)), this, SLOT(setYScale(double)));
toolBar->addSeparator();
toolBar->addWidget(lbl = new QLabel("<font size=-1>N Divs:</font>", toolBar));
toolBar->addWidget(nDivsSB = new QSpinBox(toolBar));
nDivsSB->setMinimum(0);
nDivsSB->setMaximum(10);
Connect(nDivsSB, SIGNAL(valueChanged(int)), this, SLOT(setNDivs(int)));
xDivLbl = new QLabel("", toolBar);
toolBar->addWidget(xDivLbl);
yDivLbl = new QLabel("", toolBar);
toolBar->addWidget(yDivLbl);
toolBar->addWidget(new QLabel("<font size=-1>Gain:</font>", toolBar));
toolBar->addWidget(auxGainSB = new QDoubleSpinBox(toolBar));
auxGainSB->setDecimals(3);
auxGainSB->setRange(0.001,1e6);
Connect(auxGainSB, SIGNAL(valueChanged(double)), this, SLOT(setAuxGain(double)));
toolBar->addSeparator();
highPassChk = new QCheckBox(toolBar);
lbl = new ClickableLabel("<font size=-1>Filter < 300Hz</font>", toolBar);
Connect(lbl, SIGNAL(clicked()), this, SLOT(hpfLblClk()));
toolBar->addWidget(highPassChk);
toolBar->addWidget(lbl);
Connect(highPassChk, SIGNAL(clicked(bool)), this, SLOT(hpfChk(bool)));
dcfilterChk = new QCheckBox(toolBar);
lbl = new ClickableLabel("<font size=-1>DC Filter</font>", toolBar);
Connect(lbl, SIGNAL(clicked()), this, SLOT(dcfLblClk()));
toolBar->addWidget(dcfilterChk);
toolBar->addWidget(lbl);
Connect(dcfilterChk, SIGNAL(clicked(bool)), this, SLOT(dcfChk(bool)));
toolBar->addSeparator();
QPushButton *but = new QPushButton("Set All", toolBar);
but->setToolTip("Apply these graph settings to all graphs");
toolBar->addWidget(but);
Connect(but, SIGNAL(clicked(bool)), this, SLOT(applyAllSlot()));
l->addWidget(w);
statusBar(); // creates one implicitly
//#ifdef Q_OS_MACX
// // shared menu bar on OSX
// QMenuBar *mb = mainApp()->console()->menuBar();
//#else
// // otherwise window-specific menu bar
QMenuBar *mb = menuBar();
//#endif
QMenu *m = mb->addMenu("File");
m->addAction("&Open...", this, SLOT(fileOpenMenuSlot()));
m->addAction("Misc. Options...", this, SLOT(fileOptionsMenuSlot()));
m = mb->addMenu("Color &Scheme");
for (int i = 0; i < (int)N_ColorScheme; ++i) {
QAction * a = colorSchemeActions[i] = m->addAction(colorSchemeNames[i], this, SLOT(colorSchemeMenuSlot()));
a->setCheckable(true);
}
m = mb->addMenu("&View Mode");
sortByIntan = m->addAction("Sort Graphs by Intan Channel", this, SLOT(sortGraphsByIntan()));
sortByElectrode = m->addAction("Sort Graphs by Electrode Id", this, SLOT(sortGraphsByElectrode()));
sortByIntan->setCheckable(true);
sortByIntan->setChecked(true);
sortByElectrode->setCheckable(true);
m->addSeparator();
for (int i = 0; i < (int)N_ViewMode; ++i) {
QAction *a = viewModeActions[i] = m->addAction(viewModeNames[i], this, SLOT(viewModeMenuSlot()));
a->setCheckable(true);
}
channelsMenu = mb->addMenu("&Channels");
channelsMenu->addAction("Show All", this, SLOT(showAllGraphs()));
channelsMenu->addSeparator();
closeLbl = new TaggableLabel(0, (Qt::WindowFlags)Qt::FramelessWindowHint);
QPixmap pm(close_but_16px);
closeLbl->setPixmap(pm);
closeLbl->resize(pm.size());
closeLbl->setToolTip("Hide this graph");
closeLbl->setCursor(Qt::ArrowCursor);
closeLbl->hide();
closeLbl->setTag(0);
closeLbl->move(-100,-100);
closeLbl->installEventFilter(this);
didLayout = false;
hideCloseTimer = new QTimer(this);
hideCloseTimer->setInterval(1000);
hideCloseTimer->setSingleShot(false);
Connect(hideCloseTimer, SIGNAL(timeout()), this, SLOT(hideCloseTimeout()));
exportAction = new QAction("Export...", this);
Connect(exportAction, SIGNAL(triggered()), this, SLOT(exportSlot()));
exportSelectionAction = new QAction("Export Selection", this);
Connect(exportSelectionAction, SIGNAL(triggered()), this, SLOT(exportSlot()));
exportSelectionAction->setEnabled(false);
maxunmaxAction = new QAction("Toggle Maximize", this);
Connect(maxunmaxAction, SIGNAL(triggered()), this, SLOT(toggleMaximize()));
exportCtl = new ExportDialogController(this);
}
FileViewerWindow::~FileViewerWindow()
{
/// scrollArea and graphParent automatically deleted here because they are children of us.
delete hpfilter;
// these aren't children, so delete them
delete readmeDlg, readmeDlg = 0;
delete readme, readme = 0;
}
bool FileViewerWindow::viewFile(const QString & fname, QString *errMsg /* = 0 */)
{
QString fname_no_path = QFileInfo(fname).fileName();
if (!dataFile.openForRead(fname)) {
QString err = fname_no_path + ": miscellaneous error on open.";
if (errMsg) *errMsg = err;
Error() << err;
return false;
}
if (!dataFile.scanCount()) {
QString err = fname_no_path + " is empty!";
if (errMsg) *errMsg = err;
Error() << err;
return false; // file is empty
}
setWindowTitle(QString(APPNAME) + QString(" File Viewer - ") + QFileInfo(fname_no_path).fileName() + " "
+ QString::number(dataFile.numChans()) + " channels @ "
+ QString::number(dataFile.samplingRateHz())
+ " Hz"
+ ", " + QString::number(dataFile.scanCount()) + " scans"
);
loadSettings();
const bool reusing = graphFrames.size();
for (int i = 0, n = graphHideUnhideActions.size(); i < n; ++i) {
channelsMenu->removeAction(graphHideUnhideActions[i]);
delete graphHideUnhideActions[i];
}
if (hpfilter) delete hpfilter;
hpfilter = new HPFilter(dataFile.numChans(), 300);
const int nChans = dataFile.numChans();
graphHideUnhideActions.clear(); graphHideUnhideActions.resize(nChans);
graphParams.clear(); graphParams.resize(nChans);
defaultGain = dataFile.auxGain();
chanMap = dataFile.chanMap();
graphSorting.resize(nChans); revGraphSorting.resize(nChans);
pageCB->blockSignals(true);
pageCB->setCurrentIndex((curr_graph_page=0)); // set it back to first page..
pageCB->blockSignals(false);
if (electrodeSort) {
sortByIntan->setChecked(false);
sortByElectrode->setChecked(true);
} else {
sortByIntan->setChecked(true);
sortByElectrode->setChecked(false);
}
for (int i = 0, n = nChans; i < n; ++i) {
revGraphSorting[i] = graphSorting[i] = i; ///< identity sort initially
graphParams[i].yZoom = defaultYZoom;
graphParams[i].gain = defaultGain;
graphParams[i].filter300Hz = false;
graphParams[i].dcFilter = true;
if (i < (int)chanMap.size())
graphParams[i].objname = QString(QString("Graph ") + QString::number(i) + " Electrode " + QString::number(chanMap[i].electrodeId));
else
graphParams[i].objname = QString(QString("Graph ") + QString::number(i) + " ChanID " + QString::number(dataFile.channelIDs()[i]));
QAction *a = new QAction(graphParams[i].objname, this);
a->setObjectName(QString::number(i)); // hack sorta: tag it with its id for use later in the slot
a->setCheckable(true);
a->setChecked(true);
channelsMenu->addAction(a);
Connect(a, SIGNAL(triggered()), this, SLOT(hideUnhideGraphSlot()));
graphHideUnhideActions[i] = a;
}
// NOTE THIS DELETES OLD GRAPHS & FRAMES (if any) and RE-CREATES GRAPHS
redoGraphs();
hiddenGraphs.fill(false, nChans);
didLayout = false;
pscale = 1;
pos = 0;
maximizedGraph = -1;
selectionBegin = selectionEnd = -1;
selectedGraph = mouseOverT = mouseOverV = mouseOverGNum = -1;
selectGraph(0);
repaginate();
if (electrodeSort)
sortGraphsByElectrode(); ///< implicitly calls layoutGraphs()
else
sortGraphsByIntan(); ///< implicitly calls layoutGraphs()
pos = 0;
if (reusing) configureMiscControls(true);
setFilePos64(0, true);
if (reusing) QTimer::singleShot(10, this, SLOT(updateData()));
if (errMsg) *errMsg = QString::null;
if (showReadme) {
delete readmeDlg; delete readme;
readmeDlg = new QDialog(0);
readme = new Ui::FVW_Readme;
readme->setupUi(readmeDlg);
Connect(readmeDlg, SIGNAL(accepted()), this, SLOT(readmeDlgDone()));
}
return true;
}
bool FileViewerWindow::queryCloseOK()
{
// for now, always ok..
return true;
}
void FileViewerWindow::loadSettings()
{
QSettings settings(SETTINGS_DOMAIN, SETTINGS_APP);
settings.beginGroup("FileViewerWindow");
viewMode = Tiled;
int vm = settings.value("viewMode", (int)Tiled).toInt();
if (vm >= 0 && vm < N_ViewMode) viewMode = (ViewMode)vm;
nSecsZoom = settings.value("nSecsZoom", 4.0).toDouble();
defaultYZoom = settings.value("yZoom", 1.0).toDouble();
nDivs = settings.value("nDivs", 4).toUInt();
int cs = settings.value("colorScheme", DefaultScheme).toInt();
if (cs < 0 || cs >= N_ColorScheme) cs = 0;
int fmt = settings.value("lastExportFormat", ExportParams::Bin).toInt();
if (fmt < 0 || fmt >= ExportParams::N_Format) fmt = ExportParams::Bin;
exportCtl->params.format = (ExportParams::Format)fmt;
fmt = settings.value("lastExportCSVSubFormat", ExportParams::Real).toInt();
if (fmt < 0 || fmt >= ExportParams::N_CSVSubFormat) fmt = ExportParams::Real;
exportCtl->params.csvSubFormat = (ExportParams::CSVSubFormat)fmt;
int lec = settings.value("lastExportChans", 1).toInt();
if (lec < 0 || lec > 2) lec = 1;
exportCtl->params.allChans = exportCtl->params.allShown = exportCtl->params.customSubset = false;
if (lec == 0) exportCtl->params.allChans = true;
else if (lec == 1) exportCtl->params.allShown = true;
else if (lec == 2) exportCtl->params.customSubset = true;
arrowKeyFactor = settings.value("arrowKeyFactor", .1).toDouble();
if (fabs(arrowKeyFactor) < 0.0001) arrowKeyFactor = .1;
pgKeyFactor = settings.value("pgKeyFactor", .5).toDouble();
if (fabs(pgKeyFactor) < 0.0001) pgKeyFactor = .5;
colorScheme = (ColorScheme)cs;
xScaleSB->blockSignals(true);
yScaleSB->blockSignals(true);
nDivsSB->blockSignals(true);
xScaleSB->setValue(nSecsZoom);
yScaleSB->setValue(defaultYZoom);
nDivsSB->setValue(nDivs);
nDivsSB->blockSignals(false);
xScaleSB->blockSignals(false);
yScaleSB->blockSignals(false);
electrodeSort = settings.value("sortGraphsByElectrode", false).toBool();
graphPgSz->blockSignals(true);
graphPgSz->setValue((n_graphs_pg = settings.value("graphsPerPage", DEFAULT_NUM_GRAPHS_PER_GRAPH_TAB).toUInt()));
graphPgSz->blockSignals(false);
showReadme = !settings.value("neverShowReadme", false).toBool();
}
void FileViewerWindow::saveSettings()
{
QSettings settings(SETTINGS_DOMAIN, SETTINGS_APP);
settings.beginGroup("FileViewerWindow");
settings.setValue("viewMode", (int)viewMode);
settings.setValue("colorScheme", (int)colorScheme);
settings.setValue("lastExportFormat", int(exportCtl->params.format));
settings.setValue("lastExportCSVSubFormat", int(exportCtl->params.csvSubFormat));
settings.setValue("pgKeyFactor", pgKeyFactor);
settings.setValue("arrowKeyFactor", arrowKeyFactor);
int lec = 0;
if (exportCtl->params.allShown) lec = 1;
if (exportCtl->params.customSubset) lec = 2;
settings.setValue("lastExportChans", lec);
settings.setValue("sortGraphsByElectrode", electrodeSort);
settings.setValue("graphsPerPage", graphsPerPage());
settings.setValue("neverShowReadme", !showReadme);
}
void FileViewerWindow::layoutGraphs()
{
didLayout = false;
closeLbl->hide();
static const int padding = 2;
const int n = graphFrames.size();
graphParent->setHidden(false);
allChannelsHiddenLbl->setHidden(true);
if (graphParent->layout())
delete graphParent->layout();
int graphsVisible = 0;
if (maximizedGraph > -1) {
updateGeometry();
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// hide everything except for maximized graph
for (int i = 0; i < n; ++i)
graphFrames[i]->hide();
const QSize sz = scrollArea->size();
graphParent->resize(sz.width(), sz.height());
graphFrames[maximizedGraph]->resize(sz.width(), sz.height());
graphFrames[maximizedGraph]->move(0, 0);
graphFrames[maximizedGraph]->show();
channelsMenu->setEnabled(false);
graphsVisible = 1;
} else {
channelsMenu->setEnabled(true);
// non-maximized more.. make sure non-hidden graphs are shown!
for (int i = 0; i < n; ++i) {
int idx = g2i(i);
if (idx >= 0 && idx < hiddenGraphs.size() && !hiddenGraphs.testBit(idx))
graphFrames[i]->show();
else
graphFrames[i]->hide();
}
if (viewMode == Tiled) {
updateGeometry();
const QSize sz = scrollArea->size();
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
graphParent->resize(sz.width(), sz.height());
QGridLayout *l = new QGridLayout(graphParent);
l->setHorizontalSpacing(padding);
l->setVerticalSpacing(padding);
graphParent->setLayout(l);
updateGeometry();
int hiddenGraphCount = 0;
for (int i = g2i(0); i < g2i(n); ++i)
if (i < 0 || i >= hiddenGraphs.size() || hiddenGraphs.testBit(i)) ++hiddenGraphCount;
const int ngraphs = graphFrames.size() - hiddenGraphCount;
int nrows = int(sqrtf(ngraphs)), ncols = nrows;
while (nrows*ncols < (int)ngraphs) {
if (nrows > ncols) ++ncols;
else ++nrows;
}
for (int r = 0, num = 0; r < nrows; ) {
for (int c = 0; c < ncols; ++num) {
int idx = g2i(num);
if (num >= graphs.size()) { r=nrows,c=ncols; break; } // break out of loop
if (idx >= 0 && idx < hiddenGraphs.size() && !hiddenGraphs.testBit(/*graphSorting[*/idx/*]*/)) {
l->addWidget(graphFrames[/*graphSorting[*/num/*]*/], r, c);
if (++c >= ncols) ++r;
}
}
}
graphsVisible = ngraphs;
} else if (viewMode == StackedLarge || viewMode == Stacked || viewMode == StackedHuge) {
const int stk_h = viewMode == StackedHuge ? 320 : (viewMode == StackedLarge ? 160 : 80);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
int w;
int shownGraphCount = n;
for (int i = g2i(0); i < g2i(n); ++i)
if (i < 0 || i >= hiddenGraphs.size() || hiddenGraphs.testBit(i)) --shownGraphCount;
graphParent->resize(w=(scrollArea->width() - scrollArea->verticalScrollBar()->width() - 5), (stk_h + padding) * shownGraphCount);
if (w <= 0) w = 1;
int y = 0;
for (int i = 0; i < n; ++i) {
int idx = g2i(i);
if (idx >= 0 && idx < hiddenGraphs.size() && !hiddenGraphs.testBit(idx/*graphSorting[i]*/)) {
QFrame *f = graphFrames[/*graphSorting[i]*/i];
f->resize(w, stk_h);
f->move(0,y);
y += f->height() + padding;
}
}
graphsVisible = shownGraphCount;
}
}
if (!graphsVisible) {
graphParent->setHidden(true);
allChannelsHiddenLbl->setHidden(false);
updateGeometry();
update();
}
didLayout = true;
}
void FileViewerWindow::updateData()
{
// Debug() << "updateData() called..";
const double srate = dataFile.samplingRateHz();
QBitArray channelSubset = ~hiddenGraphs;
const int nChans = graphParams.size();
int nChansOn = channelSubset.count(true);
const int nGraphs = graphs.size();
QVector<int> chanIdsOn(nChansOn);
std::vector<bool> chansToFilter(nChansOn, false), chansToDCSubtract(nChansOn, false);
int maxW = 1;
bool hasDCSubtract = false;
for (int i = 0, j = 0; i < nChans; ++i) {
const int gnum = i2g(i);
if (channelSubset.testBit(i)) {
if (gnum >= 0 && gnum < nGraphs) {
// channel is on, and on-screen. Read it.
if (maxW < graphs[gnum]->width()) maxW = graphs[gnum]->width();
if (graphParams[i].filter300Hz)
chansToFilter[j] = true;
if (graphParams[i].dcFilter)
chansToDCSubtract[j] = true, hasDCSubtract = true;
chanIdsOn[j++] = i;
} else {
// channel is on, but not on-screen. Don't read it.
channelSubset.clearBit(i);
--nChansOn;
}
}
}
chanIdsOn.resize(nChansOn);
chansToFilter.resize(nChansOn);
chansToDCSubtract.resize(nChansOn);
HPFilter & filter(*hpfilter);
if ((int)filter.scanSize() != nChansOn) filter.setScanSize(nChansOn);
i64 num = nSecsZoom * srate;
int downsample = num / maxW;
downsample /= 2; // Make sure to 2x oversample the data in the graphs. This, combined with the glBlend we enabled in our graphs should lead to sweetness in the graph detail.
if (downsample <= 0) downsample = 1;
//double t0r = getTime();
std::vector<int16> data;
i64 nread = dataFile.readScans(data, pos, num, channelSubset, downsample);
//Debug() << "dataFile.readScans() took " << ((getTime()-t0r)*1e3) << " msec";
if (nread < 0) {
Error() << "Error reading data from input file!";
} else {
//double t0 = getTime();
// demux for graphs
for (int i = 0; i < nChansOn; ++i) {
int chanId = chanIdsOn[i];
int g = i2g(chanId);
if (g >= 0 && g < nGraphs) {
graphs[g]->setPoints(0);
if (channelSubset.testBit(chanId) && graphBufs[g].capacity() != nread)
graphBufs[g].reserve(nread);
}
}
const float smin(SHRT_MIN), usmax(USHRT_MAX);
const float dt = 1.0f / (srate / float(downsample));
std::vector<float> avgs(nChansOn, 0.f);
const float avgfactor = 1.0f/float(nread);
QVector<QVector<Vec2f> > & vecs (scratchVecs);
if (vecs.size() < nChansOn) vecs.resize(nChansOn);
for (int i = 0; i < nread; ++i) {
filter.apply(&data[i * nChansOn], dt, chansToFilter);
for (int j = 0; j < nChansOn; ++j) {
const int chanId = chanIdsOn[j];
const int g = i2g(chanId);
if (g >= 0 && g < nGraphs) {
if (vecs[j].size() < nread) vecs[j].resize(nread);
int16 & rawsampl = data[i * nChansOn + j];
const float sampl = ( ((float(rawsampl) + (-smin))/(usmax)) * (2.0f) ) - 1.0f;
Vec2f & vec(vecs[j][i]);
vec.x = float(i)/float(nread);
vec.y = sampl;
//Vec2f vec(float(i)/float(nread), sampl);
//graphBufs[g].putData(&vec, 1);
avgs[j] += sampl * avgfactor;
}
}
}
for (int j = 0; j < nChansOn; ++j) {
const int chanId = chanIdsOn[j];
const int g = i2g(chanId);
if (g >= 0 && g < nGraphs) {
graphBufs[g].putData(&vecs[j][0], nread);
}
}
if (hasDCSubtract) {
// if has dc subtract, apply subtract to channels that have it enabled
for (int i = 0; i < nread; ++i) {
for (int j = 0; j < nChansOn; ++j) {
const int chanId = chanIdsOn[j];
const int g = i2g(chanId);
if (g >= 0 && g < nGraphs && chansToDCSubtract[j]) {
Vec2f & vec(graphBufs[g].at(i));
vec.y -= avgs[j];
}
}
}
}
//Debug() << "remux & filter data took " << ((getTime()-t0)*1e3) << " msec";
}
//double t0 = getTime();
for (int i = 0; i < nGraphs; ++i) {
const int idx = g2i(i);
if (idx >= 0 && idx < nChans) {
graphs[i]->setYScale(graphParams[idx].yZoom);
graphs[i]->setNumHGridLines(nDivs);
graphs[i]->setNumVGridLines(nDivs);
applyColorScheme(graphs[i]);
graphs[i]->setPoints(&graphBufs[i]);
}
}
//Debug() << "GLGraph::setPoints() took " << ((getTime()-t0)*1e3) << " msec";
// set ranges, etc of various widgets
configureMiscControls();
// now update various widgets
posScansSB->blockSignals(true);
posSecsSB->blockSignals(true);
posSlider->blockSignals(true);
posScansSB->setValue(pos); // FIXME TODO: the value here is an int. We want a 64-bit value...
posSecsSB->setValue(timeFromPos(pos));
posSlider->setValue(pos);
posScansSB->blockSignals(false);
posSecsSB->blockSignals(false);
posSlider->blockSignals(false);
int ndigs = 1;
qint64 sc = dataFile.scanCount();
while (sc /= 10LL) ndigs++;
QString fmt = "%0" + QString::number(ndigs) + "lld";
totScansLbl->setText(QString("<font size=-1 face=fixed> to ") + QString().sprintf(fmt.toLatin1(),qint64(pos + nScansPerGraph())) + "</font> <font face=fixed size=-2>(out of " + QString::number(dataFile.scanCount()-1) + ")</font>");
totSecsLbl->setText(QString("<font size=-1 face=fixed> to ") + QString::number(timeFromPos(pos + nScansPerGraph()), 'f', 3) + "</font> <font face=fixed size=-2>(out of " + QString::number(timeFromPos(dataFile.scanCount()-1), 'f', 3) + ")</font>");
xDivLbl->setText(QString("<font size=-1>Secs/Div: ") + (!nDivs ? "-" : QString::number(nSecsZoom / nDivs)) + "</font>");
QPair<double, double> yv = yVoltsAfterGain(selectedGraph);
const double yZoom = selectedGraph > -1 ? graphParams[g2i(selectedGraph)].yZoom : 1.;
yDivLbl->setText(QString("<font size=-1>Volts/Div: ") + (!nDivs ? "-" : QString::number(((yv.second-yv.first) / nDivs) / yZoom)) + "</font>");
for (int i = 0; i < (int)N_ColorScheme; ++i)
colorSchemeActions[i]->setChecked(i == (int)colorScheme);
for (int i = 0; i < (int)N_ViewMode; ++i)
viewModeActions[i]->setChecked(i == (int)viewMode);
updateSelection(false);
for (int i = 0; i < nGraphs; ++i)
graphs[i]->update();
}
void FileViewerWindow::updateSelection() {
updateSelection(true);
}
void FileViewerWindow::updateSelection(bool do_update)
{
const double srate = dataFile.samplingRateHz();
const i64 num = nSecsZoom * srate;
const int nGraphs = graphs.size();
// configure selection
if (selectionEnd >= 0 && selectionEnd >= selectionBegin && selectionEnd < (qint64)dataFile.scanCount()
&& selectionBegin >= 0 && selectionBegin < (qint64)dataFile.scanCount()) {
// transform selection from scans to 0->1 value in graph
const double gselbeg = (selectionBegin - pos) / double(num),
gselend = (selectionEnd - pos) / double(num);
for (int i = 0; i < nGraphs; ++i) {
graphs[i]->setSelectionEnabled(true);
graphs[i]->setSelectionRange(gselbeg, gselend);
if (do_update && !graphs[i]->isHidden()) graphs[i]->update();
}
exportSelectionAction->setEnabled(true);
} else {
exportSelectionAction->setEnabled(false);
for (int i = 0; i < nGraphs; ++i) {
graphs[i]->setSelectionEnabled(false);
if (do_update && !graphs[i]->isHidden()) graphs[i]->update();
}
}
printStatusMessage();
}
double FileViewerWindow::timeFromPos(qint64 p) const
{
return p / dataFile.samplingRateHz();
}
qint64 FileViewerWindow::posFromTime(double s) const
{
return dataFile.samplingRateHz() * s;
}
void FileViewerWindow::setFilePos(int p)
{
setFilePos64(p);
}
void FileViewerWindow::setFilePos64(qint64 p, bool noupdate)
{
if (p < 0) p = 0;
pos = qint64(p) * pscale;
qint64 limit = dataFile.scanCount() - nScansPerGraph() - 1;
if (limit < 0) limit = 0;
if (pos > limit) pos = limit;
if (!noupdate)
updateData();
}
void FileViewerWindow::setFilePosSecs(double s)
{
setFilePos64(posFromTime(s));
}
void FileViewerWindow::configureMiscControls(bool blockSignals)
{
xScaleSB->setDecimals(4);
xScaleSB->setRange(0.0001, dataFile.fileTimeSecs());
qint64 maxVal = (dataFile.scanCount()-1) - nScansPerGraph();
if (maxVal < 0) maxVal = 0;
if (blockSignals) {
posScansSB->blockSignals(true);
posSecsSB->blockSignals(true);
posSlider->blockSignals(true);
}
posScansSB->setMinimum(0);
posScansSB->setMaximum(maxVal);
posSecsSB->setMinimum(0);
posSecsSB->setMaximum(timeFromPos(maxVal));
// figure out appropriate scale for the slider
pscale = 1;
while ( maxVal / pscale > qint64(INT_MAX))
pscale = pscale==qint64(1) ? qint64(2) : pscale*pscale;
posSlider->setMaximum(maxVal);
posScansSB->blockSignals(true);
posSecsSB->blockSignals(true);
posSlider->blockSignals(true);
posSlider->setValue(pos);
posScansSB->setValue(pos);
posSecsSB->setValue(timeFromPos(pos));
posScansSB->blockSignals(false);
posSecsSB->blockSignals(false);
posSlider->blockSignals(false);
}
qint64 FileViewerWindow::nScansPerGraph() const { return nSecsZoom * dataFile.samplingRateHz(); }
void FileViewerWindow::setXScaleSecs(double d)
{
nSecsZoom = d;
updateData();
}
void FileViewerWindow::setYScale(double d)
{
if (selectedGraph < 0) return;
graphParams[g2i(selectedGraph)].yZoom = d;
updateData();
}
QPair<double, double> FileViewerWindow::yVoltsAfterGain(int g) const
{
QPair<double, double> ret;
const int chan = g2i(g);
if (chan < 0 || chan >= (int)graphParams.size()) return ret;
const double gain = graphParams[chan].gain;
ret.first = dataFile.rangeMin(chan) / gain;
ret.second = dataFile.rangeMax(chan) / gain;
return ret;
}
void FileViewerWindow::setNDivs(int n)
{
nDivs = n;
updateData();
}
void FileViewerWindow::applyColorScheme(GLGraph *g)
{
QColor fg, bg, grid;
switch (colorScheme) {
case Ice:
fg = QColor(0x87, 0xce, 0xfa, 0x7f);
bg.setRgbF(.15,.15,.15);
grid.setRgbF(0.4,0.4,0.4);
break;
case Fire:
fg = QColor(0xfa, 0x87, 0x37, 0x7f);
bg.setRgbF(.15,.15,.15);
grid.setRgbF(0.4,0.4,0.4);
break;
case Green:
fg = QColor(0x09, 0xca, 0x09, 0x7f);
bg.setRgbF(.07,.07,.07);
grid.setRgbF(0.4,0.4,0.4);
break;
case BlackWhite:
fg = QColor(0xca, 0xca, 0xca, 0xc0);
bg.setRgbF(.05,.05,.05);
grid.setRgbF(0.4,0.4,0.4);
break;
case Classic:
default:
bg = QColor(0x2f, 0x4f, 0x4f);
fg = QColor(0xee, 0xdd, 0x82);
grid = QColor(0x87, 0xce, 0xfa, 0x7f);
break;
}
g->bgColor() = bg;
g->graphColor() = fg;
g->gridColor() = grid;
}
void FileViewerWindow::colorSchemeMenuSlot()
{
ColorScheme oldScheme = colorScheme;
for (int i = 0; i < (int)N_ColorScheme; ++i) {
if (sender() == colorSchemeActions[i]) {
colorScheme = (ColorScheme)i;
colorSchemeActions[i]->setChecked(true);
} else {
colorSchemeActions[i]->setChecked(false);
}
}
if (oldScheme != colorScheme) {
saveSettings();
if (selectedGraph > -1) selectGraph(selectedGraph);
for (int i = 0; i < graphs.size(); ++i)
applyColorScheme(graphs[i]), graphs[i]->updateGL();
}
}
void FileViewerWindow::setAuxGain(double d)
{
if (selectedGraph < 0) return;
graphParams[g2i(selectedGraph)].gain = d;
updateData();
}
void FileViewerWindow::mouseOverGraph(double x, double y)
{
GLGraph *sendr = dynamic_cast<GLGraph *>(sender());
if (!sendr) return;
const int num = sendr->tag().toInt();
const int idx = g2i(num);
mouseOverGNum = num;
if (num < 0 || num >= (int)graphs.size()) {
statusBar()->clearMessage();
return;
}
x = timeFromPos(pos) + x*nSecsZoom; // map x, originally a 0->1 value, to a time
mouseOverT = x;
y += 1.;
y /= 2.;
const double gain = graphParams[idx].gain;
y = (y*(dataFile.rangeMax(idx)-dataFile.rangeMin(idx)) + dataFile.rangeMin(idx)) / gain;
mouseOverV = y;
// now, handle selection change
if (mouseButtonIsDown) {
qint64 p = posFromTime(x);
if (p >= (qint64)dataFile.scanCount()) p = dataFile.scanCount()-1;
else if (p < 0) p = 0;
qint64 diff = 0;
// slide graph over if we dragged selection past end of graph...
if (p < pos) {
diff = p - pos;
} else if (p > pos + nScansPerGraph()) {
diff = p - (pos + nScansPerGraph());
}
//Debug() << "mouse over graph " << num << " ch " << idx << " x=" << x << " y=" << y << " diff="<< diff << " selBeg="<<selectionBegin<<" selEnd="<<selectionEnd;
if (diff) setFilePos64(pos+diff, true);
if (selectionEnd < 0) selectionEnd = selectionBegin;
if (p < selectionBegin) selectionBegin = p;
else selectionEnd = p;
if (diff) updateData();
updateSelection();
}
printStatusMessage();
}
QString FileViewerWindow::generateGraphNameString(unsigned num, bool verbose) const
{
if (num >= (unsigned)graphs.size()) return "";
const int idx = g2i(num);
QString chStr;
const int chanId = dataFile.channelIDs()[idx];