-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.cpp
149 lines (113 loc) · 3.24 KB
/
model.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
#include "model.hpp"
#include <iostream>
#include <QString>
Model::Model(const QString& filename):DatabaseFilename(filename)
{
Root=new Library();
}
Model::~Model()
{
delete Root;
}
void Model::NewDatabase(const QString& filename)
{
if(filename!="")
DatabaseFilename=QString(filename);
odb::sqlite::database db(DatabaseFilename.toStdString(), SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
odb::core::transaction t (db.begin ());
odb::schema_catalog::create_schema (db);
Root->Persist(db);
t.commit();
}
void Model::LoadDatabase(const QString& filename)
{
if(filename!="")
DatabaseFilename=QString(filename);
odb::sqlite::database db(DatabaseFilename.toStdString(), SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
odb::core::transaction t (db.begin ());
if(Root!=NULL)
delete Root;
odb::result<Library> r (db.query<Library>());
//ograniczam się do jednej bazy
Root=r.begin()->Copy();
t.commit();
//std::cout<<*Root<<std::endl;
}
QModelIndex Model::index(int row, int column, const QModelIndex& parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
TreeItem* parentItem;
if (!parent.isValid())
parentItem = const_cast<TreeItem*>(Root);
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
TreeItem* childItem = parentItem->Children[row];
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex Model::parent(const QModelIndex& index) const
{
if (!index.isValid())
return QModelIndex();
TreeItem* childItem = static_cast<TreeItem*>(index.internalPointer());
TreeItem* parentItem = childItem->Parent;
if (parentItem == NULL)
return QModelIndex();
return createIndex(parentItem->Row(), 0, parentItem);
}
int Model::rowCount(const QModelIndex& parent) const
{
TreeItem *parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
parentItem = Root;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
return parentItem->Children.size();
}
int Model::columnCount(const QModelIndex& parent) const
{
//ilość kolumn w widkou
return 1;
}
QVariant Model::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
return item->QData();
}
void Model::Erase(const QModelIndex& index)
{
if(!index.isValid())
return;
odb::sqlite::database db(DatabaseFilename.toStdString(), SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
odb::core::transaction t (db.begin ());
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
TreeItem* parent=item->Parent;
item->Erase(db);
parent->Children.removeOne(item);
db.update(*parent);
t.commit();
delete item;
}
void Model::AddArtist(Artist* artist)
{
TreeItem* newNode=dynamic_cast<Library*>(Root)->AddArtist(artist);
odb::sqlite::database db(DatabaseFilename.toStdString(), SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
odb::core::transaction t (db.begin ());
newNode->Persist(db);
db.update(*(newNode->Parent));
t.commit();
}
std::ostream& operator<<(std::ostream& out, const Model& model)
{
out<<*model.Root<<std::endl;
return out;
}