File indexing completed on 2024-04-28 04:21:22

0001 /* SPDX-FileCopyrightText: 2003-2011 Jesper K. Pedersen <blackie@kde.org>
0002 
0003    SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 #include "SelectionMaintainer.h"
0006 
0007 #include <kpabase/FileNameList.h>
0008 
0009 ThumbnailView::SelectionMaintainer::SelectionMaintainer(ThumbnailWidget *widget, ThumbnailModel *model)
0010     : m_widget(widget)
0011     , m_model(model)
0012     , m_enabled(true)
0013 {
0014     m_currentItem = widget->currentItem();
0015     m_currentRow = widget->currentIndex().row();
0016     m_selectedItems = widget->selection(NoExpandCollapsedStacks);
0017     if (m_selectedItems.isEmpty())
0018         m_firstRow = -1;
0019     else
0020         m_firstRow = m_model->indexOf(m_selectedItems.at(0));
0021 }
0022 
0023 ThumbnailView::SelectionMaintainer::~SelectionMaintainer()
0024 {
0025     if (!m_enabled)
0026         return;
0027 
0028     // We need to set the current item before we set the selection
0029     m_widget->setCurrentItem(m_currentItem);
0030 
0031     // If the previous current item was deleted, then set the last item of the selection current
0032     // This, however, need to be an actualt item, some of the previous selected items might have been deleted.
0033     if (m_widget->currentItem().isNull()) {
0034         for (int i = m_selectedItems.size() - 1; i >= 0; --i) {
0035             m_widget->setCurrentItem(m_selectedItems.at(i));
0036             if (!m_widget->currentItem().isNull())
0037                 break;
0038         }
0039     }
0040 
0041     // Now set the selection
0042     m_widget->select(m_selectedItems);
0043 
0044     // If no item is current at this point, it means that all the items of the selection
0045     // had been deleted, so make the item just before the previous selection start the current.
0046     if (m_widget->currentItem().isNull())
0047         m_widget->setCurrentIndex(m_model->index(m_firstRow));
0048 }
0049 
0050 void ThumbnailView::SelectionMaintainer::disable()
0051 {
0052     m_enabled = false;
0053 }
0054 // vi:expandtab:tabstop=4 shiftwidth=4: