File indexing completed on 2024-05-12 16:01:28
0001 /* 0002 * SPDX-FileCopyrightText: 2009 Cyrille Berger <cberger@cberger.net> 0003 * SPDX-FileCopyrightText: 2011 Silvio Heinrich <plassy@web.de> 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "kis_composite_ops_model.h" 0009 0010 #include <QApplication> 0011 #include <QStyle> 0012 #include <QStyleOptionButton> 0013 0014 #include <KoCompositeOp.h> 0015 #include <KoCompositeOpRegistry.h> 0016 #include <kis_icon.h> 0017 0018 #include "kis_debug.h" 0019 #include "kis_config.h" 0020 0021 KoID KisCompositeOpListModel::favoriteCategory() { 0022 static KoID category("favorites", ki18n("Favorites")); 0023 return category; 0024 } 0025 0026 void KisCompositeOpListModel::initialize() 0027 { 0028 QMap<KoID, KoID> ops = KoCompositeOpRegistry::instance().getCompositeOps(); 0029 QMapIterator<KoID, KoID> it(ops); 0030 0031 while (it.hasNext()) { 0032 KoID op = *it.next(); 0033 KoID category = it.key(); 0034 BaseKoIDCategorizedListModel::DataItem *item = categoriesMapper()->addEntry(category.name(), op); 0035 item->setCheckable(true); 0036 } 0037 0038 BaseKoIDCategorizedListModel::DataItem *item = categoriesMapper()->addCategory(favoriteCategory().name()); 0039 item->setExpanded(true); 0040 0041 readFavoriteCompositeOpsFromConfig(); 0042 } 0043 0044 void KisCompositeOpListModel::initializeForLayerStyles() 0045 { 0046 QMap<KoID, KoID> ops = KoCompositeOpRegistry::instance().getLayerStylesCompositeOps(); 0047 QMapIterator<KoID, KoID> it(ops); 0048 0049 while (it.hasNext()) { 0050 KoID op = *it.next(); 0051 KoID category = it.key(); 0052 BaseKoIDCategorizedListModel::DataItem *item = categoriesMapper()->addEntry(category.name(), op); 0053 item->setCheckable(false); 0054 } 0055 0056 categoriesMapper()->expandAllCategories(); 0057 } 0058 0059 KisCompositeOpListModel* KisCompositeOpListModel::sharedInstance() 0060 { 0061 static KisCompositeOpListModel *model = 0; 0062 0063 if (!model) { 0064 model = new KisCompositeOpListModel(); 0065 model->initialize(); 0066 } 0067 0068 return model; 0069 } 0070 0071 void KisCompositeOpListModel::validate(const KoColorSpace *cs) 0072 { 0073 for (int i = 0, size = categoriesMapper()->rowCount(); i < size; i++) { 0074 DataItem *item = categoriesMapper()->itemFromRow(i); 0075 0076 if (!item->isCategory()) { 0077 bool value = KoCompositeOpRegistry::instance().colorSpaceHasCompositeOp(cs, *item->data()); 0078 item->setEnabled(value); 0079 } 0080 } 0081 } 0082 0083 bool KisCompositeOpListModel::setData(const QModelIndex& idx, const QVariant& value, int role) 0084 { 0085 if (!idx.isValid()) return false; 0086 0087 bool result = BaseKoIDCategorizedListModel::setData(idx, value, role); 0088 0089 DataItem *item = categoriesMapper()->itemFromRow(idx.row()); 0090 Q_ASSERT(item); 0091 0092 0093 if(role == Qt::CheckStateRole) { 0094 if (item->isChecked()) { 0095 addFavoriteEntry(*item->data()); 0096 } else { 0097 removeFavoriteEntry(*item->data()); 0098 } 0099 0100 writeFavoriteCompositeOpsToConfig(); 0101 } 0102 0103 return result; 0104 } 0105 0106 QVariant KisCompositeOpListModel::data(const QModelIndex& idx, int role) const 0107 { 0108 if (!idx.isValid()) return QVariant(); 0109 0110 if(role == Qt::DecorationRole) { 0111 DataItem *item = categoriesMapper()->itemFromRow(idx.row()); 0112 Q_ASSERT(item); 0113 0114 if (!item->isCategory() && !item->isEnabled()) { 0115 QStyle *style = QApplication::style(); 0116 QStyleOptionButton so; 0117 QSize size = style->sizeFromContents(QStyle::CT_CheckBox, &so, QSize(), 0); 0118 0119 const QIcon icon = KisIconUtils::loadIcon("warning").pixmap(size); 0120 return icon; 0121 } 0122 } 0123 0124 return BaseKoIDCategorizedListModel::data(idx, role); 0125 } 0126 0127 void KisCompositeOpListModel::addFavoriteEntry(const KoID &entry) 0128 { 0129 DataItem *item = categoriesMapper()->addEntry(favoriteCategory().name(), entry); 0130 item->setCheckable(false); 0131 } 0132 0133 void KisCompositeOpListModel::removeFavoriteEntry(const KoID &entry) 0134 { 0135 categoriesMapper()->removeEntry(favoriteCategory().name(), entry); 0136 } 0137 0138 void KisCompositeOpListModel::readFavoriteCompositeOpsFromConfig() 0139 { 0140 KisConfig config(true); 0141 Q_FOREACH (const QString &op, config.favoriteCompositeOps()) { 0142 KoID entry = KoCompositeOpRegistry::instance().getKoID(op); 0143 0144 DataItem *item = categoriesMapper()->fetchOneEntry(entry); 0145 if (item) { 0146 item->setChecked(true); 0147 } 0148 0149 addFavoriteEntry(entry); 0150 } 0151 } 0152 0153 void KisCompositeOpListModel::writeFavoriteCompositeOpsToConfig() const 0154 { 0155 QStringList list; 0156 QVector<DataItem*> filteredItems = 0157 categoriesMapper()->itemsForCategory(favoriteCategory().name()); 0158 0159 Q_FOREACH (DataItem *item, filteredItems) { 0160 list.append(item->data()->id()); 0161 } 0162 0163 KisConfig config(false); 0164 config.setFavoriteCompositeOps(list); 0165 }