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        : 02-08-2013
0007  * Description : Thumbnail bar for Showfoto
0008  *
0009  * SPDX-FileCopyrightText: 2013      by Mohamed_Anwer <m_dot_anwer at gmx dot com>
0010  * SPDX-FileCopyrightText: 2013-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "showfotothumbnailbar.h"
0017 
0018 // Qt includes
0019 
0020 #include <QTimer>
0021 
0022 // Local includes
0023 
0024 #include "digikam_debug.h"
0025 #include "showfotosettings.h"
0026 #include "showfotodelegate.h"
0027 #include "showfotofiltermodel.h"
0028 #include "itemviewtooltip.h"
0029 #include "showfototooltipfiller.h"
0030 #include "itemselectionoverlay.h"
0031 #include "showfotokineticscroller.h"
0032 #include "showfotocoordinatesoverlay.h"
0033 
0034 namespace ShowFoto
0035 {
0036 
0037 class Q_DECL_HIDDEN ShowfotoThumbnailBar::Private
0038 {
0039 public:
0040 
0041     explicit Private()
0042       : scrollPolicy    (Qt::ScrollBarAlwaysOn),
0043         duplicatesFilter(nullptr),
0044         kScroller       (nullptr)
0045     {
0046     }
0047 
0048     Qt::ScrollBarPolicy              scrollPolicy;
0049     NoDuplicatesShowfotoFilterModel* duplicatesFilter;
0050     ShowfotoKineticScroller*         kScroller;
0051 };
0052 
0053 ShowfotoThumbnailBar::ShowfotoThumbnailBar(QWidget* const parent)
0054     : ShowfotoCategorizedView(parent),
0055       d                      (new Private())
0056 {
0057     setItemDelegate(new ShowfotoThumbnailDelegate(this));
0058     setSpacing(3);
0059     setUsePointingHandCursor(false);
0060     setScrollStepGranularity(3);
0061     setScrollCurrentToCenter(ShowfotoSettings::instance()->getItemCenter());
0062     setScrollBarPolicy(Qt::ScrollBarAlwaysOn);
0063 
0064     setDragEnabled(true);
0065     setAcceptDrops(true);
0066     setDropIndicatorShown(true);
0067 
0068     // Disable QListView::Batched optimization
0069     // for the thumbnail bar, see bug #468593
0070 
0071     setLayoutMode(QListView::SinglePass);
0072 
0073     // NOTE: use dynamic binding as this virtual method can be re-implemented in derived classes.
0074 
0075     this->slotSetupChanged();
0076 
0077     d->kScroller = new ShowfotoKineticScroller();
0078     d->kScroller->enableKineticScrollFor(this);
0079 }
0080 
0081 ShowfotoThumbnailBar::~ShowfotoThumbnailBar()
0082 {
0083     delete d;
0084 }
0085 
0086 int ShowfotoThumbnailBar::thumbnailIndexForUrl(const QUrl& url) const
0087 {
0088     int index = 0;
0089 
0090     for (int i = 0 ; i < showfotoItemInfos().size() ; ++i)
0091     {
0092         QUrl iurl = showfotoItemInfos().at(i).url;
0093 
0094         if (iurl.matches(url, QUrl::None))
0095         {
0096             index = i + 1;
0097         }
0098     }
0099 
0100     qCDebug(DIGIKAM_GENERAL_LOG) << "Thumb index for" << url << ":" << index;
0101 
0102     return index;
0103 }
0104 
0105 void ShowfotoThumbnailBar::installOverlays()
0106 {
0107     addOverlay(new ShowfotoCoordinatesOverlay(this));
0108 }
0109 
0110 void ShowfotoThumbnailBar::slotDockLocationChanged(Qt::DockWidgetArea area)
0111 {
0112     if ((area == Qt::LeftDockWidgetArea) || (area == Qt::RightDockWidgetArea))
0113     {
0114         setFlow(TopToBottom);
0115         d->kScroller->setScrollFlow(TopToBottom);
0116     }
0117     else
0118     {
0119         setFlow(LeftToRight);
0120         d->kScroller->setScrollFlow(LeftToRight);
0121     }
0122 
0123     scrollTo(currentIndex());
0124 }
0125 
0126 void ShowfotoThumbnailBar::setScrollBarPolicy(Qt::ScrollBarPolicy policy)
0127 {
0128     if (policy == Qt::ScrollBarAsNeeded)
0129     {
0130         // Delegate resizing will cause endless relayouting, see bug #228807
0131 
0132         qCDebug(DIGIKAM_GENERAL_LOG) << "The Qt::ScrollBarAsNeeded policy is not supported by ShowfotoThumbnailBar";
0133     }
0134 
0135     d->scrollPolicy = policy;
0136 
0137     if (flow() == TopToBottom)
0138     {
0139         setVerticalScrollBarPolicy(d->scrollPolicy);
0140         setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0141     }
0142     else
0143     {
0144         setHorizontalScrollBarPolicy(d->scrollPolicy);
0145         setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0146     }
0147 }
0148 
0149 void ShowfotoThumbnailBar::setFlow(QListView::Flow flow)
0150 {
0151     setWrapping(false);
0152 
0153     ShowfotoCategorizedView::setFlow(flow);
0154 
0155     ShowfotoThumbnailDelegate* const del = static_cast<ShowfotoThumbnailDelegate*>(delegate());
0156     del->setFlow(flow);
0157 
0158     // Reset the minimum and maximum sizes.
0159 
0160     setMinimumSize(QSize(0, 0));
0161     setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
0162 
0163     // Adjust minimum and maximum width to thumbnail sizes.
0164 
0165     if (flow == TopToBottom)
0166     {
0167         int viewportFullWidgetOffset = size().width() - viewport()->size().width();
0168         setMinimumWidth(del->minimumSize() + viewportFullWidgetOffset);
0169         setMaximumWidth(del->maximumSize() + viewportFullWidgetOffset);
0170     }
0171     else
0172     {
0173         int viewportFullWidgetOffset = size().height() - viewport()->size().height();
0174         setMinimumHeight(del->minimumSize() + viewportFullWidgetOffset);
0175         setMaximumHeight(del->maximumSize() + viewportFullWidgetOffset);
0176     }
0177 
0178     setScrollBarPolicy(d->scrollPolicy);
0179 }
0180 
0181 void ShowfotoThumbnailBar::slotSetupChanged()
0182 {
0183     ShowfotoCategorizedView::slotSetupChanged();
0184 }
0185 
0186 bool ShowfotoThumbnailBar::event(QEvent* e)
0187 {
0188     // reset widget max/min sizes
0189 
0190     if ((e->type() == QEvent::StyleChange) || (e->type() == QEvent::Show))
0191     {
0192         setFlow(flow());
0193     }
0194 
0195     return ShowfotoCategorizedView::event(e);
0196 }
0197 
0198 QModelIndex ShowfotoThumbnailBar::nextIndex(const QModelIndex& index) const
0199 {
0200     return showfotoFilterModel()->index(index.row() + 1, 0);
0201 }
0202 
0203 QModelIndex ShowfotoThumbnailBar::previousIndex(const QModelIndex& index) const
0204 {
0205     return showfotoFilterModel()->index(index.row() - 1, 0);
0206 }
0207 
0208 QModelIndex ShowfotoThumbnailBar::firstIndex() const
0209 {
0210     return showfotoFilterModel()->index(0, 0);
0211 }
0212 
0213 QModelIndex ShowfotoThumbnailBar::lastIndex() const
0214 {
0215     return showfotoFilterModel()->index(showfotoFilterModel()->rowCount() - 1, 0);
0216 }
0217 
0218 ShowfotoItemInfo ShowfotoThumbnailBar::findItemByUrl(const QUrl& url)
0219 {
0220     ShowfotoItemInfoList lst = showfotoItemInfos();
0221 
0222     for (int i = 0 ; i < lst.size() ; ++i)
0223     {
0224         if (lst.at(i).url == url)
0225         {
0226             return lst.at(i);
0227         }
0228     }
0229 
0230     return ShowfotoItemInfo();
0231 }
0232 
0233 } // namespace ShowFoto
0234 
0235 #include "moc_showfotothumbnailbar.cpp"