forked from Blup1980/microsip_42
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCListCtrl_DataModel.h
96 lines (82 loc) · 2.47 KB
/
CListCtrl_DataModel.h
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
#pragma once
#include <string>
#include <vector>
using namespace std;
struct CListCtrl_DataRecord
{
CListCtrl_DataRecord()
{}
CListCtrl_DataRecord(const string& city, const string& state, const string& country)
:m_City(city)
,m_State(state)
,m_Country(country)
{}
string m_City;
string m_State;
string m_Country;
const string& GetCellText(int col, bool title) const
{
switch(col)
{
case 0: { static string title0("City"); return title ? title0 : m_City; }
case 1: { static string title1("State"); return title ? title1 : m_State; }
case 2: { static string title2("Country"); return title ? title2 : m_Country; }
default:{ static string emptyStr; return emptyStr; }
}
}
int GetColCount() const { return 3; }
};
class CListCtrl_DataModel
{
vector<CListCtrl_DataRecord> m_Records;
int m_LookupTime;
int m_RowMultiplier;
public:
CListCtrl_DataModel()
:m_RowMultiplier(0)
,m_LookupTime(0)
{
InitDataModel();
}
void InitDataModel()
{
m_Records.clear();
m_Records.push_back( CListCtrl_DataRecord("Copenhagen", "Sjaelland", "Denmark") );
m_Records.push_back( CListCtrl_DataRecord("Aarhus", "Jutland", "Denmark") );
m_Records.push_back( CListCtrl_DataRecord("Odense", "Fyn", "Denmark") );
m_Records.push_back( CListCtrl_DataRecord("Malmoe", "Skaane", "Sweeden") );
if (m_RowMultiplier > 1)
{
vector<CListCtrl_DataRecord> rowset(m_Records);
m_Records.reserve((m_RowMultiplier-1) * rowset.size());
for(int i = 0 ; i < m_RowMultiplier ; ++i)
{
m_Records.insert(m_Records.end(), rowset.begin(), rowset.end());
}
}
}
const string& GetCellText(size_t lookupId, int col) const
{
if (lookupId >= m_Records.size())
{
static const string oob("Out of Bound");
return oob;
}
// How many times should we search sequential for the row ?
for(int i=0; i < m_LookupTime; ++i)
{
for(size_t rowId = 0; rowId < m_Records.size(); ++rowId)
{
if (rowId==lookupId)
break;
}
}
return m_Records.at(lookupId).GetCellText(col, false);
}
size_t GetRowIds() const { return m_Records.size(); }
int GetColCount() const { return CListCtrl_DataRecord().GetColCount(); }
const string& GetColTitle(int col) const { return CListCtrl_DataRecord().GetCellText(col, true); }
vector<CListCtrl_DataRecord>& GetRecords() { return m_Records; }
void SetLookupTime(int lookupTimes) { m_LookupTime = lookupTimes; }
void SetRowMultiplier(int multiply) { if (m_RowMultiplier != multiply ) { m_RowMultiplier = multiply; InitDataModel(); } }
};