ctkCoordinatesWidget.h 4.4 KB

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