ctkCoordinatesWidget.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // CTK includes
  19. #include "ctkDoubleSpinBox.h"
  20. #include "ctkWidgetsExport.h"
  21. /// \ingroup Widgets
  22. ///
  23. /// ctkCoordinatesWidget is a simple container of dimension coordinates.
  24. /// For each coordinate a double spinbox is associated, everytime a value is
  25. /// modified, the signal valueChanged is fired.
  26. /// TODO: use pimpl
  27. class CTK_WIDGETS_EXPORT ctkCoordinatesWidget : public QWidget
  28. {
  29. Q_OBJECT
  30. Q_PROPERTY(int dimension READ dimension WRITE setDimension)
  31. /// This property controls whether the coordinates must be normalized.
  32. /// If true, the norm of the coordinates is enforced to be 1.
  33. /// False by default.
  34. Q_PROPERTY(bool normalized READ isNormalized WRITE setNormalized)
  35. Q_PROPERTY(int decimals READ decimals WRITE setDecimals)
  36. /// This property provides more controls over the decimals.
  37. /// \sa ctkDoubleSpinBox::DecimalsOptions, decimals
  38. Q_PROPERTY(ctkDoubleSpinBox::DecimalsOptions decimalsOption READ decimalsOption WRITE setDecimalsOption)
  39. Q_PROPERTY(double singleStep READ singleStep WRITE setSingleStep STORED false)
  40. Q_PROPERTY(double minimum READ minimum WRITE setMinimum)
  41. Q_PROPERTY(double maximum READ maximum WRITE setMaximum)
  42. Q_PROPERTY(QString coordinates READ coordinatesAsString WRITE setCoordinatesAsString)
  43. public:
  44. explicit ctkCoordinatesWidget(QWidget* parent = 0);
  45. virtual ~ctkCoordinatesWidget();
  46. /// Set/Get the dimension of the point
  47. /// The default dimension is 3
  48. void setDimension(int dim);
  49. int dimension() const;
  50. /// Get the number of decimals of each coordinate spin box
  51. /// The default number of decimals is 3.
  52. int decimals() const;
  53. /// Return the decimalsOption property value
  54. /// \sa decimalsOption
  55. ctkDoubleSpinBox::DecimalsOptions decimalsOption()const;
  56. /// Set the decimalsOption property value.
  57. /// \sa decimalsOption
  58. void setDecimalsOption(ctkDoubleSpinBox::DecimalsOptions option);
  59. /// Set/Get the single step of each coordinate spin box
  60. /// The default single step is 1.
  61. void setSingleStep(double step);
  62. double singleStep() const;
  63. /// Set/Get the minimum value of each coordinate spin box
  64. /// The default minimum is -100000.
  65. void setMinimum(double minimum);
  66. double minimum() const;
  67. /// Set/Get the maximum value of each coordinate spin box
  68. /// The default maximum is 100000.
  69. void setMaximum(double minimum);
  70. double maximum() const;
  71. /// Change the normalized property. If \a normalize is true, it normalizes
  72. /// the current coordinates, the range of possible values is reset to [-1, 1].
  73. /// \sa isNormalized
  74. void setNormalized(bool normalize);
  75. bool isNormalized()const;
  76. /// Return the norm of the coordinates.
  77. double norm()const;
  78. /// Return the squared norm of the coordinates.
  79. double squaredNorm()const;
  80. /// Set/Get the coordinates. Use commas to separate elements, spaces are
  81. /// allowed: e.g. "0,0.0, 0."
  82. void setCoordinatesAsString(QString pos);
  83. QString coordinatesAsString()const;
  84. /// Set/Get the coordinates
  85. /// The default values are 0.
  86. void setCoordinates(double* pos);
  87. double const * coordinates()const;
  88. /// Convenient function that sets up to 4 elements of the coordinates.
  89. void setCoordinates(double x, double y = 0., double z = 0., double w = 0.);
  90. public Q_SLOTS:
  91. void normalize();
  92. /// Set the number of decimals of each coordinate spin box.
  93. void setDecimals(int decimals);
  94. Q_SIGNALS:
  95. ///
  96. /// valueChanged is fired anytime a coordinate is modified, the returned
  97. /// value is the point coordinates
  98. /// TODO: Don't fire the signal if the new values are not changed
  99. void coordinatesChanged(double* pos);
  100. protected Q_SLOTS:
  101. void updateCoordinate(double);
  102. void updateCoordinates();
  103. protected:
  104. void addSpinBox();
  105. /// Normalize coordinates vector and return the previous norm.
  106. static double normalize(double* coordinates, int dimension);
  107. /// Compute the norm of a coordinates \a dimension vector
  108. static double norm(double* coordinates, int dimension);
  109. static double squaredNorm(double* coordinates, int dimension);
  110. int Decimals;
  111. ctkDoubleSpinBox::DecimalsOptions DecimalsOption;
  112. double SingleStep;
  113. double Minimum;
  114. double Maximum;
  115. bool Normalized;
  116. int Dimension;
  117. double* Coordinates;
  118. QList<int> LastUserEditedCoordinates;
  119. };
  120. #endif