ctkThumbnailLabel.cpp 9.2 KB

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