/
ConcertModel.cpp
173 lines (158 loc) · 4.19 KB
/
ConcertModel.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
169
170
171
172
173
#include "ConcertModel.h"
#include <QPainter>
#include "globals/Globals.h"
#include "globals/Helper.h"
/**
* @brief ConcertModel::ConcertModel
* @param parent
*/
ConcertModel::ConcertModel(QObject *parent) :
QAbstractItemModel(parent)
{
}
/**
* @brief Adds a concert to the model
* @param concert Concert to add
*/
void ConcertModel::addConcert(Concert *concert)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_concerts.append(concert);
endInsertRows();
connect(concert, SIGNAL(sigChanged(Concert*)), this, SLOT(onConcertChanged(Concert*)), Qt::UniqueConnection);
}
/**
* @brief Called when a concerts data has changed
* Emits dataChanged
* @param concert Concert which has changed
*/
void ConcertModel::onConcertChanged(Concert *concert)
{
QModelIndex index = createIndex(m_concerts.indexOf(concert), 0);
emit dataChanged(index, index);
}
/**
* @brief Get a specific concert
* @param row Row of the concert
* @return Concert object
*/
Concert *ConcertModel::concert(int row)
{
return m_concerts.at(row);
}
/**
* @brief Returns the rowcount in our model. (=number of movies)
* @param parent
* @return Number of rows (=number of movies)
*/
int ConcertModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_concerts.size();
}
/**
* @brief Get the column count of our model
* @param parent
* @return 1
*/
int ConcertModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
// return roleNames().size();
return 1;
}
/**
* @brief Accesses items data
* @param index Index of item
* @param role Role
* @return data
*/
QVariant ConcertModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() > m_concerts.count())
return QVariant();
if (role == Qt::UserRole)
return index.row();
Concert *concert = m_concerts[index.row()];
if (index.column() == 0 && role == Qt::DisplayRole) {
return Helper::appendArticle(concert->name());
} else if (index.column() == 0 && role == Qt::ToolTipRole) {
if (concert->files().size() == 0)
return QVariant();
return concert->files().at(0);
} else if (index.column() == 1 && role == Qt::DisplayRole) {
return concert->folderName();
} else if (role == Qt::UserRole+1) {
return concert->infoLoaded();
} else if (role == Qt::UserRole+2) {
return concert->hasChanged();
} else if (role == Qt::UserRole+3) {
return concert->syncNeeded();
} else if (role == Qt::ForegroundRole) {
if (concert->hasChanged())
return QColor(255, 0, 0);
} else if (role == Qt::FontRole) {
if (concert->hasChanged()) {
QFont font;
font.setItalic(true);
return font;
}
}
return QVariant();
}
/**
* @brief Returns an empty modelindex because no item has a parent
* @param child Childindex
* @return Modelindex of the parent item
*/
QModelIndex ConcertModel::parent(const QModelIndex &child) const
{
Q_UNUSED(child);
return QModelIndex();
}
/**
* @brief Returns a modelindex for the given row and column
* Parent is not used because our concert model uses only one column.
* @param row Row of the item
* @param column Column of the item
* @param parent Parent modelindex
* @return Index of the item
*/
QModelIndex ConcertModel::index(int row, int column, const QModelIndex &parent) const
{
Q_UNUSED(parent);
return createIndex(row, column);
}
/**
* @brief Clears the current contents
*/
void ConcertModel::clear()
{
if (m_concerts.size() == 0)
return;
beginRemoveRows(QModelIndex(), 0, m_concerts.size()-1);
foreach (Concert *concert, m_concerts)
delete concert;
m_concerts.clear();
endRemoveRows();
}
/**
* @brief Returns a list of all concerts
* @return List of concerts
*/
QList<Concert*> ConcertModel::concerts()
{
return m_concerts;
}
/**
* @brief Checks if there are new concerts (concerts where infoLoaded is false)
* @return True if there are new concerts
*/
bool ConcertModel::hasNewConcerts()
{
foreach (Concert *concert, m_concerts) {
if (!concert->infoLoaded())
return true;
}
return false;
}