/*========================================================================= 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 #include #include #include #include #include #include #include #include // 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 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(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 thumbnails) { Q_D(ctkThumbnailListWidget); for(int i=0; iScrollAreaContentWidget); 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(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; iScrollAreaContentWidget->layout()->count(); i++) { ctkThumbnailLabel* thumbnailWidget = qobject_cast(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( d->ScrollAreaContentWidget->layout()); flowLayout->setOrientation(flow); } //---------------------------------------------------------------------------- Qt::Orientation ctkThumbnailListWidget::flow()const { Q_D(const ctkThumbnailListWidget); ctkFlowLayout* flowLayout = qobject_cast( d->ScrollAreaContentWidget->layout()); return flowLayout->orientation(); } //---------------------------------------------------------------------------- void ctkThumbnailListWidget::setThumbnailSize(QSize size) { Q_D(ctkThumbnailListWidget); if (size.isValid()) { foreach( ctkThumbnailLabel* thumbnail, this->findChildren()) { 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( 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); }