Vespucci  1.0.0
bulkconversiondialog.cpp
Go to the documentation of this file.
2 #include "ui_bulkconversiondialog.h"
3 #include "Math/VespucciMath.h"
7 
8 
10  QSharedPointer<VespucciWorkspace> ws) :
11  QDialog(parent),
12  ui(new Ui::BulkConversionDialog)
13 {
14  ui->setupUi(this);
15  workspace_ = ws;
16 
17  ui->filenameListWidget->setSelectionMode(QAbstractItemView::MultiSelection);
18  //so delete key can be used to remove filenames from list
19  QShortcut *shortcut =
20  new QShortcut(QKeySequence(Qt::Key_Delete), ui->filenameListWidget);
21  connect(shortcut, SIGNAL(activated()), this, SLOT(DeleteItem()));
22 
23 }
24 
26 {
27  delete ui;
28 }
29 
30 void BulkConversionDialog::on_browsePushButton_clicked()
31 {
32  QStringList filename_list =
33  QFileDialog::getOpenFileNames(this,
34  "Select files",
35  workspace_->directory(),
36  "Text (*.txt *.csv);;"
37  "Binary(*.arma *.bin *.dat)");
38  ui->filenameListWidget->addItems(filename_list);
39 }
40 
41 void BulkConversionDialog::DeleteItem()
42 {
43  QList<QListWidgetItem*> items = ui->filenameListWidget->selectedItems();
44  for (auto item: items)
45  ui->filenameListWidget->removeItemWidget(item);
46 }
47 
48 void BulkConversionDialog::on_buttonBox_accepted()
49 {
50  close();
51  using namespace arma;
52  ui->filenameListWidget->selectAll();
53  QList<QListWidgetItem *> items = ui->filenameListWidget->selectedItems();
54  vector<string> infile_names;
55 
56  for (auto item: items)
57  infile_names.push_back(item->text().toStdString());
58 
59  string outfile_path = ui->targetLineEdit->text().toStdString();
60  infile_type intype;
61  arma::file_type outtype;
62 
63  switch (ui->inputComboBox->currentIndex()){
64  case 0:
65  intype = wide_text;
66  break;
67  case 1:
68  intype = long_text;
69  break;
70  case 2:
71  intype = binary;
72  case 3: default:
73  intype = oldbinary;
74  }
75 
76  switch (ui->outputComboBox->currentIndex()){
77  case 0:
78  outtype = arma::arma_binary;
79  break;
80  case 1:
81  outtype = arma::csv_ascii;
82  break;
83  case 2: default:
84  outtype = arma::raw_ascii;
85  }
86  vector<string> skipped = SaveFiles(infile_names, outfile_path, intype, outtype);
87  if(skipped.size()){
88  ReportMessageDialog *report_dialog = new ReportMessageDialog(this, "Files Not Imported");
89  report_dialog->setLabel("Import or export failed for the following files:");
90  for (auto& skipped_name : skipped)
91  report_dialog->appendPlainText(QString::fromStdString(skipped_name));
92  report_dialog->show();
93  }
94 }
95 
96 vector<string> BulkConversionDialog::SaveFiles(vector<string> infile_names, string outfile_path, BulkConversionDialog::infile_type intype, arma::file_type outtype, bool swap_spatial) const
97 {
98 
99  bool valid, ok;
100  vector<string> skipped_files;
101 
102  typedef vector<string>::iterator strvec_it;
103  mat spectra;
104  vec x, y, abscissa;
105  bool comma_decimals = false;
106  switch (intype){
107  case wide_text:
108  for(strvec_it it = infile_names.begin(); it!=infile_names.end(); ++it){
109  bool valid = TextImport::CheckFileValidity(QString::fromStdString(*it), comma_decimals);
110 
111  if (valid){
113  spectra,
114  abscissa,
115  x, y,
116  swap_spatial);
117  }
118 
119  if (!valid || !ok){
120  skipped_files.push_back(*it);
121  infile_names.erase(it);
122  }
123 
124  string outfilename =
125  outfile_path + "/" +
126  QFileInfo(QString::fromStdString(*it)).baseName().toStdString();
127  WriteFile(outtype, outfilename, spectra, abscissa, x, y);
128  }//for (infile)
129  break;
130 
131  case long_text:
132  for(strvec_it it = infile_names.begin(); it!=infile_names.end(); ++it){
133  valid = TextImport::CheckFileValidity(QString::fromStdString(*it), comma_decimals);
134 
135  if (valid){
137  spectra,
138  abscissa,
139  x, y,
140  swap_spatial);
141  }
142 
143  if (!valid || !ok){
144  skipped_files.push_back(*it);
145  infile_names.erase(it);
146  }
147 
148  string outfilename =
149  outfile_path + "/" +
150  QFileInfo(QString::fromStdString(*it)).baseName().toStdString();
151 
152  WriteFile(outtype, outfilename, spectra, abscissa, x, y);
153  }//for (infiles)
154  break;
155  case binary:
156  for (strvec_it it = infile_names.begin(); it != infile_names.end(); ++it){
157  QSharedPointer<VespucciDataset> dataset(new VespucciDataset(QString::fromStdString(*it), workspace_->main_window(), workspace_));
158  ok = dataset->ConstructorCancelled();
159  if (!ok){
160  skipped_files.push_back(*it);
161  infile_names.erase(it);
162  }
163 
164  string outfilename =
165  outfile_path + "/" +
166  QFileInfo(QString::fromStdString(*it)).fileName().toStdString();
167  WriteFile(outtype, outfilename, dataset->spectra(), dataset->abscissa(), dataset->x(), dataset->y());
168  }
169  case oldbinary: default:
170  for(strvec_it it = infile_names.begin(); it!=infile_names.end(); ++it){
172  spectra,
173  abscissa,
174  x, y);
175  if (!ok){
176  skipped_files.push_back(*it);
177  infile_names.erase(it);
178  }
179 
180  string outfilename =
181  outfile_path + "/" +
182  QFileInfo(QString::fromStdString(*it)).fileName().toStdString();
183  WriteFile(outtype, outfilename, spectra, abscissa, x, y);
184  }//for (infiles)
185  }//switch (intype)
186  return skipped_files;
187 }
188 
189 const QString BulkConversionDialog::GetSep(const QString &filename) const
190 {
191  QFile inputfile(filename);
192  inputfile.open(QIODevice::ReadOnly);
193  QTextStream inputstream(&inputfile);
194  QString line = inputstream.readLine();
195  inputfile.close();
196 
197 
198  bool has_tab = -1 != line.indexOf("\t");
199  bool has_comma = -1 != line.indexOf(",");
200  bool has_space = -1 !=line.indexOf(" ");
201 
202  QString out;
203  //check for tabs first, then spaces if no ta
204  if(has_tab){return "\t";}
205  else if(has_space){return " ";}
206  else if(has_comma) {return ",";}
207  else {return "";}
208 
209 }
210 
211 void BulkConversionDialog::WriteFile(const arma::file_type &type,
212  const string filename,
213  const arma::mat &spectra,
214  const arma::vec &abscissa,
215  const arma::vec &x,
216  const arma::vec &y) const
217 {
218  switch(type){
219  case csv_ascii: case raw_ascii:
220  Vespucci::SaveText(filename, spectra, x, y, abscissa, type);
221  break;
222  case hdf5_binary:
223  SaveHDF5(filename, spectra, abscissa, x, y);
224  case arma_binary: default:
225  Vespucci::SaveOldVespucciBinary(filename + ".vds", spectra, x, y, abscissa);
226  }
227 }
228 
229 void BulkConversionDialog::on_BrowsePushButton_clicked()
230 {
231  QString path = QFileDialog::getExistingDirectory(this, "Select Folder",
232  workspace_->directory());
233  ui->targetLineEdit->setText(path);
234 }
235 
236 bool BulkConversionDialog::SaveHDF5(string filename,
237  const mat &spectra,
238  const vec &abscissa,
239  const vec &x,
240  const vec &y) const
241 {
242  VespucciDataset dataset(QString::fromStdString(filename),
243  workspace_->main_window(),
244  workspace_->directory_ptr());
245  dataset.SetData(spectra, abscissa, x, y);
246  return dataset.Save(QString::fromStdString(filename));
247 }
VESPUCCI_EXPORT bool CheckFileValidity(QString filename, bool &comma_decimals)
TextImport::CheckFileValidity.
Definition: textimport.cpp:37
VESPUCCI_EXPORT bool ImportVespucciBinary(std::string filename, arma::mat &spectra, arma::vec &abscissa, arma::vec &x, arma::vec &y)
Definition: ahcadialog.h:26
void SetData(const mat &spectra, const vec &wavelength, const vec &x, const vec &y)
VespucciDataset::SetData.
The VespucciDataset class This is the main class for dealing with hyperspectral data. This handles the import and export of spectra, and the creation of maps. Images are handled by the MapData class. This class is intended to be allocated on the heap inside of a smart pointer, there is no copy constructor.
VESPUCCI_EXPORT bool SaveOldVespucciBinary(std::string filename, const arma::mat &spectra, const arma::vec &x, const arma::vec &y, const arma::vec &abscissa)
Vespucci::SaveVespucciBinary.
Definition: vespucci.cpp:36
void appendPlainText(const QString &text)
BulkConversionDialog(MainWindow *parent, QSharedPointer< VespucciWorkspace > ws)
VESPUCCI_EXPORT bool ImportWideText(std::string filename, arma::mat &spectra, arma::vec &abscissa, arma::vec &x, arma::vec &y, bool swap_spatial)
TextImport::ImportWideText.
Definition: textimport.cpp:97
void setLabel(const QString &new_label)
The MainWindow class The main window of the program, this is where the user performs most operations...
Definition: mainwindow.h:58
VESPUCCI_EXPORT bool SaveText(std::string basename, const arma::mat &spectra, const arma::vec &x, const arma::vec &y, const arma::vec &abscissa, arma::file_type type)
Definition: vespucci.cpp:59
VESPUCCI_EXPORT bool ImportLongText(std::string filename, arma::mat &spectra, arma::mat &abscissa, arma::vec &x, arma::vec &y, bool swap_spatial)
TextImport::ImportLongText.
Definition: textimport.cpp:311