| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 | 
							- /*=========================================================================
 
-   Library:   CTK
 
-   Copyright (c) Kitware Inc.
 
-   Licensed under the Apache License, Version 2.0 (the "License");
 
-   you may not use this file except in compliance with the License.
 
-   You may obtain a copy of the License at
 
-       http://www.apache.org/licenses/LICENSE-2.0.txt
 
-   Unless required by applicable law or agreed to in writing, software
 
-   distributed under the License is distributed on an "AS IS" BASIS,
 
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
-   See the License for the specific language governing permissions and
 
-   limitations under the License.
 
- =========================================================================*/
 
- // Qt include
 
- #include <QDateTime>
 
- #include <QDir>
 
- #include <QFile>
 
- #include <QFileInfo>
 
- #include <QGridLayout>
 
- #include <QPixmap>
 
- #include <QPushButton>
 
- #include <QResizeEvent>
 
- #include <QScrollBar>
 
- // ctk includes
 
- #include "ctkLogger.h"
 
- // ctkWidgets includes
 
- #include "ctkFlowLayout.h"
 
- // ctkDICOMWidgets includes
 
- #include "ctkThumbnailLabel.h"
 
- #include "ctkThumbnailListWidget.h"
 
- #include "ctkThumbnailListWidget_p.h"
 
- // STD includes
 
- #include <iostream>
 
- static ctkLogger logger("org.commontk.Widgets.ctkThumbnailListWidget");
 
- //----------------------------------------------------------------------------
 
- // ctkThumbnailListWidgetPrivate methods
 
- //----------------------------------------------------------------------------
 
- ctkThumbnailListWidgetPrivate
 
- ::ctkThumbnailListWidgetPrivate(ctkThumbnailListWidget* parent)
 
-   : q_ptr(parent)
 
- {
 
- }
 
- //----------------------------------------------------------------------------
 
- void ctkThumbnailListWidgetPrivate::init()
 
- {
 
-   Q_Q(ctkThumbnailListWidget);
 
-   this->setupUi(q);
 
-   ctkFlowLayout* flowLayout = new ctkFlowLayout;
 
-   //flowLayout->setOrientation(Qt::Vertical);
 
-   //flowLayout->setPreferredExpandingDirections(Qt::Horizontal);
 
-   flowLayout->setHorizontalSpacing(4);
 
-   this->ScrollAreaContentWidget->setLayout(flowLayout);
 
-   this->ScrollArea->installEventFilter(q);
 
-   this->ThumbnailSize = QSize(-1, -1);
 
-   this->CurrentThumbnail = -1;
 
- }
 
- //----------------------------------------------------------------------------
 
- void ctkThumbnailListWidgetPrivate::clearAllThumbnails()
 
- {
 
-   Q_Q(ctkThumbnailListWidget);
 
-   // Remove previous displayed thumbnails
 
-   QLayoutItem* item;
 
-   while((item = this->ScrollAreaContentWidget->layout()->takeAt(0)))
 
-     {
 
-     ctkThumbnailLabel* thumbnailWidget = qobject_cast<ctkThumbnailLabel*>(item->widget());
 
-     if(thumbnailWidget)
 
-       {
 
-       q->disconnect(thumbnailWidget, SIGNAL(selected(ctkThumbnailLabel)), q, SLOT(onThumbnailSelected(ctkThumbnailLabel)));
 
-       q->disconnect(thumbnailWidget, SIGNAL(selected(ctkThumbnailLabel)), q, SIGNAL(selected(ctkThumbnailLabel)));
 
-       q->disconnect(thumbnailWidget, SIGNAL(doubleClicked(ctkThumbnailLabel)), q, SIGNAL(doubleClicked(ctkThumbnailLabel)));
 
-       }
 
-     item->widget()->deleteLater();
 
-     }
 
- }
 
- //----------------------------------------------------------------------------
 
- // ctkThumbnailListWidget methods
 
- //----------------------------------------------------------------------------
 
- ctkThumbnailListWidget::ctkThumbnailListWidget(QWidget* _parent):
 
