Isis 3 Programmer Reference
FileTool.cpp
1 #include "FileTool.h"
2 
3 #include <cmath>
4 
5 #include <QAction>
6 #include <QApplication>
7 #include <QFileDialog>
8 #include <QFileInfo>
9 #include <QImage>
10 #include <QMenu>
11 #include <QMessageBox>
12 #include <QPainter>
13 #include <QPixmap>
14 #include <QPrinter>
15 #include <QPrintDialog>
16 #include <QToolBar>
17 
18 #include "Brick.h"
19 #include "BrowseDialog.h"
20 #include "CubeAttribute.h"
21 #include "Enlarge.h"
22 #include "FileDialog.h"
23 #include "Interpolator.h"
24 #include "MainWindow.h"
25 #include "MdiCubeViewport.h"
26 #include "OriginalLabel.h"
27 #include "Portal.h"
28 #include "ProcessRubberSheet.h"
29 #include "ProcessByLine.h"
30 #include "Pvl.h"
31 #include "Reduce.h"
32 #include "SaveAsDialog.h"
33 #include "SubArea.h"
34 #include "ViewportMainWindow.h"
35 #include "Workspace.h"
36 
37 namespace Isis {
46  FileTool::FileTool(QWidget *parent) : Tool(parent) {
47  p_parent = parent;
48  p_dir = "/thisDirDoesNotExist!";
49  p_open = new QAction(parent);
50  p_open->setShortcut(Qt::CTRL + Qt::Key_O);
51  p_open->setText("&Open...");
52  p_open->setIcon(QPixmap(toolIconDir() + "/fileopen.png"));
53  p_open->setToolTip("Open cube");
54  QString whatsThis =
55  "<b>Function:</b> Open an <i>Isis cube</i> in new viewport \
56  <p><b>Shortcut:</b> Ctrl+O\n</p> \
57  <p><b>Hint:</b> Use Ctrl or Shift in file dialog to open \
58  multiple cubes</p>";
59  p_open->setWhatsThis(whatsThis);
60  connect(p_open, SIGNAL(triggered()), this, SLOT(open()));
61 
62  p_browse = new QAction(parent);
63  p_browse->setShortcut(Qt::CTRL + Qt::Key_B);
64  p_browse->setText("&Browse...");
65  p_browse->setToolTip("Browse cubes");
66  whatsThis =
67  "<b>Function:</b> Browse a <i>Isis cubes</i> in new viewport \
68  <p><b>Shortcut:</b> Ctrl+B\n</p>";
69  p_browse->setWhatsThis(whatsThis);
70  connect(p_browse, SIGNAL(triggered()), this, SLOT(browse()));
71 
72  p_save = new QAction(parent);
73  p_save->setShortcut(Qt::CTRL + Qt::Key_S);
74  p_save->setText("&Save");
75  p_save->setIcon(QPixmap(toolIconDir() + "/filesave.png"));
76  p_save->setToolTip("Save");
77  whatsThis =
78  "<b>Function:</b> Save changes to the current Cube \
79  <p><b>Shortcut:</b> Ctrl+S</p>";
80  p_save->setWhatsThis(whatsThis);
81  connect(p_save, SIGNAL(triggered()), this, SLOT(save()));
82  p_save->setEnabled(false);
83 
84  p_saveAs = new QAction(parent);
85  p_saveAs->setText("Save &As...");
86  p_saveAs->setIcon(QPixmap(toolIconDir() + "/filesaveas.png"));
87  p_saveAs->setToolTip("Save As");
88  whatsThis =
89  "<b>Function:</b> Save the current Cube to the specified location";
90  p_saveAs->setWhatsThis(whatsThis);
91  connect(p_saveAs, SIGNAL(triggered()), this, SLOT(saveAs()));
92  p_saveAs->setEnabled(false);
93 
94  p_saveInfo = new QAction(parent);
95  p_saveInfo->setText("Save &Info...");
96  p_saveInfo->setIcon(QPixmap(toolIconDir() + "/filesaveas.png"));
97  p_saveInfo->setToolTip("Save Info");
98  whatsThis =
99  "<b>Function:</b> Save the current Cube's Whatsthis Info to the specified location";
100  p_saveInfo->setWhatsThis(whatsThis);
101  connect(p_saveInfo, SIGNAL(triggered()), this, SLOT(saveInfo()));
102  p_saveInfo->setEnabled(false);
103 
104  p_exportView = new QAction(parent);
105  p_exportView->setText("Export View");
106  p_exportView->setIcon(QPixmap(toolIconDir() + "/fileexport.png"));
107  p_exportView->setToolTip("Export View");
108  whatsThis =
109  "<b>Function:</b> Save visible contents of the active \
110  viewport as a png, jpg, tiff \
111  <p><b>Hint:</b> Your local installation of Qt may not support \
112  all formats. Reinstall Qt if necessary</p>";
113  p_exportView->setWhatsThis(whatsThis);
114  connect(p_exportView, SIGNAL(triggered()), this, SLOT(exportView()));
115  p_exportView->setEnabled(false);
116 
117  p_exportToList = new QAction(parent);
118  p_exportToList->setText("Export to List");
119  p_exportToList->setIcon(QPixmap(toolIconDir() + "/fileexport.png"));
120  p_exportToList->setToolTip("Export active cubes to a .lis file");
121  whatsThis =
122  "<b>Function:</b> Save all open cubes \
123  to a .lis file containing their file names";
124  p_exportToList->setWhatsThis(whatsThis);
125  connect(p_exportToList, SIGNAL(triggered()), this, SLOT(exportToList()));
126  p_exportToList->setEnabled(false);
127 
128  p_print = new QAction(parent);
129  p_print->setText("&Print...");
130  p_print->setShortcut(Qt::CTRL + Qt::Key_P);
131  p_print->setIcon(QPixmap(toolIconDir() + "/fileprint.png"));
132  p_print->setToolTip("Print");
133  whatsThis =
134  "<b>Function:</b> Print visible contents of the active viewport \
135  <p><b>Shortcut:</b> Ctrl+P</b>";
136  p_print->setWhatsThis(whatsThis);
137  connect(p_print, SIGNAL(triggered()), this, SLOT(print()));
138  p_print->setEnabled(false);
139 
140  p_closeAll = new QAction(parent);
141  p_closeAll->setText("&Close All...");
142  p_closeAll->setToolTip("Close All");
143  whatsThis =
144  "<b>Function:</b> Close all cube viewports.";
145  p_closeAll->setWhatsThis(whatsThis);
146 
147  p_exit = new QAction(this);
148  p_exit->setShortcut(Qt::CTRL + Qt::Key_Q);
149  p_exit->setText("E&xit");
150  p_exit->setIcon(QPixmap(toolIconDir() + "/fileclose.png"));
151  whatsThis =
152  "<b>Function:</b> Quit qview \
153  <p><b>Shortcut:</b> Ctrl+Q</p>";
154  p_exit->setWhatsThis(whatsThis);
155  connect(p_exit, SIGNAL(triggered()), this, SLOT(exit()));
156 
157  p_lastViewport = NULL;
158 
159  p_saveAsDialog = NULL;
160  activate(true);
161  }
162 
163 
169  void FileTool::addTo(QMenu *menu) {
170  menu->addAction(p_open);
171  menu->addAction(p_browse);
172  menu->addAction(p_save);
173  menu->addAction(p_saveAs);
174  menu->addAction(p_saveInfo);
175  menu->addAction(p_exportView);
176  menu->addAction(p_exportToList);
177  menu->addAction(p_print);
178  menu->addAction(p_closeAll);
179  menu->addAction(p_exit);
180  }
181 
182 
189  p_workSpace = ws;
190  Tool::addTo(ws);
191  connect(this, SIGNAL(fileSelected(QString)),
192  ws, SLOT(addCubeViewport(QString)));
193 
194  connect(p_closeAll, SIGNAL(triggered()), ws->mdiArea(), SLOT(closeAllSubWindows()));
195  }
196 
197 
204  perm->addAction(p_open);
205  perm->addAction(p_exportView);
206  perm->addAction(p_print);
207  perm->addAction(p_exit);
208  }
209 
210 
215  void FileTool::open() {
216  //Set up the list of filters that are default with this dialog.
217  if (!p_filterList.contains("Isis cubes (*.cub)")) {
218  p_filterList.append("Isis cubes (*.cub)");
219  p_filterList.append("All files (*)");
220  }
221  if (!p_dir.exists()) {
222  p_dir = QDir::current();
223  }
224 
225  FileDialog *fileDialog = new FileDialog("Open", p_filterList, p_dir, (QWidget *)parent());
226  fileDialog->show();
227  connect(fileDialog, SIGNAL(fileSelected(QString)),
228  p_workSpace, SLOT(addCubeViewport(QString)));
229  }
230 
231 
237  //Set up the list of filters that are default with this dialog.
238  if (!p_filterList.contains("Isis cubes (*.cub)")) {
239  p_filterList.append("Isis cubes (*.cub)");
240  p_filterList.append("All files (*)");
241  }
242  if (!p_dir.exists()) {
243  p_dir = QDir::current();
244  }
245  BrowseDialog *browser = new BrowseDialog("Browse", p_filterList, p_dir, (QWidget *)parent());
246  browser->show();
247  connect(browser, SIGNAL(fileSelected(QString)),
248  p_workSpace, SLOT(addBrowseView(QString)));
249  }
250 
251 
258  void FileTool::save() {
259  //If the current viewport is null (safety check), return from this method
260  if (cubeViewport() == NULL) {
261  QMessageBox::information((QWidget *)parent(), "Error", "No active cube to save");
262  return;
263  }
264 
265  emit saveChanges(cubeViewport());
266  p_save->setEnabled(false);
267 
268  cubeViewport()->cube()->reopen("rw");
269  }
270 
271 
285  //If the current viewport is null (safety check), return from this method
286  if (cubeViewport() == NULL) {
287  QMessageBox::information((QWidget *)parent(), "Error", "No active cube to save");
288  return;
289  }
290  //Set up the list of filters that are default with this dialog.
291  if (!p_filterList.contains("Isis cubes (*.cub)")) {
292  p_filterList.append("Isis cubes (*.cub)");
293  }
294  if (!p_dir.exists()) {
295  p_dir = QDir(p_lastDir);
296  }
297  if (p_saveAsDialog) {
298  delete p_saveAsDialog;
299  p_saveAsDialog = NULL;
300  }
301 
302  p_saveAsDialog = new SaveAsDialog("Save As", p_filterList, p_dir, (QWidget *)parent());
303  connect(p_saveAsDialog, SIGNAL(fileSelected(QString)), this, SLOT(saveAsCubeByOption(QString)));
304 
305  p_saveAsDialog->show();
306  }
307 
308 
317  void FileTool::saveAsCubeByOption(QString psOutFile) {
318  //If the current viewport is null (safety check), return from this method
319  if (cubeViewport() == NULL) {
320  QMessageBox::information((QWidget *)parent(), "Error",
321  "No active cube to save");
322  return;
323  }
324 
325  //If the filename is empty, return
326  if (psOutFile.isEmpty() || (p_saveAsDialog==NULL)){
327  QMessageBox::information((QWidget *)parent(), "Error",
328  "No output file selected");
329  return;
330  }
331 
332  //Check if the output file is already opened
333  QVector< MdiCubeViewport *> *vwportList = p_workSpace->cubeViewportList();
335  for (it = vwportList->begin(); it != vwportList->end(); ++it){
336  if (QString((*it)->cube()->fileName()) == psOutFile) {
337  QMessageBox::information((QWidget *)parent(), "Error",
338  "Output File is already open\n\""+ psOutFile + "\"");
339  return;
340  }
341  }
342 
343  //If the filename is the same as the current cube's filename, just save it
344  if (p_saveAsDialog->getSaveAsType() == SaveAsDialog::FullImage &&
345  psOutFile == cubeViewport()->cube()->fileName()) {
346  save();
347  return;
348  }
349 
350  //Save the current cube's changes by reopening it, and open an input cube
351  //from the current cube's location
352  Cube *icube = new Cube();
353  icube->open(cubeViewport()->cube()->fileName(), "rw");
354  Cube *ocube = NULL;
355 
356  // The output cube needs to be created if the scale is 1 since saveAs_FullResolution will be
357  // called, which expects a cube.
358  // NOTE: This really should be cleaned up and the cube should be created in 1 place.
359  if (p_saveAsDialog->getSaveAsType() != SaveAsDialog::ExportAsIs ||
360  p_lastViewport->scale() == 1) {
361  //Create the output cube
362  ocube = new Cube;
363  }
364 
365  if (p_saveAsDialog->getSaveAsType() == SaveAsDialog::FullImage) {
366  copyCubeDetails(psOutFile, icube, ocube, icube->sampleCount(),
367  icube->lineCount(), icube->bandCount());
368  saveAsFullImage(icube, ocube);
369  ocube->close();
370  }
371  else {
372  // start and end Samples and Lines
373  double dStartSample=0, dEndSample=0, dStartLine=0, dEndLine=0;
374  p_lastViewport->getCubeArea(dStartSample, dEndSample, dStartLine, dEndLine);
375 
376  if (p_saveAsDialog->getSaveAsType() == SaveAsDialog::ExportFullRes ||
377  p_lastViewport->scale() == 1) {
378 // int numSamples = (int)ceil(dEndSample-dStartSample);
379 // int numLines = (int)ceil(dEndLine-dStartLine);
380  int numSamples = (int)((dEndSample - dStartSample + 1) + 0.5);
381  int numLines = (int)((dEndLine - dStartLine + 1) + 0.5);
382  copyCubeDetails(psOutFile, icube, ocube, numSamples, numLines, icube->bandCount());
383  saveAs_FullResolution(icube, ocube, numSamples, numLines);
384  ocube->close();
385  }
386  else if (p_saveAsDialog->getSaveAsType() == SaveAsDialog::ExportAsIs ) {
387  saveAs_AsIs(icube, psOutFile);
388  }
389  }
390 
391  emit(fileSelected(psOutFile));
392 
393  //Disable the save action
394  p_save->setEnabled(false);
395 
396  p_lastDir = psOutFile;
397  }
398 
399 
410  void FileTool::saveAsEnlargedCube(Cube *icube, const QString & psOutFile) {
411  double dScale = p_lastViewport->scale();
412 
413  // start and end Samples and Lines
414  double dStartSample=0, dEndSample=0, dStartLine=0, dEndLine=0;
415  p_lastViewport->getCubeArea(dStartSample, dEndSample, dStartLine, dEndLine);
416 
417  double ins = dEndSample - dStartSample + 1;
418  double inl = dEndLine - dStartLine + 1;
419 
420  double ons = (int)(ins * dScale + 0.5);
421  double onl = (int)(inl * dScale + 0.5);
422 
423  try {
425  p.SetInputCube (icube);
426  Cube *ocube = p.SetOutputCube(psOutFile, CubeAttributeOutput(" "),
427  ons, onl, icube->bandCount());
428 
429  Interpolator *interp = new Interpolator(Interpolator::NearestNeighborType);
430 
431  Enlarge *imgEnlarge = new Enlarge(icube, dScale, dScale);
432  imgEnlarge->SetInputArea((int)dStartSample, (int)dEndSample, (int)dStartLine, (int)dEndLine);
433 
434  p.StartProcess(*imgEnlarge, *interp);
435  imgEnlarge->UpdateOutputLabel(ocube);
436  p.EndProcess();
437 
438  delete imgEnlarge;
439  delete interp;
440 
441  } catch(IException &e) {
442  // Stacks error message from Cube's create method
443  throw IException(e,
445  QObject::tr("The cube could not be saved, unable to create the cube"),
446  _FILEINFO_);
447  }
448  }
449 
450 
460  void FileTool::saveAsReducedCube(Cube *icube, const QString & psOutFile) {
461 
462  double dScale = p_lastViewport->scale();
463  // start and end Samples and Lines
464  double dStartSample=0, dEndSample=0, dStartLine=0, dEndLine=0;
465  p_lastViewport->getCubeArea(dStartSample, dEndSample, dStartLine, dEndLine);
466 
467  double ins = dEndSample - dStartSample + 1;
468  double inl = dEndLine - dStartLine + 1;
469 
470  double ons = (int)(ins * dScale + 0.5);
471  double onl = (int)(inl * dScale + 0.5);
472 
473  CubeAttributeInput cai(icube->fileName());
474  std::vector<QString> bands = cai.bands();
475  int inb = bands.size();
476 
477  if (inb == 0) {
478  inb = cubeViewport()->cube()->bandCount();
479  for(int i = 1; i <= inb; i++) {
480  bands.push_back(toString(i));
481  }
482  }
483 
484  try {
485  ProcessByLine p;
486  p.SetInputCube (icube);
487  Cube *ocube = NULL;
488  ocube = p.SetOutputCube(psOutFile, CubeAttributeOutput(""), ons, onl, inb);
489  // Our processing routine only needs 1
490  // the original set was for info about the cube only
491  p.ClearInputCubes();
492 
493  Cube *tempcube=new Cube;
494  tempcube->open(cubeViewport()->cube()->fileName(), "r");
495  Nearest *near = new Nearest(tempcube, ins/ons, inl/onl);
496  near->setInputBoundary((int)dStartSample, (int)dEndSample, (int)dStartLine, (int)dEndLine);
497 
498  p.ProcessCubeInPlace(*near, false);
499  near->UpdateOutputLabel(ocube);
500  p.EndProcess();
501 
502  delete near;
503  near=NULL;
504  }
505  catch(IException &e) {
506  // If there is a problem, catch it and close the cube so it isn't open next time around
507  icube->close();
508  // Stacks error message from Cube's create method
509  throw IException(e,
511  QObject::tr("The cube could not be saved, unable to create the cube"),
512  _FILEINFO_);
513  }
514  }
515 
516 
525  void FileTool::saveAs_AsIs(Cube *icube, const QString & psOutFile) {
526  double dScale = p_lastViewport->scale();
527  // Enlarge the cube area
528  if (dScale > 1) {
529  saveAsEnlargedCube(icube, psOutFile);
530  }
531  // Reduce the cube area
532  else {
533  saveAsReducedCube(icube, psOutFile);
534  }
535  }
536 
537 
552  void FileTool::copyCubeDetails(const QString & psOutFile, Cube *icube,
553  Cube *ocube, int piNumSamples, int piNumLines, int piNumBands) {
554  //Create the default output attribute with the output filename
555  CubeAttributeOutput outAtt(psOutFile);
556 
557  //Propagate all labels, tables, blobs, etc from the input to output cube
558  try {
559  ocube->setDimensions(piNumSamples, piNumLines, piNumBands);
560  ocube->setByteOrder(outAtt.byteOrder());
561  ocube->setFormat(outAtt.fileFormat());
562  ocube->setLabelsAttached(outAtt.labelAttachment() == AttachedLabel);
563 
564  if (outAtt.propagatePixelType()) {
565  ocube->setPixelType(icube->pixelType());
566  }
567  else {
568  ocube->setPixelType(outAtt.pixelType());
569  }
570 
571  if (outAtt.propagateMinimumMaximum()) {
572  if (ocube->pixelType() == Real) {
573  ocube->setBaseMultiplier(0.0, 1.0);
574  }
575  else if (ocube->pixelType() >= icube->pixelType()) {
576  double base = icube->base();
577  double mult = icube->multiplier();
578  ocube->setBaseMultiplier(base, mult);
579  }
580  else if ((ocube->pixelType() != Real) &&
581  (ocube->pixelType() != UnsignedByte) &&
582  (ocube->pixelType() != SignedWord) &&
583  (ocube->pixelType() != UnsignedWord) &&
584  (ocube->pixelType() != Isis::UnsignedInteger) &&
585  (ocube->pixelType() != Isis::SignedInteger)) {
586  QString msg = "Looks like your refactoring to add different pixel types";
587  msg += " you'll need to make changes here";
589  }
590  else {
591  QString msg = "You've chosen to reduce your output PixelType for [" +
592  psOutFile + "] you must specify the output pixel range too";
594  }
595  }
596  else {
597  // Not propagating so either the user entered or the programmer did
598  ocube->setMinMax(outAtt.minimum(), outAtt.maximum());
599  }
600 
601  int needLabBytes = icube->labelSize(true) + (1024 * 6);
602  if (needLabBytes > ocube->labelSize()) {
603  ocube->setLabelSize(needLabBytes);
604  }
605 
606  // Allocate the cube
607  ocube->create(psOutFile);
608 
609  // Transfer labels from the first input cube
610  PvlObject &incube = icube->label()->findObject("IsisCube");
611  PvlObject &outcube = ocube->label()->findObject("IsisCube");
612  for(int i = 0; i < incube.groups(); i++) {
613  outcube.addGroup(incube.group(i));
614  }
615 
616  // Transfer tables from the first input cube
617  Pvl &inlab = *icube->label();
618  for(int i = 0; i < inlab.objects(); i++) {
619  if (inlab.object(i).isNamed("Table")) {
620  Blob t((QString)inlab.object(i)["Name"], inlab.object(i).name());
621  icube->read(t);
622  ocube->write(t);
623  }
624  }
625 
626  // Transfer blobs from the first input cube
627  inlab = *icube->label();
628  for(int i = 0; i < inlab.objects(); i++) {
629  if (inlab.object(i).isNamed("Polygon")) {
630  Blob t((QString)inlab.object(i)["Name"], inlab.object(i).name());
631  icube->read(t);
632  ocube->write(t);
633  }
634  }
635 
636  // Transfer tables from the first input cube
637  inlab = *icube->label();
638  for(int i = 0; i < inlab.objects(); i++) {
639  if (inlab.object(i).isNamed("OriginalLabel")) {
640  OriginalLabel ol;
641  icube->read(ol);
642  ocube->write(ol);
643  }
644  }
645  }
646  catch(IException &) {
647  delete ocube;
648  throw;
649  }
650  }
651 
652 
663  void FileTool::saveAsFullImage(Cube *icube, Cube *ocube) {
664  //Start the copy process line by line
665  Brick ibrick(*icube, icube->sampleCount(), 1, 1);
666  Brick obrick(*ocube, ocube->sampleCount(), 1, 1);
667 
668  int numBricks;
669  if (ibrick.Bricks() > obrick.Bricks()) {
670  numBricks = ibrick.Bricks();
671  }
672  else {
673  numBricks = obrick.Bricks();
674  }
675 
676  // Loop and let the app programmer work with the bricks
677  ibrick.begin();
678  obrick.begin();
679  for(int i = 0; i < numBricks; i++) {
680  icube->read(ibrick);
681  //Copy the contents to the output cube
682  copy(ibrick, obrick);
683  ocube->write(obrick);
684  ibrick++;
685  obrick++;
686  }
687  }
688 
689 
702  Cube *pOutCube, int pNumSamples, int pNumLines) {
703  // start and end Samples and Lines
704  double dStartSample=0, dEndSample=0, dStartLine=0, dEndLine=0;
705  p_lastViewport->getCubeArea(dStartSample, dEndSample, dStartLine, dEndLine);
706  int iNumBands = pInCube->bandCount();
707 
708  PvlGroup results("Results");
709  results += PvlKeyword("InputLines", toString(pInCube->lineCount()));
710  results += PvlKeyword("InputSamples", toString(pInCube->sampleCount()));
711  results += PvlKeyword("StartingLine", toString(dStartLine));
712  results += PvlKeyword("StartingSample", toString(dStartSample));
713  results += PvlKeyword("EndingLine", toString(dEndLine));
714  results += PvlKeyword("EndingSample", toString(dEndSample));
715  results += PvlKeyword("LineIncrement", toString(1));
716  results += PvlKeyword("SampleIncrement", toString(1));
717  results += PvlKeyword("OutputLines", toString(pNumLines));
718  results += PvlKeyword("OutputSamples", toString(pNumSamples));
719  SubArea subArea;
720  subArea.SetSubArea(pInCube->lineCount(), pInCube->sampleCount(), dStartLine, dStartSample,
721  dEndLine, dEndSample, 1.0, 1.0);
722  subArea.UpdateLabel(pInCube, pOutCube, results);
723 
724  Portal iPortal (pNumSamples, 1, pInCube->pixelType());
725  Portal oPortal (pNumSamples, 1, pOutCube->pixelType());
726 
727  for(int iBand=1; iBand<=iNumBands; iBand++) {
728  int ol=1;
729  for(int iLine=(int)dStartLine; iLine<=(int)dEndLine; iLine++) {
730  iPortal.SetPosition(dStartSample, iLine, iBand);
731  pInCube->read(iPortal);
732 
733  oPortal.SetPosition(1, ol++, iBand);
734  pOutCube->read(oPortal);
735 
736  oPortal.Copy(iPortal);
737  pOutCube->write(oPortal);
738  }
739  }
740  }
741 
742 
750  {
751  if (cubeViewport() == NULL) {
752  QMessageBox::information((QWidget *)parent(), "Error", "No active cube to save info");
753  return;
754  }
755 
756  //Get the new cube's filename
757  QString output =
758  QFileDialog::getSaveFileName((QWidget *)parent(),
759  "Choose output file",
760  p_lastDir,
761  QString("PVL Files (*.pvl)"));
762 
763  //If the filename is empty, return
764  if (output.isEmpty()) {
765  return;
766  }
767  else if (!output.endsWith(".pvl")) {
768  output += ".pvl";
769  }
770 
771  Pvl whatsThisPvl;
772  cubeViewport()->getAllWhatsThisInfo(whatsThisPvl);
773  whatsThisPvl.write(output);
774  }
775 
776 
783  void FileTool::copy(Buffer &in, Buffer &out) {
784  out.Copy(in);
785  }
786 
787 
795  }
796 
797 
803  if (cubeViewport() == NULL) {
804  QMessageBox::information((QWidget *)parent(), "Error", "No active cube to export");
805  return;
806  }
807 
808  QString output =
809  QFileDialog::getSaveFileName((QWidget *)parent(),
810  QString("Choose output file"),
811  p_lastDir,
812  QString("PNG (*.png);;JPG (*.jpg);;TIF (*.tif)"));
813  if (output.isEmpty()) return;
814 
815  p_lastDir = output;
816 
817  QString format = QFileInfo(output).suffix();
818 
819  if (format.isEmpty()) {
820  if (output.endsWith('.')) {
821  output.append(QString("png"));
822  }
823  else {
824  output.append(QString(".png"));
825  }
826  }
827  else if (format.compare("png", Qt::CaseInsensitive) &&
828  format.compare("jpg", Qt::CaseInsensitive) &&
829  format.compare("tif", Qt::CaseInsensitive)) {
830 
831  QMessageBox::information((QWidget *)parent(), "Error", format + " is an invalid extension.");
832  return;
833  }
834 
835  QPixmap pm = cubeViewport()->viewport()->grab();
836 
837  //if (!cubeViewport()->pixmap().save(output,format)) {
838 
839  if (!pm.save(output)) {
840  QMessageBox::information((QWidget *)parent(), "Error", "Unable to save " + output);
841  return;
842  }
843  }
844 
845 
855  if (cubeViewport() == NULL) {
856  QMessageBox::information((QWidget *)parent(), "Error", "No active cubes to export");
857  return;
858  }
859 
860  // The ViewportMainWindow is the parent container to the FileTool.
861  // We need to grab that object so that we can loop through it's children
862  // To find the active cube viewports.
863  ViewportMainWindow* window = dynamic_cast<ViewportMainWindow*>(parent());
864 
865  if (window == NULL) {
866  QMessageBox::critical((QWidget *)parent(), "Error", "There was an error reading the viewport window.");
867  return;
868  }
869 
870  QList<CubeViewport*> openCubes = window->findChildren<CubeViewport*>();
871  QList<QString> cubeFilePaths;
872 
873  for (int i = 0; i < openCubes.size(); i++) {
874 
875  CubeViewport* cubeViewport = openCubes.value(i);
876 
877 // QString cubeFileName(cubeViewport->cube()->fileName());
878  QFileInfo cubeFileName(cubeViewport->cube()->fileName());
879 
880  cubeFilePaths.append(cubeFileName.absoluteFilePath());
881  }
882 
883  QString fileName = QFileDialog::getSaveFileName((QWidget *) parent(),
884  "Export to cube list",
885  ".",
886  "Cube List (*.lis)");
887 
888  if (!fileName.contains(".lis")) {
889  fileName.append(".lis");
890  }
891 
892  QFile outputFile(fileName);
893  outputFile.open(QIODevice::WriteOnly | QIODevice::Text);
894 
895  QTextStream out(&outputFile);
896 
897  // Write each cube filename onto it's own line inside of that file.
898  for (int i = 0; i < cubeFilePaths.size(); i++){
899  out << cubeFilePaths.value(i) << "\n";
900  }
901  outputFile.close();
902  }
903 
904 
910  // Is there anything to print
911  if (cubeViewport() == NULL) {
912  QMessageBox::information((QWidget *)parent(), "Error", "No active cube to print");
913  return;
914  }
915 
916  // Initialize a printer
917  static QPrinter *printer = NULL;
918  if (printer == NULL) printer = new QPrinter;
919  printer->setPageSize(QPrinter::Letter);
920  printer->setColorMode(QPrinter::GrayScale);
921  if (cubeViewport()->isColor()) printer->setColorMode(QPrinter::Color);
922 
923  QPrintDialog printDialog(printer, (QWidget *)parent());
924  if (printDialog.exec() == QDialog::Accepted) {
925  // Get display widget as a pixmap and convert to an image
926  QPixmap pixmap = cubeViewport()->viewport()->grab();
927  QImage img = pixmap.toImage();
928 
929  // C++ Gui Programmign with Qt, page 201
930  QPainter painter(printer);
931  QRect rect = painter.viewport();
932  QSize size = img.size();
933  size.scale(rect.size(), Qt::KeepAspectRatio);
934  painter.setViewport(rect.x(), rect.y(),
935  size.width(), size.height());
936  painter.setWindow(img.rect());
937  painter.drawImage(0, 0, img);
938  }
939  }
940 
941 
946  // Close all cubes
947  // We must create a temporary list. If not the actual
948  // list size gets modified when a close occurs and not all
949  // windows were being closed.
950  MdiCubeViewport *d;
952  for(int i = 0; i < (int)tempList.size(); i++) {
953  d = tempList.at(i);
954  //Set the current viewport to the one being closed
955  setCubeViewport(d);
956 
957  if (!d->parentWidget()->close()) {
958  return false;
959  }
960  }
961  return true;
962  }
963 
964 
973  void FileTool::exit() {
974  QWidget *mainWindow = qobject_cast<QWidget *>(p_parent);
975 
976  if (mainWindow)
977  mainWindow->close();
978  }
979 
985  void FileTool::enableSave(bool enable) {
986  p_save->setEnabled(enable);
987  }
988 
994  if (cubeViewport() == NULL) {
995  if (p_lastViewport != NULL) {
996  p_lastViewport = NULL;
997  }
998  p_print->setEnabled(false);
999  p_save->setEnabled(false);
1000  p_exportView->setEnabled(false);
1001  p_exportToList->setEnabled(false);
1002  p_saveAs->setEnabled(false);
1003  p_saveInfo->setEnabled(false);
1004  }
1005  else {
1006  if (p_lastViewport == NULL) {
1007  //Set the last viewport to the current viewport and connect signals to save and discard
1009  connect(p_lastViewport, SIGNAL(saveChanges(CubeViewport *)), this, SLOT(save()));
1010  }
1011  else {
1012  if (p_lastViewport != cubeViewport()) {
1013  //If the viewport has changes made to it enable the save action
1014  if (cubeViewport()->parentWidget()->windowTitle().endsWith("*")) {
1015  p_save->setEnabled(true);
1016  }
1017  //Else disable it
1018  else {
1019  p_save->setEnabled(false);
1020  }
1021  //disconnect signals from the old viewport and connect them to the new viewport
1022  disconnect(p_lastViewport, SIGNAL(saveChanges(CubeViewport *)), this, SLOT(save()));
1023  disconnect(p_lastViewport, SIGNAL(discardChanges(CubeViewport *)), this, SLOT(discard()));
1025  connect(p_lastViewport, SIGNAL(saveChanges(CubeViewport *)), this, SLOT(save()));
1026  connect(p_lastViewport, SIGNAL(discardChanges(CubeViewport *)), this, SLOT(discard()));
1027  }
1028  }
1029  p_print->setEnabled(true);
1030  p_exportView->setEnabled(true);
1031  p_exportToList->setEnabled(true);
1032  p_saveAs->setEnabled(true);
1033  p_saveInfo->setEnabled(true);
1034  }
1035  }
1036 }
PvlObject & object(const int index)
Return the object at the specified index.
Definition: PvlObject.cpp:460
virtual void exportToList()
FileTool::exportToList.
Definition: FileTool.cpp:854
QPointer< QAction > p_browse
Action to browse and open files.
Definition: FileTool.h:122
Cube display widget for certain Isis MDI applications.
Buffer for reading and writing cube data.
Definition: Buffer.h:69
void saveChanges(CubeViewport *vp)
This signal is called when changes should be saved.
Manipulate and parse attributes of input cube filenames.
QPointer< QAction > p_exportView
Action to export the view as a picture.
Definition: FileTool.h:127
Read and store original labels.
Definition: OriginalLabel.h:51
void ProcessCubeInPlace(const Functor &funct, bool threaded=true)
Same functionality as StartProcess(void funct(Isis::Buffer &inout)) using Functors.
QString toolIconDir() const
returns the path to the icon directory.
Definition: Tool.h:127
virtual void StartProcess(Transform &trans, Interpolator &interp)
Applies a Transform and an Interpolator to every pixel in the output cube.
void EndProcess()
End the processing sequence and cleans up by closing cubes, freeing memory, etc.
PixelType pixelType() const
Return the pixel type as an Isis::PixelType.
Cube * cube() const
Definition: CubeViewport.h:348
void fileSelected(QString)
This signal is called when a file is selected.
void discard()
This slot emits a signal to discard all changes to the current viewport.
Definition: FileTool.cpp:793
Isis::Cube * SetInputCube(const QString &parameter, const int requirements=0)
Opens an input cube specified by the user and verifies requirements are met.
virtual void open()
This method allows the user to navigate and open a cube with a file dialog.
Definition: FileTool.cpp:215
ByteOrder byteOrder() const
Return the byte order as an Isis::ByteOrder.
QDir p_dir
Directory.
Definition: FileTool.h:76
int objects() const
Returns the number of objects.
Definition: PvlObject.h:231
QStringList p_filterList
Filter List.
Definition: FileTool.h:74
Class for browsing cubes.
Definition: FileDialog.h:32
void discardChanges(CubeViewport *vp)
This signal is called when changes should be discarded.
static void copy(Buffer &in, Buffer &out)
This method copies from the input buffer to the output buffer.
Definition: FileTool.cpp:783
virtual void saveInfo()
Saves the whatsthis info of the cubeviewport.
Definition: FileTool.cpp:749
QPointer< QAction > p_open
Action to open a file.
Definition: FileTool.h:121
CubeViewportList * cubeViewportList() const
Return the list of cubeviewports.
Definition: Tool.cpp:390
Buffer for containing a two dimensional section of an image.
Definition: Portal.h:52
void saveAsFullImage(Cube *icube, Cube *ocube)
Saves the cube as a full image.
Definition: FileTool.cpp:663
Widget to save(Save As) Isis cubes(used in qview) to display the FileDialog to select the output cube...
Definition: SaveAsDialog.h:48
void enableSave(bool enable)
This slot enables or disables save and save as.
Definition: FileTool.cpp:985
double base() const
Returns the base value for converting 8-bit/16-bit pixels to 32-bit.
Definition: Cube.cpp:1142
virtual bool closeAll()
Try to close all open cubes and save/discard if necessary.
Definition: FileTool.cpp:945
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition: PvlObject.h:286
int sampleCount() const
Definition: Cube.cpp:1452
void getAllWhatsThisInfo(Pvl &pWhatsThisPvl)
Get All WhatsThis info - viewport, cube, area in PVL format.
virtual Cube * SetOutputCube(const QString &fname, const CubeAttributeOutput &att)
Create the output file.
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Definition: PvlObject.h:198
void addTo(QMenu *menu)
Adds the file tool&#39;s actions to the menu.
Definition: FileTool.cpp:169
Buffer for containing a three dimensional section of an image.
Definition: Brick.h:61
void setFormat(Format format)
Used prior to the Create method, this will specify the format of the cube, either band...
Definition: Cube.cpp:981
void setCubeViewport(MdiCubeViewport *cvp)
Sets the current viewport to the given cvp.
Definition: Tool.cpp:160
void print()
This method allows the user to print the current viewport.
Definition: FileTool.cpp:909
bool propagatePixelType() const
Return true if the pixel type is to be propagated from an input cube.
void setLabelsAttached(bool attached)
Use prior to calling create, this sets whether or not to use separate label and data files...
Definition: Cube.cpp:993
bool propagateMinimumMaximum() const
Return true if the min/max are to be propagated from an input cube.
virtual void save()
This method saves any changes made to the current cube, these changes are finalized! There is no undo...
Definition: FileTool.cpp:258
void addToPermanent(QToolBar *perm)
Adds the file tool&#39;s actions to the permanent toolbar.
Definition: FileTool.cpp:203
void saveAsEnlargedCube(Cube *icube, const QString &psOutFile)
Save image AsIs Enlarged into output.
Definition: FileTool.cpp:410
void Copy(const Buffer &in, bool includeRawBuf=true)
Allows copying of the buffer contents to another Buffer.
Definition: Buffer.cpp:271
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:226
Widget to display Isis cubes for qt apps.
Definition: CubeViewport.h:132
virtual void browse()
This method allows the user to navigate and browse cubes with a file dialog .
Definition: FileTool.cpp:236
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
A type of error that occurred when performing an actual I/O operation.
Definition: IException.h:171
QPointer< QAction > p_exit
Action to exit qview.
Definition: FileTool.h:130
virtual void EndProcess()
End the processing sequence and cleans up by closing cubes, freeing memory, etc.
Definition: Process.cpp:483
void setDimensions(int ns, int nl, int nb)
Used prior to the Create method to specify the size of the cube.
Definition: Cube.cpp:932
virtual void saveAs()
SaveAs Action - Displays the FileDialog with the filterlist (*.cub) to select the output cube...
Definition: FileTool.cpp:284
virtual void exportView()
This method allows the user to export the current view as an image file.
Definition: FileTool.cpp:802
void reopen(QString access="r")
This method will reopen an isis sube for reading or reading/writing.
Definition: Cube.cpp:691
QString name() const
Returns the container name.
Definition: PvlContainer.h:77
This was called the Qisis MainWindow.
QPointer< QAction > p_print
Action to print the current view.
Definition: FileTool.h:123
void updateTool()
Updates the tool.
Definition: FileTool.cpp:993
void SetSubArea(const int orignl, const int origns, const int sl, const int ss, const int el, const int es, const double linc, const double sinc)
Defines the subarea.
Definition: SubArea.cpp:76
void setPixelType(PixelType pixelType)
Used prior to the Create method, this will specify the output pixel type.
Definition: Cube.cpp:1019
Derivative of Process, designed for geometric transformations.
void saveAs_AsIs(Cube *icube, const QString &psOutFile)
Save image AsIs (As viewed in the viewport window) into output file.
Definition: FileTool.cpp:525
int Bricks()
Returns the number of Bricks in the cube.
Definition: Brick.h:184
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
void saveAsCubeByOption(QString)
Save as Isis Cube (FullImage, AsIs, FullRes)
Definition: FileTool.cpp:317
virtual Isis::Cube * SetInputCube(const QString &parameter, const int requirements=0)
Opens an input cube specified by the user and verifies requirements are met.
Definition: Process.cpp:243
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
Enlarge the pixel dimensions of an image.
Definition: Enlarge.h:48
QPointer< QAction > p_save
Action to save the current cube.
Definition: FileTool.h:124
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
Manipulate and parse attributes of output cube filenames.
A type of error that could only have occurred due to a mistake on the user&#39;s part (e...
Definition: IException.h:142
void setBaseMultiplier(double base, double mult)
Used prior to the Create method, this will specify the base and multiplier for converting 8-bit/16-bi...
Definition: Cube.cpp:866
A single keyword-value pair.
Definition: PvlKeyword.h:98
void saveAs_FullResolution(Cube *pInCube, Cube *pOutCube, int pNumSamples, int pNumLines)
Save image Full Resolution (image viewed in the viewport window) into output.
Definition: FileTool.cpp:701
void setInputBoundary(int startSample, int endSample, int startLine, int endLine)
Parameters to input image sub area.
Definition: Reduce.cpp:81
void ClearInputCubes()
Close owned input cubes from the list and clear the list.
Definition: Process.cpp:519
Class for browsing cubes.
Definition: BrowseDialog.h:28
void close(bool remove=false)
Closes the cube and updates the labels.
Definition: Cube.cpp:177
double maximum() const
Return the output cube attribute maximum.
void read(Blob &blob) const
This method will read data from the specified Blob object.
Definition: Cube.cpp:724
void open(const QString &cfile, QString access="r")
This method will open an isis cube for reading or reading/writing.
Definition: Cube.cpp:544
The input label is embedded in the image file.
Definition: CubeAttribute.h:48
Cube::Format fileFormat() const
Return the file format an Cube::Format.
void addTo(ViewportMainWindow *mw)
Adds the tool to the application.
Definition: Tool.cpp:78
Functor for reduce using near functionality.
Definition: Reduce.h:83
void SetInputArea(double pdStartSample, double pdEndSample, double pdStartLine, double pdEndLine)
Sets the sub area dimensions of the input image.
Definition: Enlarge.cpp:89
Container for cube-like labels.
Definition: Pvl.h:135
FileTool(QWidget *parent)
Constructs a FileTool object.
Definition: FileTool.cpp:46
PixelType pixelType() const
Definition: Cube.cpp:1403
virtual Isis::Cube * SetOutputCube(const QString &parameter)
Allocates a user-specified output cube whose size matches the first input cube.
Definition: Process.cpp:266
void SetPosition(const double sample, const double line, const int band)
Sets the line and sample position of the buffer.
Definition: Portal.h:109
Apply corrections to a cube label for subarea extraction.
Definition: SubArea.h:63
void setByteOrder(ByteOrder byteOrder)
Used prior to the Create method, this will specify the byte order of pixels, either least or most sig...
Definition: Cube.cpp:917
Isis::PvlGroup UpdateOutputLabel(Isis::Cube *pOutCube)
Create label for the reduced output image.
Definition: Reduce.cpp:106
PvlGroup UpdateOutputLabel(Cube *pOutCube)
Update the Mapping, Instrument, and AlphaCube groups in the output cube label.
Definition: Enlarge.cpp:127
void UpdateLabel(Cube *icube, Cube *ocube, PvlGroup &results)
Modifies a label for a file containing a subarea.
Definition: SubArea.cpp:142
bool isNamed(const QString &match) const
Returns whether the given string is equal to the container name or not.
Definition: PvlContainer.h:86
void copyCubeDetails(const QString &psFileName, Cube *icube, Cube *ocube, int piNumSamples, int piNumLines, int piNumBands)
Copy input cube details into output file given its dimensions.
Definition: FileTool.cpp:552
virtual void exit()
Exit the program, this slot called when the exit is chosen from the File menu.
Definition: FileTool.cpp:973
PvlGroup & group(const int index)
Return the group at the specified index.
Definition: PvlObject.cpp:423
void setMinMax(double min, double max)
Used prior to the Create method, this will compute a good base and multiplier value given the minimum...
Definition: Cube.cpp:883
QPointer< QWidget > p_parent
The parent widget of this object.
Definition: FileTool.h:131
Pvl * label() const
Returns a pointer to the IsisLabel object associated with the cube.
Definition: Cube.cpp:1346
Pixel interpolator.
Definition: Interpolator.h:51
QPointer< Workspace > p_workSpace
The workspace being used.
Definition: FileTool.h:133
virtual QString fileName() const
Returns the opened cube&#39;s filename.
Definition: Cube.cpp:1208
int lineCount() const
Definition: Cube.cpp:1379
void write(Blob &blob)
This method will write a blob of data (e.g.
Definition: Cube.cpp:763
void saveAsReducedCube(Cube *icube, const QString &psOutFile)
For AsIs option, save the reduced input image visible in the viewport window using the Reduce functio...
Definition: FileTool.cpp:460
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
virtual int bandCount() const
Returns the number of virtual bands for the cube.
Definition: Cube.cpp:1125
Base class for the Qisis tools.
Definition: Tool.h:81
QMdiArea * mdiArea()
This method returns the QMdiArea.
Definition: Workspace.cpp:295
MdiCubeViewport * cubeViewport() const
Return the current cubeviewport.
Definition: Tool.h:211
QPointer< QAction > p_saveAs
Action save the current cube as a user specified file.
Definition: FileTool.h:125
std::vector< QString > bands() const
Return a vector of the input bands specified.
int groups() const
Returns the number of groups contained.
Definition: PvlObject.h:87
QPointer< QAction > p_closeAll
Action to close all windows.
Definition: FileTool.h:129
int labelSize(bool actual=false) const
Returns the number of bytes used by the label.
Definition: Cube.cpp:1358
void write(const QString &file)
Opens and writes PVL information to a file and handles the end of line sequence.
Definition: Pvl.cpp:116
void activate(bool)
Activates the tool.
Definition: Tool.cpp:131
double minimum() const
Return the output cube attribute minimum.
QPointer< SaveAsDialog > p_saveAsDialog
SaveAs Dialog with different save options.
Definition: FileTool.h:135
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:74
Process cubes by line.
QPointer< QAction > p_exportToList
Action to export active cubes to a cube list.
Definition: FileTool.h:128
QPointer< MdiCubeViewport > p_lastViewport
The last cubeviewport that was used.
Definition: FileTool.h:134
void create(const QString &cfile)
This method will create an isis cube for writing.
Definition: Cube.cpp:331
QPointer< QAction > p_saveInfo
Action to save the current cube&#39;s Whatsthis info.
Definition: FileTool.h:126
void setLabelSize(int labelBytes)
Used prior to the Create method, this will allocate a specific number of bytes in the label area for ...
Definition: Cube.cpp:1006
double multiplier() const
Returns the multiplier value for converting 8-bit/16-bit pixels to 32-bit.
Definition: Cube.cpp:1393
IO Handler for Isis Cubes.
Definition: Cube.h:170
QString p_lastDir
The last directory opened.
Definition: FileTool.h:132