ctkThumbnailWidget.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. // ctkDICOMWidgets includes
  15. #include "ctkThumbnailWidget.h"
  16. #include "ui_ctkThumbnailWidget.h"
  17. // STD includes
  18. #include <iostream>
  19. // Qt includes
  20. #include <QColor>
  21. // logger includes
  22. #include "ctkLogger.h"
  23. static ctkLogger logger("org.commontk.Widgets.ctkDICOMThumbnailListWidget");
  24. //----------------------------------------------------------------------------
  25. class ctkThumbnailWidgetPrivate: public Ui_ctkThumbnailWidget
  26. {
  27. public:
  28. ctkThumbnailWidget* const q_ptr;
  29. Q_DECLARE_PUBLIC(ctkThumbnailWidget);
  30. // Constructor
  31. ctkThumbnailWidgetPrivate(ctkThumbnailWidget* parent);
  32. Qt::Alignment TextPosition;
  33. bool SelectedFlag;
  34. QColor SelectedColor;
  35. QModelIndex SourceIndex;
  36. QPixmap OriginalThumbnail;
  37. Qt::TransformationMode TransformationMode;
  38. // Redraw thumbnail
  39. void updateThumbnail();
  40. };
  41. //----------------------------------------------------------------------------
  42. // ctkThumbnailWidgetPrivate methods
  43. //----------------------------------------------------------------------------
  44. ctkThumbnailWidgetPrivate::ctkThumbnailWidgetPrivate(ctkThumbnailWidget* parent)
  45. : q_ptr(parent)
  46. {
  47. Q_Q(ctkThumbnailWidget);
  48. this->SelectedFlag = false;
  49. this->SelectedColor = q->palette().color(QPalette::Highlight);
  50. this->TextPosition = Qt::AlignTop | Qt::AlignHCenter;
  51. this->TransformationMode = Qt::FastTransformation;
  52. }
  53. //----------------------------------------------------------------------------
  54. void ctkThumbnailWidgetPrivate::updateThumbnail()
  55. {
  56. this->PixmapLabel->setPixmap(
  57. this->OriginalThumbnail.isNull() ? QPixmap() :
  58. this->OriginalThumbnail.scaled(this->PixmapLabel->size(),
  59. Qt::KeepAspectRatio,
  60. this->TransformationMode));
  61. }
  62. //----------------------------------------------------------------------------
  63. // ctkThumbnailWidget methods
  64. //----------------------------------------------------------------------------
  65. ctkThumbnailWidget::ctkThumbnailWidget(QWidget* parentWidget)
  66. : Superclass(parentWidget)
  67. , d_ptr(new ctkThumbnailWidgetPrivate(this))
  68. {
  69. Q_D(ctkThumbnailWidget);
  70. d->setupUi(this);
  71. this->setTextPosition(Qt::AlignTop | Qt::AlignHCenter);
  72. }
  73. //----------------------------------------------------------------------------
  74. ctkThumbnailWidget::~ctkThumbnailWidget()
  75. {
  76. }
  77. //----------------------------------------------------------------------------
  78. void ctkThumbnailWidget::setText(const QString &text)
  79. {
  80. Q_D(ctkThumbnailWidget);
  81. d->TextLabel->setText(text);
  82. }
  83. //----------------------------------------------------------------------------
  84. QString ctkThumbnailWidget::text()const
  85. {
  86. Q_D(const ctkThumbnailWidget);
  87. return d->TextLabel->text();
  88. }
  89. //----------------------------------------------------------------------------
  90. void ctkThumbnailWidget::setTextPosition(const Qt::Alignment& position)
  91. {
  92. Q_D(ctkThumbnailWidget);
  93. d->TextPosition = position;
  94. int textIndex = -1;
  95. for (textIndex = 0; textIndex < this->layout()->count(); ++textIndex)
  96. {
  97. if (this->layout()->itemAt(textIndex)->widget() == d->TextLabel)
  98. {
  99. break;
  100. }
  101. }
  102. if (textIndex > -1 && textIndex < this->layout()->count())
  103. {
  104. this->layout()->takeAt(textIndex);
  105. }
  106. int row = 1;
  107. int col = 1;
  108. QGridLayout* gridLayout = qobject_cast<QGridLayout*>(this->layout());
  109. if (position & Qt::AlignTop)
  110. {
  111. row = 0;
  112. }
  113. else if (position &Qt::AlignBottom)
  114. {
  115. row = 2;
  116. }
  117. else
  118. {
  119. row = 1;
  120. }
  121. if (position & Qt::AlignLeft)
  122. {
  123. col = 0;
  124. }
  125. else if (position & Qt::AlignRight)
  126. {
  127. col = 2;
  128. }
  129. else
  130. {
  131. col = 1;
  132. }
  133. if (row == 1 && col == 1)
  134. {
  135. d->TextLabel->setVisible(false);
  136. }
  137. else
  138. {
  139. gridLayout->addWidget(d->TextLabel,row, col);
  140. }
  141. }
  142. //----------------------------------------------------------------------------
  143. Qt::Alignment ctkThumbnailWidget::textPosition()const
  144. {
  145. Q_D(const ctkThumbnailWidget);
  146. return d->TextPosition;
  147. }
  148. //----------------------------------------------------------------------------
  149. void ctkThumbnailWidget::setPixmap(const QPixmap &pixmap)
  150. {
  151. Q_D(ctkThumbnailWidget);
  152. d->OriginalThumbnail = pixmap;
  153. d->updateThumbnail();
  154. }
  155. //----------------------------------------------------------------------------
  156. const QPixmap* ctkThumbnailWidget::pixmap()const
  157. {
  158. Q_D(const ctkThumbnailWidget);
  159. return d->OriginalThumbnail.isNull() ? 0 : &(d->OriginalThumbnail);
  160. }
  161. //----------------------------------------------------------------------------
  162. Qt::TransformationMode ctkThumbnailWidget::transformationMode()const
  163. {
  164. Q_D(const ctkThumbnailWidget);
  165. return d->TransformationMode;
  166. }
  167. //----------------------------------------------------------------------------
  168. void ctkThumbnailWidget::setTransformationMode(Qt::TransformationMode mode)
  169. {
  170. Q_D(ctkThumbnailWidget);
  171. d->TransformationMode = mode;
  172. d->updateThumbnail();
  173. }
  174. //----------------------------------------------------------------------------
  175. void ctkThumbnailWidget::setSelected(bool flag)
  176. {
  177. Q_D(ctkThumbnailWidget);
  178. if(flag && d->SelectedColor.isValid())
  179. {
  180. QPalette p(this->palette());
  181. p.setColor(QPalette::Window, d->SelectedColor);
  182. this->setPalette(p);
  183. this->setAutoFillBackground(true);
  184. }
  185. else
  186. {
  187. this->setAutoFillBackground(false);
  188. }
  189. d->SelectedFlag = flag;
  190. }
  191. //----------------------------------------------------------------------------
  192. bool ctkThumbnailWidget::isSelected()const
  193. {
  194. Q_D(const ctkThumbnailWidget);
  195. return d->SelectedFlag;
  196. }
  197. //----------------------------------------------------------------------------
  198. void ctkThumbnailWidget::setSelectedColor(const QColor& color)
  199. {
  200. Q_D(ctkThumbnailWidget);
  201. d->SelectedColor = color;
  202. // repaint if the color has changed.
  203. this->setSelected(this->isSelected());
  204. }
  205. //----------------------------------------------------------------------------
  206. QColor ctkThumbnailWidget::selectedColor()const
  207. {
  208. Q_D(const ctkThumbnailWidget);
  209. return d->SelectedColor;
  210. }
  211. //----------------------------------------------------------------------------
  212. void ctkThumbnailWidget::mousePressEvent(QMouseEvent* event)
  213. {
  214. this->Superclass::mousePressEvent(event);
  215. this->setSelected(true);
  216. emit selected(*this);
  217. }
  218. //----------------------------------------------------------------------------
  219. void ctkThumbnailWidget::mouseDoubleClickEvent(QMouseEvent *event)
  220. {
  221. this->Superclass::mouseDoubleClickEvent(event);
  222. emit doubleClicked(*this);
  223. }
  224. //----------------------------------------------------------------------------
  225. void ctkThumbnailWidget::resizeEvent(QResizeEvent *event)
  226. {
  227. Q_D(ctkThumbnailWidget);
  228. this->Superclass::resizeEvent(event);
  229. d->updateThumbnail();
  230. }