summaryrefslogtreecommitdiffstats
path: root/src/preferences/effectsettingsmodel.cpp
blob: 780a1c450f5bcd327816e8ef55c79ea3d23b452d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "preferences/effectsettingsmodel.h"

#include "moc_effectsettingsmodel.cpp"

namespace {
const int kColumnEnabled = 0;
const int kColumnName = 1;
const int kColumnType = 2;
const int kNumberOfColumns = 3;
}

EffectSettingsModel::EffectSettingsModel() {
}

EffectSettingsModel::~EffectSettingsModel() {
}

void EffectSettingsModel::resetFromEffectManager(EffectsManager* pEffectsManager) {
    if (!pEffectsManager) {
        return;
    }

    if (!m_profiles.isEmpty()) {
        beginRemoveRows(QModelIndex(), 0, m_profiles.size()-1);
        endRemoveRows();
        m_profiles.clear();
    }

    for (const EffectManifestPointer& pManifest : pEffectsManager->getAvailableEffectManifests()) {
        const bool visibility = pEffectsManager->getEffectVisibility(pManifest);
        addProfileToModel(EffectProfilePtr(new EffectProfile(pManifest, visibility)));
    }
}

bool EffectSettingsModel::addProfileToModel(EffectProfilePtr profile) {
    if (!profile)
        return false;

    int position = m_profiles.size();
    beginInsertRows(QModelIndex(), position, position);

    m_profiles.push_back(EffectProfilePtr(profile));

    endInsertRows();
    return true;
}

void EffectSettingsModel::deleteProfileFromModel(EffectProfilePtr profile) {
    if (!profile)
        return;

    int position = m_profiles.indexOf(profile);
    if (position > -1) {
        beginRemoveRows(QModelIndex(), position, position);
        endRemoveRows();
    }
    m_profiles.removeAll(profile);
}

int EffectSettingsModel::rowCount(const QModelIndex& parent) const {
    Q_UNUSED(parent);
    return m_profiles.size();
}

int EffectSettingsModel::columnCount(const QModelIndex& parent) const {
    Q_UNUSED(parent);
    return kNumberOfColumns;
}

QVariant EffectSettingsModel::data(const QModelIndex& index, int role) const {
    int rowIndex = index.row();
    if (!index.isValid() || rowIndex >= m_profiles.size()) {
        return QVariant();
    }

    EffectProfilePtr profile = m_profiles.at(rowIndex);
    if (profile) {
        if (role == Qt::UserRole) {
            return profile->pManifest->id();
        }
        int column = index.column();
        if (column == kColumnEnabled) {
            if (role == Qt::CheckStateRole) {
                return (profile->bIsVisible ? Qt::Checked : Qt::Unchecked);
            } else if (role == Qt::TextAlignmentRole) {
                return Qt::AlignCenter;
            }
        } else if (column == kColumnName && role == Qt::DisplayRole) {
            return profile->pManifest->displayName();
        } else if (column == kColumnType && role == Qt::DisplayRole) {
            return profile->pManifest->translatedBackendName();
        }
    }

    return QVariant();
}

QVariant EffectSettingsModel::headerData(int section, Qt::Orientation orientation,
        int role) const {
    if (orientation == Qt::Horizontal) {
        if (role == Qt::DisplayRole) {
            if (section == kColumnEnabled) {
                return tr("Visible");
            } else if (section == kColumnName) {
                return tr("Name");
            } else if (section == kColumnType) {
                return tr("Type");
            }
        }
    }
    return QVariant();
}

Qt::ItemFlags EffectSettingsModel::flags(const QModelIndex& index) const {
    if (index.column() == kColumnEnabled)
        return QAbstractItemModel::flags(index) | Qt::ItemIsUserCheckable;

    if (index.column() == kColumnName)
        return QAbstractItemModel::flags(index) | Qt::ItemIsSelectable;

    if (index.column() == kColumnType)
        return QAbstractItemModel::flags(index) | Qt::ItemIsSelectable;

    return QAbstractItemModel::flags(index) | Qt::ItemIsEnabled;
}

bool EffectSettingsModel::setData(const QModelIndex& index, const QVariant& value, int role) {
    if (index.isValid()) {
        EffectProfilePtr profile = m_profiles.at(index.row());
        if (profile) {
            if (index.column() == kColumnEnabled && role == Qt::CheckStateRole) {
                profile->bIsVisible = value.toBool();
            }
        }
    }
    return true;
}

QAbstractItemDelegate* EffectSettingsModel::delegateForColumn(const int i, QObject* parent) {
    Q_UNUSED(i);
    Q_UNUSED(parent);
    return nullptr;
}

bool EffectSettingsModel::isEmpty() const {
    return m_profiles.isEmpty();
}