Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FbDateTree.cpp « models « MyRuLib « sources - github.com/lintest/myrulib.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e6c1dc795a957e7e7393deed8dd8f60b0846c26d (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
#include "FbDateTree.h"
#include "FbBookEvent.h"
#include "FbConst.h"
#include "FbDateTime.h"
#include "FbMasterTypes.h"
#include <wx/tokenzr.h>

//-----------------------------------------------------------------------------
//  FbDateTreeThread
//-----------------------------------------------------------------------------

void * FbDateTreeThread::Entry()
{
	FbFrameDatabase database(this, m_counter);
	wxString sql = wxT("SELECT DISTINCT created FROM books ORDER BY 1 DESC");
	FbSQLite3ResultSet result = database.ExecuteQuery(sql);
	if (result.IsOk()) MakeModel(result);
	CreateCounter(database, m_sql);
	return NULL;
}

void FbDateTreeThread::MakeModel(FbSQLite3ResultSet &result)
{
	bool ok = true;

	FbTreeModel * model = new FbDateTreeModel;
	FbParentData * root = new FbParentData(*model);
	model->SetRoot(root);

	FbDateYearData * year = NULL;
	FbDateMonthData * mnth = NULL;
	while (result.NextRow()) {
		if (IsClosed()) { ok = false; break; }
		int day = result.GetInt(0);
		int new_year = day / 10000;
		int new_mnth = day / 100;
		if (year == NULL || year->GetCode() != new_year) {
			year = new FbDateYearData(*model, root, new_year);
			mnth = NULL;
		}
		if (mnth == NULL || mnth->GetCode() != new_mnth) {
			mnth = new FbDateMonthData(*model, year, new_mnth);
		}
		new FbDateDayData(*model, mnth, day);
	}

	if (ok) {
		FbModelEvent(ID_MODEL_CREATE, model).Post(m_frame);
	} else {
		delete model;
	}
}

//-----------------------------------------------------------------------------
//  FbDateYearData
//-----------------------------------------------------------------------------

IMPLEMENT_CLASS(FbDateYearData, FbParentData)

wxString FbDateYearData::GetValue(FbModel & model, size_t col) const
{
	switch (col) {
		case 0:
			return FbDateTime(m_code + 2000, 1, 1).Format(wxT("%Y"));
		default:
			return wxEmptyString;
	}
}

//-----------------------------------------------------------------------------
//  FbDateMonthData
//-----------------------------------------------------------------------------

IMPLEMENT_CLASS(FbDateMonthData, FbParentData)

wxString FbDateMonthData::GetValue(FbModel & model, size_t col) const
{
	switch (col) {
		case 0:
			return FbDateTime(m_code / 100 + 2000, m_code % 100, 1).Format(wxT("%B %Y"));
		default:
			return wxEmptyString;
	}
}

//-----------------------------------------------------------------------------
//  FbDateDayData
//-----------------------------------------------------------------------------

IMPLEMENT_CLASS(FbDateDayData, FbChildData)

wxString FbDateDayData::GetValue(FbModel & model, size_t col) const
{
	switch (col) {
		case 0:
			return FbDateTime(m_code).FormatDate();
		case 1: {
			FbDateTreeModel * master = wxDynamicCast(&model, FbDateTreeModel);
			if (master) {
				int count = master->GetCount(m_code);
				if (count != wxNOT_FOUND) return FbCollection::Format(count);
			}
			return wxEmptyString;
		}
		default:
			return wxEmptyString;
	}
}

FbDateDayData::FbDateDayData(FbModel & model, FbParentData * parent, int code)
	: FbChildData(model, parent), m_code(code), m_count(0)
{
}

bool FbDateDayData::operator==(const FbMasterInfo & info) const
{
	FbMasterDateInfo * data = wxDynamicCast(&info, FbMasterDateInfo);
	return data && data->GetId() == m_code;
}

//-----------------------------------------------------------------------------
//  FbDateTreeModel
//-----------------------------------------------------------------------------

IMPLEMENT_CLASS(FbDateTreeModel, FbTreeModel)

int FbDateTreeModel::GetCount(int code)
{
	if (m_counter.count(code)) return m_counter[code];
	int count = FbFrameThread::GetCount(m_database, code);
	if (count == wxNOT_FOUND) return wxNOT_FOUND;
	m_counter[code] = count;
	return count;
}

void FbDateTreeModel::SetCounter(const wxString & filename)
{
	if (!filename.IsEmpty()) m_database.Open(filename);
	m_counter.clear();
}