Isis 3 Programmer Reference
BandSpinBox.cpp
1
7/* SPDX-License-Identifier: CC0-1.0 */
8
9#include <iostream>
10
11#include "BandSpinBox.h"
12#include "IException.h"
13
14namespace Isis {
22 QStringList list;
23 list.push_back(QString::number(1));
24 p_lastKey = "BandNumber";
25 p_map[p_lastKey] = list;
26 p_bands = 1;
27
28 setValue(1);
29 setMinimum(1);
30 setMaximum(p_bands);
31 }
32
33
41 void BandSpinBox::setBandBin(Pvl &pvl, const QString &key) {
42 // Remove all connections and clear the keyword map
43 disconnect(this, 0, 0, 0);
44 p_map.clear();
45
46 // Get the number of bands and setup the spin box
47 PvlGroup &dim = pvl.findObject("IsisCube")
48 .findObject("Core")
49 .findGroup("Dimensions");
50 p_bands = dim["Bands"];
51
52 // Put in the default BandNumber list
53 QStringList list;
54 for(int i = 1; i <= p_bands; i++) {
55 list.push_back(QString::number(i));
56 }
57 p_map["BandNumber"] = list;
58
59 // Add any other lists
60 if(pvl.findObject("IsisCube").hasGroup("BandBin")) {
61 PvlGroup &bandBin = pvl.findObject("IsisCube")
62 .findGroup("BandBin");
63 for(int i = 0; i < bandBin.keywords(); i++) {
64 list.clear();
65 if(bandBin[i].size() == p_bands) {
66 for(int j = 0; j < bandBin[i].size(); j++) {
67 list.push_back(QString(bandBin[i][j]));
68 }
69 QString bandBinName = bandBin[i].name();
70 p_map[bandBinName] = list;
71 }
72 }
73 }
74
75 setKey(key);
76 p_keys = p_map.keys();
77
78 setValue(1);
79 setMinimum(1);
80 setMaximum(p_bands);
81 updateGeometry();
82 }
83
84
93
94
101 void BandSpinBox::setKey(QString key) {
102 if(p_map.contains(key)) {
103 if(key != p_lastKey) {
104 p_lastKey = key;
105 setSuffix("a"); // This makes the spinbox update because
106 setSuffix(""); // the value isn't changing
107 repaint();
108 updateGeometry();
109 }
110 }
111 else {
112 throw IException(IException::Programmer, "Invalid key", _FILEINFO_);
113 }
114 }
115
116
123 void BandSpinBox::setKey(int key) {
124 if((key < 0) || (key >= (int) p_map.size())) {
125 throw IException(IException::Programmer, "Invalid key", _FILEINFO_);
126 }
127 else {
128 setKey(p_keys[key]);
129 }
130 }
131
132
141 QString BandSpinBox::textFromValue(int val) const {
142 if((val < 1) || (val > p_bands)) {
143 std::cout << "BandSpinBox: Bad index in textFromValue" << std::endl;
144 return QString("Error");
145 }
146
147 if(p_map.contains(p_lastKey)) {
148 return p_map[p_lastKey][val-1];
149 }
150 else {
151 std::cout << "BandSpinBox: Bad value for p_lastKey in textFromValue" << std::endl;
152 return QString("Error");
153 }
154 }
155
156
165 int BandSpinBox::valueFromText(const QString &text) const {
166 if(p_map.contains(p_lastKey)) {
167 for(int i = 0; i < p_map[p_lastKey].size(); i++) {
168 if(text == p_map[p_lastKey][i]) return i + 1;
169 }
170 }
171 std::cout << "BandSpinBox: Bad text in valueFromText" << std::endl;
172 return -1;
173 }
174
175
182 QSize BandSpinBox::sizeHint() const {
183 QFontMetrics fm(font());
184
185 int w = 0;
186 for(int i = minimum(); i <= maximum(); i++) {
187 w = qMax(w, fm.width(((BandSpinBox *) this)->textFromValue(i)));
188 }
189
190 QSize s = QSpinBox::sizeHint();
191 int neww = s.width() + w;
192
193 int minw = fm.width(((BandSpinBox *) this)->textFromValue(minimum()));
194 int maxw = fm.width(((BandSpinBox *) this)->textFromValue(maximum()));
195
196 if(minw < maxw) {
197 neww -= maxw;
198 }
199 else {
200 neww -= minw;
201 }
202 s.setWidth(neww + 5);
203 return s;
204 }
205
206
216 QValidator::State BandSpinBox::validate(QString &input, int &pos) const {
217 int count = 0;
218 int exact = false;
219 for(int i = 0; i < p_map[p_lastKey].size(); i++) {
220 if(p_map[p_lastKey][i].startsWith(input))count++;
221 if(p_map[p_lastKey][i] == input) exact = true;
222 }
223
224 if(count == 0) return QValidator::Invalid;
225 if(count > 0 && exact) return QValidator::Acceptable;
226 return QValidator::Intermediate;
227 }
228}
void setBandBin(Pvl &pvl, const QString &key="BandNumber")
Sets the band bin.
QSize sizeHint() const
returns a size hint for the spin box
void setKey(QString key)
Sets the key to the provided key.
QString p_lastKey
The last key.
Definition BandSpinBox.h:45
QString textFromValue(int val) const
Gets the text using p_map.
int valueFromText(const QString &text) const
gets the value (int) using p_map.
BandSpinBox(QWidget *parent=0)
BandSpinBox constructor.
QMap< QString, QStringList > p_map
The maps the last key to all the keys.
Definition BandSpinBox.h:44
QStringList p_keys
List of all the keys.
Definition BandSpinBox.h:46
QValidator::State validate(QString &input, int &pos) const
returns how valid the value from the spin box is.
QStringList BandBinKeys()
returns the list of keys.
int p_bands
Number of bands.
Definition BandSpinBox.h:42
Isis exception class.
Definition IException.h:91
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
Contains multiple PvlContainers.
Definition PvlGroup.h:41
Container for cube-like labels.
Definition Pvl.h:119
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16