File indexing completed on 2024-11-10 04:56:50

0001 /*
0002     SPDX-FileCopyrightText: 2020 Ismael Asensio <isma.af@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "rulebookmodel.h"
0008 
0009 namespace KWin
0010 {
0011 
0012 RuleBookModel::RuleBookModel(QObject *parent)
0013     : QAbstractListModel(parent)
0014     , m_ruleBook(new RuleBookSettings(this))
0015 {
0016 }
0017 
0018 RuleBookModel::~RuleBookModel()
0019 {
0020 }
0021 
0022 QHash<int, QByteArray> RuleBookModel::roleNames() const
0023 {
0024     auto roles = QAbstractListModel::roleNames();
0025     roles.insert(DescriptionRole, QByteArray("display"));
0026     return roles;
0027 }
0028 
0029 int RuleBookModel::rowCount(const QModelIndex &parent) const
0030 {
0031     return m_ruleBook->ruleCount();
0032 }
0033 
0034 QVariant RuleBookModel::data(const QModelIndex &index, int role) const
0035 {
0036     if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) {
0037         return QVariant();
0038     }
0039 
0040     if (index.row() < 0 || index.row() >= rowCount()) {
0041         return QVariant();
0042     }
0043 
0044     const RuleSettings *settings = m_ruleBook->ruleSettingsAt(index.row());
0045 
0046     switch (role) {
0047     case RuleBookModel::DescriptionRole:
0048         return settings->description();
0049     }
0050 
0051     return QVariant();
0052 }
0053 
0054 bool RuleBookModel::setData(const QModelIndex &index, const QVariant &value, int role)
0055 {
0056     if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) {
0057         return false;
0058     }
0059 
0060     RuleSettings *settings = m_ruleBook->ruleSettingsAt(index.row());
0061 
0062     switch (role) {
0063     case RuleBookModel::DescriptionRole:
0064         if (settings->description() == value.toString()) {
0065             return true;
0066         }
0067         settings->setDescription(value.toString());
0068         break;
0069     default:
0070         return false;
0071     }
0072 
0073     Q_EMIT dataChanged(index, index, {role});
0074 
0075     return true;
0076 }
0077 
0078 bool RuleBookModel::insertRows(int row, int count, const QModelIndex &parent)
0079 {
0080     if (row < 0 || row > rowCount() || parent.isValid()) {
0081         return false;
0082     }
0083 
0084     beginInsertRows(parent, row, row + count - 1);
0085     for (int i = 0; i < count; i++) {
0086         RuleSettings *settings = m_ruleBook->insertRuleSettingsAt(row + i);
0087         settings->setWmclassmatch(Rules::ExactMatch); // We want ExactMatch as default for new rules in the UI
0088     }
0089     endInsertRows();
0090 
0091     return true;
0092 }
0093 
0094 bool RuleBookModel::removeRows(int row, int count, const QModelIndex &parent)
0095 {
0096     if (row < 0 || row > rowCount() || parent.isValid()) {
0097         return false;
0098     }
0099 
0100     beginRemoveRows(parent, row, row + count - 1);
0101     for (int i = 0; i < count; i++) {
0102         m_ruleBook->removeRuleSettingsAt(row + i);
0103     }
0104     endRemoveRows();
0105 
0106     return true;
0107 }
0108 
0109 bool RuleBookModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count,
0110                              const QModelIndex &destinationParent, int destinationChild)
0111 {
0112     if (sourceParent != destinationParent || sourceParent != QModelIndex()) {
0113         return false;
0114     }
0115 
0116     const bool isMoveDown = destinationChild > sourceRow;
0117     // QAbstractItemModel::beginMoveRows(): when moving rows down in the same parent,
0118     // the rows will be placed before the destinationChild index.
0119     if (!beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1,
0120                        destinationParent, isMoveDown ? destinationChild + 1 : destinationChild)) {
0121         return false;
0122     }
0123 
0124     for (int i = 0; i < count; i++) {
0125         m_ruleBook->moveRuleSettings(isMoveDown ? sourceRow : sourceRow + i, destinationChild);
0126     }
0127 
0128     endMoveRows();
0129     return true;
0130 }
0131 
0132 QString RuleBookModel::descriptionAt(int row) const
0133 {
0134     Q_ASSERT(row >= 0 && row < rowCount());
0135     return m_ruleBook->ruleSettingsAt(row)->description();
0136 }
0137 
0138 RuleSettings *RuleBookModel::ruleSettingsAt(int row) const
0139 {
0140     Q_ASSERT(row >= 0 && row < rowCount());
0141     return m_ruleBook->ruleSettingsAt(row);
0142 }
0143 
0144 void RuleBookModel::setDescriptionAt(int row, const QString &description)
0145 {
0146     Q_ASSERT(row >= 0 && row < rowCount());
0147     if (description == m_ruleBook->ruleSettingsAt(row)->description()) {
0148         return;
0149     }
0150 
0151     m_ruleBook->ruleSettingsAt(row)->setDescription(description);
0152 
0153     Q_EMIT dataChanged(index(row), index(row), {});
0154 }
0155 
0156 void RuleBookModel::setRuleSettingsAt(int row, const RuleSettings &settings)
0157 {
0158     Q_ASSERT(row >= 0 && row < rowCount());
0159 
0160     copySettingsTo(ruleSettingsAt(row), settings);
0161 
0162     Q_EMIT dataChanged(index(row), index(row), {});
0163 }
0164 
0165 void RuleBookModel::load()
0166 {
0167     beginResetModel();
0168 
0169     m_ruleBook->load();
0170 
0171     endResetModel();
0172 }
0173 
0174 void RuleBookModel::save()
0175 {
0176     m_ruleBook->save();
0177 }
0178 
0179 bool RuleBookModel::isSaveNeeded()
0180 {
0181     return m_ruleBook->usrIsSaveNeeded();
0182 }
0183 
0184 void RuleBookModel::copySettingsTo(RuleSettings *dest, const RuleSettings &source)
0185 {
0186     dest->setDefaults();
0187     const KConfigSkeletonItem::List itemList = source.items();
0188     for (const KConfigSkeletonItem *item : itemList) {
0189         dest->findItem(item->name())->setProperty(item->property());
0190     }
0191 }
0192 
0193 } // namespace
0194 
0195 #include "moc_rulebookmodel.cpp"