Isis 3 Programmer Reference
StretchType.cpp
1 #include "StretchType.h"
2 
3 #include <iostream>
4 
5 #include <QColor>
6 #include <QGridLayout>
7 #include <QFileDialog>
8 #include <QHeaderView>
9 #include <QMessageBox>
10 #include <QPushButton>
11 #include <QString>
12 #include <QTableWidget>
13 #include <QTextStream>
14 
15 #include "Stretch.h"
16 #include "Histogram.h"
17 #include "HistogramWidget.h"
18 #include "CubeStretch.h"
19 
20 namespace Isis {
32  const Stretch &stretch,
33  const QString &name, const QColor &color) {
34  p_stretch = NULL;
35  p_table = NULL;
36  p_cubeHist = NULL;
37  p_graph = NULL;
38  p_mainLayout = NULL;
39 
40  p_cubeHist = new Histogram(hist);
41 
42  p_stretch = new Stretch();
43 
44  p_graph = new HistogramWidget(QString("Visible ") + name ,
45  color.lighter(110), color.darker(110));
47  p_graph->setSizePolicy(QSizePolicy(QSizePolicy::Minimum,
48  QSizePolicy::Minimum));
49  p_graph->setMinimumSize(QSize(100, 50));
50 
51  p_mainLayout = new QGridLayout();
52  p_mainLayout->addWidget(p_graph, 0, 0);
53 
55  connect(this, SIGNAL(stretchChanged()), this, SLOT(updateGraph()));
56  connect(this, SIGNAL(stretchChanged()), this, SLOT(updateTable()));
57  p_mainLayout->addWidget(p_table, 2, 0);
58 
59  QPushButton *saveAsButton = new QPushButton("Save Stretch Pairs to File...");
60  connect(saveAsButton, SIGNAL(clicked(bool)), this, SLOT(savePairs()));
61  p_mainLayout->addWidget(saveAsButton, 3, 0);
62 
63  // Only display Save/Delete/Restore here for Gray Stretches. For RGB stretches,
64  // this panel is displayed 3 times, but the Save/Delete/Restore should only be displayed
65  // once.
66  if (name.compare("Gray") == 0) {
67  QPushButton *saveToCubeButton = new QPushButton("Save Stretch Pairs to Cube...");
68  connect(saveToCubeButton, SIGNAL(clicked(bool)), this, SIGNAL(saveToCube()));
69  p_mainLayout->addWidget(saveToCubeButton, 4, 0);
70 
71  QPushButton *deleteFromCubeButton = new QPushButton("Delete Stretch Pairs from Cube...");
72  connect(deleteFromCubeButton, SIGNAL(clicked(bool)), this, SIGNAL(deleteFromCube()));
73  p_mainLayout->addWidget(deleteFromCubeButton, 5, 0);
74 
75  QPushButton *loadStretchButton = new QPushButton("Restore Saved Stretch from Cube...");
76  connect(loadStretchButton, SIGNAL(clicked(bool)), this, SIGNAL(loadStretch()));
77  p_mainLayout->addWidget(loadStretchButton, 6, 0);
78 
79  QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
80  sizePolicy.setHeightForWidth(true);
81  p_graph->setSizePolicy(sizePolicy);
82  }
83  }
84 
85 
90  if(p_cubeHist) {
91  delete p_cubeHist;
92  p_cubeHist = NULL;
93  }
94 
95  if(p_stretch) {
96  delete p_stretch;
97  p_stretch = NULL;
98  }
99  }
100 
101 
110  p_graph->setHistogram(hist);
111  *p_cubeHist = hist;
113  }
114 
115 
122  QTableWidget *table = new QTableWidget(0, 2);
123 
124  QStringList labels;
125  labels << "Input" << "Output";
126  table->setHorizontalHeaderLabels(labels);
127 
128  table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
129  table->setEditTriggers(QAbstractItemView::NoEditTriggers);
130  table->setSelectionMode(QAbstractItemView::NoSelection);
131  table->setMinimumSize(QSize(50, 20));
132 
133  return table;
134  }
135 
136 
142  }
143 
144 
149  Stretch stretch = getStretch();
150  p_table->setRowCount(stretch.Pairs());
151 
152  for(int i = 0; i < stretch.Pairs(); i++) {
153  QTableWidgetItem *inputItem = new QTableWidgetItem(QString("%1").arg(
154  stretch.Input(i)));
155  inputItem->setTextAlignment(Qt::AlignCenter);
156  QTableWidgetItem *outputItem = new QTableWidgetItem(QString("%1").arg(
157  stretch.Output(i)));
158  outputItem->setTextAlignment(Qt::AlignCenter);
159 
160  p_table->setItem(i, 0, inputItem);
161  p_table->setItem(i, 1, outputItem);
162  }
163  }
164 
165 
171  QString filename = QFileDialog::getSaveFileName((QWidget *)parent(),
172  "Choose filename to save under", ".", "Text Files (*.txt)");
173  if(filename.isEmpty()) return;
174 
175  QFile outfile(filename);
176  bool success = outfile.open(QIODevice::WriteOnly);
177 
178  if(!success) {
179  QMessageBox::critical((QWidget *)parent(),
180  "Error", "Cannot open file, please check permissions");
181  return;
182  }
183 
184  QString currentText;
185  QTextStream stream(&outfile);
186 
187  Stretch stretch = getStretch();
188 
189  // Add the pairs to the file
190  stream << stretch.Text() << endl;
191 
192  outfile.close();
193  }
194 
195 
202  CubeStretch cubeStretch(*p_stretch);
203  return cubeStretch;
204  }
205 }
Isis::StretchType::setHistogram
virtual void setHistogram(const Histogram &)
This should be called when the visible area changes.
Definition: StretchType.cpp:109
QWidget
Isis::StretchType::setStretch
virtual void setStretch(Stretch)=0
Children must re-implement this to update their stretch pairs and GUI elements appropriately.
Isis::StretchType::updateGraph
void updateGraph()
This updates the graph with the current stretch object.
Definition: StretchType.cpp:140
Isis::StretchType::p_graph
HistogramWidget * p_graph
Histogram graph.
Definition: StretchType.h:70
Isis::Stretch
Pixel value mapper.
Definition: Stretch.h:58
Isis::HistogramWidget::setStretch
void setStretch(Stretch stretch)
Creates a stretch curbe from the given stretch and plots it.
Definition: HistogramWidget.cpp:115
Isis::StretchType::StretchType
StretchType(const Histogram &hist, const Stretch &stretch, const QString &name, const QColor &color)
This constructs a stretch type.
Definition: StretchType.cpp:31
Isis::Stretch::Pairs
int Pairs() const
Returns the number of stretch pairs.
Definition: Stretch.h:162
Isis::CubeStretch
Stores stretch information for a cube.
Definition: CubeStretch.h:27
Isis::StretchType::~StretchType
virtual ~StretchType()
Destructor.
Definition: StretchType.cpp:89
QStringList
Isis::StretchType::updateTable
void updateTable()
This updates the table with the current stretch pairs.
Definition: StretchType.cpp:148
Isis::StretchType::p_cubeHist
Histogram * p_cubeHist
Visible area histogram.
Definition: StretchType.h:68
Isis::StretchType::getStretch
virtual CubeStretch getStretch()
Returns the current stretch object.
Definition: StretchType.cpp:201
Isis::StretchType::savePairs
void savePairs()
This asks the user for a file and saves the current stretch pairs to that file.
Definition: StretchType.cpp:170
Isis::StretchType::p_mainLayout
QGridLayout * p_mainLayout
Main layout.
Definition: StretchType.h:67
Isis::HistogramWidget
Histogram widget used by AdvancedStretchTool.
Definition: HistogramWidget.h:36
Isis::StretchType::saveToCube
void saveToCube()
Emitted when a new Stretch object is available.
Isis::Stretch::Text
QString Text() const
Converts stretch pair to a string.
Definition: Stretch.cpp:268
Isis::StretchType::createStretchTable
QTableWidget * createStretchTable()
This creates the stretch pairs table.
Definition: StretchType.cpp:121
Isis::Stretch::Output
double Output(const int index) const
Returns the value of the output side of the stretch pair at the specified index.
Definition: Stretch.cpp:302
Isis::Histogram
Container of a cube histogram.
Definition: Histogram.h:74
Isis::HistogramWidget::setHistogram
void setHistogram(const Histogram &hist)
Creates a histogram curve from the given histogram and plots it.
Definition: HistogramWidget.cpp:63
Isis::StretchType::p_stretch
Stretch * p_stretch
Current stretch pairs stored here.
Definition: StretchType.h:71
Isis::StretchType::p_table
QTableWidget * p_table
Pairs table.
Definition: StretchType.h:69
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::Stretch::Input
double Input(const int index) const
Returns the value of the input side of the stretch pair at the specified index.
Definition: Stretch.cpp:287