ctkAxesWidget.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 axisAtPos(QPoint pos)const;
  41. ctkAxesWidget::Axis CurrentAxis;
  42. ctkAxesWidget::Axis HighlightAxis;
  43. bool AutoReset;
  44. QStringList AxesLabels;
  45. QVector<double> AxesAngles;
  46. };
  47. //-----------------------------------------------------------------------------
  48. ctkAxesWidgetPrivate::ctkAxesWidgetPrivate(ctkAxesWidget& object)
  49. :q_ptr(&object)
  50. {
  51. qRegisterMetaType<ctkAxesWidget::Axis>("ctkAxesWidget::Axis");
  52. this->CurrentAxis = ctkAxesWidget::None;
  53. this->HighlightAxis = ctkAxesWidget::None;
  54. this->AutoReset = false;
  55. this->AxesLabels << "R" << "L" << "S" << "I" << "A" << "P";
  56. this->AxesAngles << 0 << 3.14159265 << 1.57079633 << 4.71238898 << 5.49778714 << 2.35619449;
  57. }
  58. //-----------------------------------------------------------------------------
  59. QList<QPoint> ctkAxesWidgetPrivate::extremities(QPoint center, int radius)const
  60. {
  61. QList<QPoint> pos;
  62. for (int i = 0; i < 6 ; ++i)
  63. {
  64. pos << center + QPoint(radius * cos(this->AxesAngles[i]),
  65. -radius * sin(this->AxesAngles[i]));
  66. }
  67. return pos;
  68. }
  69. //-----------------------------------------------------------------------------
  70. QList<QRect> ctkAxesWidgetPrivate::labelRects(const QList<QPoint>& extremities, QSize offset)const
  71. {
  72. Q_Q(const ctkAxesWidget);
  73. QFontMetrics fm = q->fontMetrics();
  74. QSize letterSize = fm.size(Qt::TextShowMnemonic, "X") + QSize(1,1);
  75. QSize halfLetterSize = letterSize / 2;
  76. QList<QRect> rects;
  77. for (int i = 0; i < 6; ++i)
  78. {
  79. rects << QRect(extremities[i]
  80. + QPoint(cos(this->AxesAngles[i]) * (offset.width()+halfLetterSize.width()),
  81. -sin(this->AxesAngles[i]) * (offset.height()+halfLetterSize.height()))
  82. - QPoint(halfLetterSize.width(), halfLetterSize.height()),
  83. letterSize);
  84. }
  85. return rects;
  86. }
  87. //-----------------------------------------------------------------------------
  88. ctkAxesWidget::Axis ctkAxesWidgetPrivate::axisAtPos(QPoint pos)const
  89. {
  90. Q_Q(const ctkAxesWidget);
  91. QPoint center = QPoint(q->width(), q->height()) / 2;
  92. int length = qMin(q->width(), q->height());
  93. int diameter = length / goldenRatio;
  94. int blankSize = (length - diameter) / 2;
  95. QSize sphereRadius((blankSize / 2) / 1.6180339887,
  96. (blankSize / 2) / 1.6180339887);
  97. QPointF mousePos = pos - center;
  98. double distance2 =
  99. mousePos.x() * mousePos.x() + mousePos.y() * mousePos.y();
  100. if (distance2 < sphereRadius.width()*sphereRadius.width())
  101. {
  102. return ctkAxesWidget::None;
  103. }
  104. double mouseAngle = atan2(-mousePos.y(), mousePos.x());
  105. // mouseAngle is in the interval [-pi,+pi] radians
  106. // change it to be in [-pi/8, 7/8 * pi]
  107. double PI_8 = 0.392699082;
  108. if (mouseAngle < -PI_8)
  109. {
  110. mouseAngle += 2. * PI;
  111. }
  112. for (int i = 0; i < 6; ++i)
  113. {
  114. if (mouseAngle >= (this->AxesAngles[i] - PI_8) &&
  115. mouseAngle <= (this->AxesAngles[i] + PI_8))
  116. {
  117. // the mouse is over the axis
  118. return static_cast<ctkAxesWidget::Axis>(i+1);
  119. }
  120. }
  121. return ctkAxesWidget::None;
  122. }
  123. //ctkAxesWidget
  124. //-----------------------------------------------------------------------------
  125. ctkAxesWidget::ctkAxesWidget(QWidget *newParent)
  126. : QWidget(newParent)
  127. , d_ptr(new ctkAxesWidgetPrivate(*this))
  128. {
  129. }
  130. //-----------------------------------------------------------------------------
  131. ctkAxesWidget::~ctkAxesWidget()
  132. {
  133. }
  134. // ----------------------------------------------------------------------------
  135. ctkAxesWidget::Axis ctkAxesWidget::currentAxis() const
  136. {
  137. Q_D(const ctkAxesWidget);
  138. return d->CurrentAxis;
  139. }
  140. //-----------------------------------------------------------------------------
  141. void ctkAxesWidget::setCurrentAxis(ctkAxesWidget::Axis newAxis)
  142. {
  143. Q_D(ctkAxesWidget);
  144. d->HighlightAxis = newAxis;
  145. if (newAxis == d->CurrentAxis)
  146. {
  147. return;
  148. }
  149. d->CurrentAxis = newAxis;
  150. this->repaint();
  151. emit currentAxisChanged(d->CurrentAxis);
  152. }
  153. //-----------------------------------------------------------------------------
  154. void ctkAxesWidget::setCurrentAxisToNone()
  155. {
  156. this->setCurrentAxis(ctkAxesWidget::None);
  157. }
  158. // ----------------------------------------------------------------------------
  159. bool ctkAxesWidget::autoReset() const
  160. {
  161. Q_D(const ctkAxesWidget);
  162. return d->AutoReset;
  163. }
  164. // ----------------------------------------------------------------------------
  165. void ctkAxesWidget::setAutoReset(bool newAutoReset)
  166. {
  167. Q_D(ctkAxesWidget);
  168. if (d->AutoReset == newAutoReset)
  169. {
  170. return;
  171. }
  172. d->AutoReset = newAutoReset;
  173. if (d->AutoReset)
  174. {
  175. connect(this, SIGNAL(currentAxisChanged(ctkAxesWidget::Axis)),
  176. this, SLOT(setCurrentAxisToNone()));
  177. setCurrentAxisToNone();
  178. }
  179. else
  180. {
  181. disconnect(this, SIGNAL(currentAxisChanged(ctkAxesWidget::Axis)),
  182. this, SLOT(setCurrentAxisToNone()));
  183. }
  184. }
  185. //-----------------------------------------------------------------------------
  186. void ctkAxesWidget::paintEvent(QPaintEvent *)
  187. {
  188. Q_D(ctkAxesWidget);
  189. // init
  190. QPainter painter(this);
  191. //painter.setRenderHint(QPainter::Antialiasing);
  192. QPoint center = QPoint(this->width(), this->height()) / 2;
  193. int length = qMin(this->width(), this->height());
  194. int diameter = length / goldenRatio;
  195. int radius = diameter / 2;
  196. QStringList axesLabels;
  197. QList<QPoint> positions = d->extremities(center, radius);
  198. QFontMetrics fm = this->fontMetrics();
  199. QSize letterSize = fm.size(Qt::TextShowMnemonic, "X") + QSize(1,1);
  200. QSize halfLetterSize = letterSize / 2;
  201. int blankSize = (length - diameter) / 2;
  202. QSize betweenLetterSpace = QSize(blankSize - letterSize.width(), blankSize - letterSize.height()) / 2;
  203. QList<QRect> labelRects = d->labelRects(positions, betweenLetterSpace);
  204. for (int i = 0; i < 6; ++i)
  205. {
  206. //QRect rect(positions[i] + QPoint(cos(d->AxesAngles[i]) * (betweenLetterSpace.width()+halfLetterSize.width()),
  207. // -sin(d->AxesAngles[i]) * (betweenLetterSpace.height()+halfLetterSize.height()))
  208. // - QPoint(halfLetterSize.width(), halfLetterSize.height()), letterSize);
  209. QRect rect = labelRects[i];
  210. //if (d->HighlightAxes)
  211. {
  212. QFont font = painter.font();
  213. font.setBold(d->HighlightAxis == (i + 1));
  214. painter.setFont(font);
  215. }
  216. painter.drawText(rect, Qt::AlignCenter, d->AxesLabels[i]);
  217. }
  218. // Drawing the lines
  219. for (int i = 0; i < 6; ++i)
  220. {
  221. //if (d->HighlightAxes)
  222. {
  223. QPen pen;
  224. if (d->HighlightAxis == (i + 1)) // axes start at 1
  225. {
  226. pen.setWidth(3);
  227. //pen.setColor(QColor(64, 64, 72)); // Payne's grey
  228. pen.setColor(this->palette().color(QPalette::Highlight));
  229. }
  230. painter.setPen(pen);
  231. }
  232. painter.drawLine(center, positions[i]);
  233. }
  234. QSize sphereRadius((blankSize / 2) / 1.6180339887,
  235. (blankSize / 2) / 1.6180339887);
  236. // Draw the center sphere
  237. QRadialGradient rg(QPointF(0.3333, 0.3333),0.7);
  238. rg.setCoordinateMode(QGradient::ObjectBoundingMode);
  239. if (//d->HighlightAxes &&
  240. d->HighlightAxis == ctkAxesWidget::None)
  241. {
  242. rg.setColorAt(0., this->palette().color(QPalette::Highlight));
  243. }
  244. else
  245. {
  246. rg.setColorAt(0., this->palette().color(QPalette::Light));
  247. }
  248. rg.setColorAt(1., QColor(64, 64, 72));
  249. painter.setBrush(QBrush(rg));
  250. painter.setPen(QPen(Qt::NoPen));
  251. painter.drawEllipse(QPointF(center), sphereRadius.width(), sphereRadius.height());
  252. }
  253. // ----------------------------------------------------------------------------------
  254. void ctkAxesWidget::mousePressEvent(QMouseEvent *mouseEvent)
  255. {
  256. Q_D(ctkAxesWidget);
  257. d->HighlightAxis = d->axisAtPos(mouseEvent->pos());
  258. this->update();
  259. }
  260. // ----------------------------------------------------------------------------------
  261. void ctkAxesWidget::mouseMoveEvent(QMouseEvent *mouseEvent)
  262. {
  263. Q_D(ctkAxesWidget);
  264. d->HighlightAxis = d->axisAtPos(mouseEvent->pos());
  265. this->update();
  266. }
  267. // ----------------------------------------------------------------------------------
  268. void ctkAxesWidget::mouseReleaseEvent(QMouseEvent *mouseEvent)
  269. {
  270. Q_D(ctkAxesWidget);
  271. this->setCurrentAxis(d->axisAtPos(mouseEvent->pos()));
  272. }