forked from 7h0ma5/QLog
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathRotUsrButtonsProfile.cpp
149 lines (117 loc) · 4.08 KB
/
RotUsrButtonsProfile.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 <QSqlQuery>
#include <QSqlError>
#include "RotUsrButtonsProfile.h"
#include "core/debug.h"
MODULE_IDENTIFICATION("qlog.data.rotusrbuttonsprofile");
QDataStream& operator<<(QDataStream& out, const RotUsrButtonsProfile& v)
{
out << v.profileName;
for( int i = 0; i < v.shortDescs.size(); i++ )
{
out << v.shortDescs[i];
}
for( int i = 0; i < v.bearings.size(); i++ )
{
out << v.bearings[i];
}
return out;
}
QDataStream& operator>>(QDataStream& in, RotUsrButtonsProfile& v)
{
in >> v.profileName;
for( int i = 0; i < v.shortDescs.size(); i++ )
{
in >> v.shortDescs[i];
}
for( int i = 0; i < v.bearings.size(); i++ )
{
in >> v.bearings[i];
}
return in;
}
RotUsrButtonsProfilesManager::RotUsrButtonsProfilesManager() :
ProfileManagerSQL<RotUsrButtonsProfile>("rot_user_buttons_profiles")
{
FCT_IDENTIFICATION;
QSqlQuery profileQuery;
if ( ! profileQuery.prepare("SELECT profile_name, button1_short, button1_value, button2_short, button2_value, "
"button3_short, button3_value, button4_short, button4_value "
"FROM rot_user_buttons_profiles") )
{
qWarning()<< "Cannot prepare select";
}
if ( profileQuery.exec() )
{
while (profileQuery.next())
{
RotUsrButtonsProfile profileDB;
int column = 0;
profileDB.profileName = profileQuery.value(column++).toString();
for ( int i = 0; i < profileDB.shortDescs.size(); i++ )
{
profileDB.shortDescs[i] = profileQuery.value(column++).toString();
profileDB.bearings[i] = profileQuery.value(column++).toDouble();
}
addProfile(profileDB.profileName, profileDB);
}
}
else
{
qInfo() << "Rot User Button Profile DB select error " << profileQuery.lastError().text();
}
}
void RotUsrButtonsProfilesManager::save()
{
FCT_IDENTIFICATION;
QSqlQuery deleteQuery;
QSqlQuery insertQuery;
if ( ! deleteQuery.prepare("DELETE FROM rot_user_buttons_profiles") )
{
qWarning() << "Cannot prepare Delete statement";
return;
}
if ( ! insertQuery.prepare("INSERT INTO rot_user_buttons_profiles(profile_name, button1_short, button1_value, button2_short, button2_value,"
"button3_short, button3_value, button4_short, button4_value)"
"VALUES (:profile_name, :b1_short, :b1_value, :b2_short, :b2_value,"
":b3_short, :b3_value, :b4_short, :b4_value)") )
{
qWarning() << "Cannot prepare Insert statement";
return;
}
if ( deleteQuery.exec() )
{
const QStringList &keys = profileNameList();
for ( auto &key: keys )
{
const RotUsrButtonsProfile &rotUsrButtonProfile = getProfile(key);
insertQuery.bindValue(":profile_name", key);
for ( int i = 0; i < rotUsrButtonProfile.shortDescs.size(); i++ )
{
insertQuery.bindValue(QString(":b%1_short").arg(i+1), rotUsrButtonProfile.shortDescs[i]);
}
for ( int i = 0; i < rotUsrButtonProfile.bearings.size(); i++ )
{
insertQuery.bindValue(QString(":b%1_value").arg(i+1), rotUsrButtonProfile.bearings[i]);
}
if ( ! insertQuery.exec() )
{
qInfo() << "Rot User Button Profile DB insert error " << insertQuery.lastError().text() << insertQuery.lastQuery();
}
}
}
else
{
qInfo() << "Rot User Button Profile DB delete error " << deleteQuery.lastError().text();
}
saveCurProfile1();
}
bool RotUsrButtonsProfile::operator==(const RotUsrButtonsProfile &profile)
{
return (profile.profileName == this->profileName
&& profile.shortDescs == this->shortDescs
&& profile.bearings == this->bearings);
}
bool RotUsrButtonsProfile::operator!=(const RotUsrButtonsProfile &profile)
{
return !operator==(profile);
}