ctkThumbnailListWidget.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 include
  15. #include <QDateTime>
  16. #include <QDir>
  17. #include <QFile>
  18. #include <QFileInfo>
  19. #include <QGridLayout>
  20. #include <QPixmap>
  21. #include <QPushButton>
  22. #include <QResizeEvent>
  23. #include <QScrollBar>
  24. // ctk includes
  25. #include "ctkLogger.h"
  26. // ctkWidgets includes
  27. #include "ctkFlowLayout.h"
  28. // ctkDICOMWidgets includes
  29. #include "ctkThumbnailLabel.h"
  30. #include "ctkThumbnailListWidget.h"
  31. #include "ctkThumbnailListWidget_p.h"
  32. // STD includes
  33. #include <iostream>
  34. static ctkLogger logger("org.commontk.Widgets.ctkThumbnailListWidget");
  35. //----------------------------------------------------------------------------
  36. // ctkThumbnailListWidgetPrivate methods
  37. //----------------------------------------------------------------------------
  38. ctkThumbnailListWidgetPrivate
  39. ::ctkThumbnailListWidgetPrivate(ctkThumbnailListWidget* parent)
  40. : q_ptr(parent)
  41. {
  42. }
  43. //----------------------------------------------------------------------------
  44. void ctkThumbnailListWidgetPrivate::init()
  45. {
  46. Q_Q(ctkThumbnailListWidget);
  47. this->setupUi(q);
  48. ctkFlowLayout* flowLayout = new ctkFlowLayout;
  49. //flowLayout->setOrientation(Qt::Vertical);
  50. //flowLayout->setPreferredExpandingDirections(Qt::Horizontal);
  51. flowLayout->setHorizontalSpacing(4);
  52. this->ScrollAreaContentWidget->setLayout(flowLayout);
  53. this->ScrollArea->installEventFilter(q);
  54. this->ThumbnailSize = QSize(-1, -1);
  55. this->CurrentThumbnail = -1;
  56. }
  57. //----------------------------------------------------------------------------
  58. void ctkThumbnailListWidgetPrivate::clearAllThumbnails()
  59. {
  60. Q_Q(ctkThumbnailListWidget);
  61. // Remove previous displayed thumbnails
  62. QLayoutItem* item;
  63. while((item = this->ScrollAreaContentWidget->layout()->takeAt(0)))
  64. {
  65. ctkThumbnailLabel* thumbnailWidget = qobject_cast<ctkThumbnailLabel*>(item->widget());
  66. if(thumbnailWidget)
  67. {
  68. q->disconnect(thumbnailWidget, SIGNAL(selected(ctkThumbnailLabel)), q, SLOT(onThumbnailSelected(ctkThumbnailLabel)));
  69. q->disconnect(thumbnailWidget, SIGNAL(selected(ctkThumbnailLabel)), q, SIGNAL(selected(ctkThumbnailLabel)));
  70. q->disconnect(thumbnailWidget, SIGNAL(doubleClicked(ctkThumbnailLabel)), q, SIGNAL(doubleClicked(ctkThumbnailLabel)));
  71. }
  72. item->widget()->deleteLater();
  73. }
  74. }
  75. //----------------------------------------------------------------------------
  76. // ctkThumbnailListWidget methods
  77. //----------------------------------------------------------------------------
  78. ctkThumbnailListWidget::ctkThumbnailListWidget(QWidget* _parent):
  79. Superclass(_parent),
  80. d_ptr(new ctkThumbnailListWidgetPrivate(this))
  81. {
  82. Q_D(ctkThumbnailListWidget);
  83. d->init();
  84. }
  85. //----------------------------------------------------------------------------
  86. ctkThumbnailListWidget::ctkThumbnailListWidget(ctkThumbnailListWidgetPrivate *_ptr, QWidget *_parent):
  87. Superclass(_parent),
  88. d_ptr(_ptr)
  89. {
  90. Q_D(ctkThumbnailListWidget);
  91. d->init();
  92. }
  93. //----------------------------------------------------------------------------
  94. ctkThumbnailListWidget::~ctkThumbnailListWidget()
  95. {
  96. Q_D(ctkThumbnailListWidget);
  97. d->clearAllThumbnails();
  98. }
  99. //----------------------------------------------------------------------------
  100. void ctkThumbnailListWidget::addThumbnails(QList<QPixmap> thumbnails)
  101. {
  102. Q_D(ctkThumbnailListWidget);
  103. for(int i=0; i<thumbnails.count(); i++)
  104. {
  105. ctkThumbnailLabel* widget = new ctkThumbnailLabel(d->ScrollAreaContentWidget);
  106. widget->setText("");
  107. if(d->ThumbnailSize.isValid())
  108. {
  109. widget->setFixedSize(d->ThumbnailSize);
  110. }
  111. widget->setPixmap(thumbnails[i]);
  112. d->ScrollAreaContentWidget->layout()->addWidget(widget);
  113. this->connect(widget, SIGNAL(selected(ctkThumbnailLabel)), this, SLOT(onThumbnailSelected(ctkThumbnailLabel)));
  114. this->connect(widget, SIGNAL(selected(ctkThumbnailLabel)), this, SIGNAL(selected(ctkThumbnailLabel)));
  115. this->connect(widget, SIGNAL(doubleClicked(ctkThumbnailLabel)), this, SIGNAL(doubleClicked(ctkThumbnailLabel)));
  116. }
  117. }
  118. //----------------------------------------------------------------------------
  119. void ctkThumbnailListWidget::setCurrentThumbnail(int index)
  120. {
  121. Q_D(ctkThumbnailListWidget);
  122. int count = d->ScrollAreaContentWidget->layout()->count();
  123. logger.debug("Select thumbnail " + QVariant(index).toString() + " of " + QVariant(count).toString());
  124. if(index >= count)return;
  125. for(int i=0; i<count; i++)
  126. {
  127. ctkThumbnailLabel* thumbnailWidget = qobject_cast<ctkThumbnailLabel*>(d->ScrollAreaContentWidget->layout()->itemAt(i)->widget());
  128. if(i == index)
  129. {
  130. thumbnailWidget->setSelected(true);
  131. d->ScrollArea->ensureWidgetVisible(thumbnailWidget);
  132. }
  133. else
  134. {
  135. thumbnailWidget->setSelected(false);
  136. }
  137. }
  138. d->CurrentThumbnail = index;
  139. }
  140. //----------------------------------------------------------------------------
  141. int ctkThumbnailListWidget::currentThumbnail()
  142. {
  143. Q_D(ctkThumbnailListWidget);
  144. return d->CurrentThumbnail;
  145. }
  146. //----------------------------------------------------------------------------
  147. void ctkThumbnailListWidget::onThumbnailSelected(const ctkThumbnailLabel &widget)
  148. {
  149. Q_D(ctkThumbnailListWidget);
  150. for(int i=0; i<d->ScrollAreaContentWidget->layout()->count(); i++)
  151. {
  152. ctkThumbnailLabel* thumbnailWidget = qobject_cast<ctkThumbnailLabel*>(d->ScrollAreaContentWidget->layout()->itemAt(i)->widget());
  153. if(thumbnailWidget && (&widget != thumbnailWidget))
  154. {
  155. thumbnailWidget->setSelected(false);
  156. }
  157. }
  158. }
  159. //----------------------------------------------------------------------------
  160. void ctkThumbnailListWidget::setFlow(Qt::Orientation flow)
  161. {
  162. Q_D(ctkThumbnailListWidget);
  163. ctkFlowLayout* flowLayout = qobject_cast<ctkFlowLayout*>(
  164. d->ScrollAreaContentWidget->layout());
  165. flowLayout->setOrientation(flow);
  166. }
  167. //----------------------------------------------------------------------------
  168. Qt::Orientation ctkThumbnailListWidget::flow()const
  169. {
  170. Q_D(const ctkThumbnailListWidget);
  171. ctkFlowLayout* flowLayout = qobject_cast<ctkFlowLayout*>(
  172. d->ScrollAreaContentWidget->layout());
  173. return flowLayout->orientation();
  174. }
  175. //----------------------------------------------------------------------------
  176. void ctkThumbnailListWidget::setThumbnailSize(QSize size)
  177. {
  178. Q_D(ctkThumbnailListWidget);
  179. if (size.isValid())
  180. {
  181. foreach( ctkThumbnailLabel* thumbnail,
  182. this->findChildren<ctkThumbnailLabel*>())
  183. {
  184. thumbnail->setFixedSize(size);
  185. }
  186. }
  187. d->ThumbnailSize = size;
  188. }
  189. //----------------------------------------------------------------------------
  190. QSize ctkThumbnailListWidget::thumbnailSize()const
  191. {
  192. Q_D(const ctkThumbnailListWidget);
  193. return d->ThumbnailSize;
  194. }
  195. //----------------------------------------------------------------------------
  196. void ctkThumbnailListWidget::clearThumbnails()
  197. {
  198. Q_D(ctkThumbnailListWidget);
  199. d->clearAllThumbnails();
  200. }
  201. //----------------------------------------------------------------------------
  202. void ctkThumbnailListWidget::resizeEvent(QResizeEvent* event)
  203. {
  204. Q_D(ctkThumbnailListWidget);
  205. QSize newViewportSize = event->size() - QSize(2 * d->ScrollArea->lineWidth(),
  206. 2 * d->ScrollArea->lineWidth());
  207. ctkFlowLayout* flowLayout = qobject_cast<ctkFlowLayout*>(
  208. d->ScrollAreaContentWidget->layout());
  209. newViewportSize = newViewportSize.expandedTo(flowLayout->minimumSize());
  210. if (flowLayout->hasHeightForWidth())
  211. {
  212. int newViewPortHeight = newViewportSize.height();
  213. newViewportSize.rheight() = flowLayout->heightForWidth( newViewportSize.width() );
  214. if (newViewportSize.height() > newViewPortHeight)
  215. {
  216. // The new width is too narrow, to fit everything, a vertical scrollbar
  217. // is needed. Recompute with the scrollbar width.
  218. newViewportSize.rwidth() -= d->ScrollArea->verticalScrollBar()->sizeHint().width();
  219. newViewportSize.rheight() = flowLayout->heightForWidth( newViewportSize.width() );
  220. }
  221. }
  222. else if (flowLayout->hasWidthForHeight())
  223. {
  224. int newViewPortWidth = newViewportSize.width();
  225. newViewportSize.rwidth() = flowLayout->widthForHeight( newViewportSize.height() );
  226. if (newViewportSize.width() > newViewPortWidth)
  227. {
  228. // The new height is too narrow, to fit everything, an horizontal scrollbar
  229. // is needed. Recompute with the scrollbar height.
  230. newViewportSize.rheight() -= d->ScrollArea->horizontalScrollBar()->sizeHint().height();
  231. newViewportSize.rwidth() = flowLayout->widthForHeight( newViewportSize.height() );
  232. }
  233. }
  234. d->ScrollAreaContentWidget->resize(newViewportSize);
  235. }