PCL
NumericControl.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/NumericControl.h - Released 2024-06-18T15:48:54Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_NumericControl_h
53 #define __PCL_NumericControl_h
54 
56 
57 #include <pcl/Defs.h>
58 
59 #include <pcl/Atomic.h>
60 #include <pcl/AutoPointer.h>
61 #include <pcl/Control.h>
62 #include <pcl/Edit.h>
63 #include <pcl/Label.h>
64 #include <pcl/Sizer.h>
65 #include <pcl/Slider.h>
66 
67 namespace pcl
68 {
69 
70 // ----------------------------------------------------------------------------
71 
78 class PCL_CLASS NumericEdit : public Control
79 {
80 public:
81 
85 
89  NumericEdit( Control& parent = Null() );
90 
94  ~NumericEdit() override
95  {
96  }
97 
101  double Value() const
102  {
103  return m_value;
104  }
105 
109  void SetValue( double );
110 
113  String ValueAsString( double ) const;
114 
118  {
119  return ValueAsString( m_value );
120  }
121 
124  int MinEditWidth() const;
125 
129 
132  bool IsReal() const
133  {
134  return m_real;
135  }
136 
139  void SetReal( bool real = true );
140 
143  void SetInteger( bool integer = true )
144  {
145  SetReal( !integer );
146  }
147 
150  double LowerBound() const
151  {
152  return m_lowerBound;
153  }
154 
157  double UpperBound() const
158  {
159  return m_upperBound;
160  }
161 
172  virtual void SetRange( double lower, double upper );
173 
176  int Precision() const
177  {
178  return m_precision;
179  }
180 
183  void SetPrecision( int n );
184 
202  bool IsFixedPrecision() const
203  {
204  return m_fixed;
205  }
206 
211  void EnableFixedPrecision( bool enable = true );
212 
217  void DisableFixedPrecision( bool disable = true )
218  {
219  EnableFixedPrecision( !disable );
220  }
221 
224  bool IsScientificNotation() const
225  {
226  return m_scientific;
227  }
228 
231  void EnableScientificNotation( bool enable = true );
232 
235  void DisableScientificNotation( bool disable = true )
236  {
237  EnableScientificNotation( !disable );
238  }
239 
243  {
244  return m_sciTriggerExp;
245  }
246 
250 
253  bool IsFixedSign() const
254  {
255  return m_sign;
256  }
257 
260  void EnableFixedSign( bool enable = true );
261 
264  void DisableFixedSign( bool disable = true )
265  {
266  EnableFixedSign( !disable );
267  }
268 
272  {
273  return m_autoEditWidth;
274  }
275 
278  void EnableAutoAdjustEditWidth( bool enable = true )
279  {
280  if ( (m_autoEditWidth = enable) != false )
281  AdjustEditWidth();
282  }
283 
286  void DisableAutoAdjustEditWidth( bool disable = true )
287  {
288  EnableAutoAdjustEditWidth( !disable );
289  }
290 
296  {
297  return m_useRegExp;
298  }
299 
303  void EnableValidatingRegExp( bool enable = true )
304  {
305  m_useRegExp = enable;
306  UpdateRegExp();
307  }
308 
312  void DisableValidatingRegExp( bool disable = true )
313  {
314  EnableValidatingRegExp( !disable );
315  }
316 
317  // -------------------------------------------------------------------------
318  // Event handlers
319  //
320  // void OnValueUpdated( NumericControl& sender, double value );
321 
324  using value_event_handler = void (Control::*)( NumericEdit& sender, double value );
325 
329 
330 protected:
331 
332  struct EventHandlers
333  {
334  value_event_handler onValueUpdated = nullptr;
335  Control* onValueUpdatedReceiver = nullptr;
336 
337  EventHandlers() = default;
338  EventHandlers( const EventHandlers& ) = default;
339  EventHandlers& operator =( const EventHandlers& ) = default;
340  };
341 
342  AutoPointer<EventHandlers> m_handlers;
343 
344  double m_value = 0; // current value
345  double m_lowerBound = 0; // acceptable range, lower bound
346  double m_upperBound = 1; // acceptable range, upper bound
347  int m_precision = 6; // number of decimal digits in non-sci mode, [0,16]
348  bool m_real = true; // whether this is a real or integer parameter
349  bool m_fixed = false; // precision is literal instead of significant digits?
350  bool m_scientific = false; // scientific notation enabled?
351  bool m_sign = false; // always show a sign character
352  bool m_autoEditWidth = true; // set width of edit control automatically
353  int m_sciTriggerExp = -1; // exponent (of ten) to trigger sci notation
354  bool m_useRegExp = true; // use validating regular expressions
355 
356  PCL_MEMBER_REENTRANCY_GUARD( EditCompleted )
357 
358  virtual void UpdateControls();
359 
360  virtual void EditCompleted( Edit& );
361  virtual void KeyPressed( Control&, int, unsigned, bool& );
362  virtual void ReturnPressed( Edit& );
363  virtual void GetFocus( Control& );
364  virtual void LoseFocus( Control& );
365  virtual void MousePress( Control&, const pcl::Point&, int, unsigned, unsigned );
366 
367  int PrecisionForValue( double ) const;
368  bool UseScientific( double ) const;
369  void UpdateRegExp();
370 };
371 
372 // ----------------------------------------------------------------------------
373 
380 class PCL_CLASS NumericControl : public NumericEdit
381 {
382 public:
383 
385 
389  NumericControl( Control& parent = Null() );
390 
394  ~NumericControl() override
395  {
396  }
397 
411  void SetRange( double lower, double upper ) override;
412 
447  {
448  return m_exponential;
449  }
450 
461  void EnableExponentialResponse( bool enable = true );
462 
469  void DisableExponentialResponse( bool disable = true )
470  {
471  EnableExponentialResponse( !disable );
472  }
473 
474 protected:
475 
476  void UpdateControls() override;
477 
478  virtual void ValueUpdated( Slider&, int );
479  void KeyPressed( Control&, int, unsigned, bool& ) override;
480  void GetFocus( Control& ) override;
481 
482 private:
483 
484  bool m_exponential = false;
485 
486  double SliderValueToControl( int ) const;
487  int ControlValueToSlider( double ) const;
488 };
489 
490 // ----------------------------------------------------------------------------
491 
492 } // pcl
493 
494 #endif // __PCL_NumericControl_h
495 
496 // ----------------------------------------------------------------------------
497 // EOF pcl/NumericControl.h - Released 2024-06-18T15:48:54Z
Client-side interface to a PixInsight Control object.
Definition: Control.h:126
Client-side interface to a PixInsight Edit control.
Definition: Edit.h:76
A generic point in the two-dimensional space.
Definition: Point.h:100
Client-side interface to a PixInsight horizontal sizer.
Definition: Sizer.h:580
Horizontal slider control.
Definition: Slider.h:436
Client-side interface to a PixInsight Label control.
Definition: Label.h:76
A label/edit/slider compound control to edit numeric parameters.
HorizontalSlider slider
The Slider part of this NumericEdit control.
~NumericControl() override
bool IsExponentialResponse() const
void SetRange(double lower, double upper) override
void DisableExponentialResponse(bool disable=true)
void EnableExponentialResponse(bool enable=true)
NumericControl(Control &parent=Null())
A label/edit compound control to edit numeric parameters.
void(Control::*)(NumericEdit &sender, double value) value_event_handler
void SetReal(bool real=true)
double Value() const
bool IsScientificNotation() const
void SetValue(double)
bool IsFixedSign() const
bool IsValidatingRegExpEnabled() const
void AdjustEditWidth()
double LowerBound() const
int MinEditWidth() const
void SetInteger(bool integer=true)
void EnableAutoAdjustEditWidth(bool enable=true)
bool IsReal() const
int Precision() const
bool IsFixedPrecision() const
double UpperBound() const
void EnableFixedPrecision(bool enable=true)
NumericEdit(Control &parent=Null())
void OnValueUpdated(value_event_handler, Control &)
~NumericEdit() override
HorizontalSizer sizer
The Sizer object of this NumericEdit control.
String ValueAsString() const
String ValueAsString(double) const
void DisableAutoAdjustEditWidth(bool disable=true)
Label label
The Label part of this NumericEdit control.
Edit edit
The Edit part of this NumericEdit control.
void EnableFixedSign(bool enable=true)
void DisableFixedPrecision(bool disable=true)
bool IsAutoAdjustEditWidth() const
void DisableFixedSign(bool disable=true)
virtual void SetRange(double lower, double upper)
void SetPrecision(int n)
int ScientificNotationTriggerExponent() const
void SetScientificNotationTriggerExponent(int exp10)
void DisableScientificNotation(bool disable=true)
void DisableValidatingRegExp(bool disable=true)
void EnableScientificNotation(bool enable=true)
void EnableValidatingRegExp(bool enable=true)
Client-side interface to a PixInsight Slider control.
Definition: Slider.h:117
Unicode (UTF-16) string.
Definition: String.h:8113
#define PCL_MEMBER_REENTRANCY_GUARD(member)
Definition: Atomic.h:637
PCL root namespace.
Definition: AbstractImage.h:77