-   Superclass(_parent),
 
-   d_ptr(new ctkThumbnailListWidgetPrivate(this))
 
- {
 
-   Q_D(ctkThumbnailListWidget);
 
-   d->init();
 
- }
 
- //----------------------------------------------------------------------------
 
- ctkThumbnailListWidget::ctkThumbnailListWidget(ctkThumbnailListWidgetPrivate *_ptr, QWidget *_parent):
 
-   Superclass(_parent),
 
-   d_ptr(_ptr)
 
- {
 
-   Q_D(ctkThumbnailListWidget);
 
-   d->init();
 
- }
 
- //----------------------------------------------------------------------------
 
- ctkThumbnailListWidget::~ctkThumbnailListWidget()
 
- {
 
-   Q_D(ctkThumbnailListWidget);
 
-   d->clearAllThumbnails();
 
- }
 
- //----------------------------------------------------------------------------
 
- void ctkThumbnailListWidget::addThumbnails(QList<QPixmap> thumbnails)
 
- {
 
-   Q_D(ctkThumbnailListWidget);
 
-   for(int i=0; i<thumbnails.count(); i++)
 
-     {
 
-     ctkThumbnailLabel* widget = new ctkThumbnailLabel(d->ScrollAreaContentWidget);
 
-     widget->setText("");
 
-     if(d->ThumbnailSize.isValid())
 
-       {
 
-       widget->setFixedSize(d->ThumbnailSize);
 
-       }
 
-     widget->setPixmap(thumbnails[i]);
 
-     d->ScrollAreaContentWidget->layout()->addWidget(widget);
 
-     this->connect(widget, SIGNAL(selected(ctkThumbnailLabel)), this, SLOT(onThumbnailSelected(ctkThumbnailLabel)));
 
-     this->connect(widget, SIGNAL(selected(ctkThumbnailLabel)), this, SIGNAL(selected(ctkThumbnailLabel)));
 
-     this->connect(widget, SIGNAL(doubleClicked(ctkThumbnailLabel)), this, SIGNAL(doubleClicked(ctkThumbnailLabel)));
 
-     }
 
- }
 
- //----------------------------------------------------------------------------
 
- void ctkThumbnailListWidget::setCurrentThumbnail(int index)
 
- {
 
-   Q_D(ctkThumbnailListWidget);
 
-   int count = d->ScrollAreaContentWidget->layout()->count();
 
-   logger.debug("Select thumbnail " + QVariant(index).toString() + " of " + QVariant(count).toString());
 
-   if(index >= count)return;
 
-   for(int i=0; i<count; i++)
 
-     {
 
-     ctkThumbnailLabel* thumbnailWidget = qobject_cast<ctkThumbnailLabel*>(d->ScrollAreaContentWidget->layout()->itemAt(i)->widget());
 
-     if(i == index)
 
-       {
 
-       thumbnailWidget->setSelected(true);
 
-       d->ScrollArea->ensureWidgetVisible(thumbnailWidget);
 
-       }
 
-     else
 
-       {
 
-       thumbnailWidget->setSelected(false);
 
-       }
 
-     }
 
-   d->CurrentThumbnail = index;
 
- }
 
- //----------------------------------------------------------------------------
 
- int ctkThumbnailListWidget::currentThumbnail()
 
- {
 
-   Q_D(ctkThumbnailListWidget);
 
-   return d->CurrentThumbnail;
 
- }
 
- //----------------------------------------------------------------------------
 
- void ctkThumbnailListWidget::onThumbnailSelected(const ctkThumbnailLabel &widget)
 
- {
 
-   Q_D(ctkThumbnailListWidget);
 
-   for(int i=0; i<d->ScrollAreaContentWidget->layout()->count(); i++)
 
-     {
 
-     ctkThumbnailLabel* thumbnailWidget = qobject_cast<ctkThumbnailLabel*>(d->ScrollAreaContentWidget->layout()->itemAt(i)->widget());
 
-     if(thumbnailWidget && (&widget != thumbnailWidget))
 
-       {
 
-       thumbnailWidget->setSelected(false);
 
-       }
 
-     }
 
- }
 
