File indexing completed on 2025-01-05 03:51:10

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2011-02-28
0007  * Description : overlay for extra functionality of the group indicator
0008  *
0009  * SPDX-FileCopyrightText: 2008-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0010  * SPDX-FileCopyrightText: 2008      by Peter Penz <peter dot penz at gmx dot at>
0011  * SPDX-FileCopyrightText: 2009-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "groupindicatoroverlay.h"
0018 
0019 // KDE includes
0020 
0021 #include <klocalizedstring.h>
0022 
0023 // Local includes
0024 
0025 #include "digikam_debug.h"
0026 #include "itemdelegate.h"
0027 #include "itemmodel.h"
0028 #include "itemcategorizedview.h"
0029 #include "itemfiltermodel.h"
0030 
0031 namespace Digikam
0032 {
0033 
0034 GroupIndicatorOverlayWidget::GroupIndicatorOverlayWidget(QWidget* const parent)
0035     : QAbstractButton(parent)
0036 {
0037 }
0038 
0039 void GroupIndicatorOverlayWidget::contextMenuEvent(QContextMenuEvent* event)
0040 {
0041     Q_EMIT contextMenu(event);
0042 }
0043 
0044 void GroupIndicatorOverlayWidget::paintEvent(QPaintEvent*)
0045 {
0046 }
0047 
0048 // --------------------------------------------------------------------------------
0049 
0050 GroupIndicatorOverlay::GroupIndicatorOverlay(QObject* const parent)
0051     : AbstractWidgetDelegateOverlay(parent)
0052 {
0053 }
0054 
0055 GroupIndicatorOverlayWidget* GroupIndicatorOverlay::buttonWidget() const
0056 {
0057     return static_cast<GroupIndicatorOverlayWidget*>(m_widget);
0058 }
0059 
0060 QWidget* GroupIndicatorOverlay::createWidget()
0061 {
0062     QAbstractButton* const button = new GroupIndicatorOverlayWidget(parentWidget());
0063     button->setCursor(Qt::PointingHandCursor);
0064     return button;
0065 }
0066 
0067 void GroupIndicatorOverlay::setActive(bool active)
0068 {
0069     AbstractWidgetDelegateOverlay::setActive(active);
0070 
0071     if (active)
0072     {
0073         connect(buttonWidget(), SIGNAL(clicked()),
0074                 this, SLOT(slotButtonClicked()));
0075 
0076         connect(buttonWidget(), SIGNAL(contextMenu(QContextMenuEvent*)),
0077                 this, SLOT(slotButtonContextMenu(QContextMenuEvent*)));
0078     }
0079     else
0080     {
0081         // widget is deleted
0082     }
0083 }
0084 
0085 void GroupIndicatorOverlay::visualChange()
0086 {
0087     if (m_widget && m_widget->isVisible())
0088     {
0089         updatePosition();
0090     }
0091 }
0092 
0093 void GroupIndicatorOverlay::updatePosition()
0094 {
0095     if (!m_index.isValid())
0096     {
0097         return;
0098     }
0099 
0100     QRect rect       = static_cast<ItemDelegate*>(delegate())->groupIndicatorRect();
0101     QRect visualRect = m_view->visualRect(m_index);
0102     rect.translate(visualRect.topLeft());
0103 
0104     m_widget->setFixedSize(rect.width() + 1, rect.height() + 1);
0105     m_widget->move(rect.topLeft());
0106 }
0107 
0108 bool GroupIndicatorOverlay::checkIndex(const QModelIndex& index) const
0109 {
0110     ItemInfo info = ItemModel::retrieveItemInfo(index);
0111     QRect rect    = static_cast<ItemDelegate*>(delegate())->groupIndicatorRect();
0112 
0113     if (!rect.isNull() && info.hasGroupedImages())
0114     {
0115         QString tip = i18ncp("@info:tooltip",
0116                              "1 grouped item.\n",
0117                              "%1 grouped items.\n",
0118                              info.numberOfGroupedImages());
0119 
0120         if (index.data(ItemFilterModel::GroupIsOpenRole).toBool())
0121         {
0122             tip += i18nc("@info:tooltip", "Group is open.");
0123         }
0124         else
0125         {
0126             tip += i18nc("@info:tooltip", "Group is closed.");
0127         }
0128 
0129         m_widget->setToolTip(tip);
0130 
0131         return true;
0132     }
0133 
0134     return false;
0135 }
0136 
0137 void GroupIndicatorOverlay::slotButtonClicked()
0138 {
0139     Q_EMIT toggleGroupOpen(m_index);
0140 }
0141 
0142 void GroupIndicatorOverlay::slotButtonContextMenu(QContextMenuEvent* event)
0143 {
0144     Q_EMIT showButtonContextMenu(m_index, event);
0145 }
0146 
0147 void GroupIndicatorOverlay::slotEntered(const QModelIndex& index)
0148 {
0149     AbstractWidgetDelegateOverlay::slotEntered(index);
0150     m_index = index;
0151     updatePosition();
0152 }
0153 
0154 } // namespace Digikam
0155 
0156 #include "moc_groupindicatoroverlay.cpp"