1 #include "preferences/effectsettingsmodel.h"
2 
3 #include "moc_effectsettingsmodel.cpp"
4 
5 namespace {
6 const int kColumnEnabled = 0;
7 const int kColumnName = 1;
8 const int kColumnType = 2;
9 const int kNumberOfColumns = 3;
10 } // namespace
11 
EffectSettingsModel()12 EffectSettingsModel::EffectSettingsModel() {
13 }
14 
~EffectSettingsModel()15 EffectSettingsModel::~EffectSettingsModel() {
16 }
17 
resetFromEffectManager(EffectsManager * pEffectsManager)18 void EffectSettingsModel::resetFromEffectManager(EffectsManager* pEffectsManager) {
19     if (!pEffectsManager) {
20         return;
21     }
22 
23     if (!m_profiles.isEmpty()) {
24         beginRemoveRows(QModelIndex(), 0, m_profiles.size()-1);
25         endRemoveRows();
26         m_profiles.clear();
27     }
28 
29     for (const EffectManifestPointer& pManifest : pEffectsManager->getAvailableEffectManifests()) {
30         const bool visibility = pEffectsManager->getEffectVisibility(pManifest);
31         addProfileToModel(EffectProfilePtr(new EffectProfile(pManifest, visibility)));
32     }
33 }
34 
addProfileToModel(EffectProfilePtr profile)35 bool EffectSettingsModel::addProfileToModel(EffectProfilePtr profile) {
36     if (!profile) {
37         return false;
38     }
39 
40     int position = m_profiles.size();
41     beginInsertRows(QModelIndex(), position, position);
42 
43     m_profiles.push_back(EffectProfilePtr(profile));
44 
45     endInsertRows();
46     return true;
47 }
48 
deleteProfileFromModel(EffectProfilePtr profile)49 void EffectSettingsModel::deleteProfileFromModel(EffectProfilePtr profile) {
50     if (!profile) {
51         return;
52     }
53 
54     int position = m_profiles.indexOf(profile);
55     if (position > -1) {
56         beginRemoveRows(QModelIndex(), position, position);
57         endRemoveRows();
58     }
59     m_profiles.removeAll(profile);
60 }
61 
rowCount(const QModelIndex & parent) const62 int EffectSettingsModel::rowCount(const QModelIndex& parent) const {
63     Q_UNUSED(parent);
64     return m_profiles.size();
65 }
66 
columnCount(const QModelIndex & parent) const67 int EffectSettingsModel::columnCount(const QModelIndex& parent) const {
68     Q_UNUSED(parent);
69     return kNumberOfColumns;
70 }
71 
data(const QModelIndex & index,int role) const72 QVariant EffectSettingsModel::data(const QModelIndex& index, int role) const {
73     int rowIndex = index.row();
74     if (!index.isValid() || rowIndex >= m_profiles.size()) {
75         return QVariant();
76     }
77 
78     EffectProfilePtr profile = m_profiles.at(rowIndex);
79     if (profile) {
80         if (role == Qt::UserRole) {
81             return profile->pManifest->id();
82         }
83         int column = index.column();
84         if (column == kColumnEnabled) {
85             if (role == Qt::CheckStateRole) {
86                 return (profile->bIsVisible ? Qt::Checked : Qt::Unchecked);
87             } else if (role == Qt::TextAlignmentRole) {
88                 return Qt::AlignCenter;
89             }
90         } else if (column == kColumnName && role == Qt::DisplayRole) {
91             return profile->pManifest->displayName();
92         } else if (column == kColumnType && role == Qt::DisplayRole) {
93             return profile->pManifest->translatedBackendName();
94         }
95     }
96 
97     return QVariant();
98 }
99 
headerData(int section,Qt::Orientation orientation,int role) const100 QVariant EffectSettingsModel::headerData(int section, Qt::Orientation orientation,
101         int role) const {
102     if (orientation == Qt::Horizontal) {
103         if (role == Qt::DisplayRole) {
104             if (section == kColumnEnabled) {
105                 return tr("Visible");
106             } else if (section == kColumnName) {
107                 return tr("Name");
108             } else if (section == kColumnType) {
109                 return tr("Type");
110             }
111         }
112     }
113     return QVariant();
114 }
115 
flags(const QModelIndex & index) const116 Qt::ItemFlags EffectSettingsModel::flags(const QModelIndex& index) const {
117     if (index.column() == kColumnEnabled) {
118         return QAbstractItemModel::flags(index) | Qt::ItemIsUserCheckable;
119     }
120 
121     if (index.column() == kColumnName) {
122         return QAbstractItemModel::flags(index) | Qt::ItemIsSelectable;
123     }
124 
125     if (index.column() == kColumnType) {
126         return QAbstractItemModel::flags(index) | Qt::ItemIsSelectable;
127     }
128 
129     return QAbstractItemModel::flags(index) | Qt::ItemIsEnabled;
130 }
131 
setData(const QModelIndex & index,const QVariant & value,int role)132 bool EffectSettingsModel::setData(const QModelIndex& index, const QVariant& value, int role) {
133     if (index.isValid()) {
134         EffectProfilePtr profile = m_profiles.at(index.row());
135         if (profile) {
136             if (index.column() == kColumnEnabled && role == Qt::CheckStateRole) {
137                 profile->bIsVisible = value.toBool();
138             }
139         }
140     }
141     return true;
142 }
143 
delegateForColumn(const int i,QObject * parent)144 QAbstractItemDelegate* EffectSettingsModel::delegateForColumn(const int i, QObject* parent) {
145     Q_UNUSED(i);
146     Q_UNUSED(parent);
147     return nullptr;
148 }
149 
isEmpty() const150 bool EffectSettingsModel::isEmpty() const {
151     return m_profiles.isEmpty();
152 }
153