ctkThumbnailLabel.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. // Qt includes
  15. #include <QApplication>
  16. #include <QColor>
  17. #include <QPainter>
  18. // ctkCore includes
  19. #include "ctkLogger.h"
  20. static ctkLogger logger("org.commontk.Widgets.ctkThumbnailLabel");
  21. // ctkWidgets includes
  22. #include "ctkThumbnailLabel.h"
  23. #include "ui_ctkThumbnailLabel.h"
  24. // STD includes
  25. #include <iostream>
  26. //----------------------------------------------------------------------------
  27. class ctkThumbnailLabelPrivate: public Ui_ctkThumbnailLabel
  28. {
  29. Q_DECLARE_PUBLIC(ctkThumbnailLabel);
  30. protected:
  31. ctkThumbnailLabel* const q_ptr;
  32. public:
  33. typedef Ui_ctkThumbnailLabel Superclass;
  34. // Constructor
  35. ctkThumbnailLabelPrivate(ctkThumbnailLabel* parent);
  36. virtual void setupUi(QWidget* widget);
  37. Qt::Alignment TextPosition;
  38. bool SelectedFlag;
  39. QColor SelectedColor;
  40. QModelIndex SourceIndex;
  41. QPixmap OriginalThumbnail;
  42. Qt::TransformationMode TransformationMode;
  43. // Redraw thumbnail
  44. void updateThumbnail();
  45. };
  46. //----------------------------------------------------------------------------
  47. // ctkThumbnailLabelPrivate methods
  48. //----------------------------------------------------------------------------
  49. ctkThumbnailLabelPrivate::ctkThumbnailLabelPrivate(ctkThumbnailLabel* parent)
  50. : q_ptr(parent)
  51. {
  52. Q_Q(ctkThumbnailLabel);
  53. this->SelectedFlag = false;
  54. this->SelectedColor = q->palette().color(QPalette::Highlight);
  55. this->TextPosition = Qt::AlignTop | Qt::AlignHCenter;
  56. this->TransformationMode = Qt::FastTransformation;
  57. }
  58. //----------------------------------------------------------------------------
  59. void ctkThumbnailLabelPrivate::setupUi(QWidget* widget)
  60. {
  61. Q_Q(ctkThumbnailLabel);
  62. this->Superclass::setupUi(widget);
  63. q->layout()->setSizeConstraint(QLayout::SetNoConstraint);
  64. // no text by default
  65. q->setText(QString());
  66. }
  67. //----------------------------------------------------------------------------
  68. void ctkThumbnailLabelPrivate::updateThumbnail()
  69. {
  70. this->PixmapLabel->setPixmap(
  71. this->OriginalThumbnail.isNull() ? QPixmap() :
  72. this->OriginalThumbnail.scaled(this->PixmapLabel->size(),
  73. Qt::KeepAspectRatio,
  74. this->TransformationMode));
  75. }
  76. //----------------------------------------------------------------------------
  77. // ctkThumbnailLabel methods
  78. //----------------------------------------------------------------------------
  79. ctkThumbnailLabel::ctkThumbnailLabel(QWidget* parentWidget)
  80. : Superclass(parentWidget)
  81. , d_ptr(new ctkThumbnailLabelPrivate(this))
  82. {
  83. Q_D(ctkThumbnailLabel);
  84. d->setupUi(this);
  85. }
  86. //----------------------------------------------------------------------------
  87. ctkThumbnailLabel::~ctkThumbnailLabel()
  88. {
  89. }
  90. //----------------------------------------------------------------------------
  91. void ctkThumbnailLabel::setText(const QString &text)
  92. {
  93. Q_D(ctkThumbnailLabel);
  94. d->TextLabel->setText(text);
  95. d->TextLabel->setVisible(!text.isEmpty() &&
  96. ! (d->TextPosition & Qt::AlignHCenter &&
  97. d->TextPosition & Qt::AlignVCenter) );
  98. }
  99. //----------------------------------------------------------------------------
  100. QString ctkThumbnailLabel::text()const
  101. {
  102. Q_D(const ctkThumbnailLabel);
  103. return d->TextLabel->text();
  104. }
  105. //----------------------------------------------------------------------------
  106. void ctkThumbnailLabel::setTextPosition(const Qt::Alignment& position)
  107. {
  108. Q_D(ctkThumbnailLabel);
  109. d->TextPosition = position;
  110. int textIndex = -1;
  111. for (textIndex = 0; textIndex < this->layout()->count(); ++textIndex)
  112. {
  113. if (this->layout()->itemAt(textIndex)->widget() == d->TextLabel)
  114. {
  115. break;
  116. }
  117. }
  118. if (textIndex > -1 && textIndex < this->layout()->count())
  119. {
  120. this->layout()->takeAt(textIndex);
  121. }
  122. int row = 1;
  123. int col = 1;
  124. QGridLayout* gridLayout = qobject_cast<QGridLayout*>(this->layout());
  125. if (position & Qt::AlignTop)
  126. {
  127. row = 0;
  128. }
  129. else if (position &Qt::AlignBottom)
  130. {
  131. row = 2;
  132. }
  133. else
  134. {
  135. row = 1;
  136. }
  137. if (position & Qt::AlignLeft)
  138. {
  139. col = 0;
  140. }
  141. else if (position & Qt::AlignRight)
  142. {
  143. col = 2;
  144. }
  145. else
  146. {
  147. col = 1;
  148. }
  149. if (row == 1 && col == 1)
  150. {
  151. d->TextLabel->setVisible(false);
  152. }
  153. else
  154. {
  155. gridLayout->addWidget(d->TextLabel,row, col);
  156. }
  157. }
  158. //----------------------------------------------------------------------------
  159. Qt::Alignment ctkThumbnailLabel::textPosition()const
  160. {
  161. Q_D(const ctkThumbnailLabel);
  162. return d->TextPosition;
  163. }
  164. //----------------------------------------------------------------------------
  165. void ctkThumbnailLabel::setPixmap(const QPixmap &pixmap)
  166. {
  167. Q_D(ctkThumbnailLabel);
  168. d->OriginalThumbnail = pixmap;
  169. d->updateThumbnail();
  170. }
  171. //----------------------------------------------------------------------------
  172. const QPixmap* ctkThumbnailLabel::pixmap()const
  173. {
  174. Q_D(const ctkThumbnailLabel);
  175. return d->OriginalThumbnail.isNull() ? 0 : &(d->OriginalThumbnail);
  176. }
  177. //----------------------------------------------------------------------------
  178. Qt::TransformationMode ctkThumbnailLabel::transformationMode()const
  179. {
  180. Q_D(const ctkThumbnailLabel);
  181. return d->TransformationMode;
  182. }
  183. //----------------------------------------------------------------------------
  184. void ctkThumbnailLabel::setTransformationMode(Qt::TransformationMode mode)
  185. {
  186. Q_D(ctkThumbnailLabel);
  187. d->TransformationMode = mode;
  188. d->updateThumbnail();
  189. }
  190. //----------------------------------------------------------------------------
  191. void ctkThumbnailLabel::paintEvent(QPaintEvent* event)
  192. {
  193. Q_D(ctkThumbnailLabel);
  194. this->Superclass::paintEvent(event);
  195. if (d->SelectedFlag && d->SelectedColor.isValid())
  196. {
  197. QPainter p(this);
  198. QPen pen(d->SelectedColor);
  199. pen.setWidth(7);
  200. p.setPen(pen);
  201. p.drawRect(QRect(0,0, this->width() -1, this->height() -1));
  202. }
  203. }
  204. //----------------------------------------------------------------------------
  205. void ctkThumbnailLabel::setSelected(bool flag)
  206. {
  207. Q_D(ctkThumbnailLabel);
  208. d->SelectedFlag = flag;
  209. this->update();
  210. }
  211. //----------------------------------------------------------------------------
  212. bool ctkThumbnailLabel::isSelected()const
  213. {
  214. Q_D(const ctkThumbnailLabel);
  215. return d->SelectedFlag;
  216. }
  217. //----------------------------------------------------------------------------
  218. void ctkThumbnailLabel::setSelectedColor(const QColor& color)
  219. {
  220. Q_D(ctkThumbnailLabel);
  221. d->SelectedColor = color;
  222. this->update();
  223. }
  224. //----------------------------------------------------------------------------
  225. QColor ctkThumbnailLabel::selectedColor()const
  226. {
  227. Q_D(const ctkThumbnailLabel);
  228. return d->SelectedColor;
  229. }
  230. //----------------------------------------------------------------------------
  231. QSize ctkThumbnailLabel::minimumSizeHint()const
  232. {
  233. Q_D(const ctkThumbnailLabel);
  234. if (d->TextLabel->isVisibleTo(const_cast<ctkThumbnailLabel*>(this)) &&
  235. !d->TextLabel->text().isEmpty())
  236. {
  237. return d->TextLabel->minimumSizeHint();
  238. }
  239. return QSize();
  240. }
  241. //----------------------------------------------------------------------------
  242. QSize ctkThumbnailLabel::sizeHint()const
  243. {
  244. Q_D(const ctkThumbnailLabel);
  245. return d->OriginalThumbnail.isNull() ?
  246. this->Superclass::sizeHint() :
  247. d->OriginalThumbnail.size().expandedTo(QApplication::globalStrut());
  248. }
  249. //----------------------------------------------------------------------------
  250. int ctkThumbnailLabel::heightForWidth(int width)const
  251. {
  252. Q_D(const ctkThumbnailLabel);
  253. if (d->OriginalThumbnail.isNull() ||
  254. d->OriginalThumbnail.width() == 0)
  255. {
  256. return this->Superclass::heightForWidth(width);
  257. }
  258. double ratio = static_cast<double>(d->OriginalThumbnail.height()) /
  259. static_cast<double>(d->OriginalThumbnail.width());
  260. return static_cast<int>(ratio * width + 0.5);
  261. }
  262. //----------------------------------------------------------------------------
  263. void ctkThumbnailLabel::mousePressEvent(QMouseEvent* event)
  264. {
  265. this->Superclass::mousePressEvent(event);
  266. this->setSelected(true);
  267. emit selected(*this);
  268. }
  269. //----------------------------------------------------------------------------
  270. void ctkThumbnailLabel::mouseDoubleClickEvent(QMouseEvent *event)
  271. {
  272. this->Superclass::mouseDoubleClickEvent(event);
  273. emit doubleClicked(*this);
  274. }
  275. //----------------------------------------------------------------------------
  276. void ctkThumbnailLabel::resizeEvent(QResizeEvent *event)
  277. {
  278. Q_D(ctkThumbnailLabel);
  279. this->Superclass::resizeEvent(event);
  280. d->updateThumbnail();
  281. }