File indexing completed on 2025-01-05 03:58:31

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2010-03-22
0007  * Description : A model for the view to display a list of items.
0008  *
0009  * SPDX-FileCopyrightText: 2010-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2010      by Michael G. Hansen <mike at mghansen dot de>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "gpsitemdelegate.h"
0017 
0018 // Qt includes
0019 
0020 
0021 #include <QPainter>
0022 #include <QIcon>
0023 
0024 // Local includes
0025 
0026 #include "digikam_debug.h"
0027 
0028 namespace Digikam
0029 {
0030 
0031 class Q_DECL_HIDDEN GPSItemDelegate::Private
0032 {
0033 public:
0034 
0035     explicit Private()
0036       : imageList    (nullptr),
0037         thumbnailSize(60)
0038     {
0039     }
0040 
0041     GPSItemList* imageList;
0042     int          thumbnailSize;
0043 };
0044 
0045 GPSItemDelegate::GPSItemDelegate(GPSItemList* const imageList, QObject* const parent)
0046     : QItemDelegate(parent),
0047       d            (new Private())
0048 {
0049     d->imageList = imageList;
0050 }
0051 
0052 GPSItemDelegate::~GPSItemDelegate()
0053 {
0054     delete d;
0055 }
0056 
0057 void GPSItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& sortMappedindex) const
0058 {
0059     if (sortMappedindex.column() != GPSItemContainer::ColumnThumbnail)
0060     {
0061         QItemDelegate::paint(painter, option, sortMappedindex);
0062 
0063         return;
0064     }
0065 
0066     const QModelIndex& sourceModelIndex = d->imageList->getSortProxyModel()->mapToSource(sortMappedindex);
0067 
0068     if (option.state & QStyle::State_Selected)
0069     {
0070         painter->fillRect(option.rect, option.palette.highlight());
0071     }
0072 
0073     // TODO: clipping, selected state, disabled state, etc.
0074 
0075     QPixmap itemPixmap = d->imageList->getModel()->getPixmapForIndex(sourceModelIndex, d->thumbnailSize);
0076 
0077     if (itemPixmap.isNull())
0078     {
0079         // TODO: paint some default logo
0080         // TODO: cache this logo
0081 
0082         itemPixmap = QIcon::fromTheme(QLatin1String("view-preview"))
0083                                       .pixmap(d->thumbnailSize, QIcon::Disabled);
0084     }
0085 
0086     const QSize availableSize = option.rect.size();
0087     const QSize pixmapSize    = itemPixmap.size().boundedTo(availableSize);
0088     QPoint startPoint((availableSize.width()  - pixmapSize.width())  / 2,
0089                       (availableSize.height() - pixmapSize.height()) / 2);
0090     startPoint               += option.rect.topLeft();
0091     painter->drawPixmap(QRect(startPoint, pixmapSize), itemPixmap, QRect(QPoint(0, 0), pixmapSize));
0092 }
0093 
0094 QSize GPSItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& sortMappedindex) const
0095 {
0096     if (sortMappedindex.column() == GPSItemContainer::ColumnThumbnail)
0097     {
0098         return QSize(d->thumbnailSize, d->thumbnailSize);
0099     }
0100 
0101     const QSize realSizeHint = QItemDelegate::sizeHint(option, sortMappedindex);
0102 
0103     return QSize(realSizeHint.width(), d->thumbnailSize);
0104 }
0105 
0106 void GPSItemDelegate::setThumbnailSize(const int size)
0107 {
0108     d->thumbnailSize               = size;
0109     GPSItemModel* const imageModel = d->imageList->getModel();
0110 
0111     if (!imageModel)
0112     {
0113         return;
0114     }
0115 
0116     if (imageModel->rowCount() > 0)
0117     {
0118         // TODO: is it enough to Q_EMIT this signal for only 1 item?
0119         // seems to work in Qt4.5 with QTreeView::setUniformRowHeights(true)
0120 
0121         Q_EMIT sizeHintChanged(imageModel->index(0, 0));
0122     }
0123 }
0124 
0125 int GPSItemDelegate::getThumbnailSize() const
0126 {
0127     return d->thumbnailSize;
0128 }
0129 
0130 } // namespace Digikam
0131 
0132 #include "moc_gpsitemdelegate.cpp"