- //----------------------------------------------------------------------------
 
- void ctkThumbnailListWidget::setFlow(Qt::Orientation flow)
 
- {
 
-   Q_D(ctkThumbnailListWidget);
 
-   ctkFlowLayout* flowLayout = qobject_cast<ctkFlowLayout*>(
 
-     d->ScrollAreaContentWidget->layout());
 
-   flowLayout->setOrientation(flow);
 
- }
 
- //----------------------------------------------------------------------------
 
- Qt::Orientation ctkThumbnailListWidget::flow()const
 
- {
 
-   Q_D(const ctkThumbnailListWidget);
 
-   ctkFlowLayout* flowLayout = qobject_cast<ctkFlowLayout*>(
 
-     d->ScrollAreaContentWidget->layout());
 
-   return flowLayout->orientation();
 
- }
 
- //----------------------------------------------------------------------------
 
- void ctkThumbnailListWidget::setThumbnailSize(QSize size)
 
- {
 
-   Q_D(ctkThumbnailListWidget);
 
-   if (size.isValid())
 
-     {
 
-     foreach( ctkThumbnailLabel* thumbnail,
 
-              this->findChildren<ctkThumbnailLabel*>())
 
-       {
 
-       thumbnail->setFixedSize(size);
 
-       }
 
-     }
 
-   d->ThumbnailSize = size;
 
- }
 
- //----------------------------------------------------------------------------
 
- QSize ctkThumbnailListWidget::thumbnailSize()const
 
- {
 
-   Q_D(const ctkThumbnailListWidget);
 
-   return d->ThumbnailSize;
 
- }
 
- //----------------------------------------------------------------------------
 
- void ctkThumbnailListWidget::clearThumbnails()
 
- {
 
-   Q_D(ctkThumbnailListWidget);
 
-   d->clearAllThumbnails();
 
- }
 
- //----------------------------------------------------------------------------
 
- void ctkThumbnailListWidget::resizeEvent(QResizeEvent* event)
 
- {
 
-   Q_D(ctkThumbnailListWidget);
 
-   QSize newViewportSize = event->size() - QSize(2 * d->ScrollArea->lineWidth(),
 
-                                                 2 * d->ScrollArea->lineWidth());
 
-   ctkFlowLayout* flowLayout = qobject_cast<ctkFlowLayout*>(
 
-     d->ScrollAreaContentWidget->layout());
 
-   newViewportSize = newViewportSize.expandedTo(flowLayout->minimumSize());
 
-   if (flowLayout->hasHeightForWidth())
 
-     {
 
-     int newViewPortHeight = newViewportSize.height();
 
-     newViewportSize.rheight() = flowLayout->heightForWidth( newViewportSize.width() );
 
-     if (newViewportSize.height() > newViewPortHeight)
 
-       {
 
-       // The new width is too narrow, to fit everything, a vertical scrollbar
 
-       // is needed. Recompute with the scrollbar width.
 
-       newViewportSize.rwidth() -= d->ScrollArea->verticalScrollBar()->sizeHint().width();
 
-       newViewportSize.rheight() = flowLayout->heightForWidth( newViewportSize.width() );
 
-       }
 
-     }
 
-   else if (flowLayout->hasWidthForHeight())
 
-     {
 
-     int newViewPortWidth = newViewportSize.width();
 
-     newViewportSize.rwidth() = flowLayout->widthForHeight( newViewportSize.height() );
 
-     if (newViewportSize.width() > newViewPortWidth)
 
-       {
 
-       // The new height is too narrow, to fit everything, an horizontal scrollbar
 
-       // is needed. Recompute with the scrollbar height.
 
-       newViewportSize.rheight() -= d->ScrollArea->horizontalScrollBar()->sizeHint().height();
 
-       newViewportSize.rwidth() = flowLayout->widthForHeight( newViewportSize.height() );
 
-       }
 
-     }
 
-   d->ScrollAreaContentWidget->resize(newViewportSize);
 
- }
 
 
  |