ctkAxesWidget.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.commontk.org/LICENSE
  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. //Qt includes
  15. #include <QDebug>
  16. #include <QBrush>
  17. #include <QGridLayout>
  18. #include <QLine>
  19. #include <QMouseEvent>
  20. #include <QPainter>
  21. //CTK includes
  22. #include "ctkAxesWidget.h"
  23. //Test purposes
  24. #include <QPushButton>
  25. #include <cmath>
  26. #include <math.h>
  27. static const double goldenRatio = 1.6180339887;
  28. static const double PI = 3.14159265358979323846;
  29. //ctkAxesWidgetPrivate
  30. //-----------------------------------------------------------------------------
  31. class ctkAxesWidgetPrivate
  32. {
  33. Q_DECLARE_PUBLIC(ctkAxesWidget);
  34. protected:
  35. ctkAxesWidget* const q_ptr;
  36. public:
  37. ctkAxesWidgetPrivate(ctkAxesWidget& object);
  38. QList<QPoint> extremities(QPoint center, int radius)const;
  39. QList<QRect> labelRects(const QList<QPoint>& extremities, QSize offset)const;
  40. ctkAxesWidget::Axis CurrentAxis;
  41. int Offset;
  42. bool HighlightCurrentAxis;
  43. QStringList AxesLabels;
  44. QVector<double> AxesAngles;
  45. };
  46. //-----------------------------------------------------------------------------
  47. ctkAxesWidgetPrivate::ctkAxesWidgetPrivate(ctkAxesWidget& object)
  48. :q_ptr(&object)
  49. {
  50. this->CurrentAxis = ctkAxesWidget::None;
  51. this->Offset = 10;
  52. this->HighlightCurrentAxis = true;
  53. this->AxesLabels << "R" << "L" << "S" << "I" << "A" << "P";
  54. this->AxesAngles << 0 << 3.14159265 << 1.57079633 << 4.71238898 << 5.49778714 << 2.35619449;
  55. }
  56. //-----------------------------------------------------------------------------
  57. QList<QPoint> ctkAxesWidgetPrivate::extremities(QPoint center, int radius)const
  58. {
  59. QList<QPoint> pos;
  60. for (int i = 0; i < 6 ; ++i)
  61. {
  62. pos << center + QPoint(radius * cos(this->AxesAngles[i]),
  63. -radius * sin(this->AxesAngles[i]));
  64. }
  65. return pos;
  66. }
  67. //-----------------------------------------------------------------------------
  68. QList<QRect> ctkAxesWidgetPrivate::labelRects(const QList<QPoint>& extremities, QSize offset)const
  69. {
  70. Q_Q(const ctkAxesWidget);
  71. QFontMetrics fm = q->fontMetrics();
  72. QSize letterSize = fm.size(Qt::TextShowMnemonic, "X") + QSize(1,1);
  73. QSize halfLetterSize = letterSize / 2;
  74. QList<QRect> rects;
  75. for (int i = 0; i < 6; ++i)
  76. {
  77. rects << QRect(extremities[i]
  78. + QPoint(cos(this->AxesAngles[i]) * (offset.width()+halfLetterSize.width()),
  79. -sin(this->AxesAngles[i]) * (offset.height()+halfLetterSize.height()))
  80. - QPoint(halfLetterSize.width(), halfLetterSize.height()),
  81. letterSize);
  82. }
  83. return rects;
  84. }
  85. //ctkAxesWidget
  86. //-----------------------------------------------------------------------------
  87. ctkAxesWidget::ctkAxesWidget(QWidget *newParent)
  88. : QWidget(newParent)
  89. , d_ptr(new ctkAxesWidgetPrivate(*this))
  90. {
  91. }
  92. //-----------------------------------------------------------------------------
  93. ctkAxesWidget::~ctkAxesWidget()
  94. {
  95. }
  96. // ----------------------------------------------------------------------------
  97. ctkAxesWidget::Axis ctkAxesWidget::currentAxis() const
  98. {
  99. Q_D(const ctkAxesWidget);
  100. return d->CurrentAxis;
  101. }
  102. //-----------------------------------------------------------------------------
  103. void ctkAxesWidget::setCurrentAxis(ctkAxesWidget::Axis newAxis)
  104. {
  105. Q_D(ctkAxesWidget);
  106. if (newAxis == d->CurrentAxis)
  107. {
  108. return;
  109. }
  110. d->CurrentAxis = newAxis;
  111. this->update();
  112. emit currentAxisChanged(d->CurrentAxis);
  113. }
  114. //-----------------------------------------------------------------------------
  115. void ctkAxesWidget::paintEvent(QPaintEvent *)
  116. {
  117. Q_D(ctkAxesWidget);
  118. // init
  119. QPainter painter(this);
  120. //painter.setRenderHint(QPainter::Antialiasing);
  121. QPoint center = QPoint(this->width(), this->height()) / 2;
  122. int length = qMin(this->width(), this->height());
  123. int diameter = length / goldenRatio;
  124. int radius = diameter / 2;
  125. QStringList axesLabels;
  126. QList<QPoint> positions = d->extremities(center, radius);
  127. QFontMetrics fm = this->fontMetrics();
  128. QSize letterSize = fm.size(Qt::TextShowMnemonic, "X") + QSize(1,1);
  129. QSize halfLetterSize = letterSize / 2;
  130. int blankSize = (length - diameter) / 2;
  131. QSize betweenLetterSpace = QSize(blankSize - letterSize.width(), blankSize - letterSize.height()) / 2;
  132. QList<QRect> labelRects = d->labelRects(positions, betweenLetterSpace);
  133. for (int i = 0; i < 6; ++i)
  134. {
  135. //QRect rect(positions[i] + QPoint(cos(d->AxesAngles[i]) * (betweenLetterSpace.width()+halfLetterSize.width()),
  136. // -sin(d->AxesAngles[i]) * (betweenLetterSpace.height()+halfLetterSize.height()))
  137. // - QPoint(halfLetterSize.width(), halfLetterSize.height()), letterSize);
  138. QRect rect = labelRects[i];
  139. if (d->HighlightCurrentAxis)
  140. {
  141. QFont font = painter.font();
  142. font.setBold(d->CurrentAxis == (i + 1));
  143. painter.setFont(font);
  144. }
  145. painter.drawText(rect, Qt::AlignCenter, d->AxesLabels[i]);
  146. }
  147. // Drawing the lines
  148. for (int i = 0; i < 6; ++i)
  149. {
  150. if (d->HighlightCurrentAxis)
  151. {
  152. QPen pen;
  153. if (d->CurrentAxis == (i + 1)) // axes start at 1
  154. {
  155. pen.setWidth(3);
  156. //pen.setColor(QColor(64, 64, 72)); // Payne's grey
  157. pen.setColor(this->palette().color(QPalette::Highlight));
  158. }
  159. painter.setPen(pen);
  160. }
  161. painter.drawLine(center, positions[i]);
  162. }
  163. QSize sphereRadius((blankSize / 2) / 1.6180339887,
  164. (blankSize / 2) / 1.6180339887);
  165. // Draw the center sphere
  166. QRadialGradient rg(QPointF(0.3333, 0.3333),0.7);
  167. rg.setCoordinateMode(QGradient::ObjectBoundingMode);
  168. if (d->HighlightCurrentAxis &&
  169. d->CurrentAxis == ctkAxesWidget::None)
  170. {
  171. rg.setColorAt(0., this->palette().color(QPalette::Highlight));
  172. }
  173. else
  174. {
  175. rg.setColorAt(0., this->palette().color(QPalette::Light));
  176. }
  177. rg.setColorAt(1., QColor(64, 64, 72));
  178. painter.setBrush(QBrush(rg));
  179. painter.setPen(QPen(Qt::NoPen));
  180. painter.drawEllipse(QPointF(center), sphereRadius.width(), sphereRadius.height());
  181. }
  182. // ----------------------------------------------------------------------------------
  183. void ctkAxesWidget::mousePressEvent(QMouseEvent *mouseEvent)
  184. {
  185. Q_D(ctkAxesWidget);
  186. QPoint center = QPoint(this->width(), this->height()) / 2;
  187. int length = qMin(this->width(), this->height());
  188. int diameter = length / goldenRatio;
  189. int blankSize = (length - diameter) / 2;
  190. QSize sphereRadius((blankSize / 2) / 1.6180339887,
  191. (blankSize / 2) / 1.6180339887);
  192. QPointF mousePos = mouseEvent->pos() - center;
  193. double distance2 =
  194. mousePos.x() * mousePos.x() + mousePos.y() * mousePos.y();
  195. if (distance2 < sphereRadius.width()*sphereRadius.width())
  196. {
  197. this->setCurrentAxis(None);
  198. return;
  199. }
  200. double mouseAngle = atan2(-mousePos.y(), mousePos.x());
  201. // mouseAngle is in the interval [-pi,+pi] radians
  202. // change it to be in [-pi/8, 7/8 * pi]
  203. double PI_8 = 0.392699082;
  204. if (mouseAngle < -PI_8)
  205. {
  206. mouseAngle += 2. * PI;
  207. }
  208. for (int i = 0; i < 6; ++i)
  209. {
  210. if (mouseAngle >= (d->AxesAngles[i] - PI_8) &&
  211. mouseAngle <= (d->AxesAngles[i] + PI_8))
  212. {
  213. // the user has clicked close on the axe
  214. this->setCurrentAxis(static_cast<Axis>(i+1));
  215. return;
  216. }
  217. }
  218. this->setCurrentAxis(None);
  219. }