File indexing completed on 2024-05-05 04:39:18

0001 /*
0002     SPDX-FileCopyrightText: 2020 Friedrich W. H. Kossebau <kossebau@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "checksetselectionlistmodel.h"
0008 
0009 // plugin
0010 #include "checksetselectionmanager.h"
0011 // KF
0012 #include <KLocalizedString>
0013 // Qt
0014 #include <QUuid>
0015 // Std
0016 #include <algorithm>
0017 
0018 
0019 namespace Clazy {
0020 
0021 CheckSetSelectionListModel::CheckSetSelectionListModel(CheckSetSelectionManager* checkSetSelectionManager,
0022                                                        QObject* parent)
0023     : QAbstractItemModel(parent)
0024     , m_checkSetSelectionManager(checkSetSelectionManager)
0025     , m_checkSetSelections(checkSetSelectionManager->checkSetSelections())
0026     , m_defaultCheckSetSelectionId(checkSetSelectionManager->defaultCheckSetSelectionId())
0027 {
0028 }
0029 
0030 CheckSetSelectionListModel::~CheckSetSelectionListModel() = default;
0031 
0032 int CheckSetSelectionListModel::rowCount(const QModelIndex& parent) const
0033 {
0034     if (parent.isValid()) {
0035         return 0;
0036     }
0037 
0038     return m_checkSetSelections.count();
0039 }
0040 
0041 int CheckSetSelectionListModel::columnCount(const QModelIndex& parent) const
0042 {
0043     if (parent.isValid()) {
0044         return 0;
0045     }
0046 
0047     return 1;
0048 }
0049 
0050 QVariant CheckSetSelectionListModel::data(const QModelIndex& index, int role) const
0051 {
0052     if (!index.isValid() ||
0053         index.row() < 0 || index.row() >= rowCount() ||
0054         index.column() != 0) {
0055         return {};
0056     }
0057 
0058     if (role == Qt::DisplayRole) {
0059         const int checkSetSelectionIndex = index.row();
0060 
0061         const auto& checkSetSelection = m_checkSetSelections.at(checkSetSelectionIndex);
0062         auto checkSetSelectionName = checkSetSelection.name();
0063         if (m_defaultCheckSetSelectionId == checkSetSelection.id()) {
0064             checkSetSelectionName = i18nc("@item:inlistbox", "%1 (default selection)", checkSetSelectionName);
0065         }
0066         return checkSetSelectionName;
0067     }
0068 
0069     return {};
0070 }
0071 
0072 QModelIndex CheckSetSelectionListModel::index(int row, int column, const QModelIndex& parent) const
0073 {
0074     if (parent.isValid()) {
0075         return {};
0076     }
0077     return createIndex(row, column);
0078 }
0079 
0080 QModelIndex CheckSetSelectionListModel::parent(const QModelIndex& index) const
0081 {
0082     Q_UNUSED(index);
0083     return {};
0084 }
0085 
0086 void CheckSetSelectionListModel::reload()
0087 {
0088     beginResetModel();
0089 
0090     m_checkSetSelections = m_checkSetSelectionManager->checkSetSelections();
0091     m_defaultCheckSetSelectionId = m_checkSetSelectionManager->defaultCheckSetSelectionId();
0092 
0093     m_added.clear();
0094     m_edited.clear();
0095     m_removed.clear();
0096     m_defaultChanged = false;
0097 
0098     endResetModel();
0099 
0100     emit defaultCheckSetSelectionChanged(m_defaultCheckSetSelectionId);
0101 }
0102 
0103 void CheckSetSelectionListModel::store() const
0104 {
0105     if (!m_edited.isEmpty()) {
0106         QVector<CheckSetSelection> m_checkSetSelectionsToSave;
0107         for (const auto& selection : qAsConst(m_checkSetSelections)) {
0108             const auto id = selection.id();
0109             if (m_edited.contains(id)) {
0110                 m_checkSetSelectionsToSave.append(selection);
0111             }
0112         }
0113         m_checkSetSelectionManager->saveCheckSetSelections(m_checkSetSelectionsToSave);
0114     }
0115     m_checkSetSelectionManager->setDefaultCheckSetSelection(m_defaultCheckSetSelectionId);
0116     m_checkSetSelectionManager->removeCheckSetSelections(m_removed);
0117 
0118     m_added.clear();
0119     m_edited.clear();
0120     m_removed.clear();
0121     m_defaultChanged = false;
0122 }
0123 
0124 
0125 QString CheckSetSelectionListModel::checkSetSelectionId(const QModelIndex& index) const
0126 {
0127     const int checkSetSelectionIndex = index.row();
0128     const bool isValidIndex =
0129         (0 <= checkSetSelectionIndex) && (checkSetSelectionIndex < m_checkSetSelections.count());
0130 
0131     return isValidIndex ? m_checkSetSelections.at(checkSetSelectionIndex).id() : QString();
0132 }
0133 
0134 QString CheckSetSelectionListModel::checkSetSelectionId(int row) const
0135 {
0136     const bool isValidIndex =
0137         (0 <= row) && (row < m_checkSetSelections.count());
0138 
0139     return isValidIndex ? m_checkSetSelections.at(row).id() : QString();
0140 }
0141 
0142 QString CheckSetSelectionListModel::checkSetSelectionName(int row) const
0143 {
0144     const bool isValidIndex =
0145         (0 <= row) && (row < m_checkSetSelections.count());
0146 
0147     return isValidIndex ? m_checkSetSelections.at(row).name() : QString();
0148 }
0149 
0150 QString CheckSetSelectionListModel::checkSetSelectionAsString(int row) const
0151 {
0152     const bool isValidIndex =
0153         (0 <= row) && (row < m_checkSetSelections.count());
0154 
0155     return isValidIndex ? m_checkSetSelections.at(row).selectionAsString() : QString();
0156 }
0157 
0158 int CheckSetSelectionListModel::row(const QString& checkSetSelectionId) const
0159 {
0160     int result = -1;
0161 
0162     const int checkSetSelectionsCount = m_checkSetSelections.count();
0163     for (int i = 0; i < checkSetSelectionsCount; ++i) {
0164         if (checkSetSelectionId == m_checkSetSelections.at(i).id()) {
0165             result = i;
0166             break;
0167         }
0168     }
0169 
0170     return result;
0171 }
0172 
0173 int CheckSetSelectionListModel::defaultCheckSetSelectionRow() const
0174 {
0175     return row(m_defaultCheckSetSelectionId);
0176 }
0177 
0178 bool CheckSetSelectionListModel::hasCheckSetSelection(const QString& name) const
0179 {
0180     return std::any_of(m_checkSetSelections.begin(), m_checkSetSelections.end(),
0181                        [name](const CheckSetSelection& checkSetSelection) {
0182         return checkSetSelection.name() == name;
0183     });
0184 }
0185 
0186 
0187 int CheckSetSelectionListModel::addCheckSetSelection(const QString& name)
0188 {
0189     const int newRow = m_checkSetSelections.count();
0190     beginInsertRows(QModelIndex(), newRow, newRow);
0191 
0192     CheckSetSelection checkSetSelection;
0193     const QString id = QUuid::createUuid().toString();
0194     checkSetSelection.setId(id);
0195     checkSetSelection.setName(name);
0196 
0197     const bool isNewDefault = m_checkSetSelections.isEmpty();
0198     m_checkSetSelections.append(checkSetSelection);
0199 
0200     m_added.append(id);
0201     m_edited.insert(id);
0202     if (isNewDefault) {
0203         m_defaultChanged = true;
0204         m_defaultCheckSetSelectionId = id;
0205     }
0206 
0207     endInsertRows();
0208     if (isNewDefault) {
0209         emit defaultCheckSetSelectionChanged(m_defaultCheckSetSelectionId);
0210     }
0211 
0212     return newRow;
0213 }
0214 
0215 int CheckSetSelectionListModel::cloneCheckSetSelection(const QString& name, int row)
0216 {
0217     const int newRow = m_checkSetSelections.count();
0218     beginInsertRows(QModelIndex(), newRow, newRow);
0219 
0220     const CheckSetSelection& original = m_checkSetSelections.at(row);
0221     CheckSetSelection checkSetSelection(original);
0222     const QString id = QUuid::createUuid().toString();
0223     checkSetSelection.setId(id);
0224     checkSetSelection.setName(name);
0225 
0226     m_checkSetSelections.append(checkSetSelection);
0227 
0228     m_added.append(id);
0229     m_edited.insert(id);
0230 
0231     endInsertRows();
0232 
0233     return newRow;
0234 }
0235 
0236 void CheckSetSelectionListModel::removeCheckSetSelection(int row)
0237 {
0238     if ((row < 0) || m_checkSetSelections.count() <= row) {
0239         return;
0240     }
0241 
0242     beginRemoveRows(QModelIndex(), row, row);
0243 
0244     const QString id = m_checkSetSelections.at(row).id();
0245 
0246     m_checkSetSelections.removeAt(row);
0247 
0248     m_edited.remove(id);
0249     if (!m_added.removeOne(id)) {
0250         m_removed.append(id);
0251     }
0252 
0253     endRemoveRows();
0254 
0255     // set new default if this was the default before
0256     if (id == m_defaultCheckSetSelectionId) {
0257         if (m_checkSetSelections.isEmpty()) {
0258             m_defaultChanged = true;
0259             m_defaultCheckSetSelectionId.clear();
0260             emit defaultCheckSetSelectionChanged(m_defaultCheckSetSelectionId);
0261         } else {
0262             setDefaultCheckSetSelection(0);
0263         }
0264     }
0265 }
0266 
0267 void CheckSetSelectionListModel::setDefaultCheckSetSelection(int row)
0268 {
0269     if ((row < 0) || m_checkSetSelections.count() <= row) {
0270         return;
0271     }
0272 
0273     const QString newDefaultId = m_checkSetSelections.at(row).id();
0274     if (newDefaultId == m_defaultCheckSetSelectionId) {
0275         return;
0276     }
0277 
0278     m_defaultCheckSetSelectionId = newDefaultId;
0279     m_defaultChanged = true;
0280 
0281     const QModelIndex modelIndex = index(row, 0);
0282     emit dataChanged(modelIndex, modelIndex);
0283     emit defaultCheckSetSelectionChanged(m_defaultCheckSetSelectionId);
0284 }
0285 
0286 void CheckSetSelectionListModel::setName(int row, const QString& name)
0287 {
0288     if ((row < 0) || m_checkSetSelections.count() <= row) {
0289         return;
0290     }
0291 
0292     auto& checkSetSelection = m_checkSetSelections[row];
0293     if (checkSetSelection.name() == name) {
0294         return;
0295     }
0296 
0297     checkSetSelection.setName(name);
0298 
0299     const QString id = checkSetSelection.id();
0300     m_edited.insert(id);
0301 
0302     const QModelIndex modelIndex = index(row, 0);
0303     emit dataChanged(modelIndex, modelIndex);
0304     emit checkSetSelectionChanged(id);
0305 }
0306 
0307 void CheckSetSelectionListModel::setSelection(int row, const QString& selection)
0308 {
0309     if ((row < 0) || m_checkSetSelections.count() <= row) {
0310         return;
0311     }
0312 
0313     auto& checkSetSelection = m_checkSetSelections[row];
0314     if (checkSetSelection.selectionAsString() == selection) {
0315         return;
0316     }
0317 
0318     checkSetSelection.setSelection(selection);
0319 
0320     const QString id = checkSetSelection.id();
0321     m_edited.insert(id);
0322 
0323     emit checkSetSelectionChanged(id);
0324 }
0325 
0326 }
0327 
0328 #include "moc_checksetselectionlistmodel.cpp"