-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
169 lines (145 loc) · 4.55 KB
/
mainwindow.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "mainwindow.h"
void MainWindow::debug(QString str) {
QMessageBox msg;
msg.setText(str);
msg.exec();
}
int MainWindow::stringToInt(QString str) {
return str.toInt();
}
QString MainWindow::intToString(int x) {
QString str;
return str.number(x);
}
QString MainWindow::doubleToString(double x) {
QString str;
return str.number(x);
}
int MainWindow::stringToDouble(QString str) {
return str.toDouble();
}
MainWindow::MainWindow(QWidget *parent) : QDeclarativeView(parent)
{
this->setResizeMode(QDeclarativeView::SizeRootObjectToView); // this one is for image background to move with
// the main window while in scaling
imageProv = new ImageProvider(QDeclarativeImageProvider::Image);
this->engine()->addImageProvider(QLatin1String("provider"), imageProv);
this->setSource(QUrl("qrc:/main.qml"));
this->makeConnectionWithDB();
this->makeConnectionWithQml();
tasksInfo.resize(37); // 37 is the number of checkboxes..
}
void MainWindow::makeConnectionWithDB() {
// HREN'
#ifdef QT_NO_DEBUG
loader.setFileName("C:/QtSDK/Desktop/Qt/4.8.1/mingw/plugins/sqldrivers/qsqlmysql4.dll");
#else
loader.setFileName("C:/QtSDK/Desktop/Qt/4.8.1/mingw/plugins/sqldrivers/qsqlmysqld4.dll");
#endif
if (!loader.load())
debug(loader.errorString());
// set everything for data base
xPl = qobject_cast<QSqlDriverPlugin *> ( loader.instance() );
db = QSqlDatabase::addDatabase(xPl->create("QMYSQL"));
db.setHostName("localhost");
db.setDatabaseName("tasks_archive");
db.setUserName("root");
db.setPassword("1524323");
if (!db.open()) {
QSqlError err;
err = db.lastError();
debug(err.text());
}
query = new QSqlQuery(db);
}
void MainWindow::makeConnectionWithQml() {
// get root object
object = (QObject *)this->rootObject();
this->rootContext()->setContextProperty("getTask", this);
}
void MainWindow::toDatabase(QString diff) {
execution = "SELECT task, answer, difficulty, comment FROM tasks WHERE (difficulty = " + diff + ") and (";
for (int i = 0; i < tasksInfo.size(); i++) {
execution += "class" + intToString(i + 1) + " = ";
execution += tasksInfo[i] == false ? "0" : "1";
if (i != tasksInfo.size() - 1)
execution += ") and (";
}
execution += ");";
// debug(execution);
if (!query->exec(execution)) {
QSqlError err;
err = query->lastError();
debug(err.text());
}
}
double MainWindow::getAnswer() {
return query->value(1).toDouble();
}
void MainWindow::setTask() {
ba = query->value(0).toByteArray();
imageProv->svg.load(ba);
}
bool MainWindow::nextTask() {
query->next();
debug(intToString(query->at()));
debug(intToString(query->size() - 1));
return query->at() == (query->size() - 1);
}
bool MainWindow::prevTask() {
query->previous();
return query->at() == 0;
}
int MainWindow::getDifficulty() {
return query->value(2).toInt();
}
QString MainWindow::getComment() {
return query->value(3).toString();
}
// this function is called whenever a checkbox changes it's status
void MainWindow::updateTasksInfo(int number, bool checked) {
tasksInfo[number] = checked;
}
void MainWindow::addTask(QString difficulty, QString comment, QString answer, QString source) {
// *********************
QFile f(source);
if (f.open(QIODevice::ReadOnly)) {
ba = f.readAll();
f.close();
}
else {
debug("Cannot load an image");
return;
}
// ********************* loading a task
execution = "INSERT INTO tasks VALUES (DEFAULT, :IMAGE, " + answer + ", '" + comment + "', " + difficulty + ", ";
for (int i = 0; i < tasksInfo.size(); i++) {
execution += tasksInfo[i] == false ? "0, " : "1, ";
}
execution.resize(execution.size() - 2);
execution += ");";
// debug(execution);
query->prepare(execution.toUtf8()); //
query->bindValue(":IMAGE", ba);
if (!query->exec()) {
QSqlError err;
err = query->lastError();
debug(err.text());
}
else {
debug("Works fine");
}
}
QString MainWindow::findSource() {
return QFileDialog::getOpenFileName(this,
QString::fromLocal8Bit("Âûáåðèòå çàäà÷ó"), QCoreApplication::applicationDirPath(),
QString::fromLocal8Bit("Âåêòîðíîå èçîáðàæåíèå (*.svg)"));
}
MainWindow::~MainWindow()
{
delete object;
delete xPl;
delete qmlClass;
delete query;
delete imageProv;
}