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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-04-30
0007  * Description : selection icon view item at mouse hover
0008  *
0009  * SPDX-FileCopyrightText: 2008      by Peter Penz <peter dot penz at gmx dot at>
0010  * SPDX-FileCopyrightText: 2009-2011 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 "itemselectionoverlay.h"
0017 
0018 // KDE includes
0019 
0020 #include <klocalizedstring.h>
0021 
0022 // Local includes
0023 
0024 #include "itemcategorizedview.h"
0025 
0026 namespace Digikam
0027 {
0028 
0029 ItemSelectionOverlayButton::ItemSelectionOverlayButton(QAbstractItemView* const parentView)
0030     : ItemViewHoverButton(parentView)
0031 {
0032     setup();
0033 }
0034 
0035 QSize ItemSelectionOverlayButton::sizeHint() const
0036 {
0037     return QSize(32, 32);
0038 }
0039 
0040 QIcon ItemSelectionOverlayButton::icon()
0041 {
0042     return QIcon::fromTheme(isChecked() ? QLatin1String("list-remove")
0043                                         : QLatin1String("list-add"));
0044 }
0045 
0046 void ItemSelectionOverlayButton::updateToolTip()
0047 {
0048     setToolTip(isChecked() ? i18nc("@info:tooltip", "Deselect Item")
0049                            : i18nc("@info:tooltip", "Select Item"));
0050 }
0051 
0052 // --------------------------------------------------------------------
0053 
0054 ItemSelectionOverlay::ItemSelectionOverlay(QObject* const parent)
0055     : HoverButtonDelegateOverlay(parent)
0056 {
0057 }
0058 
0059 void ItemSelectionOverlay::setActive(bool active)
0060 {
0061     HoverButtonDelegateOverlay::setActive(active);
0062 
0063     if (active)
0064     {
0065         connect(button(), SIGNAL(clicked(bool)),
0066                 this, SLOT(slotClicked(bool)));
0067 
0068         connect(m_view->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
0069                 this, SLOT(slotSelectionChanged(QItemSelection,QItemSelection)));
0070     }
0071     else
0072     {
0073         // button is deleted
0074 
0075         if (m_view)
0076         {
0077             disconnect(m_view->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
0078                        this, SLOT(slotSelectionChanged(QItemSelection,QItemSelection)));
0079         }
0080     }
0081 }
0082 
0083 ItemViewHoverButton* ItemSelectionOverlay::createButton()
0084 {
0085     return new ItemSelectionOverlayButton(view());
0086 }
0087 
0088 void ItemSelectionOverlay::updateButton(const QModelIndex& index)
0089 {
0090     const QRect rect = m_view->visualRect(index);
0091     const int size   = qBound(16, rect.width() / 8 - 2, 48);
0092     const int gap    = 5;
0093     const int x      = rect.left() + gap;
0094     const int y      = rect.top() + gap;
0095     button()->resize(size, size);
0096     button()->move(QPoint(x, y));
0097 
0098     QItemSelectionModel* const selModel = m_view->selectionModel();
0099     button()->setChecked(selModel->isSelected(index));
0100 }
0101 
0102 void ItemSelectionOverlay::slotClicked(bool checked)
0103 {
0104     QModelIndex index = button()->index();
0105 
0106     if (index.isValid())
0107     {
0108         QItemSelectionModel* const selModel = m_view->selectionModel();
0109 
0110         if (checked)
0111         {
0112             selModel->select(index, QItemSelectionModel::Select);
0113         }
0114         else
0115         {
0116             selModel->select(index, QItemSelectionModel::Deselect);
0117         }
0118 
0119         selModel->setCurrentIndex(index, QItemSelectionModel::Current);
0120     }
0121 }
0122 
0123 void ItemSelectionOverlay::slotSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
0124 {
0125     QModelIndex index = button()->index();
0126 
0127     if (index.isValid())
0128     {
0129         if (selected.contains(index))
0130         {
0131             button()->setChecked(true);
0132         }
0133         else if (deselected.contains(index))
0134         {
0135             button()->setChecked(false);
0136         }
0137     }
0138 }
0139 
0140 } // namespace Digikam
0141 
0142 #include "moc_itemselectionoverlay.cpp"