Vespucci  1.0.0
spectrumeditor.cpp
Go to the documentation of this file.
2 #include "ui_spectrumeditor.h"
3 
4 SpectrumEditor::SpectrumEditor(MainWindow *main_window, QSharedPointer<VespucciWorkspace> workspace) :
5  QDockWidget(main_window),
6  ui(new Ui::SpectrumEditor)
7 {
8  ui->setupUi(this);
9  main_window_ = main_window;
10  workspace_ = workspace;
11  plot_viewer_ = main_window_->plot_viewer();
12  dataset_ = QSharedPointer<VespucciDataset>(0);
13  table_model_ = NULL;
14 }
15 
17 {
18  delete ui;
19  if (table_model_) delete table_model_;
20 }
21 
22 void SpectrumEditor::closeEvent(QCloseEvent *ev)
23 {
24  QDockWidget::closeEvent(ev);
25  emit SetActionChecked(false);
26 }
27 
28 void SpectrumEditor::keyPressEvent(QKeyEvent *event)
29 {
30  QModelIndex current_index = ui->tableView->currentIndex();
31  if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)
32  RequestSpectrumPlot(current_index);
33 }
34 
35 void SpectrumEditor::DatasetSelectionChanged(QString dataset_key)
36 {
37  dataset_ = workspace_->GetDataset(dataset_key);
38  if (dataset_.isNull()) return;
39  if (table_model_) delete table_model_;
40  table_model_ = new SpectraTableModel(this, dataset_);
41  ui->tableView->setModel(table_model_);
42  ui->tableView->resizeColumnsToContents();
43  ui->tableView->resizeRowsToContents();
44  //we'll change even if not visible, the overhead of creating the model
45  //isn't that high.
46 }
47 
49 {
50  if (!dataset_.isNull() && dataset_->name() == key){
51  if (table_model_) delete table_model_;
52  table_model_ = new SpectraTableModel(this);
53  ui->tableView->setModel(table_model_);
54  ui->tableView->resizeColumnsToContents();
55  ui->tableView->resizeRowsToContents();
56  }
57 
58 }
59 
60 void SpectrumEditor::on_tableView_clicked(const QModelIndex &index)
61 {
62  RequestSpectrumPlot(index);
63 }
64 
65 void SpectrumEditor::SpectrumRemoved(int row)
66 {
67  if (dataset_.isNull()) return; //should never happen, but let's be safe
68 
69  //after deletion, row may be out of bounds
70  if (uword(row) >= dataset_->spectra_ptr()->n_cols)
71  row = dataset_->spectra_ptr()->n_cols - 1; //make the last row appear.
72 
73  try{
74  uvec indices = {uword(row)};
75  if (ui->holdCheckBox->isChecked())
76  plot_viewer_->AddPlot(dataset_->abscissa(),
77  dataset_->spectra(indices),
78  dataset_->name());
79 
80  else
81  plot_viewer_->AddTransientPlot(dataset_->abscissa(),
82  dataset_->spectra(indices),
83  dataset_->name());
84 
85 
86  }
87  catch(std::exception e){
88  main_window_->DisplayExceptionWarning(e);
89  }
90 }
91 
92 void SpectrumEditor::on_deletePushButton_clicked()
93 {
94  if (dataset_.isNull()) return; //should never happen, but let's be safe
95 
96  int row = ui->tableView->currentIndex().row();
97 
98  int response = QMessageBox::question(this, "Delete Spectrum?",
99  "Are you sure you want to delete the spectrum at index " + QString::number(row) + "?",
100  QMessageBox::Yes, QMessageBox::No);
101  cout << (response == QMessageBox::Yes ? "QMessageBox::Yes" : "!QMessageBox::Yes") << "\n";
102 
103  if (response == QMessageBox::Yes){
104  try{
105  cout << "call ShedRow\n";
106 
107  dataset_->ShedSpectrum(row);
108  }
109  catch(std::exception e){
110  main_window_->DisplayExceptionWarning(e);
111  }
112  }
113 }
114 
115 void SpectrumEditor::on_exportPushButton_clicked()
116 {
117  if (dataset_.isNull()) return; //should never happen, but let's be safe
118 
119  QModelIndexList model_indices = ui->tableView->selectionModel()->selectedIndexes();
120  mat data = dataset_->abscissa();
121  uvec indices(model_indices.size());
122  for (uword i = 0; i < indices.n_rows; ++i) indices(i) = model_indices[i].row();
123  data = join_horiz(data, dataset_->spectra(indices));
124  QString filename =
125  QFileDialog::getSaveFileName(this, "Save Spectrum",
126  workspace_->directory(),
127  "Comma-separated variables (*.csv);; "
128  "Tab-delimited text (*.txt);; "
129  "Raw binary (*.bin)");
130  QFileInfo file_info(filename);
131  QString extension = file_info.suffix();
132  bool success;
133  file_type type;
134 
135  if (extension.toLower() == "bin")
136  type = raw_binary;
137  else if (extension.toLower() == "txt")
138  type = raw_ascii;
139  else
140  type = csv_ascii;
141 
142  success = data.save(filename.toStdString(), type);
143  if (success)
144  QMessageBox::information(main_window_, "File Saved", filename + " saved successfully");
145  else
146  QMessageBox::warning(main_window_, "File not saved", "File not saved successfully");
147 }
148 
149 void SpectrumEditor::RequestSpectrumPlot(const QModelIndex &index)
150 {
151  if (!dataset_.isNull() && index.isValid() && table_model_->rowCount(index)){
152  QSharedPointer<mat> data(new mat(join_horiz(dataset_->abscissa(),
153  dataset_->spectra_ptr()->col(index.row()))));
154  if (ui->holdCheckBox->isChecked())
155  plot_viewer_->AddPlot(*data, dataset_->name());
156  else
157  plot_viewer_->AddTransientPlot(*data, dataset_->name());
158  }
159 }
160 
161 void SpectrumEditor::on_zeroPushButton_clicked()
162 {
163  if (dataset_.isNull()) return; //should never happen, but let's be safe
164 
165  int row = ui->tableView->currentIndex().row();
166 
167  int response = QMessageBox::question(this, "Zero Spectrum?",
168  "Are you sure you want to zero the spectrum at index " + QString::number(row) + "?",
169  QMessageBox::Yes, QMessageBox::No);
170  cout << (response == QMessageBox::Yes ? "QMessageBox::Yes" : "!QMessageBox::Yes") << "\n";
171 
172  if (response == QMessageBox::Yes){
173  try{
174 
175  dataset_->ShedSpectrum(row);
176  }
177  catch(std::exception e){
178  main_window_->DisplayExceptionWarning(e);
179  }
180  }
181 }
void closeEvent(QCloseEvent *ev)
void DatasetSelectionChanged(QString dataset_key)
Definition: ahcadialog.h:26
void DisplayExceptionWarning(std::exception e)
MainWindow::DisplayExceptionWarning.
Definition: mainwindow.cpp:845
PlotViewer * plot_viewer()
Definition: mainwindow.cpp:890
void keyPressEvent(QKeyEvent *event)
void DatasetToBeRemoved(QString key)
void AddPlot(const mat &paired_data, const QString &tab_title)
Definition: plotviewer.cpp:19
int rowCount(const QModelIndex &parent) const
The MainWindow class The main window of the program, this is where the user performs most operations...
Definition: mainwindow.h:58
void AddTransientPlot(const vec &abscissa, const vec &data, const QString &tab_title)
Definition: plotviewer.cpp:47
void SetActionChecked(bool checked)
SpectrumEditor(MainWindow *main_window, QSharedPointer< VespucciWorkspace > workspace)