ctkThumbnailWidget.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. // Redraw thumbnail
  38. void updateThumbnail();
  39. };
  40. //----------------------------------------------------------------------------
  41. // ctkThumbnailWidgetPrivate methods
  42. //----------------------------------------------------------------------------
  43. ctkThumbnailWidgetPrivate::ctkThumbnailWidgetPrivate(ctkThumbnailWidget* parent)
  44. : q_ptr(parent)
  45. {
  46. Q_Q(ctkThumbnailWidget);
  47. this->SelectedFlag = false;
  48. this->SelectedColor = q->palette().color(QPalette::Highlight);
  49. this->TextPosition = Qt::AlignTop | Qt::AlignHCenter;
  50. }
  51. //----------------------------------------------------------------------------
  52. void ctkThumbnailWidgetPrivate::updateThumbnail()
  53. {
  54. this->PixmapLabel->setPixmap(
  55. this->OriginalThumbnail.isNull() ? QPixmap() :
  56. this->OriginalThumbnail.scaledToWidth(this->PixmapLabel->width()));
  57. }
  58. //----------------------------------------------------------------------------
  59. // ctkThumbnailWidget methods
  60. //----------------------------------------------------------------------------
  61. ctkThumbnailWidget::ctkThumbnailWidget(QWidget* parentWidget)
  62. : Superclass(parentWidget)
  63. , d_ptr(new ctkThumbnailWidgetPrivate(this))
  64. {
  65. Q_D(ctkThumbnailWidget);
  66. d->setupUi(this);
  67. this->setTextPosition(Qt::AlignTop | Qt::AlignHCenter);
  68. }
  69. //----------------------------------------------------------------------------
  70. ctkThumbnailWidget::~ctkThumbnailWidget()
  71. {
  72. }
  73. //----------------------------------------------------------------------------
  74. void ctkThumbnailWidget::setText(const QString &text)
  75. {
  76. Q_D(ctkThumbnailWidget);
  77. d->TextLabel->setText(text);
  78. }
  79. //----------------------------------------------------------------------------
  80. QString ctkThumbnailWidget::text()const
  81. {
  82. Q_D(const ctkThumbnailWidget);
  83. return d->TextLabel->text();
  84. }
  85. //----------------------------------------------------------------------------
  86. void ctkThumbnailWidget::setTextPosition(const Qt::Alignment& position)
  87. {
  88. Q_D(ctkThumbnailWidget);
  89. d->TextPosition = position;
  90. int textIndex = -1;
  91. for (textIndex = 0; textIndex < this->layout()->count(); ++textIndex)
  92. {
  93. if (this->layout()->itemAt(textIndex)->widget() == d->TextLabel)
  94. {
  95. break;
  96. }
  97. }
  98. if (textIndex > -1 && textIndex < this->layout()->count())
  99. {
  100. this->layout()->takeAt(textIndex);
  101. }
  102. int row = 1;
  103. int col = 1;
  104. QGridLayout* gridLayout = qobject_cast<QGridLayout*>(this->layout());
  105. if (position & Qt::AlignTop)
  106. {
  107. row = 0;
  108. }
  109. else if (position &Qt::AlignBottom)
  110. {
  111. row = 2;
  112. }
  113. else
  114. {
  115. row = 1;
  116. }
  117. if (position & Qt::AlignLeft)
  118. {
  119. col = 0;
  120. }
  121. else if (position & Qt::AlignRight)
  122. {
  123. col = 2;
  124. }
  125. else
  126. {
  127. col = 1;
  128. }
  129. if (row == 1 && col == 1)
  130. {
  131. d->TextLabel->setVisible(false);
  132. }
  133. else
  134. {
  135. gridLayout->addWidget(d->TextLabel,row, col);
  136. }
  137. }
  138. //----------------------------------------------------------------------------
  139. Qt::Alignment ctkThumbnailWidget::textPosition()const
  140. {
  141. Q_D(const ctkThumbnailWidget);
  142. return d->TextPosition;
  143. }
  144. //----------------------------------------------------------------------------
  145. void ctkThumbnailWidget::setPixmap(const QPixmap &pixmap)
  146. {
  147. Q_D(ctkThumbnailWidget);
  148. d->OriginalThumbnail = pixmap;
  149. d->updateThumbnail();
  150. }
  151. //----------------------------------------------------------------------------
  152. const QPixmap* ctkThumbnailWidget::pixmap()const
  153. {
  154. Q_D(const ctkThumbnailWidget);
  155. return d->OriginalThumbnail.isNull() ? 0 : &(d->OriginalThumbnail);
  156. }
  157. //----------------------------------------------------------------------------
  158. void ctkThumbnailWidget::setSelected(bool flag)
  159. {
  160. Q_D(ctkThumbnailWidget);
  161. if(flag && d->SelectedColor.isValid())
  162. {
  163. QPalette p(this->palette());
  164. p.setColor(QPalette::Window, d->SelectedColor);
  165. this->setPalette(p);
  166. this->setAutoFillBackground(true);
  167. }
  168. else
  169. {
  170. this->setAutoFillBackground(false);
  171. }
  172. d->SelectedFlag = flag;
  173. }
  174. //----------------------------------------------------------------------------
  175. bool ctkThumbnailWidget::isSelected()const
  176. {
  177. Q_D(const ctkThumbnailWidget);
  178. return d->SelectedFlag;
  179. }
  180. //----------------------------------------------------------------------------
  181. void ctkThumbnailWidget::setSelectedColor(const QColor& color)
  182. {
  183. Q_D(ctkThumbnailWidget);
  184. d->SelectedColor = color;
  185. // repaint if the color has changed.
  186. this->setSelected(this->isSelected());
  187. }
  188. //----------------------------------------------------------------------------
  189. QColor ctkThumbnailWidget::selectedColor()const
  190. {
  191. Q_D(const ctkThumbnailWidget);
  192. return d->SelectedColor;
  193. }
  194. //----------------------------------------------------------------------------
  195. void ctkThumbnailWidget::mousePressEvent(QMouseEvent* event)
  196. {
  197. Q_UNUSED(event);
  198. this->setSelected(true);
  199. emit selected(*this);
  200. }
  201. //----------------------------------------------------------------------------
  202. void ctkThumbnailWidget::mouseDoubleClickEvent(QMouseEvent *event){
  203. Q_UNUSED(event);
  204. emit doubleClicked(*this);
  205. }
  206. //----------------------------------------------------------------------------
  207. void ctkThumbnailWidget::resizeEvent(QResizeEvent *event){
  208. Q_D(ctkThumbnailWidget);
  209. Q_UNUSED(event);
  210. d->updateThumbnail();
  211. }