File indexing completed on 2024-05-12 16:01:39

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2010 Cyrille Berger <cberger@cberger.net>
0003  * SPDX-FileCopyrightText: 2011 Silvio Heinrich <plassyqweb.de>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #include "kis_paintop_options_model.h"
0009 
0010 #include <klocalizedstring.h>
0011 
0012 
0013 KisPaintOpOptionListModel::KisPaintOpOptionListModel(QObject *parent)
0014     : BaseOptionCategorizedListModel(parent)
0015 {
0016 }
0017 
0018 void KisPaintOpOptionListModel::addPaintOpOption(KisPaintOpOption *option, int widgetIndex, const QString &label, KisPaintOpOption::PaintopCategory categoryType)
0019 {
0020 
0021     QString category;
0022     switch(categoryType) {
0023     case KisPaintOpOption::GENERAL:
0024         category = i18nc("option category", "General");
0025         break;
0026     case KisPaintOpOption::COLOR:
0027         category = i18nc("option category", "Color");
0028         break;
0029     case KisPaintOpOption::TEXTURE:
0030         category = i18nc("option category", "Texture");
0031         break;
0032     case KisPaintOpOption::FILTER:
0033         category = i18nc("option category", "Filter");
0034         break;
0035     case KisPaintOpOption::MASKING_BRUSH:
0036         category = i18nc("option category", "Masked Brush");
0037         break;
0038     };
0039 
0040     addPaintOpOption(option, widgetIndex, label, category);
0041 }
0042 
0043 void KisPaintOpOptionListModel::addPaintOpOption(KisPaintOpOption *option, int widgetIndex, const QString &label, const QString &category) {
0044 
0045     DataItem *item = categoriesMapper()->addEntry(category, KisOptionInfo(option, widgetIndex, label));
0046 
0047     if (option->isCheckable()) {
0048         item->setCheckable(true);
0049         item->setChecked(option->isChecked());
0050     }
0051 
0052     categoriesMapper()->expandAllCategories();
0053 }
0054 
0055 QVariant KisPaintOpOptionListModel::data(const QModelIndex& idx, int role) const
0056 {
0057     if (!idx.isValid()) return false;
0058 
0059     DataItem *item = categoriesMapper()->itemFromRow(idx.row());
0060     Q_ASSERT(item);
0061 
0062     // Lazy fetching of the real checked value (there are no notifications
0063     // when changing the pointop preset)
0064 
0065     if (role == Qt::CheckStateRole && item->isCheckable()) {
0066         bool realChecked = item->data()->option->isChecked();
0067 
0068         if (realChecked != item->isChecked()) {
0069             item->setChecked(realChecked);
0070         }
0071     }
0072 
0073     return BaseOptionCategorizedListModel::data(idx, role);
0074 }
0075 
0076 bool KisPaintOpOptionListModel::setData(const QModelIndex& idx, const QVariant& value, int role)
0077 {
0078     if (!idx.isValid()) return false;
0079 
0080     DataItem *item = categoriesMapper()->itemFromRow(idx.row());
0081     Q_ASSERT(item);
0082 
0083     if (role == Qt::CheckStateRole && item->isCheckable()) {
0084         item->data()->option->setChecked(value.toInt() == Qt::Checked);
0085     }
0086 
0087     return BaseOptionCategorizedListModel::setData(idx, value, role);
0088 }
0089 
0090 bool operator==(const KisOptionInfo& a, const KisOptionInfo& b)
0091 {
0092     if (a.index != b.index) return false;
0093     if (a.option->objectName() == b.option->objectName())
0094     if (a.option->category() != b.option->category()) return false;
0095     if (a.option->isCheckable() != b.option->isCheckable()) return false;
0096     if (a.option->isChecked() != b.option->isChecked()) return false;
0097     return true;
0098 }
0099 void KisPaintOpOptionListModel::signalDataChanged(const QModelIndex& index)
0100 {
0101     emit dataChanged(index,index);
0102 }