-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
338 lines (300 loc) · 11.3 KB
/
Copy pathmainwindow.cpp
File metadata and controls
338 lines (300 loc) · 11.3 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextCodec>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// QLocale Rus(QLocale::Russia, QLocale::Russian);
ui->setupUi(this);
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForLocale(codec);
// QTextCodec::setCodecForLocale;
date = QDate::currentDate();
QTime time = QTime::currentTime();
QString str = date.toString();
ID=0;
QString nullName = "Выбрать Фамилию";
db = QSqlDatabase::addDatabase("QMYSQL", "my_sql_db");
db.setHostName ("192.168.12.224");
db.setDatabaseName ("sakila");
db.setUserName ("root");
db.setPassword ("134679");
db.setPort(3306);
if(db.open())
{
// qDebug()<<db.lastError().text;
ui->label_6->setText("success!");
}
else
{
ui->label->setText("breaking bad news");
}
//обзываем базу для работы с ней
quaery = QSqlQuery(db);
//подготовка таблиц
quaery.prepare( "CREATE TABLE IF NOT EXISTS RPR (id INTEGER UNIQUE PRIMARY KEY, firstname VARCHAR(30), lastname VARCHAR(30), incdate DATE, inctime TIME, outtime TIME)" );
//Проверка подготовки таблиц
if(quaery.exec())
{
ui->textEdit->append("Table created");
}
else
{
ui->textEdit->append("Error");
ui->textEdit->append("MySQL error:" + quaery.lastError().text());
ui->textEdit->append("MySQL error code:"+ QString::number(quaery.lastError().number()));
}
//заполнение стоковых полей
ui->label->setText("Сегодняшняя дата:");
ui->label_2->setText(str);
ui->timeinc->setTime(time);
ui->timeout->setTime(time);
ui->table->setColumnCount(5);
ui->table->setRowCount(quaery.size());
ui->comboName->addItem(nullName);
inctime=ui->timeinc->time();
tableUpdate();
/*Вывод всей таблицы
quaery.exec("SELECT * FROM rpr");
ui->table->setColumnCount(5);
ui->table->setRowCount(quaery.size());
int index=0;
while (quaery.next())
{
ui->table->setItem(index,0,new QTableWidgetItem(quaery.value(0).toString()));
ui->table->setItem(index,1,new QTableWidgetItem(quaery.value(1).toString()));
ui->table->setItem(index,2,new QTableWidgetItem(quaery.value(2).toString()));
ui->table->setItem(index,3,new QTableWidgetItem(quaery.value(3).toString()));
ui->table->setItem(index,4,new QTableWidgetItem(quaery.value(4).toString()));
index++;
bool exist=false;
//Поиск и добавление из таблицы имен/новых имен
for (int i=1; i<(sizeof(firstnames)/sizeof(firstnames[0])); i++)
{
//маркер повторяющихся имен
if (firstnames[i]==quaery.value(1).toString())
{
exist=true;
}
//найдено новое имя + добавить в список и матрицу
if (firstnames[i]=="" && !exist)
{
firstnames[i]=quaery.value(1).toString();
ui->comboName->addItem(firstnames[i]);
exist=true;
}
//Принудительный выход из цикла
if (firstnames[i+1]=="" && exist)
{
i=(sizeof(firstnames)/sizeof(firstnames[0]));
}
// qDebug()<<firstnames[i-1];
}
ID=quaery.value(0).toInt();
}
ui->table->show();
*/
// ui->inctime->setTime(Qt::LocalTime); //текущее время для времени ухода
// ----------События---------------------------
connect(ui->pushButton, SIGNAL (clicked()),this,SLOT(incdateclick()));
connect(ui->pushButton_2, SIGNAL (clicked()),this,SLOT(outdateclick()));
connect(ui->pushButton_3, SIGNAL (clicked()),this,SLOT(showtables()));
connect(ui->pushButton_4, SIGNAL (clicked()),this,SLOT(showname()));
connect(ui->debugButton, SIGNAL (clicked()),this,SLOT(debugfunc()));
connect(ui->comboName, SIGNAL (currentIndexChanged(int)), this, SLOT (nameChanged()));
//--------------------------------------------------------
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::incdateclick()
{
//обновляем данные о времени
inctime=ui->timeinc->time();
//просто чтоб знать что нажато
ui->textEdit->setText("clicked-1");
if (ui->comboName->currentIndex()==0)
{
textName=ui->Name->toPlainText();
}
// Проверка заполнения фамилии
if (textName!="") //textName not null
{
quaery.exec("SELECT * FROM RPR WHERE incdate='"+date.toString("yyyy-MM-dd")+"' AND firstname='"+textName+"'");
// if(quaery.value(0).toInt())
if (quaery.next())
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Предупреждение", "Данные о приходе сотрудника на текущую дату содержатся в таблице. Изменить?",
QMessageBox::Yes|QMessageBox::No);
if (reply==QMessageBox::Yes)
{
qDebug()<<inctime.toString();
quaery.exec("UPDATE RPR SET inctime ='"+inctime.toString()+"' WHERE incdate='"+date.toString("yyyy-MM-dd")+"' AND firstname='"+textName+"'");
}
}
else
{
quaery.prepare("INSERT INTO rpr (id, firstname, lastname, incdate, inctime) "
"VALUES (:id, :firstname, :lastname, :incdate, :inctime)");
quaery.bindValue(":id", ++ID);
quaery.bindValue(":firstname", textName);
quaery.bindValue(":lastname", "");
quaery.bindValue(":incdate", QDate::currentDate());
quaery.bindValue(":inctime", inctime);
quaery.exec();
tableUpdate();
}
}
else ui->textEdit->setText("Введите имя");
/*
*/
}
//Событие записи времени ухода
void MainWindow::outdateclick()
{
ui->textEdit->setText("clicked-2");
outtime=ui->timeout->time();
//просто чтоб знать что нажато
ui->textEdit->setText("clicked-1");
if (ui->comboName->currentIndex()==0)
{
textName=ui->Name->toPlainText();
}
// Проверка заполнения фамилии
if (textName!="") //textName not null
{
quaery.exec("SELECT * FROM RPR WHERE incdate='"+date.toString("yyyy-MM-dd")+"' AND firstname='"+textName+"'");
// if(quaery.value(0).toInt())
if (quaery.next())
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Предупреждение", "Данные о приходе сотрудника на текущую дату содержатся в таблице. Изменить?",
QMessageBox::Yes|QMessageBox::No);
if (reply==QMessageBox::Yes)
{
qDebug()<<inctime.toString();
quaery.exec("UPDATE RPR SET outtime ='"+outtime.toString()+"' WHERE incdate='"+date.toString("yyyy-MM-dd")+"' AND firstname='"+textName+"'");
}
}
}
tableUpdate();
}
//Отображение даных за сегодняшний день
void MainWindow::showtables()
{
quaery.exec("SELECT * FROM RPR WHERE incdate='"+date.toString("yyyy-MM-dd")+"'");
//()<<date.toString("yyyy-MM-dd");
//qDebug()<<date;
ui->table->setColumnCount(5);
ui->table->setRowCount(quaery.size());
int index=0;
while (quaery.next())
{
ui->table->setItem(index,0,new QTableWidgetItem(quaery.value(1).toString()));
ui->table->setItem(index,1,new QTableWidgetItem(quaery.value(2).toString()));
ui->table->setItem(index,2,new QTableWidgetItem(quaery.value(3).toString()));
ui->table->setItem(index,3,new QTableWidgetItem(quaery.value(4).toString()));
ui->table->setItem(index,4,new QTableWidgetItem(quaery.value(5).toString()));
index++;
}
ui->table->show();
}
//данные указанного сотрудника
void MainWindow::showname()
{
// textName = ui->Name->toPlainText();
quaery.exec("SELECT * FROM RPR WHERE firstname='"+textName+"'");
ui->table->setColumnCount(5);
ui->table->setRowCount(quaery.size());
int index = 0;
while (quaery.next())
{
ui->table->setItem(index,0,new QTableWidgetItem(quaery.value(1).toString()));
ui->table->setItem(index,1,new QTableWidgetItem(quaery.value(2).toString()));
ui->table->setItem(index,2,new QTableWidgetItem(quaery.value(3).toString()));
ui->table->setItem(index,3,new QTableWidgetItem(quaery.value(4).toString()));
ui->table->setItem(index,4,new QTableWidgetItem(quaery.value(5).toString()));
index++;
}
}
//отладочные функции
void MainWindow::debugfunc()
{
quaery.exec("SELECT * FROM RPR WHERE incdate='"+date.toString("yyyy-MM-dd")+"' AND firstname='"+textName+"'");
// if(quaery.value(0).toInt())
if (quaery.next())
{
qDebug()<<"False"<<quaery.value(1).toString();
// qDebug()<<ui->comboName->currentText();
}
else
qDebug()<<"true"<<quaery.value(1).toString();
}
//Событие туллбокса
void MainWindow::nameChanged()
{
if (ui->comboName->currentIndex()!=0){
textName = ui->comboName->currentText();
}
else
{
textName = ui->Name->toPlainText();
}
qDebug()<<textName;
}
void MainWindow::tableUpdate()
{
//Вывод всей таблицы
quaery.exec("SELECT * FROM rpr");
ui->table->setColumnCount(5);
ui->table->setRowCount(quaery.size());
int index=0;
while (quaery.next())
{
ui->table->setItem(index,0,new QTableWidgetItem(quaery.value(0).toString()));
ui->table->setItem(index,1,new QTableWidgetItem(quaery.value(1).toString()));
ui->table->setItem(index,2,new QTableWidgetItem(quaery.value(3).toString()));
ui->table->setItem(index,3,new QTableWidgetItem(quaery.value(4).toString()));
ui->table->setItem(index,4,new QTableWidgetItem(quaery.value(5).toString()));
index++;
bool exist=false;
//Поиск и добавление из таблицы имен/новых имен
for (int i=1; i<(sizeof(firstnames)/sizeof(firstnames[0])); i++)
{
//маркер повторяющихся имен
if (firstnames[i]==quaery.value(1).toString())
{
exist=true;
}
//найдено новое имя + добавить в список и матрицу
if (firstnames[i]=="" && !exist)
{
firstnames[i]=quaery.value(1).toString();
ui->comboName->addItem(firstnames[i]);
exist=true;
}
//Принудительный выход из цикла
if (firstnames[i+1]=="" && exist)
{
i=(sizeof(firstnames)/sizeof(firstnames[0]));
}
// qDebug()<<firstnames[i-1];
}
ID=quaery.value(0).toInt();
}
ui->table->show();
}