File indexing completed on 2024-05-19 04:56:00

0001 /**
0002  * \file checkablestringlistmodel.cpp
0003  * String list model with checkable items.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 29 Dec 2012
0008  *
0009  * Copyright (C) 2012-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "checkablestringlistmodel.h"
0028 
0029 /**
0030  * Constructor.
0031  * @param parent parent widget
0032  */
0033 CheckableStringListModel::CheckableStringListModel(QObject* parent)
0034   : QStringListModel(parent), m_bitMask(0)
0035 {
0036 }
0037 
0038 /**
0039  * Get item flags for index.
0040  * @param index model index
0041  * @return item flags
0042  */
0043 Qt::ItemFlags CheckableStringListModel::flags(const QModelIndex& index) const
0044 {
0045   Qt::ItemFlags theFlags = QStringListModel::flags(index);
0046   if (index.isValid()) {
0047     theFlags &= ~(Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
0048     theFlags |= Qt::ItemIsUserCheckable;
0049   }
0050   return theFlags;
0051 }
0052 
0053 /**
0054  * Get data for a given role.
0055  * @param index model index
0056  * @param role item data role
0057  * @return data for role
0058  */
0059 QVariant CheckableStringListModel::data(const QModelIndex& index, int role) const
0060 {
0061   if (role == Qt::CheckStateRole &&
0062       index.isValid() && index.column() == 0 &&
0063       index.row() >= 0 && index.row() < 64) {
0064     return (m_bitMask & (1ULL << index.row())) ? Qt::Checked : Qt::Unchecked;
0065   }
0066   return QStringListModel::data(index, role);
0067 }
0068 
0069 /**
0070  * Set data for a given role.
0071  * @param index model index
0072  * @param value data value
0073  * @param role item data role
0074  * @return true if successful
0075  */
0076 bool CheckableStringListModel::setData(const QModelIndex& index,
0077                                        const QVariant& value, int role)
0078 {
0079   if (role == Qt::CheckStateRole &&
0080       index.isValid() && index.column() == 0 &&
0081       index.row() >= 0 && index.row() < 64) {
0082     quint64 mask = 1ULL << index.row();
0083     if (value == Qt::Checked) {
0084       m_bitMask |= mask;
0085     } else if (value == Qt::Unchecked) {
0086       m_bitMask &= ~mask;
0087     }
0088     return true;
0089   }
0090   return QStringListModel::setData(index, value, role);
0091 }
0092 
0093 /**
0094  * Insert rows.
0095  * @param row first row
0096  * @param count number of rows to insert
0097  * @param parent parent model index
0098  * @return true if rows were successfully inserted.
0099  */
0100 bool CheckableStringListModel::insertRows(int row, int count,
0101                                           const QModelIndex& parent)
0102 {
0103   quint64 mask = (1ULL << row) - 1;
0104   m_bitMask = (m_bitMask & mask) | ((m_bitMask & ~mask) << count);
0105   return QStringListModel::insertRows(row, count, parent);
0106 }
0107 
0108 /**
0109  * removeRows
0110  * @param row first row
0111  * @param count number of rows to remove
0112  * @param parent parent model index
0113  * @return true if rows were successfully removed.
0114  */
0115 bool CheckableStringListModel::removeRows(int row, int count,
0116                                           const QModelIndex& parent)
0117 {
0118   m_bitMask = (m_bitMask & ((1ULL << row) - 1)) |
0119       ((m_bitMask & ~((1ULL << (row + count)) - 1)) >> count);
0120   return QStringListModel::removeRows(row, count, parent);
0121 }