ctkCoordinatesWidget.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkCoordinatesWidget_h
  15. #define __ctkCoordinatesWidget_h
  16. // Qt includes
  17. #include <QWidget>
  18. #include <QWeakPointer>
  19. // CTK includes
  20. #include "ctkDoubleSpinBox.h"
  21. #include "ctkWidgetsExport.h"
  22. /// \ingroup Widgets
  23. ///
  24. /// ctkCoordinatesWidget is a simple container of dimension coordinates.
  25. /// For each coordinate a double spinbox is associated, everytime a value is
  26. /// modified, the signal valueChanged is fired.
  27. /// TODO: use pimpl
  28. class CTK_WIDGETS_EXPORT ctkCoordinatesWidget : public QWidget
  29. {
  30. Q_OBJECT
  31. Q_PROPERTY(int dimension READ dimension WRITE setDimension)
  32. /// This property controls whether the coordinates must be normalized.
  33. /// If true, the norm of the coordinates is enforced to be 1.
  34. /// False by default.
  35. Q_PROPERTY(bool normalized READ isNormalized WRITE setNormalized)
  36. Q_PROPERTY(int decimals READ decimals WRITE setDecimals)
  37. /// This property provides more controls over the decimals.
  38. /// \sa ctkDoubleSpinBox::DecimalsOptions, decimals
  39. Q_PROPERTY(ctkDoubleSpinBox::DecimalsOptions decimalsOption READ decimalsOption WRITE setDecimalsOption)
  40. Q_PROPERTY(double singleStep READ singleStep WRITE setSingleStep STORED false)
  41. Q_PROPERTY(double minimum READ minimum WRITE setMinimum)
  42. Q_PROPERTY(double maximum READ maximum WRITE setMaximum)
  43. Q_PROPERTY(QString coordinates READ coordinatesAsString WRITE setCoordinatesAsString)
  44. public:
  45. explicit ctkCoordinatesWidget(QWidget* parent = 0);
  46. virtual ~ctkCoordinatesWidget();
  47. /// Set/Get the dimension of the point
  48. /// The default dimension is 3
  49. void setDimension(int dim);
  50. int dimension() const;
  51. /// Get the number of decimals of each coordinate spin box
  52. /// The default number of decimals is 3.
  53. int decimals() const;
  54. /// Return the decimalsOption property value
  55. /// \sa decimalsOption
  56. ctkDoubleSpinBox::DecimalsOptions decimalsOption()const;
  57. /// Set the decimalsOption property value.
  58. /// \sa decimalsOption
  59. void setDecimalsOption(ctkDoubleSpinBox::DecimalsOptions option);
  60. /// Set/Get the single step of each coordinate spin box
  61. /// The default single step is 1.
  62. void setSingleStep(double step);
  63. double singleStep() const;
  64. /// Set/Get the minimum value of each coordinate spin box
  65. /// The default minimum is -100000.
  66. void setMinimum(double minimum);
  67. double minimum() const;
  68. /// Set/Get the maximum value of each coordinate spin box
  69. /// The default maximum is 100000.
  70. void setMaximum(double minimum);
  71. double maximum() const;
  72. /// Set the minimum and maximum of each coordinate spinbox at once.
  73. /// \sa minimum, maximum
  74. void setRange(double minimum, double maximum);
  75. /// Change the normalized property. If \a normalize is true, it normalizes
  76. /// the current coordinates, the range of possible values is reset to [-1, 1].
  77. /// \sa isNormalized
  78. void setNormalized(bool normalize);
  79. bool isNormalized()const;
  80. /// Return the norm of the coordinates.
  81. double norm()const;
  82. /// Return the squared norm of the coordinates.
  83. double squaredNorm()const;
  84. /// Set/Get the coordinates. Use commas to separate elements, spaces are
  85. /// allowed: e.g. "0,0.0, 0."
  86. void setCoordinatesAsString(QString pos);
  87. QString coordinatesAsString()const;
  88. /// Set/Get the coordinates
  89. /// The default values are 0.
  90. void setCoordinates(double* pos);
  91. double const * coordinates()const;
  92. /// Convenient function that sets up to 4 elements of the coordinates.
  93. void setCoordinates(double x, double y = 0., double z = 0., double w = 0.);
  94. /// Set/Get the value proxy of the spinboxes used to display the coordinates.
  95. /// \sa setValueProxy(), valueProxy()
  96. void setValueProxy(ctkValueProxy* proxy);
  97. ctkValueProxy* valueProxy() const;
  98. /// Return the spinbox identitfied by id
  99. ctkDoubleSpinBox* spinBox(int id);
  100. public Q_SLOTS:
  101. void normalize();
  102. /// Set the number of decimals of each coordinate spin box.
  103. void setDecimals(int decimals);
  104. Q_SIGNALS:
  105. ///
  106. /// valueChanged is fired anytime a coordinate is modified, the returned
  107. /// value is the point coordinates
  108. /// TODO: Don't fire the signal if the new values are not changed
  109. void coordinatesChanged(double* pos);
  110. protected Q_SLOTS:
  111. void updateCoordinate(double);
  112. void updateCoordinates();
  113. void updateDecimals();
  114. void updateOtherDecimals(int);
  115. void setTemporaryDecimals(int);
  116. void onValueProxyAboutToBeModified();
  117. void onValueProxyModified();
  118. protected:
  119. void addSpinBox();
  120. /// Normalize coordinates vector and return the previous norm.
  121. static double normalize(double* coordinates, int dimension);
  122. /// Compute the norm of a coordinates \a dimension vector
  123. static double norm(double* coordinates, int dimension);
  124. static double squaredNorm(double* coordinates, int dimension);
  125. /// Return the ideal number of decimals based on the spinBox value or
  126. /// 16 if there is no "good" number of decimals.
  127. /// \sa ctk::significantDecimals()
  128. static int spinBoxSignificantDecimals(ctkDoubleSpinBox* spinBox);
  129. int Decimals;
  130. ctkDoubleSpinBox::DecimalsOptions DecimalsOption;
  131. double SingleStep;
  132. double Minimum;
  133. double Maximum;
  134. bool Normalized;
  135. int Dimension;
  136. double* Coordinates;
  137. QList<int> LastUserEditedCoordinates;
  138. bool ChangingDecimals;
  139. QWeakPointer<ctkValueProxy> Proxy;
  140. };
  141. #endif