Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PanelForDoubleValueWx.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include <Gui/PanelForDoubleValueWx.hh>
26 #include <wx/event.h>
27 #include <wx/valtext.h>
28 
29 using namespace BIAS;
30 
32  wxWindowID id,
33  const wxString& label,
34  const double minlimit,
35  const double maxlimit,
36  const double initialval,
37  const int stepsize,
38  const wxPoint& pos,
39  const wxSize& size,
40  long style)
41 : wxPanel(parent, id, pos, size, style),
42  MainSizer(new wxBoxSizer(wxHORIZONTAL)),
43  Label(new wxStaticText(this,
44  wxID_STATIC,
45  label,
46  wxDefaultPosition,
47  wxDefaultSize)),
48  StepSizeLabel(new wxStaticText(this,
49  wxID_STATIC,
50  wxT("STEPSIZE:"),
51  wxDefaultPosition,
52  wxDefaultSize)),
53  TextCtrl(new wxTextCtrl(this,
54  PanelForDoubleValueWx::ID_TEXT_CTRL,
55  wxT(""))),
56  Slider(new wxSlider(this,
57  PanelForDoubleValueWx::ID_SLIDER,
58  0,
59  0,
60  INT_MAX,
61  wxDefaultPosition,
62  wxSize(250, -1),
63  wxSL_HORIZONTAL | wxSL_AUTOTICKS)),
64  SpinCtrl(new wxSpinCtrl(this,
65  PanelForDoubleValueWx::ID_SPIN_CTRL,
66  wxEmptyString,
67  wxDefaultPosition,
68  wxDefaultSize,
69  wxSP_ARROW_KEYS,
70  100,
71  INT_MAX,
72  stepsize)),
73  StepSize(stepsize),
74  TextCtrlValue(0.0),
75  ParamMaxLimit(maxlimit),
76  ParamMinLimit(minlimit)
77 {
78  if (this->ParamMaxLimit <= this->ParamMinLimit) {
79 
80  }
81 
82  this->SetSizer(MainSizer);
83 
84  this->MainSizer->Add(this->Label, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
85  this->MainSizer->Add(this->TextCtrl, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
86  this->MainSizer->Add(this->Slider, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
87  this->MainSizer->Add(this->StepSizeLabel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
88  this->MainSizer->Add(this->SpinCtrl, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
89 
90  Connect(PanelForDoubleValueWx::ID_TEXT_CTRL,
91  wxEVT_COMMAND_TEXT_UPDATED,
92  wxCommandEventHandler(PanelForDoubleValueWx::OnTextCtrl));
93  Connect(PanelForDoubleValueWx::ID_SLIDER,
94  wxEVT_COMMAND_SLIDER_UPDATED,
95  wxCommandEventHandler(PanelForDoubleValueWx::OnSlider));
96  Connect(PanelForDoubleValueWx::ID_SPIN_CTRL,
97  wxEVT_COMMAND_SPINCTRL_UPDATED,
98  wxSpinEventHandler(PanelForDoubleValueWx::OnSpinCtrl));
99 
100  // Feed TextCtrl with initialval twice...because somehow on the first time
101  // it doesn't take the intended value
102  wxString strVal(wxT(""));
103  strVal = wxString::Format(wxT("%f"), initialval);
104  this->TextCtrl->SetValue(strVal);
105  this->TextCtrl->SetValue(strVal);
106 
107  // apply the range for Slider
108  this->Slider->SetRange(0, stepsize);
109 
110  this->MainSizer->Fit(this);
111 }
112 
113 
115 
116 
118 {
119  wxString strVal = this->TextCtrl->GetValue();
120  double val;
121  strVal.ToDouble(&val);
122  return val;
123 }
124 
125 
127 {
128  return this->ParamMaxLimit;
129 }
130 
131 
133 {
134  return this->ParamMinLimit;
135 }
136 
137 
139 {
140  return this->StepSize;
141 }
142 
143 
144 bool PanelForDoubleValueWx::SetParamMaxLimit(const double maxlimit)
145 {
146  if (maxlimit >= this->ParamMinLimit) {
147 
148  if (maxlimit < this->TextCtrlValue) {
149  this->TextCtrlValue = maxlimit;
150  this->TextCtrl->SetValue(wxString::Format(wxT("%f"), this->TextCtrlValue));
151  }
152 
153  this->ParamMaxLimit = maxlimit;
154  this->UpdateSlider();
155  return true;
156 
157  } else {
158 
159  return false;
160 
161  }
162 }
163 
164 
165 bool PanelForDoubleValueWx::SetParamMinLimit(const double minlimit)
166 {
167  if (minlimit <= this->ParamMaxLimit) {
168 
169  if (minlimit > this->TextCtrlValue) {
170  this->TextCtrlValue = minlimit;
171  this->TextCtrl->SetValue(wxString::Format(wxT("%f"), this->TextCtrlValue));
172  }
173 
174  this->ParamMinLimit = minlimit;
175  this->UpdateSlider();
176  return true;
177 
178  } else {
179 
180  return false;
181 
182  }
183 }
184 
185 
186 void PanelForDoubleValueWx::SetParamLabel(const wxString& label)
187 {
188  this->Label->SetLabel(label);
189 }
190 
191 
192 void PanelForDoubleValueWx::SetStepSize(const int stepsize)
193 {
194  this->StepSize = stepsize;
195  this->Slider->SetRange(0, this->StepSize);
196  this->UpdateSlider();
197 }
198 
199 
200 /* ============================= private ====================================*/
201 
202 void PanelForDoubleValueWx::OnTextCtrl(wxCommandEvent& WXUNUSED(event))
203 {
204  wxString strVal = this->TextCtrl->GetValue();
205  double val;
206 
207  if (strVal.ToDouble(&val)) { // conversion to float succeeded
208 
209  // we MUST check if value contains e or E (e = Exponent)
210  if (strVal.Contains(wxT("e")) || strVal.Contains(wxT("E"))) {
211  // if so then take the old value
212  strVal = wxString::Format(wxT("%f"), this->TextCtrlValue);
213  this->TextCtrl->SetValue(strVal);
214 
215  } else {
216 
217  // so no exponent expression
218  // now check ranges ([ParamMinLimit, ParamMaxLimit])
219  if (val > this->ParamMaxLimit) {
220  this->TextCtrlValue = this->ParamMaxLimit;
221  strVal = wxString::Format(wxT("%f"), this->TextCtrlValue);
222  this->TextCtrl->SetValue(strVal);
223  } else if (val < this->ParamMinLimit) {
224  this->TextCtrlValue = this->ParamMinLimit;
225  strVal = wxString::Format(wxT("%f"), this->TextCtrlValue);
226  this->TextCtrl->SetValue(strVal);
227  } else {
228  this->TextCtrlValue = val;
229  }
230  }
231 
232  } else { // conversion error --> we stay with our old values
233  strVal = wxString::Format(wxT("%f"), this->TextCtrlValue);
234  this->TextCtrl->SetValue(strVal);
235  }
236 
237  this->UpdateSlider();
238 }
239 
240 
241 void PanelForDoubleValueWx::OnSlider(wxCommandEvent& WXUNUSED(event))
242 {
243  // we have to disconnect textctrl and later connect again
244  // else slider won't react as expected
245  Disconnect(PanelForDoubleValueWx::ID_TEXT_CTRL,
246  wxEVT_COMMAND_TEXT_UPDATED,
247  wxCommandEventHandler(PanelForDoubleValueWx::OnTextCtrl));
248  int sliderValue = this->Slider->GetValue();
249  double ratio = static_cast<double>(sliderValue) / this->StepSize;
250  double diffMinMax = this->ParamMaxLimit - this->ParamMinLimit;
251  double distance = this->ParamMinLimit + (diffMinMax * ratio);
252  wxString strVal = wxString::Format(wxT("%f"), distance);
253  this->TextCtrlValue = distance;
254  this->TextCtrl->SetValue(strVal);
255  Connect(PanelForDoubleValueWx::ID_TEXT_CTRL,
256  wxEVT_COMMAND_TEXT_UPDATED,
257  wxCommandEventHandler(PanelForDoubleValueWx::OnTextCtrl));
258 }
259 
260 
261 void PanelForDoubleValueWx::OnSpinCtrl(wxSpinEvent& WXUNUSED(event))
262 {
263  this->SetStepSize(this->SpinCtrl->GetValue());
264 }
265 
266 
267 void PanelForDoubleValueWx::UpdateSlider()
268 {
269  double diffMinMax = this->ParamMaxLimit - this->ParamMinLimit;
270  double distance = this->TextCtrlValue - this->ParamMinLimit;
271  double ratio = distance / diffMinMax;
272  int newSliderVal = ratio * this->StepSize;
273  this->Slider->SetValue(newSliderVal);
274 }
PanelForDoubleValueWx(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &label=wxT("LABEL:"), const double minlimit=0.0, const double maxlimit=1.0, const double initialval=0.5, const int stepsize=10000, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxTAB_TRAVERSAL)
void SetParamLabel(const wxString &label)
Gives the parameter a name.
Helper Class For Parameter Input To Take Floating Point Numbers.
void SetStepSize(const int stepsize)
Sets the RANGE for the slider for precision (and also here: name is misleading)
bool SetParamMaxLimit(const double maxlimit)
Sets the maximum limit for a parameter.
bool SetParamMinLimit(const double minlimit)
Sets the minimum limit for a parameter.
does a connected component analysis and labels all connected regions
Definition: Label.hh:84