-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
853 lines (687 loc) · 34.6 KB
/
mainwindow.cpp
File metadata and controls
853 lines (687 loc) · 34.6 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "customgraphicsscene.h"
#include "worker.h"
// Displays debug information if defined
#define OCTAVE_DEBUG
#define DIRECTORY_DEBUG
// Colormap mode1
#define HSV_COLORMAP
//#define RB_COLORMAP
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Assigns stylesheet to the GUI
QFile styleFile("style.qss");
styleFile.open(QFile::ReadOnly);
QString style(styleFile.readAll());
this->setStyleSheet(style);
initializeGraph();
// Disable some of the user interactions while there is no image loaded
ui->verticalSlider->setEnabled(false);
ui->resetZoomButton->setEnabled(false);
ui->addButton->setEnabled(false);
connect(ui->verticalSlider, SIGNAL(valueChanged(int)), this, SLOT(bandChange(int)));
// Initialize octave
string_vector argv(2);
argv(0) = "embedded";
argv(1) = "-q";
octave_main(2, argv.c_str_vec(), 1);
#ifdef OCTAVE_DEBUG
qDebug() << "Octave initialized.";
#endif
}
MainWindow::~MainWindow()
{
delete ui;
}
/********************************************************************************/
/* MainWindow::initializeGraphicsScene() */
/* */
/* Description - Initializes Graphics container which displays the hyperspec */
/* image */
/********************************************************************************/
void MainWindow::initializeGraphicsScene()
{
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap::fromImage(imageVector[ui->verticalSlider->value()]));
scene = new CustomGraphicsScene(ui->hyperspecView);
scene->addItem(item);
ui->hyperspecView->setScene(scene);
ui->hyperspecView->fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
ui->verticalSlider->setEnabled(true);
ui->resetZoomButton->setEnabled(true);
ui->addButton->setEnabled(true);
ui->hyperspecView->setDragMode(QGraphicsView::ScrollHandDrag);
xMax = imageVector.length() - 1;
recenterGraph();
}
/********************************************************************************/
/* MainWindow::initializeGraph() */
/* */
/* Description - Initializes QCustomPlot widget which displays the spectral */
/* signature for selected points */
/********************************************************************************/
void MainWindow::initializeGraph()
{
// Desired axes ranges for graphs
// Note: xMax is modified once an image is loaded.
xMin = 0;
xMax = 270;
yMin = 0;
yMax = 4096;
recenterGraph();
ui->customPlot->axisRect()->setupFullAxesBox();
ui->customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignBottom|Qt::AlignRight);
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |
QCP::iSelectLegend | QCP::iSelectPlottables | QCP::iMultiSelect);
/*
// Adds a title to the top of the graph
ui->customPlot->plotLayout()->insertRow(0);
QCPTextElement *title = new QCPTextElement(ui->customPlot, "Graph Title", QFont("sans", 10, QFont::Normal));
ui->customPlot->plotLayout()->addElement(0, 0, title);
*/
// Connect slots for title double click if desired
//connect(title, SIGNAL(doubleClicked(QMouseEvent*)), this, SLOT(titleDoubleClick(QMouseEvent*)));
// Sets up the legend
QFont legendFont = font();
legendFont.setPointSize(7);
ui->customPlot->legend->setFont(legendFont);
legendFont.setPointSize(10);
ui->customPlot->legend->setSelectedFont(legendFont);
ui->customPlot->legend->setSelectableParts(QCPLegend::spItems);
// Connects slot that ties some axis selections together
connect(ui->customPlot, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged()));
// Connect slots for holding selected axis stationary
connect(ui->customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress()));
connect(ui->customPlot, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel()));
// Transfers ranges from bottom and left axes to the top and right axes, respectively
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
// Connect slots for axis and legend double clicks
//connect(ui->customPlot, SIGNAL(axisDoubleClick(QCPAxis*,QCPAxis::SelectablePart,QMouseEvent*)), this, SLOT(axisLabelDoubleClick(QCPAxis*,QCPAxis::SelectablePart)));
connect(ui->customPlot, SIGNAL(legendDoubleClick(QCPLegend*,QCPAbstractLegendItem*,QMouseEvent*)), this, SLOT(legendDoubleClick(QCPLegend*,QCPAbstractLegendItem*)));
// Shows a message in the status bar when a graph is clicked
connect(ui->customPlot, SIGNAL(plottableClick(QCPAbstractPlottable*,int,QMouseEvent*)), this, SLOT(graphClicked(QCPAbstractPlottable*,int)));
// Sets up policy and connects slot for context menu popup
ui->customPlot->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->customPlot, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequest(QPoint)));
// Bottom xAxis Styling
ui->customPlot->xAxis->setBasePen(QPen(Qt::white, 1));
ui->customPlot->xAxis->setTickPen(QPen(Qt::white, 1));
ui->customPlot->xAxis->setSubTickPen(QPen(Qt::white, 1));
ui->customPlot->xAxis->setTickLabelColor(Qt::white);
ui->customPlot->xAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
ui->customPlot->xAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
ui->customPlot->xAxis->grid()->setSubGridVisible(true);
ui->customPlot->xAxis->grid()->setZeroLinePen(Qt::NoPen);
ui->customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
// Left yAxis Styling
ui->customPlot->yAxis->setBasePen(QPen(Qt::white, 1));
ui->customPlot->yAxis->setTickPen(QPen(Qt::white, 1));
ui->customPlot->yAxis->setSubTickPen(QPen(Qt::white, 1));
ui->customPlot->yAxis->setTickLabelColor(Qt::white);
ui->customPlot->yAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
ui->customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
ui->customPlot->yAxis->grid()->setSubGridVisible(true);
ui->customPlot->yAxis->grid()->setZeroLinePen(Qt::NoPen);
ui->customPlot->yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
// Top xAxis Styling
ui->customPlot->xAxis2->setBasePen(QPen(Qt::white, 1));
ui->customPlot->xAxis2->setTickPen(QPen(Qt::white, 1));
ui->customPlot->xAxis2->setSubTickPen(QPen(Qt::white, 1));
ui->customPlot->xAxis2->setTickLabelColor(Qt::white);
ui->customPlot->xAxis2->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
ui->customPlot->xAxis2->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
ui->customPlot->xAxis2->grid()->setSubGridVisible(true);
ui->customPlot->xAxis2->grid()->setZeroLinePen(Qt::NoPen);
// Right yAxis Styling
ui->customPlot->yAxis2->setBasePen(QPen(Qt::white, 1));
ui->customPlot->yAxis2->setTickPen(QPen(Qt::white, 1));
ui->customPlot->yAxis2->setSubTickPen(QPen(Qt::white, 1));
ui->customPlot->yAxis2->setTickLabelColor(Qt::white);
ui->customPlot->yAxis2->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
ui->customPlot->yAxis2->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
ui->customPlot->yAxis2->grid()->setSubGridVisible(true);
ui->customPlot->yAxis2->grid()->setZeroLinePen(Qt::NoPen);
ui->customPlot->setBackground(QColor(0x3a, 0x3a, 0x3a));
QLinearGradient axisRectGradient;
axisRectGradient.setStart(0, 0);
axisRectGradient.setFinalStop(0, 350);
axisRectGradient.setColorAt(0, QColor(0x80, 0x80, 0x80));
axisRectGradient.setColorAt(1, QColor(30, 30, 30));
ui->customPlot->axisRect()->setBackground(axisRectGradient);
}
/********************************************************************************/
/* MainWindow::loadOctaveMatrix(QString &fileName) */
/* */
/* Description - Loads a .mat file using octave. Was used to import 3D image, */
/* might be replaced for implementation of all bands. */
/********************************************************************************/
bool MainWindow::loadOctaveMatrix(QString &fileName)
{
imageVector.clear();
outputMatrix.clear();
octave_value_list input;
QByteArray ba = fileName.toLatin1();
const char *fileNameChar = ba.data();
input(0) = octave_value(fileNameChar);
octave_value_list output = feval("readMat", input);
#ifdef OCTAVE_DEBUG
qDebug() << "\nOctave Debug:\n";
qDebug() << output.length() << "item(s) in output.";
for(int i = 0; i < output.length(); i++)
{
if(output(i).is_matrix_type())
{
qDebug() << "Output:" << i
<< " Type:" << QString::fromStdString(output(0).type_name())
<< " Rows:" << output(i).rows()
<< " Columns:" << output(i).columns();
} else
{
qDebug() << "Output:" << i
<< " Type:" << QString::fromStdString(output(0).type_name())
<< " Length:" << output(i).length();
}
}
#endif
outputMatrix = output(0).matrix_value();
matrixRows = outputMatrix.rows();
matrixCols = outputMatrix.columns();
int numberChannels = matrixCols / output(0).columns();
matrixCols = matrixCols / numberChannels;
QImage bandImage(QSize(matrixRows, matrixCols), QImage::Format_RGB888);
int hValue, sValue, lValue;
QColor color;
#ifdef RB_COLORMAP
#ifdef OCTAVE_DEBUG
qDebug() << "\n\nBlue<->Red Colormap Debug:\n";
#endif
for(int k = 0; k < numberChannels; k++)
{
#ifdef OCTAVE_DEBUG
QTime time;
time.restart();
#endif
for(int i = 0; i < matrixRows; i++)
{
for(int j = 0; j < matrixCols; j++)
{
// {0-4096}
if (outputMatrix.elem(i, (matrixCols * k) + j) > 2048)
outputMatrix.elem(i, (matrixCols * k) + j) = 2048;
if (outputMatrix.elem(i, (matrixCols * k) + j) < 1028) {
hValue = 240;
sValue = 100 * ( outputMatrix.elem(i, (matrixCols * k) + j) / 1028);
lValue = 100 - 50 * ( outputMatrix.elem(i, (matrixCols * k) + j) / 1028);
}
else {
hValue = 0;
sValue = 100 * ( outputMatrix.elem(i, (matrixCols * k) + j) - 1028) / 1028;
lValue = 100 - 50 * ( outputMatrix.elem(i, (matrixCols * k) + j) - 1028) / 1028;
}
color.setHsl(hValue, sValue, lValue);
bandImage.setPixelColor(QPoint(i, j), color);
}
}
imageVector.append(bandImage);
#ifdef OCTAVE_DEBUG
qDebug() << "Band" << k
<< "time:" << time.elapsed() << "ms";
#endif
}
#endif
#ifdef HSV_COLORMAP
sValue = 100;
lValue = 100;
#ifdef OCTAVE_DEBUG
qDebug() << "\nHSV Colormap Debug:\n";
#endif
for(int k = 0; k < numberChannels; k++)
{
#ifdef OCTAVE_DEBUG
QTime time;
time.restart();
#endif
for(int i = 0; i < matrixRows; i++)
{
for(int j = 0; j < matrixCols; j++)
{
// Value range: {0, 4096}
hValue = 360 * ( outputMatrix.elem(i, (matrixCols * k) + j) / 4096);
color.setHsv(hValue, sValue, lValue);
bandImage.setPixelColor(QPoint(i, j), color);
}
}
imageVector.append(bandImage);
#ifdef OCTAVE_DEBUG
qDebug() << "Band" << k
<< "time:" << time.elapsed() << "ms";
#endif
}
#endif
ui->verticalSlider->setMinimum(0);
ui->verticalSlider->setMaximum(numberChannels - 1);
#ifdef OCTAVE_DEBUG
qDebug() << "\nMainWindow::imageVector:"
<< " Size:" << imageVector[0].size()
<< " Number of channels:" << imageVector.length();
#endif
if(imageVector.length())
return true;
else
return false;
}
/********************************************************************************/
/* MainWindow::loadOctaveMatrix(QString &fileName) */
/* */
/* Description - Loads a .mat file using octave. Was used to import 3D image, */
/* might be replaced for implementation of all bands. */
/********************************************************************************/
void MainWindow::loadOctaveDirectory(QVector<QString> filePaths)
{
QVector<QThread*> threads(filePaths.length(), new QThread);
for (int i = 0; i < filePaths.length(); i++)
{
QString filePath = filePaths[i];
QString filename = filePath.right(filePath.length() - (filePath.lastIndexOf("/") + 1));
filename = filename.left(filename.indexOf("."));
int bandNumber = filename.right(3).toInt();
#ifdef DIRECTORY_DEBUG
qDebug() << "File Path:" << filePath
<< " Filename:" << filename
<< " Band Number:" << bandNumber;
#endif
Worker* bandWorker = new Worker(bandNumber);
bandWorker->moveToThread(threads[i]);
connect(threads[i], SIGNAL(started()), bandWorker, SLOT(process()));
connect(bandWorker, SIGNAL(finished()), threads[i], SLOT(quit()));
connect(bandWorker, SIGNAL(finished()), threads[i], SLOT(deleteLater()));
for (int i = 0; i < filePaths.length(); i++)
{
threads[i]->start();
}
}
}
/********************************************************************************/
/* MainWindow::titleDoubleClick(QMouseEvent* event) */
/* */
/* Description - Allows the graph title to be changed by double clicking it. */
/* - If desired, title initialization and slot connection in */
/* MainWindow::initializeGraph() located in this file */
/********************************************************************************/
void MainWindow::titleDoubleClick(QMouseEvent* event)
{
Q_UNUSED(event)
if (QCPTextElement *title = qobject_cast<QCPTextElement*>(sender()))
{
bool ok;
QString newTitle = QInputDialog::getText(this, "QCustomPlot example", "New plot title:", QLineEdit::Normal, title->text(), &ok);
if (ok)
{
title->setText(newTitle);
ui->customPlot->replot();
}
}
}
/********************************************************************************/
/* MainWindow::legendDoubleClick(QCPLegend*, QCPAbstractLegendItem*) */
/* */
/* Description - Allows items in the legend to be double clicked in order to */
/* change their name. */
/********************************************************************************/
void MainWindow::legendDoubleClick(QCPLegend *legend, QCPAbstractLegendItem *item)
{
Q_UNUSED(legend)
if (item)
{
QCPPlottableLegendItem *plItem = qobject_cast<QCPPlottableLegendItem*>(item);
bool okay;
QString newName = QInputDialog::getText(this, "QCustomPlot example", "New graph name:", QLineEdit::Normal, plItem->plottable()->name(), &okay);
if (okay)
{
plItem->plottable()->setName(newName);
ui->customPlot->replot();
}
}
}
/********************************************************************************/
/* MainWindow::selectionChanged() */
/* */
/* Description - Makes axes synchronously selectable and groups axis parts */
/* - Synchronizes graph selection with corresponding legend item */
/********************************************************************************/
void MainWindow::selectionChanged()
{
// Makes top and bottom axes synchronously selectable and groups axis selectable parts
if (ui->customPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis) || ui->customPlot->xAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
ui->customPlot->xAxis2->selectedParts().testFlag(QCPAxis::spAxis) || ui->customPlot->xAxis2->selectedParts().testFlag(QCPAxis::spTickLabels))
{
ui->customPlot->xAxis2->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
ui->customPlot->xAxis->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
}
// Makes left and right axes synchronously selectable and groups axis selectable parts
if (ui->customPlot->yAxis->selectedParts().testFlag(QCPAxis::spAxis) || ui->customPlot->yAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
ui->customPlot->yAxis2->selectedParts().testFlag(QCPAxis::spAxis) || ui->customPlot->yAxis2->selectedParts().testFlag(QCPAxis::spTickLabels))
{
ui->customPlot->yAxis2->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
ui->customPlot->yAxis->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
}
// Synchronizes graph selection with corresponding legend item
for (int i=0; i<ui->customPlot->graphCount(); ++i)
{
QCPGraph *graph = ui->customPlot->graph(i);
QCPPlottableLegendItem *item = ui->customPlot->legend->itemWithPlottable(graph);
if (item->selected() || graph->selected())
{
item->setSelected(true);
graph->setSelection(QCPDataSelection(graph->data()->dataRange()));
}
}
}
/********************************************************************************/
/* MainWindow::mousePress() */
/* */
/* Description - Loads a .mat file using octave. Was used to import 3D image, */
/* might be replaced. */
/********************************************************************************/
void MainWindow::mousePress()
{
// If an axis is selected, only allow the direction of that axis to be dragged
// If no axis is selected, both directions may be dragged
if (ui->customPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis))
ui->customPlot->axisRect()->setRangeDrag(ui->customPlot->xAxis->orientation());
else if (ui->customPlot->yAxis->selectedParts().testFlag(QCPAxis::spAxis))
ui->customPlot->axisRect()->setRangeDrag(ui->customPlot->yAxis->orientation());
else
ui->customPlot->axisRect()->setRangeDrag(Qt::Horizontal|Qt::Vertical);
}
/********************************************************************************/
/* MainWindow::mouseWheel() */
/* */
/* Description - Loads a .mat file using octave. Was used to import 3D image, */
/* might be replaced. */
/********************************************************************************/
void MainWindow::mouseWheel()
{
// If an axis is selected, only allow the direction of that axis to be zoomed
// If no axis is selected, both directions may be zoomed
if (ui->customPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis))
ui->customPlot->axisRect()->setRangeZoom(ui->customPlot->xAxis->orientation());
else if (ui->customPlot->yAxis->selectedParts().testFlag(QCPAxis::spAxis))
ui->customPlot->axisRect()->setRangeZoom(ui->customPlot->yAxis->orientation());
else
ui->customPlot->axisRect()->setRangeZoom(Qt::Horizontal|Qt::Vertical);
}
/********************************************************************************/
/* MainWindow::contextMenuRequest(QPoint pos) */
/* */
/* Description - Handles and populates context menus for qCustomPlot. */
/********************************************************************************/
void MainWindow::contextMenuRequest(QPoint pos)
{
QMenu *menu = new QMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose);
if (ui->customPlot->legend->selectTest(pos, false) >= 0) // Legend
{
menu->addAction("New graph", this, SLOT(on_addButton_released()));
if (ui->customPlot->selectedGraphs().size() > 1)
{
menu->addAction("Edit color of selected graphs", this, SLOT(changeGraphColor()));
menu->addAction("Remove selected graphs", this, SLOT(removeSelectedGraphs()));
}
else if (ui->customPlot->selectedGraphs().size() == 1)
{
menu->addAction("Edit color of selected graph", this, SLOT(changeGraphColor()));
menu->addAction("Remove selected graph", this, SLOT(removeSelectedGraphs()));
}
menu->addAction("Save graph window", this, SLOT(on_saveButton_released()));
}
else // Not legend
{
menu->addAction("New graph", this, SLOT(contextMenuAddRequested()));
if (ui->customPlot->selectedGraphs().size() > 1)
{
menu->addAction("Edit color of selected graphs", this, SLOT(changeGraphColor()));
menu->addAction("Remove selected graphs", this, SLOT(removeSelectedGraphs()));
}
else if (ui->customPlot->selectedGraphs().size() == 1)
{
menu->addAction("Edit color of selected graph", this, SLOT(changeGraphColor()));
menu->addAction("Remove selected graph", this, SLOT(removeSelectedGraphs()));
}
if (ui->customPlot->graphCount() > 0)
menu->addAction("Save graph window", this, SLOT(on_saveButton_released()));
}
menu->popup(ui->customPlot->mapToGlobal(pos));
}
/********************************************************************************/
/* MainWindow::removeSelectedGraphs() */
/* */
/* Description - Removes the selected plots from qCustomPlot */
/********************************************************************************/
void MainWindow::removeSelectedGraphs()
{
while (ui->customPlot->selectedGraphs().size() > 0)
{
ui->customPlot->removeGraph(ui->customPlot->selectedGraphs().first());
}
ui->customPlot->replot();
}
/********************************************************************************/
/* MainWindow::changeGraphColor() */
/* */
/* Description - Changes the color of the selected plots from qCustomPlot */
/********************************************************************************/
void MainWindow::changeGraphColor()
{
for(int i = 0; i < ui->customPlot->selectedGraphs().size(); ++i)
{
//qDebug() << "changeGraphColor()" << i;
QColor color = QColorDialog::getColor(Qt::black, this, "Graph Color", QColorDialog::DontUseNativeDialog);
ui->customPlot->selectedGraphs().at(i)->setPen(QPen(color));
}
ui->customPlot->replot();
}
/********************************************************************************/
/* MainWindow::recenterGraph() */
/* */
/* Description - Recenters and realigns qCustomPlot. */
/********************************************************************************/
void MainWindow::recenterGraph()
{
ui->customPlot->xAxis->setRange(xMin, xMax);
ui->customPlot->yAxis->setRange(yMin, yMax);
ui->customPlot->replot();
if (ui->customPlot->graphCount() == 0)
ui->customPlot->legend->setVisible(false);
}
/********************************************************************************/
/* MainWindow::graphClicked(QCPAbstractPlottable *plottable, int dataIndex) */
/* */
/* Description - Handles the event in which a plot is clicked. */
/********************************************************************************/
void MainWindow::graphClicked(QCPAbstractPlottable *plottable, int dataIndex)
{
// Since we know we only have QCPGraphs in the plot, we can immediately access interface1D().
// Usually it's better to first check whether interface1D() returns non-zero, and only then use it.
double dataValue = plottable->interface1D()->dataMainValue(dataIndex);
QString message = QString("Clicked on graph '%1' at data point #%2 with value %3.").arg(plottable->name()).arg(dataIndex).arg(dataValue);
ui->statusBar->showMessage(message, 2500);
}
/********************************************************************************/
/* MainWindow::addPlot(QVector<double> pointVector) */
/* */
/* Description - Adds a plot (passed as QVector<double> argument to */
/* qCustomPlot. */
/********************************************************************************/
void MainWindow::addPlot(QVector<double> pointVector)
{
// Retrieves current number of graphs in ui->customPlot
int graphCount = ui->customPlot->graphCount();
ui->customPlot->addGraph();
QVector<double> band(xMax - xMin + 1);
for (int i = xMin; i <= xMax; ++i)
band[i] = i;
ui->customPlot->graph(graphCount)->setData(band, pointVector);
ui->customPlot->legend->setVisible(true);
ui->customPlot->replot();
}
/********************************************************************************/
/* MainWindow::addPoint(QGraphicsSceneMouseEvent *event) */
/* */
/* Description - Adds spectral signature for pixel in image when clicked. */
/********************************************************************************/
void MainWindow::addPoint(QGraphicsSceneMouseEvent *event)
{
int x = nearbyint(event->scenePos().x());
int y = nearbyint(event->scenePos().y());
QPoint point(x, y);
QVector<double> pointVector;
for(int i = 0; i < imageVector.length(); i++)
{
pointVector.append(outputMatrix.elem(x, (matrixCols * i) + y));
}
qDebug() << "\naddPoint Debug:"
<< "\n(x , y):" << point.x()<< "," << point.y()
<< "pointVector:" << pointVector;
addPlot(pointVector);
}
/********************************************************************************/
/* MainWindow::bandChange(int newBand) */
/* */
/* Description - Changes displayed band when bandSelector scrollBar is */
/* adjusted. */
/********************************************************************************/
void MainWindow::bandChange(int newBand)
{
if(imageVector.length() > 1)
{
// FIXME: Add check for when no image is loaded
scene->clear();
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap::fromImage(imageVector[newBand]));
scene->addItem(item);
ui->hyperspecView->update();
}
}
/********************************************************************************/
/* MainWindow::on_loadButton_released() */
/* */
/* Description - When the load button is released, opens file dialogue and */
/* calls methods necessary to map image to container. */
/********************************************************************************/
void MainWindow::on_loadButton_released()
{
QString directoryName = QFileDialog::getExistingDirectory(this, tr("Open Directory"), QString());
QVector<QString> filePaths;
iterateDirectory(directoryName, &filePaths);
loadOctaveDirectory(filePaths);
//initializeGraphicsScene();
}
/********************************************************************************/
/* MainWindow::iterateDirectory(QString &directoryName) */
/* */
/* Description - Iterates the selected directory. */
/********************************************************************************/
void MainWindow::iterateDirectory(QString directoryName, QVector<QString> *filenames)
{
#ifdef DIRECTORY_DEBUG
qDebug() << "\nDirectory Iterator Debug:\n";
qDebug() << directoryName;
#endif
QDirIterator directory(directoryName, QDirIterator::NoIteratorFlags);
QTime time;
time.restart();
while (directory.hasNext())
{
QString nextEntry = directory.next();
if (nextEntry.right(4) == ".mat")
{
filenames->append(nextEntry);
}
}
}
/********************************************************************************/
/* MainWindow::on_addButton_toggled() */
/* */
/* Description - Changes between panning mode and selecting mode for image when */
/* the add button is toggled. */
/********************************************************************************/
void MainWindow::on_addButton_toggled()
{
bool checked = ui->addButton->isChecked();
if (checked)
{
ui->hyperspecView->setDragMode(QGraphicsView::NoDrag);
connect(scene, SIGNAL(mousePress(QGraphicsSceneMouseEvent*)),
this, SLOT(addPoint(QGraphicsSceneMouseEvent*)));
}
else
{
ui->hyperspecView->setDragMode(QGraphicsView::ScrollHandDrag);
disconnect(scene, SIGNAL(mousePress(QGraphicsSceneMouseEvent*)),
this, SLOT(addPoint(QGraphicsSceneMouseEvent*)));
}
}
/********************************************************************************/
/* MainWindow::contextMenuAddRequested() */
/* */
/* Description - Toggles add button when "Add Point" is selected from the */
/* context menu. */
/********************************************************************************/
void MainWindow::contextMenuAddRequested()
{
ui->addButton->setChecked(true);
}
/********************************************************************************/
/* MainWindow::on_centerGraphButton_released() */
/* */
/* Description - Recenters and realigns qCustomPlot. */
/********************************************************************************/
void MainWindow::on_centerGraphButton_released()
{
recenterGraph();
}
/********************************************************************************/
/* MainWindow::on_clearButton_released() */
/* */
/* Description - Clears all plots from qCustomPlot as well as recenters and */
/* realigns qCustomPlot */
/********************************************************************************/
void MainWindow::on_clearButton_released()
{
ui->customPlot->clearGraphs();
ui->customPlot->legend->setVisible(false);
ui->addButton->setChecked(false);
recenterGraph();
}
/********************************************************************************/
/* MainWindow::on_saveButton_released() */
/* */
/* Description - Saves qCustomPlot window to a .jpg file. */
/********************************************************************************/
void MainWindow::on_saveButton_released()
{
QFileDialog saveImageFileDialog;
QString fileName = QDateTime::currentDateTime().toString("MMddyyyy_hhmmss")+".jpg";
saveImageFileDialog.setNameFilter("Images (*.jpg)");
saveImageFileDialog.setDirectory(QDir::currentPath());
saveImageFileDialog.selectFile(fileName);
if (saveImageFileDialog.exec())
{
fileName = fileName + ".jpg";
ui->customPlot->saveJpg(fileName, 0, 0, 5.0);
}
}
/********************************************************************************/
/* MainWindow::on_resetZoomButton_released() */
/* */
/* Description - Resets the zoom of the image window while keeping the aspect */
/* ratio. */
/********************************************************************************/
void MainWindow::on_resetZoomButton_released()
{
ui->hyperspecView->fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
}