File indexing completed on 2025-01-05 03:56:42

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2010-12-26
0007  * Description : images versions tree view overlays
0008  *
0009  * SPDX-FileCopyrightText: 2010 by Martin Klapetek <martin dot klapetek at gmail dot com>
0010  * SPDX-FileCopyrightText: 2010 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "versionsoverlays.h"
0017 
0018 // KDE includes
0019 
0020 #include <klocalizedstring.h>
0021 
0022 // Local includes
0023 
0024 #include "digikam_debug.h"
0025 #include "iteminfo.h"
0026 #include "itemhistorygraphmodel.h"
0027 #include "itemlistmodel.h"
0028 #include "itemviewhoverbutton.h"
0029 #include "tagscache.h"
0030 #include "versionmanagersettings.h"
0031 
0032 namespace Digikam
0033 {
0034 
0035 class Q_DECL_HIDDEN ShowHideVersionsOverlay::Button : public ItemViewHoverButton
0036 {
0037     Q_OBJECT
0038 
0039 public:
0040 
0041     explicit Button(QAbstractItemView* const parentView);
0042     QSize sizeHint() const override;
0043 
0044 protected:
0045 
0046     QIcon icon() override;
0047     void updateToolTip() override;
0048 };
0049 
0050 ShowHideVersionsOverlay::Button::Button(QAbstractItemView* const parentView)
0051     : ItemViewHoverButton(parentView)
0052 {
0053     setup();
0054 }
0055 
0056 QSize ShowHideVersionsOverlay::Button::sizeHint() const
0057 {
0058     return QSize(24, 24);
0059 }
0060 
0061 QIcon ShowHideVersionsOverlay::Button::icon()
0062 {
0063     QString icon = isChecked() ? QLatin1String("edit-bomb")
0064                                : QLatin1String("edit-clear-history");
0065     return QIcon::fromTheme(icon);
0066 }
0067 
0068 void ShowHideVersionsOverlay::Button::updateToolTip()
0069 {
0070     setToolTip(isChecked() ?
0071                i18nc("@info:tooltip", "Hide item permanently") :
0072                i18nc("@info:tooltip", "Show item permanently"));
0073 }
0074 
0075 // ------------------------------------------------------------------------------------
0076 
0077 ShowHideVersionsOverlay::ShowHideVersionsOverlay(QObject* const parent)
0078     : HoverButtonDelegateOverlay(parent)
0079 {
0080 }
0081 
0082 void ShowHideVersionsOverlay::setSettings(const VersionManagerSettings& settings)
0083 {
0084     m_filter = VersionItemFilterSettings(settings);
0085 }
0086 
0087 void ShowHideVersionsOverlay::setActive(bool active)
0088 {
0089     HoverButtonDelegateOverlay::setActive(active);
0090 
0091     if (active)
0092     {
0093         connect(button(), SIGNAL(clicked(bool)),
0094                 this, SLOT(slotClicked(bool)));
0095     }
0096     else
0097     {
0098         // button is deleted
0099     }
0100 }
0101 
0102 ItemViewHoverButton* ShowHideVersionsOverlay::createButton()
0103 {
0104     return new Button(view());
0105 }
0106 
0107 void ShowHideVersionsOverlay::updateButton(const QModelIndex& index)
0108 {
0109     const QRect rect = m_view->visualRect(index);
0110     const QSize size = button()->size();
0111 
0112     const int gap = 5;
0113     const int x   = rect.right()  - gap - size.width();
0114     const int y   = rect.bottom() - gap - size.height();
0115     button()->move(QPoint(x, y));
0116 
0117     ItemInfo info = ItemModel::retrieveItemInfo(index);
0118     button()->setChecked(m_filter.isExemptedBySettings(info));
0119 }
0120 
0121 void ShowHideVersionsOverlay::slotClicked(bool checked)
0122 {
0123     QModelIndex index = button()->index();
0124 
0125     if (index.isValid())
0126     {
0127         ItemInfo info = ItemModel::retrieveItemInfo(index);
0128         int tagId     = TagsCache::instance()->getOrCreateInternalTag(InternalTagName::versionAlwaysVisible());
0129 
0130         if (checked)
0131         {
0132             info.setTag(tagId);
0133         }
0134         else
0135         {
0136             info.removeTag(tagId);
0137         }
0138     }
0139 }
0140 
0141 bool ShowHideVersionsOverlay::checkIndex(const QModelIndex& index) const
0142 {
0143     if (index.data(ItemHistoryGraphModel::IsImageItemRole).toBool())
0144     {
0145         ItemInfo info = ItemModel::retrieveItemInfo(index);
0146         return m_filter.isHiddenBySettings(info);
0147     }
0148 
0149     return false;
0150 }
0151 
0152 // --------------------------------------------------------------------
0153 
0154 class Q_DECL_HIDDEN ActionVersionsOverlay::Button : public ItemViewHoverButton
0155 {
0156     Q_OBJECT
0157 
0158 public:
0159 
0160     Button(QAbstractItemView* const parentView, const QIcon& icon, const QString& text, const QString& tip);
0161     QSize sizeHint() const override;
0162 
0163 protected:
0164 
0165     QIcon icon() override;
0166     void updateToolTip() override;
0167 
0168 protected:
0169 
0170     QIcon   m_icon;
0171     QString m_text;
0172     QString m_tip;
0173 };
0174 
0175 ActionVersionsOverlay::Button::Button(QAbstractItemView* const parentView, const QIcon& icon, const QString& text, const QString& tip)
0176     : ItemViewHoverButton(parentView),
0177       m_icon             (icon),
0178       m_text             (text),
0179       m_tip              (tip)
0180 {
0181     setup();
0182 }
0183 
0184 QSize ActionVersionsOverlay::Button::sizeHint() const
0185 {
0186     return QSize(24, 24);
0187 }
0188 
0189 QIcon ActionVersionsOverlay::Button::icon()
0190 {
0191     return m_icon;
0192 }
0193 
0194 void ActionVersionsOverlay::Button::updateToolTip()
0195 {
0196     setToolTip(m_tip);
0197 }
0198 
0199 // ------------------------------------------------------------------------------------------
0200 
0201 ActionVersionsOverlay::ActionVersionsOverlay(QObject* const parent, const QIcon& icon, const QString& text, const QString& tip)
0202     : HoverButtonDelegateOverlay(parent),
0203       m_icon                    (icon),
0204       m_text                    (text),
0205       m_tip                     (tip),
0206       m_referenceModel          (nullptr)
0207 {
0208 }
0209 
0210 ActionVersionsOverlay::Button* ActionVersionsOverlay::button() const
0211 {
0212     return static_cast<Button*>(HoverButtonDelegateOverlay::button());
0213 }
0214 
0215 void ActionVersionsOverlay::setReferenceModel(const ItemModel* model)
0216 {
0217     m_referenceModel = model;
0218 }
0219 
0220 void ActionVersionsOverlay::setActive(bool active)
0221 {
0222     HoverButtonDelegateOverlay::setActive(active);
0223 
0224     if (active)
0225     {
0226         connect(button(), SIGNAL(clicked(bool)),
0227                 this, SLOT(slotClicked(bool)));
0228     }
0229     else
0230     {
0231         // button is deleted
0232     }
0233 }
0234 
0235 ItemViewHoverButton* ActionVersionsOverlay::createButton()
0236 {
0237     return new Button(view(), m_icon, m_text, m_tip);
0238 }
0239 
0240 void ActionVersionsOverlay::updateButton(const QModelIndex& index)
0241 {
0242     const QRect rect = m_view->visualRect(index);
0243     const QSize size = button()->size();
0244 
0245     const int gap    = 5;
0246     const int x      = rect.right() - gap - size.width();
0247     const int y      = rect.top()   + gap;
0248     button()->move(QPoint(x, y));
0249 }
0250 
0251 void ActionVersionsOverlay::slotClicked(bool checked)
0252 {
0253     Q_UNUSED(checked);
0254     QModelIndex index = button()->index();
0255 
0256     if (index.isValid())
0257     {
0258         Q_EMIT activated(ItemModel::retrieveItemInfo(index));
0259     }
0260 }
0261 
0262 bool ActionVersionsOverlay::checkIndex(const QModelIndex& index) const
0263 {
0264     if (index.data(ItemHistoryGraphModel::IsImageItemRole).toBool())
0265     {
0266         if (m_referenceModel)
0267         {
0268             ItemInfo info = ItemModel::retrieveItemInfo(index);
0269 
0270             // show overlay if image is not contained in reference model
0271 
0272             return (!m_referenceModel->hasImage(info));
0273         }
0274 
0275         return true;
0276     }
0277 
0278     return false;
0279 }
0280 
0281 } // namespace Digikam
0282 
0283 #include "versionsoverlays.moc"
0284 
0285 #include "moc_versionsoverlays.cpp"