-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredpostviewmodel.cpp
More file actions
215 lines (182 loc) · 5.21 KB
/
redpostviewmodel.cpp
File metadata and controls
215 lines (182 loc) · 5.21 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "redpostparser.h"
#include "redposttrackercore.h"
#include "redpostviewmodel.h"
////////////////////////////////////////////////////////////////////////////////
const char* const HEADER_TITLE_TEXT = "Post Title";
const char* const HEADER_AUTHOR_TEXT = "Post Author";
const char* const HEADER_DATE_TEXT = "Post Date";
////////////////////////////////////////////////////////////////////////////////
/**
* Constructor.
*/
RedPostViewModel::RedPostViewModel( QObject* parent /* = nullptr */ )
: QAbstractTableModel( parent )
, m_data( new RedPostsList )
{
m_headerTitles[ ID_TITLE ] = HEADER_TITLE_TEXT;
m_headerTitles[ ID_AUTHOR ] = HEADER_AUTHOR_TEXT;
m_headerTitles[ ID_DATE ] = HEADER_DATE_TEXT;
connect( red_core.getRedPostParser(), SIGNAL( dataAppended( int ) ), this, SLOT( onDataAppended( int ) ) );
}
////////////////////////////////////////////////////////////////////////////////
/**
* Destructor.
*/
RedPostViewModel::~RedPostViewModel()
{
for( RedPostsList::iterator it = m_data->begin();
it != m_data->end();
++it )
{
delete *it;
}
delete m_data;
}
////////////////////////////////////////////////////////////////////////////////
/**
* This method cleares the data of the model.
*/
void RedPostViewModel::clearData()
{
beginResetModel();
m_data->clear();
endResetModel();
}
////////////////////////////////////////////////////////////////////////////////
/**
* This method returns the number of items that the model has.
*/
int RedPostViewModel::itemsSize()
{
return m_data->size();
}
////////////////////////////////////////////////////////////////////////////////
/**
* This is a setter for filter string.
*/
void RedPostViewModel::setFilterString( const QString& filterString )
{
m_filterString = filterString;
reconstructData();
}
////////////////////////////////////////////////////////////////////////////////
/**
* This is setter for filter string (using rrefs).
*/
void RedPostViewModel::setFilterString( QString&& filterString )
{
m_filterString = filterString;
reconstructData();
}
////////////////////////////////////////////////////////////////////////////////
/**
* This is a reimplemented method. It returns the count of the rows.
*/
int RedPostViewModel::rowCount( const QModelIndex& ) const
{
if( m_data == nullptr )
return 0;
return m_data->size();
}
////////////////////////////////////////////////////////////////////////////////
/**
* This is reimplemented method. It returns the count of the columns.
*/
int RedPostViewModel::columnCount( const QModelIndex& ) const
{
return TOTAL_TABLE_COLUMNS;
}
////////////////////////////////////////////////////////////////////////////////
/**
* This is reimplemented method. It returns information about an item on a given
* row.
*/
QVariant RedPostViewModel::data( const QModelIndex& index, int role ) const
{
RedPostData* item = m_data->at( index.row() );
switch( role )
{
case Qt::DisplayRole:
{
switch( index.column() )
{
case ID_TITLE:
return item->getTitle();
case ID_AUTHOR:
return item->getAuthor();
case ID_DATE:
return item->getDate();
default:
break;
}
break;
}
case ID_HREF:
return item->getHref();
default:
break;
}
return QVariant();
}
////////////////////////////////////////////////////////////////////////////////
/**
* This is reimplemented method. It returns information about a given header.
*/
QVariant RedPostViewModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
if( orientation == Qt::Vertical )
return QAbstractTableModel::headerData( section, orientation, role );
if( role != Qt::DisplayRole )
return QAbstractTableModel::headerData( section, orientation, role );
switch( section )
{
case ID_TITLE:
case ID_AUTHOR:
case ID_DATE:
return m_headerTitles[ section ];
default:
break;
}
return QAbstractTableModel::headerData( section, orientation, role );
}
////////////////////////////////////////////////////////////////////////////////
/**
* This is slot that is called when new data is added to the parsed items.
*
* @param fromIndex The index of the new data point.
*/
void RedPostViewModel::onDataAppended( int fromIndex )
{
beginResetModel();
reconstructData( fromIndex );
endResetModel();
}
////////////////////////////////////////////////////////////////////////////////
/**
* Reconstructs the data items based on the items in the parser class and the
* filter string.
*
* @param fromIndex The index of the new items (if 0 - starts from the begining).
*/
void RedPostViewModel::reconstructData( int fromIndex /* = 0 */ )
{
bool isFilterStringEmpty = m_filterString.isEmpty() ;
if( fromIndex == 0 || isFilterStringEmpty )
m_data->clear();
RedPostsList* itemsList = red_core.getRedPostParser()->getItems();
if( isFilterStringEmpty )
{
m_data->append( *itemsList );
return;
}
for( RedPostsList::iterator it = itemsList->begin() + fromIndex;
it != itemsList->end();
++it )
{
RedPostData* currentItem = *it;
if( currentItem->getTitle().contains( m_filterString, Qt::CaseInsensitive ) ||
currentItem->getContent().contains( m_filterString, Qt::CaseInsensitive ) )
m_data->append( currentItem );
}
}
////////////////////////////////////////////////////////////////////////////////