-
Notifications
You must be signed in to change notification settings - Fork 2
/
modlistmodel.cpp
168 lines (138 loc) · 4.56 KB
/
modlistmodel.cpp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "modlistmodel.h"
#include <QIcon>
ModlistModel::ModlistModel(QList<Mod*>& mods, QObject* parent) : QAbstractTableModel(parent), modList(mods)
{
}
int ModlistModel::rowCount(const QModelIndex& parent) const
{
return modList.size();
}
int ModlistModel::columnCount(const QModelIndex& parent) const
{
return Columns::NOTES_LAST + 1;
}
QVariant ModlistModel::data(const QModelIndex& index, int role) const
{
QVariant result;
if (!index.isValid()) {
return result;
}
const Mod* currentMod = modList.at(index.row());
static QIcon yesIcon(":/images/icons/yes.ico");
static QIcon noIcon(":/images/icons/no.ico");
if (role == Qt::BackgroundRole) {
if (currentMod->modState == MERGED) {
return QVariant(QColor(0, 255, 0, 40));
}
else if ((currentMod->modState == MERGED_PACK)) {
return QVariant(QColor(0, 255, 0, 80));
}
if ((currentMod->modState == CORRUPTED)) {
return QVariant(QColor(255, 0, 0, 150));
}
}
if ( role == Qt::DecorationRole ) {
switch( index.column() ) {
case Columns::HAS_BUNDLES:
result = ( currentMod->hasBundles ) ? yesIcon : noIcon;
break;
case Columns::HAS_CACHE:
result = ( currentMod->hasCache ) ? yesIcon : noIcon;
break;
case Columns::HAS_SCRIPTS:
result = ( currentMod->hasScripts ) ? yesIcon : noIcon;
break;
};
}
if ( role == Qt::CheckStateRole && index.column() == Columns::CHECKBOX ) {
return currentMod->checked ? Qt::Checked : Qt::Unchecked;
}
if (role == Qt::FontRole ) {
QFont font;
if (currentMod->modState == MERGED_PACK) {
font.setBold(true);
font.setItalic(true);
}
else if (currentMod->modState == MERGED) {
font.setItalic(true);
}
return font;
}
if (role != Qt::DisplayRole) {
return result;
}
switch( index.column() ) {
case Columns::MOD_NAME:
result = currentMod->modName;
break;
case Columns::STATUS:
if ( currentMod->modState == MERGED) {
result = tr("Merged", "One of the possible mod states.");
}
else if ( currentMod->modState == NOT_MERGED) {
result = tr("Not merged", "One of the possible mod states.");
}
else if (currentMod->modState == MERGED_PACK) {
result = tr("Merged pack", "One of the possible mod states.");
}
else if (currentMod->modState == CORRUPTED) {
result = tr("Corrupted", "One of the possible mod states.");
}
break;
case Columns::NOTES_LAST:
result = currentMod->notes;
break;
};
return result;
}
QVariant ModlistModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole) {
return QVariant();
}
if (orientation == Qt::Horizontal) {
switch (section) {
case Columns::MOD_NAME:
return tr("Mod name", "Mods table column name.");
case Columns::HAS_BUNDLES:
return tr("Bundles", "Mods table column name.");
case Columns::HAS_CACHE:
return tr("Textures", "Mods table column name.");
case Columns::HAS_SCRIPTS:
return tr("Scripts", "Mods table column name.");
case Columns::STATUS:
return tr("Status", "Mods table column name.");
case Columns::NOTES_LAST:
return tr("Notes", "Mods table column name.");
}
}
return QVariant();
}
Qt::ItemFlags ModlistModel::flags(const QModelIndex& index) const
{
Qt::ItemFlags result;
if (!index.isValid()) {
return Qt::ItemIsEnabled;
}
result = QAbstractTableModel::flags(index);
result |= Qt::ItemIsDragEnabled;
result |= Qt::ItemIsDropEnabled;
if (index.column() == Columns::CHECKBOX) {
result |= Qt::ItemIsEnabled;
result |= Qt::ItemIsUserCheckable;
}
return result;
}
bool ModlistModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if (index.isValid() && role == Qt::CheckStateRole) {
modList.at( index.row() )->checked = value.toBool();
emit dataChanged(index, index);
return true;
}
return false;
}
Qt::DropActions ModlistModel::supportedDropActions() const
{
return Qt::MoveAction;
}