Isis 3.0 Programmer Reference
Back | Home
FileTool.cpp
1 #include "FileTool.h"
2 
3 #include <cmath>
4 
5 #include <QAction>
6 #include <QApplication>
7 #include <QFileDialog>
8 #include <QImage>
9 #include <QMenu>
10 #include <QMessageBox>
11 #include <QPainter>
12 #include <QPixmap>
13 #include <QPrinter>
14 #include <QPrintDialog>
15 #include <QToolBar>
16 
17 #include "Brick.h"
18 #include "BrowseDialog.h"
19 #include "CubeAttribute.h"
20 #include "Enlarge.h"
21 #include "FileDialog.h"
22 #include "Interpolator.h"
23 #include "MainWindow.h"
24 #include "MdiCubeViewport.h"
25 #include "OriginalLabel.h"
26 #include "Portal.h"
27 #include "ProcessRubberSheet.h"
28 #include "ProcessByLine.h"
29 #include "Pvl.h"
30 #include "Reduce.h"
31 #include "SaveAsDialog.h"
32 #include "SubArea.h"
33 #include "Workspace.h"
34 
35 namespace Isis {
44  FileTool::FileTool(QWidget *parent) : Tool(parent) {
45  p_parent = parent;
46  p_dir = "/thisDirDoesNotExist!";
47  p_open = new QAction(parent);
48  p_open->setShortcut(Qt::CTRL + Qt::Key_O);
49  p_open->setText("&Open...");
50  p_open->setIcon(QPixmap(toolIconDir() + "/fileopen.png"));
51  p_open->setToolTip("Open cube");
52  QString whatsThis =
53  "<b>Function:</b> Open an <i>Isis cube</i> in new viewport \
54  <p><b>Shortcut:</b> Ctrl+O\n</p> \
55  <p><b>Hint:</b> Use Ctrl or Shift in file dialog to open \
56  multiple cubes</p>";
57  p_open->setWhatsThis(whatsThis);
58  connect(p_open, SIGNAL(triggered()), this, SLOT(open()));
59 
60  p_browse = new QAction(parent);
61  p_browse->setShortcut(Qt::CTRL + Qt::Key_B);
62  p_browse->setText("&Browse...");
63  p_browse->setToolTip("Browse cubes");
64  whatsThis =
65  "<b>Function:</b> Browse a <i>Isis cubes</i> in new viewport \
66  <p><b>Shortcut:</b> Ctrl+B\n</p>";
67  p_browse->setWhatsThis(whatsThis);
68  connect(p_browse, SIGNAL(triggered()), this, SLOT(browse()));
69 
70  p_save = new QAction(parent);
71  p_save->setShortcut(Qt::CTRL + Qt::Key_S);
72  p_save->setText("&Save");
73  p_save->setIcon(QPixmap(toolIconDir() + "/filesave.png"));
74  p_save->setToolTip("Save");
75  whatsThis =
76  "<b>Function:</b> Save changes to the current Cube \
77  <p><b>Shortcut:</b> Ctrl+S</p>";
78  p_save->setWhatsThis(whatsThis);
79  connect(p_save, SIGNAL(triggered()), this, SLOT(save()));
80  p_save->setEnabled(false);
81 
82  p_saveAs = new QAction(parent);
83  p_saveAs->setText("Save &As...");
84  p_saveAs->setIcon(QPixmap(toolIconDir() + "/filesaveas.png"));
85  p_saveAs->setToolTip("Save As");
86  whatsThis =
87  "<b>Function:</b> Save the current Cube to the specified location";
88  p_saveAs->setWhatsThis(whatsThis);
89  connect(p_saveAs, SIGNAL(triggered()), this, SLOT(saveAs()));
90  p_saveAs->setEnabled(false);
91 
92  p_saveInfo = new QAction(parent);
93  p_saveInfo->setText("Save &Info...");
94  p_saveInfo->setIcon(QPixmap(toolIconDir() + "/filesaveas.png"));
95  p_saveInfo->setToolTip("Save Info");
96  whatsThis =
97  "<b>Function:</b> Save the current Cube's Whatsthis Info to the specified location";
98  p_saveInfo->setWhatsThis(whatsThis);
99  connect(p_saveInfo, SIGNAL(triggered()), this, SLOT(saveInfo()));
100  p_saveInfo->setEnabled(false);
101 
102  p_exportView = new QAction(parent);
103  p_exportView->setText("Export View");
104  p_exportView->setIcon(QPixmap(toolIconDir() + "/fileexport.png"));
105  p_exportView->setToolTip("Export View");
106  whatsThis =
107  "<b>Function:</b> Save visible contents of the active \
108  viewport as a png, jpg, tiff \
109  <p><b>Hint:</b> Your local installation of Qt may not support \
110  all formats. Reinstall Qt if necessary</p>";
111  p_exportView->setWhatsThis(whatsThis);
112  connect(p_exportView, SIGNAL(triggered()), this, SLOT(exportView()));
113  p_exportView->setEnabled(false);
114 
115  p_print = new QAction(parent);
116  p_print->setText("&Print...");
117  p_print->setShortcut(Qt::CTRL + Qt::Key_P);
118  p_print->setIcon(QPixmap(toolIconDir() + "/fileprint.png"));
119  p_print->setToolTip("Print");
120  whatsThis =
121  "<b>Function:</b> Print visible contents of the active viewport \
122  <p><b>Shortcut:</b> Ctrl+P</b>";
123  p_print->setWhatsThis(whatsThis);
124  connect(p_print, SIGNAL(triggered()), this, SLOT(print()));
125  p_print->setEnabled(false);
126 
127  p_closeAll = new QAction(parent);
128  p_closeAll->setText("&Close All...");
129  p_closeAll->setToolTip("Close All");
130  whatsThis =
131  "<b>Function:</b> Close all cube viewports.";
132  p_closeAll->setWhatsThis(whatsThis);
133 
134  p_exit = new QAction(this);
135  p_exit->setShortcut(Qt::CTRL + Qt::Key_Q);
136  p_exit->setText("E&xit");
137  p_exit->setIcon(QPixmap(toolIconDir() + "/fileclose.png"));
138  whatsThis =
139  "<b>Function:</b> Quit qview \
140  <p><b>Shortcut:</b> Ctrl+Q</p>";
141  p_exit->setWhatsThis(whatsThis);
142  connect(p_exit, SIGNAL(triggered()), this, SLOT(exit()));
143 
144  p_lastViewport = NULL;
145 
146  p_saveAsDialog = NULL;
147  activate(true);
148  }
149 
155  void FileTool::addTo(QMenu *menu) {
156  menu->addAction(p_open);
157  menu->addAction(p_browse);
158  menu->addAction(p_save);
159  menu->addAction(p_saveAs);
160  menu->addAction(p_saveInfo);
161  menu->addAction(p_exportView);
162  menu->addAction(p_print);
163  menu->addAction(p_closeAll);
164  menu->addAction(p_exit);
165  }
166 
173  p_workSpace = ws;
174  Tool::addTo(ws);
175  connect(this, SIGNAL(fileSelected(QString)),
176  ws, SLOT(addCubeViewport(QString)));
177 
178  connect(p_closeAll, SIGNAL(triggered()), ws->mdiArea(), SLOT(closeAllSubWindows()));
179  }
180 
187  perm->addAction(p_open);
188  perm->addAction(p_exportView);
189  perm->addAction(p_print);
190  perm->addAction(p_exit);
191  }
192 
197  void FileTool::open() {
198  //Set up the list of filters that are default with this dialog.
199  if (!p_filterList.contains("Isis cubes (*.cub)")) {
200  p_filterList.append("Isis cubes (*.cub)");
201  p_filterList.append("All files (*)");
202  }
203  if (!p_dir.exists()) {
204  p_dir = QDir::current();
205  }
206 
207  FileDialog *fileDialog = new FileDialog("Open", p_filterList, p_dir, (QWidget *)parent());
208  fileDialog->show();
209  connect(fileDialog, SIGNAL(fileSelected(QString)),
210  p_workSpace, SLOT(addCubeViewport(QString)));
211  }
212 
218  //Set up the list of filters that are default with this dialog.
219  if (!p_filterList.contains("Isis cubes (*.cub)")) {
220  p_filterList.append("Isis cubes (*.cub)");
221  p_filterList.append("All files (*)");
222  }
223  if (!p_dir.exists()) {
224  p_dir = QDir::current();
225  }
226  BrowseDialog *browser = new BrowseDialog("Browse", p_filterList, p_dir, (QWidget *)parent());
227  browser->show();
228  connect(browser, SIGNAL(fileSelected(QString)),
229  p_workSpace, SLOT(addBrowseView(QString)));
230  }
231 
238  void FileTool::save() {
239  //If the current viewport is null (safety check), return from this method
240  if (cubeViewport() == NULL) {
241  QMessageBox::information((QWidget *)parent(), "Error", "No active cube to save");
242  return;
243  }
244 
245  emit saveChanges(cubeViewport());
246  p_save->setEnabled(false);
247 
248  cubeViewport()->cube()->reopen("rw");
249  }
250 
264  //If the current viewport is null (safety check), return from this method
265  if (cubeViewport() == NULL) {
266  QMessageBox::information((QWidget *)parent(), "Error", "No active cube to save");
267  return;
268  }
269  //Set up the list of filters that are default with this dialog.
270  if (!p_filterList.contains("Isis cubes (*.cub)")) {
271  p_filterList.append("Isis cubes (*.cub)");
272  }
273  if (!p_dir.exists()) {
274  p_dir = QDir(p_lastDir);
275  }
276  if (p_saveAsDialog) {
277  delete p_saveAsDialog;
278  p_saveAsDialog = NULL;
279  }
280 
281  p_saveAsDialog = new SaveAsDialog("Save As", p_filterList, p_dir, (QWidget *)parent());
282  connect(p_saveAsDialog, SIGNAL(fileSelected(QString)), this, SLOT(saveAsCubeByOption(QString)));
283 
284  p_saveAsDialog->show();
285  }
286 
295  void FileTool::saveAsCubeByOption(QString psOutFile) {
296  //If the current viewport is null (safety check), return from this method
297  if (cubeViewport() == NULL) {
298  QMessageBox::information((QWidget *)parent(), "Error",
299  "No active cube to save");
300  return;
301  }
302 
303  //If the filename is empty, return
304  if (psOutFile.isEmpty() || (p_saveAsDialog==NULL)){
305  QMessageBox::information((QWidget *)parent(), "Error",
306  "No output file selected");
307  return;
308  }
309 
310  //Check if the output file is already opened
311  QVector< MdiCubeViewport *> *vwportList = p_workSpace->cubeViewportList();
313  for (it = vwportList->begin(); it != vwportList->end(); ++it){
314  if (QString((*it)->cube()->fileName()) == psOutFile) {
315  QMessageBox::information((QWidget *)parent(), "Error",
316  "Output File is already open\n\""+ psOutFile + "\"");
317  return;
318  }
319  }
320 
321  //If the filename is the same as the current cube's filename, just save it
322  if (p_saveAsDialog->getSaveAsType() == SaveAsDialog::FullImage &&
323  psOutFile == cubeViewport()->cube()->fileName()) {
324  save();
325  return;
326  }
327 
328  //Save the current cube's changes by reopening it, and open an input cube
329  //from the current cube's location
330  Cube *icube = new Cube();
331  icube->open(cubeViewport()->cube()->fileName(), "rw");
332  Cube *ocube = NULL;
333 
334  // The output cube needs to be created if the scale is 1 since saveAs_FullResolution will be
335  // called, which expects a cube.
336  // NOTE: This really should be cleaned up and the cube should be created in 1 place.
337  if (p_saveAsDialog->getSaveAsType() != SaveAsDialog::ExportAsIs ||
338  p_lastViewport->scale() == 1) {
339  //Create the output cube
340  ocube = new Cube;
341  }
342 
343  if (p_saveAsDialog->getSaveAsType() == SaveAsDialog::FullImage) {
344  copyCubeDetails(psOutFile, icube, ocube, icube->sampleCount(),
345  icube->lineCount(), icube->bandCount());
346  saveAsFullImage(icube, ocube);
347  ocube->close();
348  }
349  else {
350  // start and end Samples and Lines
351  double dStartSample=0, dEndSample=0, dStartLine=0, dEndLine=0;
352  p_lastViewport->getCubeArea(dStartSample, dEndSample, dStartLine, dEndLine);
353 
354  if (p_saveAsDialog->getSaveAsType() == SaveAsDialog::ExportFullRes ||
355  p_lastViewport->scale() == 1) {
356 // int numSamples = (int)ceil(dEndSample-dStartSample);
357 // int numLines = (int)ceil(dEndLine-dStartLine);
358  int numSamples = (int)((dEndSample - dStartSample + 1) + 0.5);
359  int numLines = (int)((dEndLine - dStartLine + 1) + 0.5);
360  copyCubeDetails(psOutFile, icube, ocube, numSamples, numLines, icube->bandCount());
361  saveAs_FullResolution(icube, ocube, numSamples, numLines);
362  ocube->close();
363  }
364  else if (p_saveAsDialog->getSaveAsType() == SaveAsDialog::ExportAsIs ) {
365  saveAs_AsIs(icube, psOutFile);
366  }
367  }
368 
369  emit(fileSelected(psOutFile));
370 
371  //Disable the save action
372  p_save->setEnabled(false);
373 
374  p_lastDir = psOutFile;
375  }
376 
387  void FileTool::saveAsEnlargedCube(Cube *icube, const QString & psOutFile) {
388  double dScale = p_lastViewport->scale();
389 
390  // start and end Samples and Lines
391  double dStartSample=0, dEndSample=0, dStartLine=0, dEndLine=0;
392  p_lastViewport->getCubeArea(dStartSample, dEndSample, dStartLine, dEndLine);
393 
394  double ins = dEndSample - dStartSample + 1;
395  double inl = dEndLine - dStartLine + 1;
396 
397  double ons = (int)(ins * dScale + 0.5);
398  double onl = (int)(inl * dScale + 0.5);
399 
400  try {
402  p.SetInputCube (icube);
403  Cube *ocube = p.SetOutputCube(psOutFile, CubeAttributeOutput(" "),
404  ons, onl, icube->bandCount());
405 
406  Interpolator *interp = new Interpolator(Interpolator::NearestNeighborType);
407 
408  Enlarge *imgEnlarge = new Enlarge(icube, dScale, dScale);
409  imgEnlarge->SetInputArea((int)dStartSample, (int)dEndSample, (int)dStartLine, (int)dEndLine);
410 
411  p.StartProcess(*imgEnlarge, *interp);
412  imgEnlarge->UpdateOutputLabel(ocube);
413  p.EndProcess();
414 
415  delete imgEnlarge;
416  delete interp;
417 
418  } catch(IException &) {
419  QMessageBox::critical((QWidget *)parent(),
420  "Error", "Cannot open file, please check permissions");
421  }
422  }
423 
433  void FileTool::saveAsReducedCube(Cube *icube, const QString & psOutFile) {
434 
435  double dScale = p_lastViewport->scale();
436  // start and end Samples and Lines
437  double dStartSample=0, dEndSample=0, dStartLine=0, dEndLine=0;
438  p_lastViewport->getCubeArea(dStartSample, dEndSample, dStartLine, dEndLine);
439 
440  double ins = dEndSample - dStartSample + 1;
441  double inl = dEndLine - dStartLine + 1;
442 
443  double ons = (int)(ins * dScale + 0.5);
444  double onl = (int)(inl * dScale + 0.5);
445 
446  CubeAttributeInput cai(icube->fileName());
447  std::vector<QString> bands = cai.bands();
448  int inb = bands.size();
449 
450  if (inb == 0) {
451  inb = cubeViewport()->cube()->bandCount();
452  for(int i = 1; i <= inb; i++) {
453  bands.push_back(toString(i));
454  }
455  }
456 
457  ProcessByLine p;
458  p.SetInputCube (icube);
459  Cube *ocube = NULL;
460  try {
461  ocube = p.SetOutputCube(psOutFile, CubeAttributeOutput(""), ons, onl, inb);
462  // Our processing routine only needs 1
463  // the original set was for info about the cube only
464  p.ClearInputCubes();
465  }
466  catch(IException &) {
467  // If there is a problem, catch it and close the cube so it isn't open next time around
468  icube->close();
469  throw;
470  }
471 
472  Cube *tempcube=new Cube;
473  tempcube->open(cubeViewport()->cube()->fileName(), "r");
474  Nearest *near = new Nearest(tempcube, ins/ons, inl/onl);
475  near->setInputBoundary((int)dStartSample, (int)dEndSample, (int)dStartLine, (int)dEndLine);
476 
477  p.ProcessCubeInPlace(*near, false);
478  near->UpdateOutputLabel(ocube);
479  p.EndProcess();
480 
481  delete near;
482  near=NULL;
483  }
484 
493  void FileTool::saveAs_AsIs(Cube *icube, const QString & psOutFile) {
494  double dScale = p_lastViewport->scale();
495  // Enlarge the cube area
496  if (dScale > 1) {
497  saveAsEnlargedCube(icube, psOutFile);
498  }
499  // Reduce the cube area
500  else {
501  saveAsReducedCube(icube, psOutFile);
502  }
503  }
504 
519  void FileTool::copyCubeDetails(const QString & psOutFile, Cube *icube,
520  Cube *ocube, int piNumSamples, int piNumLines, int piNumBands) {
521  //Create the default output attribute with the output filename
522  CubeAttributeOutput outAtt(psOutFile);
523 
524  //Propagate all labels, tables, blobs, etc from the input to output cube
525  try {
526  ocube->setDimensions(piNumSamples, piNumLines, piNumBands);
527  ocube->setByteOrder(outAtt.byteOrder());
528  ocube->setFormat(outAtt.fileFormat());
529  ocube->setLabelsAttached(outAtt.labelAttachment() == AttachedLabel);
530 
531  if (outAtt.propagatePixelType()) {
532  ocube->setPixelType(icube->pixelType());
533  }
534  else {
535  ocube->setPixelType(outAtt.pixelType());
536  }
537 
538  if (outAtt.propagateMinimumMaximum()) {
539  if (ocube->pixelType() == Real) {
540  ocube->setBaseMultiplier(0.0, 1.0);
541  }
542  else if (ocube->pixelType() >= icube->pixelType()) {
543  double base = icube->base();
544  double mult = icube->multiplier();
545  ocube->setBaseMultiplier(base, mult);
546  }
547  else if ((ocube->pixelType() != Real) &&
548  (ocube->pixelType() != UnsignedByte) &&
549  (ocube->pixelType() != SignedWord) &&
550  (ocube->pixelType() != UnsignedWord)) {
551  QString msg = "Looks like your refactoring to add different pixel types";
552  msg += " you'll need to make changes here";
554  }
555  else {
556  QString msg = "You've chosen to reduce your output PixelType for [" +
557  psOutFile + "] you must specify the output pixel range too";
559  }
560  }
561  else {
562  // Not propagating so either the user entered or the programmer did
563  ocube->setMinMax(outAtt.minimum(), outAtt.maximum());
564  }
565 
566  int needLabBytes = icube->labelSize(true) + (1024 * 6);
567  if (needLabBytes > ocube->labelSize()) {
568  ocube->setLabelSize(needLabBytes);
569  }
570 
571  // Allocate the cube
572  ocube->create(psOutFile);
573 
574  // Transfer labels from the first input cube
575  PvlObject &incube = icube->label()->findObject("IsisCube");
576  PvlObject &outcube = ocube->label()->findObject("IsisCube");
577  for(int i = 0; i < incube.groups(); i++) {
578  outcube.addGroup(incube.group(i));
579  }
580 
581  // Transfer tables from the first input cube
582  Pvl &inlab = *icube->label();
583  for(int i = 0; i < inlab.objects(); i++) {
584  if (inlab.object(i).isNamed("Table")) {
585  Blob t((QString)inlab.object(i)["Name"], inlab.object(i).name());
586  icube->read(t);
587  ocube->write(t);
588  }
589  }
590 
591  // Transfer blobs from the first input cube
592  inlab = *icube->label();
593  for(int i = 0; i < inlab.objects(); i++) {
594  if (inlab.object(i).isNamed("Polygon")) {
595  Blob t((QString)inlab.object(i)["Name"], inlab.object(i).name());
596  icube->read(t);
597  ocube->write(t);
598  }
599  }
600 
601  // Transfer tables from the first input cube
602  inlab = *icube->label();
603  for(int i = 0; i < inlab.objects(); i++) {
604  if (inlab.object(i).isNamed("OriginalLabel")) {
605  OriginalLabel ol;
606  icube->read(ol);
607  ocube->write(ol);
608  }
609  }
610  }
611  catch(IException &) {
612  delete ocube;
613  throw;
614  }
615  }
616 
627  void FileTool::saveAsFullImage(Cube *icube, Cube *ocube) {
628  //Start the copy process line by line
629  Brick ibrick(*icube, icube->sampleCount(), 1, 1);
630  Brick obrick(*ocube, ocube->sampleCount(), 1, 1);
631 
632  int numBricks;
633  if (ibrick.Bricks() > obrick.Bricks()) {
634  numBricks = ibrick.Bricks();
635  }
636  else {
637  numBricks = obrick.Bricks();
638  }
639 
640  // Loop and let the app programmer work with the bricks
641  ibrick.begin();
642  obrick.begin();
643  for(int i = 0; i < numBricks; i++) {
644  icube->read(ibrick);
645  //Copy the contents to the output cube
646  copy(ibrick, obrick);
647  ocube->write(obrick);
648  ibrick++;
649  obrick++;
650  }
651  }
652 
665  Cube *pOutCube, int pNumSamples, int pNumLines) {
666  // start and end Samples and Lines
667  double dStartSample=0, dEndSample=0, dStartLine=0, dEndLine=0;
668  p_lastViewport->getCubeArea(dStartSample, dEndSample, dStartLine, dEndLine);
669  int iNumBands = pInCube->bandCount();
670 
671  PvlGroup results("Results");
672  results += PvlKeyword("InputLines", toString(pInCube->lineCount()));
673  results += PvlKeyword("InputSamples", toString(pInCube->sampleCount()));
674  results += PvlKeyword("StartingLine", toString(dStartLine));
675  results += PvlKeyword("StartingSample", toString(dStartSample));
676  results += PvlKeyword("EndingLine", toString(dEndLine));
677  results += PvlKeyword("EndingSample", toString(dEndSample));
678  results += PvlKeyword("LineIncrement", toString(1));
679  results += PvlKeyword("SampleIncrement", toString(1));
680  results += PvlKeyword("OutputLines", toString(pNumLines));
681  results += PvlKeyword("OutputSamples", toString(pNumSamples));
682  SubArea subArea;
683  subArea.SetSubArea(pInCube->lineCount(), pInCube->sampleCount(), dStartLine, dStartSample,
684  dEndLine, dEndSample, 1.0, 1.0);
685  subArea.UpdateLabel(pInCube, pOutCube, results);
686 
687  Portal iPortal (pNumSamples, 1, pInCube->pixelType());
688  Portal oPortal (pNumSamples, 1, pOutCube->pixelType());
689 
690  for(int iBand=1; iBand<=iNumBands; iBand++) {
691  int ol=1;
692  for(int iLine=(int)dStartLine; iLine<=(int)dEndLine; iLine++) {
693  iPortal.SetPosition(dStartSample, iLine, iBand);
694  pInCube->read(iPortal);
695 
696  oPortal.SetPosition(1, ol++, iBand);
697  pOutCube->read(oPortal);
698 
699  oPortal.Copy(iPortal);
700  pOutCube->write(oPortal);
701  }
702  }
703  }
704 
712  {
713  if (cubeViewport() == NULL) {
714  QMessageBox::information((QWidget *)parent(), "Error", "No active cube to save info");
715  return;
716  }
717 
718  //Get the new cube's filename
719  QString output =
720  QFileDialog::getSaveFileName((QWidget *)parent(),
721  "Choose output file",
722  p_lastDir,
723  QString("PVL Files (*.pvl)"));
724 
725  //If the filename is empty, return
726  if (output.isEmpty()) {
727  return;
728  }
729  else if (!output.endsWith(".pvl")) {
730  output += ".pvl";
731  }
732 
733  Pvl whatsThisPvl;
734  cubeViewport()->getAllWhatsThisInfo(whatsThisPvl);
735  whatsThisPvl.write(output);
736  }
737 
744  void FileTool::copy(Buffer &in, Buffer &out) {
745  out.Copy(in);
746  }
747 
755  }
756 
762  if (cubeViewport() == NULL) {
763  QMessageBox::information((QWidget *)parent(), "Error", "No active cube to export");
764  return;
765  }
766 
767  QString output =
768  QFileDialog::getSaveFileName((QWidget *)parent(),
769  QString("Choose output file"),
770  p_lastDir,
771  QString("PNG (*.png);;JPG (*.jpg);;TIF (*.tif)"));
772  if (output.isEmpty()) return;
773 
774  p_lastDir = output;
775 
776  QString format = QFileInfo(output).suffix();
777 
778  if (format.isEmpty()) {
779  if (output.endsWith('.')) {
780  output.append(QString("png"));
781  }
782  else {
783  output.append(QString(".png"));
784  }
785  }
786  else if (format.compare("png", Qt::CaseInsensitive) &&
787  format.compare("jpg", Qt::CaseInsensitive) &&
788  format.compare("tif", Qt::CaseInsensitive)) {
789 
790  QMessageBox::information((QWidget *)parent(), "Error", format + " is an invalid extension.");
791  return;
792  }
793 
794  QPixmap pm = cubeViewport()->viewport()->grab();
795 
796  //if (!cubeViewport()->pixmap().save(output,format)) {
797 
798  if (!pm.save(output)) {
799  QMessageBox::information((QWidget *)parent(), "Error", "Unable to save " + output);
800  return;
801  }
802  }
803 
809  // Is there anything to print
810  if (cubeViewport() == NULL) {
811  QMessageBox::information((QWidget *)parent(), "Error", "No active cube to print");
812  return;
813  }
814 
815  // Initialize a printer
816  static QPrinter *printer = NULL;
817  if (printer == NULL) printer = new QPrinter;
818  printer->setPageSize(QPrinter::Letter);
819  printer->setColorMode(QPrinter::GrayScale);
820  if (cubeViewport()->isColor()) printer->setColorMode(QPrinter::Color);
821 
822  QPrintDialog printDialog(printer, (QWidget *)parent());
823  if (printDialog.exec() == QDialog::Accepted) {
824  // Get display widget as a pixmap and convert to an image
825  QPixmap pixmap = cubeViewport()->viewport()->grab();
826  QImage img = pixmap.toImage();
827 
828  // C++ Gui Programmign with Qt, page 201
829  QPainter painter(printer);
830  QRect rect = painter.viewport();
831  QSize size = img.size();
832  size.scale(rect.size(), Qt::KeepAspectRatio);
833  painter.setViewport(rect.x(), rect.y(),
834  size.width(), size.height());
835  painter.setWindow(img.rect());
836  painter.drawImage(0, 0, img);
837  }
838  }
839 
844  // Close all cubes
845  // We must create a temporary list. If not the actual
846  // list size gets modified when a close occurs and not all
847  // windows were being closed.
848  MdiCubeViewport *d;
850  for(int i = 0; i < (int)tempList.size(); i++) {
851  d = tempList.at(i);
852  //Set the current viewport to the one being closed
853  setCubeViewport(d);
854 
855  if (!d->parentWidget()->close()) {
856  return false;
857  }
858  }
859  return true;
860  }
861 
870  void FileTool::exit() {
871  QWidget *mainWindow = qobject_cast<QWidget *>(p_parent);
872 
873  if (mainWindow)
874  mainWindow->close();
875  }
876 
882  void FileTool::enableSave(bool enable) {
883  p_save->setEnabled(enable);
884  }
885 
891  if (cubeViewport() == NULL) {
892  if (p_lastViewport != NULL) {
893  p_lastViewport = NULL;
894  }
895  p_print->setEnabled(false);
896  p_save->setEnabled(false);
897  p_exportView->setEnabled(false);
898  p_saveAs->setEnabled(false);
899  p_saveInfo->setEnabled(false);
900  }
901  else {
902  if (p_lastViewport == NULL) {
903  //Set the last viewport to the current viewport and connect signals to save and discard
905  connect(p_lastViewport, SIGNAL(saveChanges(CubeViewport *)), this, SLOT(save()));
906  }
907  else {
908  if (p_lastViewport != cubeViewport()) {
909  //If the viewport has changes made to it enable the save action
910  if (cubeViewport()->parentWidget()->windowTitle().endsWith("*")) {
911  p_save->setEnabled(true);
912  }
913  //Else disable it
914  else {
915  p_save->setEnabled(false);
916  }
917  //disconnect signals from the old viewport and connect them to the new viewport
918  disconnect(p_lastViewport, SIGNAL(saveChanges(CubeViewport *)), this, SLOT(save()));
919  disconnect(p_lastViewport, SIGNAL(discardChanges(CubeViewport *)), this, SLOT(discard()));
921  connect(p_lastViewport, SIGNAL(saveChanges(CubeViewport *)), this, SLOT(save()));
922  connect(p_lastViewport, SIGNAL(discardChanges(CubeViewport *)), this, SLOT(discard()));
923  }
924  }
925  p_print->setEnabled(true);
926  p_exportView->setEnabled(true);
927  p_saveAs->setEnabled(true);
928  p_saveInfo->setEnabled(true);
929  }
930  }
931 }
PvlObject & object(const int index)
Return the object at the specified index.
Definition: PvlObject.cpp:460
QPointer< QAction > p_browse
Action to browse and open files.
Definition: FileTool.h:116
Cube display widget for certain Isis MDI applications.
Buffer for reading and writing cube data.
Definition: Buffer.h:68
void saveChanges(CubeViewport *vp)
This signal is called when changes should be saved.
Manipulate and parse attributes of input cube filenames.
CubeViewportList * cubeViewportList() const
Return the list of cubeviewports.
Definition: Tool.cpp:390
QPointer< QAction > p_exportView
Action to export the view as a picture.
Definition: FileTool.h:121
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 &amp;inout)) using Functors.
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.
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:753
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:197
bool propagateMinimumMaximum() const
Return true if the min/max are to be propagated from an input cube.
PixelType pixelType() const
Definition: Cube.cpp:1355
double maximum() const
Return the output cube attribute maximum.
QDir p_dir
Directory.
Definition: FileTool.h:71
bool propagatePixelType() const
Return true if the pixel type is to be propagated from an input cube.
QStringList p_filterList
Filter List.
Definition: FileTool.h:69
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:744
virtual void saveInfo()
Saves the whatsthis info of the cubeviewport.
Definition: FileTool.cpp:711
QPointer< QAction > p_open
Action to open a file.
Definition: FileTool.h:115
Buffer for containing a two dimensional section of an image.
Definition: Portal.h:52
void saveAsFullImage(Cube *icube, Cube *ocube)
Save Image in its entirety to an output file.
Definition: FileTool.cpp:627
Widget to save(Save As) Isis cubes(used in qview) to display the FileDialog to select the output cube...
Definition: SaveAsDialog.h:46
void enableSave(bool enable)
This slot enables or disables save and save as.
Definition: FileTool.cpp:882
virtual bool closeAll()
Try to close all open cubes and save/discard if necessary.
Definition: FileTool.cpp:843
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
double multiplier() const
Returns the multiplier value for converting 8-bit/16-bit pixels to 32-bit.
Definition: Cube.cpp:1345
Pvl * label() const
Returns a pointer to the IsisLabel object associated with the cube.
Definition: Cube.cpp:1298
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:155
Buffer for containing a three dimensional section of an image.
Definition: Brick.h:60
void setFormat(Format format)
Used prior to the Create method, this will specify the format of the cube, either band...
Definition: Cube.cpp:938
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:808
void setLabelsAttached(bool attached)
Use prior to calling create, this sets whether or not to use separate label and data files...
Definition: Cube.cpp:950
virtual void save()
This method saves any changes made to the current cube, these changes are finalized! There is no undo...
Definition: FileTool.cpp:238
void addToPermanent(QToolBar *perm)
Adds the file tool&#39;s actions to the permanent toolbar.
Definition: FileTool.cpp:186
void saveAsEnlargedCube(Cube *icube, const QString &psOutFile)
Save image AsIs Enlarged into output.
Definition: FileTool.cpp:387
void Copy(const Buffer &in, bool includeRawBuf=true)
Allows copying of the buffer contents to another Buffer.
Definition: Buffer.cpp:269
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:121
void read(Blob &blob) const
This method will read data from the specified Blob object.
Definition: Cube.cpp:686
virtual void browse()
This method allows the user to navigate and browse cubes with a file dialog .
Definition: FileTool.cpp:217
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:154
QPointer< QAction > p_exit
Action to exit qview.
Definition: FileTool.h:123
virtual void EndProcess()
End the processing sequence and cleans up by closing cubes, freeing memory, etc.
Definition: Process.cpp:481
void setDimensions(int ns, int nl, int nb)
Used prior to the Create method to specify the size of the cube.
Definition: Cube.cpp:894
virtual void saveAs()
SaveAs Action - Displays the FileDialog with the filterlist (*.cub) to select the output cube...
Definition: FileTool.cpp:263
virtual void exportView()
This method allows the user to export the current view as an image file.
Definition: FileTool.cpp:761
void reopen(QString access="r")
This method will reopen an isis sube for reading or reading/writing.
Definition: Cube.cpp:653
QPointer< QAction > p_print
Action to print the current view.
Definition: FileTool.h:117
void updateTool()
Updates the tool.
Definition: FileTool.cpp:890
int sampleCount() const
Definition: Cube.cpp:1404
MdiCubeViewport * cubeViewport() const
Return the current cubeviewport.
Definition: Tool.h:211
Cube * cube() const
Return the cube associated with viewport.
Definition: CubeViewport.h:228
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:976
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:493
int labelSize(bool actual=false) const
Returns the number of bytes used by the label.
Definition: Cube.cpp:1310
int Bricks()
Returns the number of Bricks in the cube.
Definition: Brick.h:160
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
void saveAsCubeByOption(QString)
Save as Isis Cube (FullImage, AsIs, FullRes)
Definition: FileTool.cpp:295
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:38
Enlarge the pixel dimensions of an image.
Definition: Enlarge.h:48
QPointer< QAction > p_save
Action to save the current cube.
Definition: FileTool.h:118
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
int objects() const
Returns the number of objects.
Definition: PvlObject.h:231
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:134
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:828
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:664
void setInputBoundary(int startSample, int endSample, int startLine, int endLine)
Parameters to input image sub area.
Definition: Reduce.cpp:81
int bandCount() const
Returns the number of virtual bands for the cube.
Definition: Cube.cpp:1077
void ClearInputCubes()
Close owned input cubes from the list and clear the list.
Definition: Process.cpp:517
Class for browsing cubes.
Definition: BrowseDialog.h:28
Cube::Format fileFormat() const
Return the file format an Cube::Format.
void close(bool remove=false)
Closes the cube and updates the labels.
Definition: Cube.cpp:175
void open(const QString &cfile, QString access="r")
This method will open an isis cube for reading or reading/writing.
Definition: Cube.cpp:509
The input label is embedded in the image file.
Definition: CubeAttribute.h:48
void addTo(ViewportMainWindow *mw)
Adds the tool to the application.
Definition: Tool.cpp:78
double minimum() const
Return the output cube attribute minimum.
Functor for reduce using near functionality.
Definition: Reduce.h:83
double base() const
Returns the base value for converting 8-bit/16-bit pixels to 32-bit.
Definition: Cube.cpp:1094
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:44
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:879
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
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:519
int groups() const
Returns the number of groups contained.
Definition: PvlObject.h:87
virtual void exit()
Exit the program, this slot called when the exit is chosen from the File menu.
Definition: FileTool.cpp:870
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:845
QPointer< QWidget > p_parent
The parent widget of this object.
Definition: FileTool.h:124
Pixel interpolator.
Definition: Interpolator.h:51
QPointer< Workspace > p_workSpace
The workspace being used.
Definition: FileTool.h:126
void write(Blob &blob)
This method will write a blob of data (e.g.
Definition: Cube.cpp:725
void saveAsReducedCube(Cube *icube, const QString &psOutFile)
Save image AsIs Reduced into output.
Definition: FileTool.cpp:433
Isis exception class.
Definition: IException.h:99
QString toolIconDir() const
returns the path to the icon directory.
Definition: Tool.h:127
std::vector< QString > bands() const
Return a vector of the input bands specified.
Base class for the Qisis tools.
Definition: Tool.h:81
ByteOrder byteOrder() const
Return the byte order as an Isis::ByteOrder.
bool isNamed(const QString &match) const
Returns whether the given string is equal to the container name or not.
Definition: PvlContainer.h:87
QString fileName() const
Returns the opened cube&#39;s filename.
Definition: Cube.cpp:1160
QPointer< QAction > p_saveAs
Action save the current cube as a user specified file.
Definition: FileTool.h:119
QPointer< QAction > p_closeAll
Action to close all windows.
Definition: FileTool.h:122
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
QPointer< SaveAsDialog > p_saveAsDialog
SaveAs Dialog with different save options.
Definition: FileTool.h:128
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:74
Process cubes by line.
int lineCount() const
Definition: Cube.cpp:1331
QPointer< MdiCubeViewport > p_lastViewport
The last cubeviewport that was used.
Definition: FileTool.h:127
void create(const QString &cfile)
This method will create an isis cube for writing.
Definition: Cube.cpp:321
QPointer< QAction > p_saveInfo
Action to save the current cube&#39;s Whatsthis info.
Definition: FileTool.h:120
QString name() const
Returns the container name.
Definition: PvlContainer.h:78
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:963
IO Handler for Isis Cubes.
Definition: Cube.h:158
PixelType pixelType() const
Return the pixel type as an Isis::PixelType.
QString p_lastDir
The last directory opened.
Definition: FileTool.h:125

U.S. Department of the Interior | U.S. Geological Survey
ISIS | Privacy & Disclaimers | Astrogeology Research Program
To contact us, please post comments and questions on the ISIS Support Center
File Modified: 07/12/2023 23:18:11