qse  0.5.4
qsedataexport.cpp
Go to the documentation of this file.
1 #include "qsedataexport.h"
2 #include "qsedataexportpreview.h"
4 #include <QDirModel>
5 #include <QString>
6 #include "qsesettings.h"
7 #include <QMessageBox>
8 #include <QFileDialog>
9 
10 #include <stdio.h>
11 
12 QseDataExport::QseDataExport(const QString &id, QseDataSet *data, QList<int> selections, QWidget *parent)
13  : QDialog(parent),
14  m_Identifier(id),
15  m_Selections(selections),
16  m_DataSet(data),
17  m_Preview(NULL),
18  m_PreviewUI(NULL)
19 {
20  ui.setupUi(this);
21 
22  m_Preview = new QseDataExportPreview(this, this);
23 
24  readSettings();
25 
26  int i;
27  int nscans = data->scanCount();
28 
29  for (i = 0; i<nscans; i++) {
30  QString scanname = data->scan(i)->name();
31 
32  ui.m_ScanList->addItem(scanname);
33  }
34 
35  scanRevert();
36 
37  ui.m_DirectoryView->setModel(&m_DirModel);
38  ui.m_DirectoryView->hideColumn(1);
39  ui.m_DirectoryView->hideColumn(2);
40  ui.m_DirectoryView->hideColumn(3);
41 
43 
44  ui.m_OutputPatternEdit -> setText(m_OutputPattern);
45 
46  ui.m_SaveHeaderOption -> setChecked(m_Options & SaveHeader);
47  ui.m_BriefHeaderOption -> setChecked(m_Options & BriefHeader);
48  ui.m_SingleFileOption -> setChecked(m_Options & SingleFile);
49  ui.m_FileHeaderInEachOption -> setChecked(m_Options & FileHeaderInEach);
50  ui.m_ColumnHeaderLineOption -> setChecked(m_Options & ColumnHeaderLine);
51  ui.m_AutoRenameOption -> setChecked(m_Options & AutoRename);
52 
53  setWindowTitle(QString("Export Data from %1").arg(data->fileName()));
54 
56 }
57 
59 {
60  connect(ui.m_DirectoryCombo, SIGNAL(currentIndexChanged(const QString&)),
61  this, SLOT(directoryChanged(const QString&)));
62 
63  connect(ui.m_DirectoryNewButton, SIGNAL(clicked()), this, SLOT(newDirectory()));
64  connect(ui.m_DirectoryUpButton, SIGNAL(clicked()), this, SLOT(upDirectory()));
65  connect(ui.m_DirectoryHomeButton, SIGNAL(clicked()), this, SLOT(homeDirectory()));
66  connect(ui.m_DirectoryView, SIGNAL(doubleClicked(const QModelIndex&)),
67  this, SLOT(directoryChanged(const QModelIndex&)));
68 
69  connect(ui.m_ScanSelectAllButton, SIGNAL(clicked()), this, SLOT(scanSelectAll()));
70  connect(ui.m_ScanSelectNoneButton, SIGNAL(clicked()), this, SLOT(scanSelectNone()));
71  connect(ui.m_ScanRevertButton, SIGNAL(clicked()), this, SLOT(scanRevert()));
72 
73  connect(ui.m_OutputPatternEdit, SIGNAL(textChanged(const QString&)), this, SLOT(newOutputPattern(const QString&)));
74  connect(ui.m_SaveHeaderOption, SIGNAL(clicked(bool)), this, SLOT(saveHeaderOptionChanged(bool)));
75  connect(ui.m_BriefHeaderOption, SIGNAL(clicked(bool)), this, SLOT(briefHeaderOptionChanged(bool)));
76  connect(ui.m_SingleFileOption, SIGNAL(clicked(bool)), this, SLOT(singleFileOptionChanged(bool)));
77  connect(ui.m_FileHeaderInEachOption, SIGNAL(clicked(bool)), this, SLOT(fileHeaderInEachOptionChanged(bool)));
78  connect(ui.m_ColumnHeaderLineOption, SIGNAL(clicked(bool)), this, SLOT(columnHeaderLineOptionChanged(bool)));
79  connect(ui.m_AutoRenameOption, SIGNAL(clicked(bool)), this, SLOT(autoRenameOptionChanged(bool)));
80 
81  connect(ui.m_CancelButton, SIGNAL(clicked()), this, SLOT(cancelPressed()));
82  connect(ui.m_ExportButton, SIGNAL(clicked()), this, SLOT(exportPressed()));
83  connect(ui.m_PreviewButton, SIGNAL(clicked()), this, SLOT(previewPressed()));
84 
85  connect(this, SIGNAL(dataChanged()), m_Preview, SLOT(onDataChanged()));
86 }
87 
88 void QseDataExport::newOutputPattern(const QString &patt)
89 {
90  m_OutputPattern = patt;
91 }
92 
94 {
95  if (opt) {
97  } else {
99  }
100 }
101 
103 {
104  if (opt) {
106  } else {
108  }
109 }
110 
112 {
113  if (opt) {
115  } else {
116  m_Options &= ~SingleFile;
117  }
118 }
119 
121 {
122  if (opt) {
124  } else {
126  }
127 }
128 
130 {
131  if (opt) {
133  } else {
135  }
136 }
137 
139 {
140  if (opt) {
142  } else {
143  m_Options &= ~AutoRename;
144  }
145 }
146 
147 void QseDataExport::exportSelectedData(const QString &id, QseDataSet *data, QList<int> selections)
148 {
149  QseDataExport dialog(id, data, selections);
150 
151  dialog.exec();
152 }
153 
155 {
156  ui.m_DirectoryView->setRootIndex(m_DirModel.index(m_OutputDir.path()));
157 
158  ui.m_DirectoryCombo->blockSignals(true);
159 
160  ui.m_DirectoryCombo->clear();
161 
162  QDir qd = m_OutputDir;
163 
164  do {
165  if (qd.exists()) ui.m_DirectoryCombo->addItem(qd.absolutePath());
166  } while(qd.cdUp());
167 
168  ui.m_DirectoryCombo->blockSignals(false);
169 }
170 
172 {
173  QString newdir = QFileDialog::getExistingDirectory(this);
174 
175  if (newdir.length()) {
176  m_OutputDir.cd(newdir);
177  }
178 
180 }
181 
183 {
184  m_OutputDir.cdUp();
185 
187 }
188 
190 {
191  m_OutputDir.cd(QDir::currentPath());
192 
194 }
195 
196 void QseDataExport::directoryChanged(const QModelIndex& index)
197 {
198  QString fp = m_DirModel.filePath(index);
199 
200  directoryChanged(fp);
201 }
202 
203 void QseDataExport::directoryChanged(const QString& newdir)
204 {
205  QDir qd(newdir);
206 
207  if (qd.exists()) {
208  m_OutputDir.cd(newdir);
209 
211  }
212 }
213 
215 {
216  ui.m_ScanList->selectAll();
217 }
218 
220 {
221  ui.m_ScanList->clearSelection();
222 }
223 
225 {
226  int i;
227  ui.m_ScanList->clearSelection();
228 
229  foreach(i, m_Selections) {
230  QModelIndex sel = ui.m_ScanList->model()->index(i,0);
231 
232  ui.m_ScanList->selectionModel()->select(sel, QItemSelectionModel::Select);
233  }
234 }
235 
237 {
238  QList<int> res;
239  QList<QListWidgetItem*> selected = ui.m_ScanList->selectedItems();
240  QListWidgetItem* w;
241 
242  foreach(w, selected) {
243  res.append(ui.m_ScanList->row(w));
244  }
245 
246  return res;
247 }
248 
250 {
251  QList<QString> dups;
252  QList<QString> files;
253 
254  int nscans = m_Preview -> countFiles();
255 
256  for(int i=0; i<nscans; i++) {
257  QString fnam = m_Preview -> getFile(i);
258 
259  if (QFile::exists(m_OutputDir.filePath(fnam)) ||
260  files.contains(fnam)) {
261 
262  for (int n=1; ; n++) {
263  QString fnam2 = fnam+QString("_%1").arg(n,5,10,QLatin1Char('0'));
264 
265  if ((!QFile::exists(m_OutputDir.filePath(fnam2))) &&
266  (!files.contains(fnam2))) {
267  files.append(fnam2);
268  m_Preview -> setFile(i, fnam2);
269  break;
270  }
271  }
272  }
273  }
274 }
275 
277 {
278  QList<QString> dups;
279  QList<QString> files;
280  int i, nd=0, nid=0;
281  int nscans = m_Preview -> countFiles();
282 
283  for(int i=0; i<nscans; i++) {
284  QString fnam = m_Preview -> getFile(i);
285 
286  if (QFile::exists(m_OutputDir.filePath(fnam))) {
287  dups.append(fnam);
288  }
289 
290  if (files.contains(fnam)) {
291  nid++;
292  } else {
293  files.append(fnam);
294  }
295  }
296 
297  nd = dups.count();
298  QString msg;
299 
300  if (nd) {
301  msg.append(QString("%1 files will be overwritten by this operation").arg(nd));
302  for (i = 0; i < qMin(5, nd); i++) {
303  msg.append("\n");
304  msg.append(dups[i]);
305  }
306 
307  if (nd > 5) {
308  msg.append("\n...");
309  }
310  }
311 
312  if (nid) {
313  msg.append(QString("%1 of the output file names are duplicated").arg(nd));
314  }
315 
316  if (nd || nid) {
317  msg.append("\nHow do you want to proceed?");
318  QMessageBox msgBox;
319  QPushButton *goAheadButton = msgBox.addButton("Overwrite", QMessageBox::ActionRole);
320  QPushButton *renameButton = msgBox.addButton("Rename", QMessageBox::ActionRole);
321  QPushButton *cancelButton = msgBox.addButton("Cancel", QMessageBox::ActionRole);
322 
323  msgBox.setWindowTitle("Overwrite...");
324  msgBox.setText("Some of the filenames to be used for output are either duplicated or already exist. What do you want to do?");
325  msgBox.setDetailedText(msg);
326  msgBox.setIcon(QMessageBox::Question);
327 
328  msgBox.exec();
329 
330  if (msgBox.clickedButton() == goAheadButton) {
331  return true;
332  } else if (msgBox.clickedButton() == renameButton) {
333  autoRenameFiles();
334  return true;
335  } else {
336  return false;
337  }
338  }
339 
340  return true;
341 }
342 
344 {
345  updatePreview();
346 
347  if (m_PreviewUI == NULL) {
349  }
350 
351  m_PreviewUI -> show();
352 
353  emit dataChanged();
354 }
355 
357 {
358  updatePreview();
359 
360  if (checkForOverwriting()) {
361  writePreview();
362  done(0);
363  }
364 }
365 
367 {
368  done(0);
369 }
370 
372 {
373  int nfiles = m_Preview -> countFiles();
374 
375  for (int i=0; i<nfiles; i++) {
376  QString fnam = m_Preview -> getFile(i);
377  QString path = m_OutputDir.absoluteFilePath(fnam);
378 
379  FILE* f = fopen(qPrintable(path), "w");
380 
381  fputs(qPrintable(m_Preview -> getLines(i)), f);
382 
383  fclose(f);
384  }
385 }
386 
388 {
389  printf("Export Options = %d\n", m_Options);
390 
391  QString patt = ui.m_OutputPatternEdit -> text();
392  int i;
393  QList<int> selected = selectedScans();
394  QString fnam = "";
395  int f;
396 
397  m_Preview -> clear();
398 
399  foreach(i, selected) {
400  QseScan *scan = m_DataSet -> scan(i);
401  int nrows = scan -> maxRowCount();
402  int ncols = scan -> columnCount();
403 
404  if (fnam == "") {
405  if (m_Options & SingleFile) {
406  fnam = substituteFilenamePatterns(patt, 0, m_DataSet);
407  } else {
408  fnam = substituteFilenamePatterns(patt, i, m_DataSet);
409  }
410 
411 // if (QFile::exists(m_OutputDir.filePath(fnam))) {
412 // if (m_Options & AutoRename) {
413 // int n=1;
414 
415 // while (QFile::exists(m_OutputDir.filePath(fnam+QString("_%1").arg(n,5,10,QLatin1Char('0'))))) {
416 // n += 1;
417 // }
418 
419 // fnam = fnam+QString("_%1").arg(n,5,10,QLatin1Char('0'));
420 // } else {
421 // printf("Duplicate file name %s\n", qPrintable(fnam));
422 // }
423 // }
424 
425 // printf("Scan %d : %s\n", i, qPrintable(fnam));
426 
427  f = m_Preview -> appendNewFile(fnam);
428  }
429 
430  if (f >= 0) {
431  if (m_Options & FileHeaderInEach) {
432  QString s;
433  QStringList hdr = m_DataSet->header();
434 
435  foreach(s,hdr) {
436  m_Preview -> appendLine(f, s);
437  }
438  }
439 
440  if (m_Options & SaveHeader) {
441  QString s;
442  m_Preview -> appendLine(f, scan->scanCommand());
443  m_Preview -> appendLine(f, scan->scanDateLine());
444 
445  QStringList hdr = scan->header();
446 
447  foreach(s,hdr) {
448  m_Preview -> appendLine(f, s);
449  }
450 
451  m_Preview -> appendLine(f, scan->scanColumnCountLine());
452  m_Preview -> appendLine(f, scan->scanColumnNamesLine());
453  } else if (m_Options & BriefHeader) {
454  m_Preview -> appendLine(f, scan->scanCommand());
455  m_Preview -> appendLine(f, scan->scanDateLine());
456  m_Preview -> appendLine(f, scan->scanColumnCountLine());
457  m_Preview -> appendLine(f, scan->scanColumnNamesLine());
458  }
459 
460  if (m_Options & ColumnHeaderLine) {
461  QString colnames;
462  for (int i=0; i<ncols; i++) {
463  QseColumn *col = scan->column(i);
464  if (i == 0) {
465  colnames = col->name();
466  } else{
467  colnames += "\t";
468  colnames += col->name();
469  }
470  }
471 
472  m_Preview -> appendLine(f, colnames);
473  }
474 
475  for (int r=0; r<nrows; r++) {
476  QString dataline;
477  for (int c=0; c<ncols; c++) {
478  if (c == 0) {
479  dataline = tr("%1").arg(scan->column(c)->data(r));
480  } else {
481  dataline += "\t";
482  dataline += tr("%1").arg(scan->column(c)->data(r));
483  }
484  }
485  m_Preview -> appendLine(f, dataline);
486  }
487 
488  if (!(m_Options & SingleFile)) {
489  fnam = "";
490  }
491  }
492  }
493 
494  if (m_Options & AutoRename) {
495  autoRenameFiles();
496  }
497 
498  writeSettings();
499 }
500 
501 QString QseDataExport::substituteFilenamePatterns(const QString &patt, int index, const QseDataSet *ds)
502 {
503  QseScan *scan = ds->scan(index);
504  QString fnam = ds->fileName();
505  int scannum = scan -> scanNumber();
506  QDateTime dt = scan -> scanDateTime();
507 
508  QString res = patt;
509 
510  res.replace("%i", QString("%1").arg(index+1));
511  res.replace("%n", QString("%1").arg(scannum));
512  res.replace("%f", QFileInfo(fnam).completeBaseName());
513  res.replace("%x", QFileInfo(fnam).suffix());
514  res.replace("%d", dt.toString("yyyyMMdd_hhmmss"));
515  res.replace("%t", dt.toString("yyyyMMdd"));
516 
517  return res;
518 }
519 
521 {
522  QseSettings settings;
523 
524  m_Options = settings.value(QString("%1/options").arg(m_Identifier),0).toInt();
525  m_OutputDir.cd(settings.value(QString("%1/outputdir").arg(m_Identifier),QDir::currentPath()).toString());
526  m_OutputPattern = settings.value(QString("%1/outputpattern").arg(m_Identifier),"%f_%d_%n").toString();
527 }
528 
530 {
531  QseSettings settings;
532 
533  settings.setValue(QString("%1/options").arg(m_Identifier), int(m_Options));
534  settings.setValue(QString("%1/outputdir").arg(m_Identifier), m_OutputDir.absolutePath());
535  settings.setValue(QString("%1/outputpattern").arg(m_Identifier), m_OutputPattern);
536 }
int scanCount() const
Definition: qsedataset.cpp:31
QseScan * scan(int n) const
Definition: qsedataset.cpp:70
void newOutputPattern(const QString &patt)
void saveHeaderOptionChanged(bool opt)
const double * data() const
Definition: qsecolumn.cpp:62
QStringList header() const
Definition: qsescan.cpp:167
QseDataExportPreview * m_Preview
Definition: qsedataexport.h:85
void updateNewDirectory()
QseDataExport(const QString &id, QseDataSet *data, QList< int > selections, QWidget *parent=0)
void autoRenameFiles()
void autoRenameOptionChanged(bool opt)
QList< int > m_Selections
Definition: qsedataexport.h:78
Ui::QseDataExport ui
Definition: qsedataexport.h:76
QDirModel m_DirModel
Definition: qsedataexport.h:83
QList< int > selectedScans() const
QseDataSet * m_DataSet
Definition: qsedataexport.h:79
void singleFileOptionChanged(bool opt)
void briefHeaderOptionChanged(bool opt)
QString fileName() const
Definition: qsedataset.cpp:186
QString m_OutputPattern
Definition: qsedataexport.h:82
QString scanDateLine() const
Definition: qsescan.cpp:321
QString substituteFilenamePatterns(const QString &patt, int index, const QseDataSet *ds)
QString scanCommand() const
Definition: qsescan.cpp:226
QString scanColumnNamesLine() const
Definition: qsescan.cpp:335
void dataChanged()
bool checkForOverwriting()
static void exportSelectedData(const QString &id, QseDataSet *data, QList< int > selections)
void directoryChanged(const QModelIndex &index)
void columnHeaderLineOptionChanged(bool opt)
QString m_Identifier
Definition: qsedataexport.h:77
QString name() const
Definition: qsecolumn.cpp:26
QseColumn * column(int n) const
Definition: qsescan.cpp:104
QseDataExportPreviewUI * m_PreviewUI
Definition: qsedataexport.h:86
void fileHeaderInEachOptionChanged(bool opt)
void setupConnections()
QString scanColumnCountLine() const
Definition: qsescan.cpp:328
QString name() const
Definition: qsescan.cpp:31
QStringList header() const
Definition: qsedataset.cpp:125