USGS

Isis 3.0 Object Programmers' Reference

Home

BandSpinBox.cpp
1 #include <iostream>
2 
3 #include "BandSpinBox.h"
4 #include "IException.h"
5 
6 namespace Isis {
14  QStringList list;
15  list.push_back(QString::number(1));
16  p_lastKey = "BandNumber";
17  p_map[p_lastKey] = list;
18  p_bands = 1;
19 
20  setValue(1);
21  setMinimum(1);
22  setMaximum(p_bands);
23  }
24 
25 
33  void BandSpinBox::setBandBin(Pvl &pvl, const QString &key) {
34  // Remove all connections and clear the keyword map
35  disconnect(this, 0, 0, 0);
36  p_map.clear();
37 
38  // Get the number of bands and setup the spin box
39  PvlGroup &dim = pvl.findObject("IsisCube")
40  .findObject("Core")
41  .findGroup("Dimensions");
42  p_bands = dim["Bands"];
43 
44  // Put in the default BandNumber list
45  QStringList list;
46  for(int i = 1; i <= p_bands; i++) {
47  list.push_back(QString::number(i));
48  }
49  p_map["BandNumber"] = list;
50 
51  // Add any other lists
52  if(pvl.findObject("IsisCube").hasGroup("BandBin")) {
53  PvlGroup &bandBin = pvl.findObject("IsisCube")
54  .findGroup("BandBin");
55  for(int i = 0; i < bandBin.keywords(); i++) {
56  list.clear();
57  if(bandBin[i].size() == p_bands) {
58  for(int j = 0; j < bandBin[i].size(); j++) {
59  list.push_back(QString(bandBin[i][j]));
60  }
61  QString bandBinName = bandBin[i].name();
62  p_map[bandBinName] = list;
63  }
64  }
65  }
66 
67  setKey(key);
68  p_keys = p_map.keys();
69 
70  setValue(1);
71  setMinimum(1);
72  setMaximum(p_bands);
73  updateGeometry();
74  }
75 
76 
83  return p_keys;
84  }
85 
86 
93  void BandSpinBox::setKey(QString key) {
94  if(p_map.contains(key)) {
95  if(key != p_lastKey) {
96  p_lastKey = key;
97  setSuffix("a"); // This makes the spinbox update because
98  setSuffix(""); // the value isn't changing
99  repaint();
100  updateGeometry();
101  }
102  }
103  else {
104  throw IException(IException::Programmer, "Invalid key", _FILEINFO_);
105  }
106  }
107 
108 
115  void BandSpinBox::setKey(int key) {
116  if((key < 0) || (key >= (int) p_map.size())) {
117  throw IException(IException::Programmer, "Invalid key", _FILEINFO_);
118  }
119  else {
120  setKey(p_keys[key]);
121  }
122  }
123 
124 
133  QString BandSpinBox::textFromValue(int val) const {
134  if((val < 1) || (val > p_bands)) {
135  std::cout << "BandSpinBox: Bad index in textFromValue" << std::endl;
136  return QString("Error");
137  }
138 
139  if(p_map.contains(p_lastKey)) {
140  return p_map[p_lastKey][val-1];
141  }
142  else {
143  std::cout << "BandSpinBox: Bad value for p_lastKey in textFromValue" << std::endl;
144  return QString("Error");
145  }
146  }
147 
148 
157  int BandSpinBox::valueFromText(const QString &text) const {
158  if(p_map.contains(p_lastKey)) {
159  for(int i = 0; i < p_map[p_lastKey].size(); i++) {
160  if(text == p_map[p_lastKey][i]) return i + 1;
161  }
162  }
163  std::cout << "BandSpinBox: Bad text in valueFromText" << std::endl;
164  return -1;
165  }
166 
167 
174  QSize BandSpinBox::sizeHint() const {
175  QFontMetrics fm(font());
176 
177  int w = 0;
178  for(int i = minimum(); i <= maximum(); i++) {
179  w = qMax(w, fm.width(((BandSpinBox *) this)->textFromValue(i)));
180  }
181 
182  QSize s = QSpinBox::sizeHint();
183  int neww = s.width() + w;
184 
185  int minw = fm.width(((BandSpinBox *) this)->textFromValue(minimum()));
186  int maxw = fm.width(((BandSpinBox *) this)->textFromValue(maximum()));
187 
188  if(minw < maxw) {
189  neww -= maxw;
190  }
191  else {
192  neww -= minw;
193  }
194  s.setWidth(neww + 5);
195  return s;
196  }
197 
198 
208  QValidator::State BandSpinBox::validate(QString &input, int &pos) const {
209  int count = 0;
210  int exact = false;
211  for(int i = 0; i < p_map[p_lastKey].size(); i++) {
212  if(p_map[p_lastKey][i].startsWith(input))count++;
213  if(p_map[p_lastKey][i] == input) exact = true;
214  }
215 
216  if(count == 0) return QValidator::Invalid;
217  if(count > 0 && exact) return QValidator::Acceptable;
218  return QValidator::Intermediate;
219  }
220 }