File indexing completed on 2025-01-19 03:57:35

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2013-08-01
0007  * Description : Qt model view for Showfoto item - the delegate
0008  *
0009  * SPDX-FileCopyrightText: 2013 by Mohamed_Anwer <m_dot_anwer at gmx dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "showfotoitemviewdelegate_p.h"
0016 
0017 namespace ShowFoto
0018 {
0019 
0020 ShowfotoItemViewDelegatePrivate::ShowfotoItemViewDelegatePrivate()
0021     : spacing      (0),
0022       thumbSize    (ThumbnailSize(0)),
0023       q            (nullptr),
0024       displayWidget(nullptr),
0025       radius       (3),                ///< painting constants
0026       margin       (5)
0027 {
0028 }
0029 
0030 void ShowfotoItemViewDelegatePrivate::init(ShowfotoItemViewDelegate* const _q, QWidget* const _widget)
0031 {
0032     q             = _q;
0033     displayWidget = _widget;
0034 
0035     q->connect(ThemeManager::instance(), SIGNAL(signalThemeChanged()),
0036                q, SLOT(slotThemeChanged()));
0037 }
0038 
0039 void ShowfotoItemViewDelegatePrivate::clearRects()
0040 {
0041     gridSize   = QSize(0, 0);
0042     rect       = QRect(0, 0, 0, 0);
0043 }
0044 
0045 // ---- ShowfotoItemViewDelegate -----------------------------------------------
0046 
0047 ShowfotoItemViewDelegate::ShowfotoItemViewDelegate(QWidget* const parent)
0048     : DItemDelegate(parent),
0049       d_ptr        (new ShowfotoItemViewDelegatePrivate)
0050 {
0051     d_ptr->init(this, parent);
0052 }
0053 
0054 ShowfotoItemViewDelegate::ShowfotoItemViewDelegate(ShowfotoItemViewDelegatePrivate& dd, QWidget* const parent)
0055     : DItemDelegate(parent),
0056       d_ptr        (&dd)
0057 {
0058     d_ptr->init(this, parent);
0059 }
0060 
0061 ShowfotoItemViewDelegate::~ShowfotoItemViewDelegate()
0062 {
0063     Q_D(ShowfotoItemViewDelegate);
0064 
0065     removeAllOverlays();
0066     delete d;
0067 }
0068 
0069 ThumbnailSize ShowfotoItemViewDelegate::thumbnailSize() const
0070 {
0071     Q_D(const ShowfotoItemViewDelegate);
0072 
0073     return d->thumbSize;
0074 }
0075 
0076 double ShowfotoItemViewDelegate::displayRatio() const
0077 {
0078     Q_D(const ShowfotoItemViewDelegate);
0079 
0080     return d->displayWidget->devicePixelRatio();
0081 }
0082 
0083 void ShowfotoItemViewDelegate::setThumbnailSize(const ThumbnailSize& thumbSize)
0084 {
0085     Q_D(ShowfotoItemViewDelegate);
0086 
0087     if (d->thumbSize != thumbSize)
0088     {
0089         d->thumbSize = thumbSize;
0090         invalidatePaintingCache();
0091     }
0092 }
0093 
0094 void ShowfotoItemViewDelegate::setSpacing(int spacing)
0095 {
0096     Q_D(ShowfotoItemViewDelegate);
0097 
0098     if (d->spacing == spacing)
0099     {
0100         return;
0101     }
0102 
0103     d->spacing = spacing;
0104     invalidatePaintingCache();
0105 }
0106 
0107 int ShowfotoItemViewDelegate::spacing() const
0108 {
0109     Q_D(const ShowfotoItemViewDelegate);
0110 
0111     return d->spacing;
0112 }
0113 
0114 QRect ShowfotoItemViewDelegate::rect() const
0115 {
0116     Q_D(const ShowfotoItemViewDelegate);
0117 
0118     return d->rect;
0119 }
0120 
0121 QRect ShowfotoItemViewDelegate::pixmapRect() const
0122 {
0123     return QRect();
0124 }
0125 
0126 QRect ShowfotoItemViewDelegate::imageInformationRect() const
0127 {
0128     return QRect();
0129 }
0130 
0131 QSize ShowfotoItemViewDelegate::sizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/) const
0132 {
0133     Q_D(const ShowfotoItemViewDelegate);
0134 
0135     return d->rect.size();
0136 }
0137 
0138 QSize ShowfotoItemViewDelegate::gridSize() const
0139 {
0140     Q_D(const ShowfotoItemViewDelegate);
0141 
0142     return d->gridSize;
0143 }
0144 
0145 bool ShowfotoItemViewDelegate::acceptsToolTip(const QPoint&, const QRect& visualRect, const QModelIndex&, QRect* retRect) const
0146 {
0147     if (retRect)
0148     {
0149         *retRect = visualRect;
0150     }
0151 
0152     return true;
0153 }
0154 
0155 bool ShowfotoItemViewDelegate::acceptsActivation(const QPoint& , const QRect& visualRect, const QModelIndex&, QRect* retRect) const
0156 {
0157     if (retRect)
0158     {
0159         *retRect = visualRect;
0160     }
0161 
0162     return true;
0163 }
0164 
0165 QAbstractItemDelegate* ShowfotoItemViewDelegate::asDelegate()
0166 {
0167     return this;
0168 }
0169 
0170 void ShowfotoItemViewDelegate::overlayDestroyed(QObject* o)
0171 {
0172     ItemDelegateOverlayContainer::overlayDestroyed(o);
0173 }
0174 
0175 void ShowfotoItemViewDelegate::mouseMoved(QMouseEvent* e, const QRect& visualRect, const QModelIndex& index)
0176 {
0177     // 3-way indirection AbstractShowfotoItemDelegate -> ShowfotoItemViewDelegate -> ItemDelegateOverlayContainer
0178 
0179     ItemDelegateOverlayContainer::mouseMoved(e, visualRect, index);
0180 }
0181 
0182 void ShowfotoItemViewDelegate::setDefaultViewOptions(const QStyleOptionViewItem& option)
0183 {
0184     Q_D(ShowfotoItemViewDelegate);
0185 
0186     d->font = option.font;
0187     invalidatePaintingCache();
0188 }
0189 
0190 void ShowfotoItemViewDelegate::slotThemeChanged()
0191 {
0192     invalidatePaintingCache();
0193 }
0194 
0195 void ShowfotoItemViewDelegate::slotSetupChanged()
0196 {
0197     invalidatePaintingCache();
0198 }
0199 
0200 void ShowfotoItemViewDelegate::invalidatePaintingCache()
0201 {
0202     Q_D(ShowfotoItemViewDelegate);
0203 
0204     QSize oldGridSize = d->gridSize;
0205     updateSizeRectsAndPixmaps();
0206 
0207     if (oldGridSize != d->gridSize)
0208     {
0209         Q_EMIT gridSizeChanged(d->gridSize);
0210 /*
0211         // Q_EMIT sizeHintChanged(QModelIndex());
0212 */
0213     }
0214 
0215     Q_EMIT visualChange();
0216 }
0217 
0218 QRect ShowfotoItemViewDelegate::drawThumbnail(QPainter* p, const QRect& thumbRect, const QPixmap& background,
0219                                               const QPixmap& thumbnail) const
0220 {
0221     p->drawPixmap(0, 0, background);
0222 
0223     if (thumbnail.isNull())
0224     {
0225         return QRect();
0226     }
0227 
0228     QRect r    = thumbRect;
0229     double dpr = displayRatio();
0230     int thumbW = qRound((double)thumbnail.width()  / dpr);
0231     int thumbH = qRound((double)thumbnail.height() / dpr);
0232 
0233     QRect actualPixmapRect(r.x() + (r.width()  - thumbW) / 2,
0234                            r.y() + (r.height() - thumbH) / 2,
0235                            thumbW, thumbH);
0236 
0237     QPixmap borderPix = thumbnailBorderPixmap(actualPixmapRect.size());
0238 
0239     p->drawPixmap(actualPixmapRect.x() - 3,
0240                   actualPixmapRect.y() - 3, borderPix);
0241 
0242     p->drawPixmap(r.x() + (r.width()  - thumbW) / 2,
0243                   r.y() + (r.height() - thumbH) / 2,
0244                   thumbW, thumbH, thumbnail);
0245 
0246     return actualPixmapRect;
0247 }
0248 
0249 void ShowfotoItemViewDelegate::drawName(QPainter* p,const QRect& nameRect, const QString& name) const
0250 {
0251     Q_D(const ShowfotoItemViewDelegate);
0252 
0253     p->setFont(d->fontReg);
0254     p->drawText(nameRect, Qt::AlignCenter, name);   //squeezedTextCached(p, nameRect.width(), name));
0255 }
0256 
0257 void ShowfotoItemViewDelegate::drawCreationDate(QPainter* p, const QRect& dateRect, const QDateTime& date) const
0258 {
0259     Q_D(const ShowfotoItemViewDelegate);
0260 
0261     p->setFont(d->fontXtra);
0262     QString str = dateToString(date);
0263     str         = i18nc("date of image creation", "created: %1", str);
0264     p->drawText(dateRect, Qt::AlignCenter, str);    //squeezedTextCached(p, dateRect.width(), str));
0265 }
0266 
0267 void ShowfotoItemViewDelegate::drawImageFormat(QPainter* p, const QRect& r, const QString& mime) const
0268 {
0269     Q_D(const ShowfotoItemViewDelegate);
0270 
0271     if (!mime.isEmpty() && !r.isNull())
0272     {
0273         QString type = mime;
0274 
0275         p->save();
0276 
0277         QFont fnt(d->fontReg);
0278         fnt.setWeight(QFont::Black);
0279         fnt.setItalic(false);
0280         p->setFont(fnt);
0281         p->setPen(QPen(Qt::gray));
0282         p->setOpacity(0.50);
0283 
0284         QRect bRect = p->boundingRect(r, Qt::AlignBottom | Qt::AlignHCenter, type.toUpper());
0285         bRect.adjust(1, 1, -1, -1);
0286         bRect.translate(0, 1);
0287 
0288         p->fillRect(bRect, Qt::SolidPattern);
0289         p->setPen(QPen(Qt::white));
0290         p->setOpacity(1.0);
0291         p->drawText(bRect, Qt::AlignBottom | Qt::AlignHCenter, type.toUpper());
0292 
0293         p->restore();
0294     }
0295 }
0296 
0297 void ShowfotoItemViewDelegate::drawGeolocationIndicator(QPainter* p, const QRect& r) const
0298 {
0299     if (!r.isNull())
0300     {
0301         QIcon icon(QIcon::fromTheme(QLatin1String("globe")).pixmap(r.size()));
0302         QBrush brush = p->brush();
0303         p->setOpacity(0.50);
0304         p->setPen(QPen(p->background().color()));
0305         p->setBrush(QBrush(p->background().color()));
0306         p->drawEllipse(r);
0307         p->setBrush(brush);
0308         p->setOpacity(1.0);
0309         icon.paint(p, r);
0310     }
0311 }
0312 
0313 void ShowfotoItemViewDelegate::drawImageSize(QPainter* p, const QRect& dimsRect, const QSize& dims) const
0314 {
0315     Q_D(const ShowfotoItemViewDelegate);
0316 
0317     if (dims.isValid())
0318     {
0319         p->setFont(d->fontXtra);
0320         QString mpixels, resolution;
0321         mpixels = QLocale().toString(dims.width()*dims.height()/1000000.0, 'f', 1);
0322 
0323         if (dims.isValid())
0324         {
0325             resolution = i18nc("%1 width, %2 height, %3 mpixels", "%1x%2 (%3Mpx)",
0326                                dims.width(), dims.height(), mpixels);
0327         }
0328         else
0329         {
0330             resolution = i18nc("unknown image resolution", "Unknown");
0331         }
0332 
0333         p->drawText(dimsRect, Qt::AlignCenter, resolution);
0334     }
0335 }
0336 
0337 void ShowfotoItemViewDelegate::drawFileSize(QPainter* p, const QRect& r, qlonglong bytes) const
0338 {
0339     Q_D(const ShowfotoItemViewDelegate);
0340 
0341     p->setFont(d->fontXtra);
0342     p->drawText(r, Qt::AlignCenter, ItemPropertiesTab::humanReadableBytesCount(bytes));
0343 }
0344 
0345 void ShowfotoItemViewDelegate::drawFocusRect(QPainter* p, const QStyleOptionViewItem& option,
0346                                              bool isSelected) const
0347 {
0348     Q_D(const ShowfotoItemViewDelegate);
0349 
0350     if (option.state & QStyle::State_HasFocus) //?? is current item
0351     {
0352         p->setPen(QPen(isSelected ? qApp->palette().color(QPalette::HighlightedText)
0353                                   : qApp->palette().color(QPalette::Text),
0354                        1, Qt::DotLine));
0355         p->drawRect(1, 1, d->rect.width()-3, d->rect.height()-3);
0356     }
0357 }
0358 
0359 void ShowfotoItemViewDelegate::drawMouseOverRect(QPainter* p, const QStyleOptionViewItem& option) const
0360 {
0361     Q_D(const ShowfotoItemViewDelegate);
0362 
0363     if (option.state & QStyle::State_MouseOver)
0364     {
0365         p->setPen(QPen(option.palette.color(QPalette::Highlight), 3, Qt::SolidLine));
0366         p->drawRect(1, 1, d->rect.width()-3, d->rect.height()-3);
0367     }
0368 }
0369 
0370 void ShowfotoItemViewDelegate::prepareFonts()
0371 {
0372     Q_D(ShowfotoItemViewDelegate);
0373 
0374     d->fontReg  = d->font;
0375     d->fontCom  = d->font;
0376     d->fontXtra = d->font;
0377     d->fontCom.setItalic(true);
0378 
0379     int fnSz    = d->fontReg.pointSize();
0380 
0381     if (fnSz > 0)
0382     {
0383         d->fontCom.setPointSize(fnSz-1);
0384         d->fontXtra.setPointSize(fnSz-2);
0385     }
0386     else
0387     {
0388         fnSz = d->fontReg.pixelSize();
0389         d->fontCom.setPixelSize(fnSz-1);
0390         d->fontXtra.setPixelSize(fnSz-2);
0391     }
0392 }
0393 
0394 void ShowfotoItemViewDelegate::prepareMetrics(int maxWidth)
0395 {
0396     Q_D(ShowfotoItemViewDelegate);
0397 
0398     QFontMetrics fm(d->fontReg);
0399     d->oneRowRegRect = fm.boundingRect(0, 0, maxWidth, 0xFFFFFFFF,
0400                                        Qt::AlignTop | Qt::AlignHCenter,
0401                                        QLatin1String("XXXXXXXXX"));
0402     fm = QFontMetrics(d->fontCom);
0403     d->oneRowComRect = fm.boundingRect(0, 0, maxWidth, 0xFFFFFFFF,
0404                                        Qt::AlignTop | Qt::AlignHCenter,
0405                                        QLatin1String("XXXXXXXXX"));
0406     fm = QFontMetrics(d->fontXtra);
0407     d->oneRowXtraRect = fm.boundingRect(0, 0, maxWidth, 0xFFFFFFFF,
0408                                         Qt::AlignTop | Qt::AlignHCenter,
0409                                         QLatin1String("XXXXXXXXX"));
0410 }
0411 
0412 void ShowfotoItemViewDelegate::prepareBackground()
0413 {
0414     Q_D(ShowfotoItemViewDelegate);
0415 
0416     if (!d->rect.isValid())
0417     {
0418         d->regPixmap = QPixmap();
0419         d->selPixmap = QPixmap();
0420     }
0421     else
0422     {
0423         d->regPixmap = QPixmap(d->rect.width(), d->rect.height());
0424         d->regPixmap.fill(qApp->palette().color(QPalette::Base));
0425         QPainter p1(&d->regPixmap);
0426         p1.setPen(qApp->palette().color(QPalette::Midlight));
0427         p1.drawRect(0, 0, d->rect.width()-1, d->rect.height()-1);
0428 
0429         d->selPixmap = QPixmap(d->rect.width(), d->rect.height());
0430         d->selPixmap.fill(qApp->palette().color(QPalette::Highlight));
0431         QPainter p2(&d->selPixmap);
0432         p2.setPen(qApp->palette().color(QPalette::Midlight));
0433         p2.drawRect(0, 0, d->rect.width()-1, d->rect.height()-1);
0434     }
0435 }
0436 
0437 } // namespace ShowFoto
0438 
0439 #include "moc_showfotoitemviewdelegate.cpp"