File indexing completed on 2024-04-21 05:54:07
0001 /** 0002 * SPDX-FileCopyrightText: 2021 by Alexander Stippich <a.stippich@gmx.net> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 //KDE includes 0008 #include <KConfigGroup> 0009 #include <KSharedConfig> 0010 0011 #include "OptionsModel.h" 0012 #include "skanpage_debug.h" 0013 0014 class OptionsModelPrivate 0015 { 0016 public: 0017 QList<KSaneCore::Option *> mOptionsList; 0018 QList<bool> mQuickAccessList; 0019 QSet<QString> mQuickAccessOptions; 0020 bool mQuickAccessListChanged = false; 0021 }; 0022 0023 OptionsModel::OptionsModel(QObject *parent) 0024 : QAbstractListModel(parent) 0025 , d(std::make_unique<OptionsModelPrivate>()) 0026 { 0027 //if there are no defined quick access options yet, insert default ones 0028 KConfigGroup quickOptions(KSharedConfig::openConfig(), QStringLiteral("quickAccessOptions")); 0029 if (!quickOptions.exists()) { 0030 d->mQuickAccessOptions.insert(QStringLiteral("KSane::PageSize")); 0031 d->mQuickAccessOptions.insert(QStringLiteral("resolution")); 0032 d->mQuickAccessOptions.insert(QStringLiteral("source")); 0033 d->mQuickAccessOptions.insert(QStringLiteral("mode")); 0034 } else { 0035 const QStringList keys = quickOptions.keyList(); 0036 d->mQuickAccessOptions = QSet(keys.begin(), keys.end()); 0037 } 0038 } 0039 0040 OptionsModel::~OptionsModel() 0041 { 0042 if (d->mQuickAccessListChanged) { 0043 KConfigGroup quickOptions(KSharedConfig::openConfig(), QStringLiteral("quickAccessOptions")); 0044 quickOptions.deleteGroup(); 0045 for (int i = 0; i < d->mOptionsList.size(); i++) { 0046 if (d->mQuickAccessList.at(i)) { 0047 quickOptions.writeEntry(d->mOptionsList.at(i)->name(), true); 0048 } 0049 } 0050 } 0051 } 0052 0053 QHash<int, QByteArray> OptionsModel::roleNames() const 0054 { 0055 QHash<int, QByteArray> roles; 0056 roles[NameRole] = "name"; 0057 roles[TitleRole] = "title"; 0058 roles[DescriptionRole] = "description"; 0059 roles[ValueRole] = "value"; 0060 roles[MaximumValueRole] = "maximum"; 0061 roles[MinimumValueRole] = "minimum"; 0062 roles[StepValueRole] = "step"; 0063 roles[ValueListRole] = "valueList"; 0064 roles[UnitRole] = "unit"; 0065 roles[TypeRole] = "type"; 0066 roles[StateRole] = "state"; 0067 roles[QuickAccessRole] = "quickAccess"; 0068 return roles; 0069 } 0070 0071 int OptionsModel::rowCount(const QModelIndex &) const 0072 { 0073 return d->mOptionsList.count(); 0074 } 0075 0076 QVariant OptionsModel::data(const QModelIndex &index, int role) const 0077 { 0078 if (!index.isValid()) { 0079 return QVariant(); 0080 } 0081 0082 if (index.row() >= d->mOptionsList.size() || index.row() < 0) { 0083 return QVariant(); 0084 } 0085 0086 switch (role) { 0087 case NameRole: 0088 return d->mOptionsList.at(index.row())->name(); 0089 break; 0090 case TitleRole: 0091 return d->mOptionsList.at(index.row())->title(); 0092 break; 0093 case DescriptionRole: 0094 return d->mOptionsList.at(index.row())->description(); 0095 break; 0096 case ValueRole: 0097 return d->mOptionsList.at(index.row())->value(); 0098 break; 0099 case MaximumValueRole: 0100 return d->mOptionsList.at(index.row())->maximumValue(); 0101 break; 0102 case MinimumValueRole: 0103 return d->mOptionsList.at(index.row())->minimumValue(); 0104 break; 0105 case StepValueRole: 0106 return d->mOptionsList.at(index.row())->stepValue(); 0107 break; 0108 case ValueListRole: 0109 return d->mOptionsList.at(index.row())->valueList(); 0110 break; 0111 case UnitRole: 0112 return d->mOptionsList.at(index.row())->valueUnit(); 0113 break; 0114 case TypeRole: 0115 return d->mOptionsList.at(index.row())->type(); 0116 break; 0117 case StateRole: 0118 return d->mOptionsList.at(index.row())->state(); 0119 break; 0120 case QuickAccessRole: 0121 return d->mQuickAccessList.at(index.row()); 0122 break; 0123 default: 0124 break; 0125 } 0126 return QVariant(); 0127 } 0128 0129 bool OptionsModel::setData(const QModelIndex &index, const QVariant &value, int role) 0130 { 0131 if ((role != ValueRole && role != QuickAccessRole) || index.row() < 0 || index.row() >= d->mOptionsList.size()) { 0132 return false; 0133 } 0134 if (role == ValueRole) { 0135 qCDebug(SKANPAGE_LOG()) << "OptionsModel: Writing to option" << d->mOptionsList.at(index.row())->name() << value; 0136 d->mOptionsList.at(index.row())->setValue(value); 0137 Q_EMIT dataChanged(index, index, {ValueRole}); 0138 return true; 0139 } 0140 if (role == QuickAccessRole) { 0141 d->mQuickAccessList[index.row()] = value.toBool(); 0142 d->mQuickAccessListChanged = true; 0143 Q_EMIT dataChanged(index, index, {QuickAccessRole}); 0144 } 0145 return true; 0146 } 0147 0148 void OptionsModel::setOptionsList(const QList<KSaneCore::Option *> &optionsList) 0149 { 0150 beginResetModel(); 0151 d->mOptionsList = optionsList; 0152 d->mQuickAccessList.clear(); 0153 d->mQuickAccessList.reserve(optionsList.size()); 0154 for (int i = 0; i < d->mOptionsList.size(); i++) { 0155 KSaneCore::Option *option = d->mOptionsList.at(i); 0156 qCDebug(SKANPAGE_LOG()) << "OptionsModel: Importing option " << option->name() << ", type" << option->type() << ", state" << option->state(); 0157 connect(option, &KSaneCore::Option::optionReloaded, this, [=]() { Q_EMIT dataChanged(index(i, 0), index(i, 0), {StateRole}); }); 0158 connect(option, &KSaneCore::Option::valueChanged, this, [=]() { Q_EMIT dataChanged(index(i, 0), index(i, 0), {ValueRole}); }); 0159 d->mQuickAccessList.insert(i, d->mQuickAccessOptions.contains(option->name())); 0160 } 0161 endResetModel(); 0162 Q_EMIT rowCountChanged(); 0163 } 0164 0165 void OptionsModel::clearOptions() 0166 { 0167 beginResetModel(); 0168 d->mOptionsList.clear(); 0169 d->mQuickAccessList.clear(); 0170 endResetModel(); 0171 Q_EMIT rowCountChanged(); 0172 } 0173 0174 #include "moc_OptionsModel.cpp"