123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- /*=========================================================================
- 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 includes
- #include <QApplication>
- #include <QColor>
- #include <QPainter>
- // ctkCore includes
- #include "ctkLogger.h"
- static ctkLogger logger("org.commontk.Widgets.ctkThumbnailLabel");
- // ctkWidgets includes
- #include "ctkThumbnailLabel.h"
- #include "ui_ctkThumbnailLabel.h"
- // STD includes
- #include <iostream>
- //----------------------------------------------------------------------------
- class ctkThumbnailLabelPrivate: public Ui_ctkThumbnailLabel
- {
- Q_DECLARE_PUBLIC(ctkThumbnailLabel);
- protected:
- ctkThumbnailLabel* const q_ptr;
- public:
- typedef Ui_ctkThumbnailLabel Superclass;
- // Constructor
- ctkThumbnailLabelPrivate(ctkThumbnailLabel* parent);
- virtual ~ctkThumbnailLabelPrivate();
- virtual void setupUi(QWidget* widget);
- Qt::Alignment TextPosition;
- bool SelectedFlag;
- QColor SelectedColor;
- QModelIndex SourceIndex;
- QPixmap OriginalThumbnail;
- Qt::TransformationMode TransformationMode;
- // Redraw thumbnail
- void updateThumbnail();
- };
- //----------------------------------------------------------------------------
- // ctkThumbnailLabelPrivate methods
- //----------------------------------------------------------------------------
- ctkThumbnailLabelPrivate::ctkThumbnailLabelPrivate(ctkThumbnailLabel* parent)
- : q_ptr(parent)
- {
- Q_Q(ctkThumbnailLabel);
- this->SelectedFlag = false;
- this->SelectedColor = q->palette().color(QPalette::Highlight);
- this->TextPosition = Qt::AlignTop | Qt::AlignHCenter;
- this->TransformationMode = Qt::FastTransformation;
- }
- //----------------------------------------------------------------------------
- ctkThumbnailLabelPrivate::~ctkThumbnailLabelPrivate()
- {
- }
- //----------------------------------------------------------------------------
- void ctkThumbnailLabelPrivate::setupUi(QWidget* widget)
- {
- Q_Q(ctkThumbnailLabel);
- this->Superclass::setupUi(widget);
- q->layout()->setSizeConstraint(QLayout::SetNoConstraint);
- // no text by default
- q->setText(QString());
- }
- //----------------------------------------------------------------------------
- void ctkThumbnailLabelPrivate::updateThumbnail()
- {
- this->PixmapLabel->setPixmap(
- this->OriginalThumbnail.isNull() ? QPixmap() :
- this->OriginalThumbnail.scaled(this->PixmapLabel->size(),
- Qt::KeepAspectRatio,
- this->TransformationMode));
- }
- //----------------------------------------------------------------------------
- // ctkThumbnailLabel methods
- //----------------------------------------------------------------------------
- ctkThumbnailLabel::ctkThumbnailLabel(QWidget* parentWidget)
- : Superclass(parentWidget)
- , d_ptr(new ctkThumbnailLabelPrivate(this))
- {
- Q_D(ctkThumbnailLabel);
- d->setupUi(this);
- }
- //----------------------------------------------------------------------------
- ctkThumbnailLabel::~ctkThumbnailLabel()
- {
- }
- //----------------------------------------------------------------------------
- void ctkThumbnailLabel::setText(const QString &text)
- {
- Q_D(ctkThumbnailLabel);
- d->TextLabel->setText(text);
- d->TextLabel->setVisible(!text.isEmpty() &&
- ! (d->TextPosition & Qt::AlignHCenter &&
- d->TextPosition & Qt::AlignVCenter) );
- }
- //----------------------------------------------------------------------------
- QString ctkThumbnailLabel::text()const
- {
- Q_D(const ctkThumbnailLabel);
- return d->TextLabel->text();
- }
- //----------------------------------------------------------------------------
- void ctkThumbnailLabel::setTextPosition(const Qt::Alignment& position)
- {
- Q_D(ctkThumbnailLabel);
- d->TextPosition = position;
- int textIndex = -1;
- for (textIndex = 0; textIndex < this->layout()->count(); ++textIndex)
- {
- if (this->layout()->itemAt(textIndex)->widget() == d->TextLabel)
- {
- break;
- }
- }
- if (textIndex > -1 && textIndex < this->layout()->count())
- {
- this->layout()->takeAt(textIndex);
- }
- int row = 1;
- int col = 1;
- QGridLayout* gridLayout = qobject_cast<QGridLayout*>(this->layout());
- if (position & Qt::AlignTop)
- {
- row = 0;
- }
- else if (position &Qt::AlignBottom)
- {
- row = 2;
- }
- else
- {
- row = 1;
- }
- if (position & Qt::AlignLeft)
- {
- col = 0;
- }
- else if (position & Qt::AlignRight)
- {
- col = 2;
- }
- else
- {
- col = 1;
- }
- if (row == 1 && col == 1)
- {
- d->TextLabel->setVisible(false);
- }
- else
- {
- gridLayout->addWidget(d->TextLabel,row, col);
- }
- }
- //----------------------------------------------------------------------------
- Qt::Alignment ctkThumbnailLabel::textPosition()const
- {
- Q_D(const ctkThumbnailLabel);
- return d->TextPosition;
- }
- //----------------------------------------------------------------------------
- void ctkThumbnailLabel::setPixmap(const QPixmap &pixmap)
- {
- Q_D(ctkThumbnailLabel);
- d->OriginalThumbnail = pixmap;
- d->updateThumbnail();
- }
- //----------------------------------------------------------------------------
- const QPixmap* ctkThumbnailLabel::pixmap()const
- {
- Q_D(const ctkThumbnailLabel);
- return d->OriginalThumbnail.isNull() ? 0 : &(d->OriginalThumbnail);
- }
- //----------------------------------------------------------------------------
- Qt::TransformationMode ctkThumbnailLabel::transformationMode()const
- {
- Q_D(const ctkThumbnailLabel);
- return d->TransformationMode;
- }
- //----------------------------------------------------------------------------
- void ctkThumbnailLabel::setTransformationMode(Qt::TransformationMode mode)
- {
- Q_D(ctkThumbnailLabel);
- d->TransformationMode = mode;
- d->updateThumbnail();
- }
- //----------------------------------------------------------------------------
- void ctkThumbnailLabel::paintEvent(QPaintEvent* event)
- {
- Q_D(ctkThumbnailLabel);
- this->Superclass::paintEvent(event);
- if (d->SelectedFlag && d->SelectedColor.isValid())
- {
- QPainter p(this);
- QPen pen(d->SelectedColor);
- pen.setWidth(7);
- p.setPen(pen);
- p.drawRect(QRect(0,0, this->width() -1, this->height() -1));
- }
- }
- //----------------------------------------------------------------------------
- void ctkThumbnailLabel::setSelected(bool flag)
- {
- Q_D(ctkThumbnailLabel);
- d->SelectedFlag = flag;
- this->update();
- }
- //----------------------------------------------------------------------------
- bool ctkThumbnailLabel::isSelected()const
- {
- Q_D(const ctkThumbnailLabel);
- return d->SelectedFlag;
- }
- //----------------------------------------------------------------------------
- void ctkThumbnailLabel::setSelectedColor(const QColor& color)
- {
- Q_D(ctkThumbnailLabel);
- d->SelectedColor = color;
- this->update();
- }
- //----------------------------------------------------------------------------
- QColor ctkThumbnailLabel::selectedColor()const
- {
- Q_D(const ctkThumbnailLabel);
- return d->SelectedColor;
- }
- //----------------------------------------------------------------------------
- QSize ctkThumbnailLabel::minimumSizeHint()const
- {
- Q_D(const ctkThumbnailLabel);
- if (d->TextLabel->isVisibleTo(const_cast<ctkThumbnailLabel*>(this)) &&
- !d->TextLabel->text().isEmpty())
- {
- return d->TextLabel->minimumSizeHint();
- }
- return QSize();
- }
- //----------------------------------------------------------------------------
- QSize ctkThumbnailLabel::sizeHint()const
- {
- Q_D(const ctkThumbnailLabel);
- return d->OriginalThumbnail.isNull() ?
- this->Superclass::sizeHint() :
- d->OriginalThumbnail.size().expandedTo(QApplication::globalStrut());
- }
- //----------------------------------------------------------------------------
- int ctkThumbnailLabel::heightForWidth(int width)const
- {
- Q_D(const ctkThumbnailLabel);
- if (d->OriginalThumbnail.isNull() ||
- d->OriginalThumbnail.width() == 0)
- {
- return this->Superclass::heightForWidth(width);
- }
- double ratio = static_cast<double>(d->OriginalThumbnail.height()) /
- static_cast<double>(d->OriginalThumbnail.width());
- return static_cast<int>(ratio * width + 0.5);
- }
- //----------------------------------------------------------------------------
- void ctkThumbnailLabel::mousePressEvent(QMouseEvent* event)
- {
- this->Superclass::mousePressEvent(event);
- this->setSelected(true);
- emit selected(*this);
- }
- //----------------------------------------------------------------------------
- void ctkThumbnailLabel::mouseDoubleClickEvent(QMouseEvent *event)
- {
- this->Superclass::mouseDoubleClickEvent(event);
- emit doubleClicked(*this);
- }
- //----------------------------------------------------------------------------
- void ctkThumbnailLabel::resizeEvent(QResizeEvent *event)
- {
- Q_D(ctkThumbnailLabel);
- this->Superclass::resizeEvent(event);
- d->updateThumbnail();
- }
|