File indexing completed on 2024-12-22 04:40:11

0001 /*
0002     SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar>
0003     SPDX-FileCopyrightText: 2010-2022 Mladen Milinkovic <max@smoothware.net>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "linesselectionmodel.h"
0009 #include "core/subtitle.h"
0010 #include "core/subtitleline.h"
0011 #include "gui/treeview/linesmodel.h"
0012 
0013 #include <QDebug>
0014 #include <QSignalBlocker>
0015 
0016 using namespace SubtitleComposer;
0017 
0018 /**
0019  * @brief QItemSelectionModel that doesn't lose selection during model reset
0020  */
0021 LinesSelectionModel::LinesSelectionModel(LinesModel *model)
0022     : QItemSelectionModel(model),
0023       m_resetInProgress(false),
0024       m_currentLine(nullptr)
0025 {
0026 }
0027 
0028 void
0029 LinesSelectionModel::setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
0030 {
0031     Subtitle *sub = static_cast<LinesModel *>(model())->subtitle();
0032     m_currentLine = sub ? sub->line(index.row()) : nullptr;
0033     QItemSelectionModel::setCurrentIndex(index, command);
0034 }
0035 
0036 void
0037 LinesSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
0038 {
0039     select(QItemSelection(index, index), command);
0040 }
0041 
0042 void
0043 LinesSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
0044 {
0045     QItemSelectionModel::select(selection, command);
0046 
0047     if(command == NoUpdate)
0048         return;
0049 
0050     if(!m_resetInProgress && (command & Clear))
0051         m_selection.clear();
0052 
0053     const Subtitle *subtitle = static_cast<LinesModel *>(model())->subtitle();
0054     if(!subtitle)
0055         return;
0056 
0057     QModelIndexList sel = selection.indexes();
0058     while(!sel.empty()) {
0059         const SubtitleLine *line = subtitle->line(sel.takeFirst().row());
0060         if(command & Select) {
0061             m_selection.insert(line);
0062         } else if(command & Deselect) {
0063             m_selection.remove(line);
0064         } else if(command & Toggle) {
0065             if(m_selection.contains(line))
0066                 m_selection.remove(line);
0067             else
0068                 m_selection.insert(line);
0069         }
0070     }
0071 }
0072 
0073 void
0074 LinesSelectionModel::clear()
0075 {
0076     QItemSelectionModel::clear();
0077     if(!m_resetInProgress) {
0078         m_currentLine = nullptr;
0079         m_selection.clear();
0080     }
0081 }
0082 
0083 void
0084 LinesSelectionModel::reset()
0085 {
0086     m_resetInProgress = true;
0087     QItemSelectionModel::reset();
0088     m_resetInProgress = false;
0089 
0090     const LinesModel *model = static_cast<LinesModel *>(this->model());
0091     Subtitle *subtitle = model->subtitle();
0092     if(!subtitle) {
0093         QItemSelectionModel::clear();
0094         return;
0095     }
0096 
0097     if(m_currentLine)
0098         QItemSelectionModel::setCurrentIndex(model->index(m_currentLine->index(), 0), QItemSelectionModel::Current);
0099 
0100     const int lastCol = model->columnCount() - 1;
0101     for(auto it = m_selection.cbegin(); it != m_selection.cend(); ++it) {
0102         const SubtitleLine *line = *it;
0103         if(line && line->subtitle() == subtitle) {
0104             const int i = line->index();
0105             QItemSelectionModel::select(QItemSelection(model->index(i), model->index(i, lastCol)), QItemSelectionModel::Select);
0106         }
0107     }
0108 }