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 
19 namespace Isis {
31  const Stretch &stretch,
32  const QString &name, const QColor &color) {
33  p_stretch = NULL;
34  p_table = NULL;
35  p_cubeHist = NULL;
36  p_graph = NULL;
37  p_mainLayout = NULL;
38 
39  p_cubeHist = new Histogram(hist);
40 
41  p_stretch = new Stretch();
42 
43  p_graph = new HistogramWidget(QString("Visible ") + name + QString(" Hist"),
44  color.lighter(110), color.darker(110));
46  p_graph->setSizePolicy(QSizePolicy(QSizePolicy::Minimum,
47  QSizePolicy::Minimum));
48  p_graph->setMinimumSize(QSize(100, 50));
49 
50  p_mainLayout = new QGridLayout();
51  p_mainLayout->addWidget(p_graph, 0, 0);
52 
54  connect(this, SIGNAL(stretchChanged()), this, SLOT(updateGraph()));
55  connect(this, SIGNAL(stretchChanged()), this, SLOT(updateTable()));
56  p_mainLayout->addWidget(p_table, 2, 0);
57 
58  QPushButton *saveAsButton = new QPushButton("Save Stretch Pairs...");
59  connect(saveAsButton, SIGNAL(clicked(bool)), this, SLOT(savePairs()));
60  p_mainLayout->addWidget(saveAsButton, 3, 0);
61 
62  QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
63  sizePolicy.setHeightForWidth(true);
64  p_graph->setSizePolicy(sizePolicy);
65  }
66 
67 
72  if(p_cubeHist) {
73  delete p_cubeHist;
74  p_cubeHist = NULL;
75  }
76 
77  if(p_stretch) {
78  delete p_stretch;
79  p_stretch = NULL;
80  }
81  }
82 
83 
92  p_graph->setHistogram(hist);
93  *p_cubeHist = hist;
95  }
96 
97 
104  QTableWidget *table = new QTableWidget(0, 2);
105 
106  QStringList labels;
107  labels << "Input" << "Output";
108  table->setHorizontalHeaderLabels(labels);
109 
110  table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
111  table->setEditTriggers(QAbstractItemView::NoEditTriggers);
112  table->setSelectionMode(QAbstractItemView::NoSelection);
113  table->setMinimumSize(QSize(50, 20));
114 
115  return table;
116  }
117 
118 
124  }
125 
126 
131  Stretch stretch = getStretch();
132  p_table->setRowCount(stretch.Pairs());
133 
134  for(int i = 0; i < stretch.Pairs(); i++) {
135  QTableWidgetItem *inputItem = new QTableWidgetItem(QString("%1").arg(
136  stretch.Input(i)));
137  inputItem->setTextAlignment(Qt::AlignCenter);
138  QTableWidgetItem *outputItem = new QTableWidgetItem(QString("%1").arg(
139  stretch.Output(i)));
140  outputItem->setTextAlignment(Qt::AlignCenter);
141 
142  p_table->setItem(i, 0, inputItem);
143  p_table->setItem(i, 1, outputItem);
144  }
145  }
146 
147 
153  QString filename = QFileDialog::getSaveFileName((QWidget *)parent(),
154  "Choose filename to save under", ".", "Text Files (*.txt)");
155  if(filename.isEmpty()) return;
156 
157  QFile outfile(filename);
158  bool success = outfile.open(QIODevice::WriteOnly);
159 
160  if(!success) {
161  QMessageBox::critical((QWidget *)parent(),
162  "Error", "Cannot open file, please check permissions");
163  return;
164  }
165 
166  QString currentText;
167  QTextStream stream(&outfile);
168 
169  Stretch stretch = getStretch();
170 
171  //Add the pairs to the file
172  stream << stretch.Text() << endl;
173 
174  outfile.close();
175  }
176 
177 
184  return *p_stretch;
185  }
186 }
void savePairs()
This asks the user for a file and saves the current stretch pairs to that file.
virtual Stretch getStretch()
Returns the current stretch object.
QTableWidget * createStretchTable()
This creates the stretch pairs table.
double Input(const int index) const
Returns the value of the input side of the stretch pair at the specified index.
Definition: Stretch.cpp:302
void stretchChanged()
Emitted when a new Stretch object is available.
void setStretch(Stretch stretch)
Creates a stretch curbe from the given stretch and plots it.
Stretch * p_stretch
Current stretch pairs stored here.
Definition: StretchType.h:70
QTableWidget * p_table
Pairs table.
Definition: StretchType.h:68
int Pairs() const
Returns the number of stretch pairs.
Definition: Stretch.h:176
Container of a cube histogram.
Definition: Histogram.h:86
Pixel value mapper.
Definition: Stretch.h:72
double Output(const int index) const
Returns the value of the output side of the stretch pair at the specified index.
Definition: Stretch.cpp:317
QString Text() const
Converts stretch pair to a string.
Definition: Stretch.cpp:283
virtual void setHistogram(const Histogram &)
This should be called when the visible area changes.
Definition: StretchType.cpp:91
virtual void setStretch(Stretch)=0
Children must re-implement this to update their stretch pairs and GUI elements appropriately.
StretchType(const Histogram &hist, const Stretch &stretch, const QString &name, const QColor &color)
This constructs a stretch type.
Definition: StretchType.cpp:30
void updateGraph()
This updates the graph with the current stretch object.
HistogramWidget * p_graph
Histogram graph.
Definition: StretchType.h:69
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
void updateTable()
This updates the table with the current stretch pairs.
void setHistogram(const Histogram &hist)
Creates a histogram curve from the given histogram and plots it.
virtual ~StretchType()
Destructor.
Definition: StretchType.cpp:71
Histogram widget used by AdvancedStretchTool.
Histogram * p_cubeHist
Visible area histogram.
Definition: StretchType.h:67
QGridLayout * p_mainLayout
Main layout.
Definition: StretchType.h:66