ctkCursorPixmapWidget.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <QColor>
  16. #include <QDebug>
  17. #include <QEvent>
  18. #include <QPainter>
  19. #include <QSize>
  20. // CTK includes
  21. #include "ctkCursorPixmapWidget.h"
  22. #include "ctkLogger.h"
  23. // STD includes
  24. #include <math.h>
  25. //--------------------------------------------------------------------------
  26. static ctkLogger logger("org.commontk.visualization.vtk.widgets.ctkCursorPixmapWidget");
  27. //--------------------------------------------------------------------------
  28. //-----------------------------------------------------------------------------
  29. class ctkCursorPixmapWidgetPrivate
  30. {
  31. Q_DECLARE_PUBLIC(ctkCursorPixmapWidget);
  32. protected:
  33. ctkCursorPixmapWidget* const q_ptr;
  34. public:
  35. ctkCursorPixmapWidgetPrivate(ctkCursorPixmapWidget& object);
  36. void init();
  37. void drawCursor();
  38. void drawCrossHairCursor(QPainter& painter);
  39. void drawBullsEyeCursor(QPainter& painter);
  40. bool ShowCursor;
  41. ctkCursorPixmapWidget::CursorTypes CursorType;
  42. int BullsEyeWidth;
  43. static const double BULLS_EYE_BLANK_FRACTION = 0.1;
  44. };
  45. // --------------------------------------------------------------------------
  46. // ctkCursorPixmapWidgetPrivate methods
  47. // --------------------------------------------------------------------------
  48. ctkCursorPixmapWidgetPrivate::ctkCursorPixmapWidgetPrivate(ctkCursorPixmapWidget& object)
  49. :q_ptr(&object)
  50. {
  51. this->ShowCursor = true;
  52. this->CursorType = ctkCursorPixmapWidget::CrossHairCursor;
  53. this->BullsEyeWidth = 15;
  54. }
  55. //---------------------------------------------------------------------------
  56. void ctkCursorPixmapWidgetPrivate::init()
  57. {
  58. Q_Q(ctkCursorPixmapWidget);
  59. q->setAutoFillBackground(true);
  60. q->setAlignment(Qt::AlignCenter);
  61. }
  62. //---------------------------------------------------------------------------
  63. void ctkCursorPixmapWidgetPrivate::drawCursor()
  64. {
  65. // Abort if we are not to draw the cursor
  66. if (!this->ShowCursor)
  67. {
  68. return;
  69. }
  70. // Setup the painter object to paint on the label
  71. Q_Q(ctkCursorPixmapWidget);
  72. QPainter painter(q);
  73. painter.setPen(q->cursorColor());
  74. // Draw cursor (based on current parameters) onto the label
  75. switch (this->CursorType)
  76. {
  77. case ctkCursorPixmapWidget::CrossHairCursor:
  78. this->drawCrossHairCursor(painter);
  79. break;
  80. case ctkCursorPixmapWidget::BullsEyeCursor:
  81. this->drawBullsEyeCursor(painter);
  82. break;
  83. default:
  84. qDebug() << "Unsupported cursor type" << this->CursorType;
  85. break;
  86. }
  87. }
  88. //---------------------------------------------------------------------------
  89. void ctkCursorPixmapWidgetPrivate::drawCrossHairCursor(QPainter& painter)
  90. {
  91. Q_Q(ctkCursorPixmapWidget);
  92. QSize size = q->size();
  93. double halfWidth = (size.width()-1.0) / 2.0;
  94. double halfHeight = (size.height()-1.0) / 2.0;
  95. painter.drawLine(QPointF(0, halfHeight), QPointF(size.width(), halfHeight));
  96. painter.drawLine(QPointF(halfWidth, 0), QPointF(halfWidth, size.height()));
  97. }
  98. // --------------------------------------------------------------------------
  99. void ctkCursorPixmapWidgetPrivate::drawBullsEyeCursor(QPainter& painter)
  100. {
  101. Q_Q(ctkCursorPixmapWidget);
  102. QSize size = q->size();
  103. // Draw rectangle
  104. double bullsEye = this->BullsEyeWidth;
  105. double x = (size.width()-bullsEye) / 2.0;
  106. double y = (size.height()-bullsEye) / 2.0;
  107. painter.drawRect(QRectF(x, y, bullsEye-1.0, bullsEye-1.0));
  108. // Draw the lines
  109. double halfWidth = (size.width()-1.0) / 2.0;
  110. double halfHeight = (size.height()-1.0) / 2.0;
  111. double blank = round(std::min(halfWidth, halfHeight) * this->BULLS_EYE_BLANK_FRACTION);
  112. painter.drawLine(QPointF(0, halfHeight), QPointF(x-blank-1.0, halfHeight));
  113. painter.drawLine(QPointF(x+bullsEye+blank, halfHeight), QPointF(size.width(), halfHeight));
  114. painter.drawLine(QPointF(halfWidth, 0), QPointF(halfWidth, y-blank-1.0));
  115. painter.drawLine(QPointF(halfWidth, y+bullsEye+blank), QPointF(halfWidth, size.height()));
  116. }
  117. //---------------------------------------------------------------------------
  118. // ctkCursorPixmapWidget methods
  119. // --------------------------------------------------------------------------
  120. ctkCursorPixmapWidget::ctkCursorPixmapWidget(QWidget* parent)
  121. : Superclass(parent)
  122. , d_ptr(new ctkCursorPixmapWidgetPrivate(*this))
  123. {
  124. Q_D(ctkCursorPixmapWidget);
  125. d->init();
  126. }
  127. // --------------------------------------------------------------------------
  128. ctkCursorPixmapWidget::~ctkCursorPixmapWidget()
  129. {
  130. }
  131. // --------------------------------------------------------------------------
  132. CTK_GET_CPP(ctkCursorPixmapWidget, bool, showCursor, ShowCursor);
  133. // --------------------------------------------------------------------------
  134. void ctkCursorPixmapWidget::setShowCursor(bool newShow)
  135. {
  136. Q_D(ctkCursorPixmapWidget);
  137. if (newShow == d->ShowCursor)
  138. {
  139. return;
  140. }
  141. d->ShowCursor = newShow;
  142. this->update();
  143. }
  144. // --------------------------------------------------------------------------
  145. QColor ctkCursorPixmapWidget::cursorColor() const
  146. {
  147. return this->palette().color(QPalette::Highlight);
  148. }
  149. // --------------------------------------------------------------------------
  150. void ctkCursorPixmapWidget::setCursorColor(const QColor& newColor)
  151. {
  152. if (newColor.isValid())
  153. {
  154. QPalette palette(this->palette());
  155. palette.setColor(QPalette::Highlight, newColor);
  156. this->setPalette(palette);
  157. this->update();
  158. }
  159. }
  160. // --------------------------------------------------------------------------
  161. CTK_GET_CPP(ctkCursorPixmapWidget, const ctkCursorPixmapWidget::CursorTypes&, cursorType, CursorType);
  162. // --------------------------------------------------------------------------
  163. void ctkCursorPixmapWidget::setCursorType(const CursorTypes& newType)
  164. {
  165. Q_D(ctkCursorPixmapWidget);
  166. if (newType == d->CursorType)
  167. {
  168. return;
  169. }
  170. d->CursorType = newType;
  171. this->update();
  172. }
  173. // --------------------------------------------------------------------------
  174. QColor ctkCursorPixmapWidget::marginColor() const
  175. {
  176. return this->palette().color(QPalette::Window);
  177. }
  178. // --------------------------------------------------------------------------
  179. void ctkCursorPixmapWidget::setMarginColor(const QColor& newColor)
  180. {
  181. if (newColor.isValid())
  182. {
  183. QPalette palette(this->palette());
  184. palette.setColor(QPalette::Window, newColor);
  185. this->setPalette(palette);
  186. this->update();
  187. }
  188. }
  189. // --------------------------------------------------------------------------
  190. CTK_GET_CPP(ctkCursorPixmapWidget, int, bullsEyeWidth, BullsEyeWidth);
  191. // --------------------------------------------------------------------------
  192. void ctkCursorPixmapWidget::setBullsEyeWidth(int newWidth)
  193. {
  194. if (newWidth >= 0)
  195. {
  196. Q_D(ctkCursorPixmapWidget);
  197. d->BullsEyeWidth = newWidth;
  198. this->update();
  199. }
  200. }
  201. // --------------------------------------------------------------------------
  202. void ctkCursorPixmapWidget::paintEvent(QPaintEvent * event)
  203. {
  204. Superclass::paintEvent(event);
  205. Q_D(ctkCursorPixmapWidget);
  206. d->drawCursor();
  207. }
  208. // --------------------------------------------------------------------------
  209. QSize ctkCursorPixmapWidget::minimumSizeHint()const
  210. {
  211. // Pretty arbitrary size (matches ctkVTKAbstractView)
  212. return QSize(50, 50);
  213. }
  214. // --------------------------------------------------------------------------
  215. QSize ctkCursorPixmapWidget::sizeHint()const
  216. {
  217. // Pretty arbitrary size (matches ctkVTKAbstractView)
  218. return QSize(300, 300);
  219. }
  220. //----------------------------------------------------------------------------
  221. bool ctkCursorPixmapWidget::hasHeightForWidth()const
  222. {
  223. return true;
  224. }
  225. //----------------------------------------------------------------------------
  226. int ctkCursorPixmapWidget::heightForWidth(int width)const
  227. {
  228. // Tends to be square
  229. return width;
  230. }