customsqlmodel.cpp
1.87 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
#include "customsqlmodel.h"
#include <iostream>
#include <QSqlRecord>
#include <QDate>
#include <QString>
#include <QFont>
#include <QSqlQuery>
CustomSQLModel::CustomSQLModel(QObject *parent) :
QSqlQueryModel(parent)
{
}
QVariant CustomSQLModel::data(const QModelIndex &item, int role) const
{
if(columnCount() != 1)
{
if ((role == Qt::BackgroundColorRole) && compareDates(record(item.row()).value("Data").toString()))
{
return Qt::red;
}
if ((role == Qt::FontRole) && compareDates(record(item.row()).value("Data").toString()))
{
QFont f;
f.setBold(true);
return f;
}
}
return QSqlQueryModel::data(item, role);
}
Qt::ItemFlags CustomSQLModel::flags(const QModelIndex &index) const
{
if(columnCount() == 1)
{
if(isReservat(record(index.row()).value(0).toString()))
{
return Qt::NoItemFlags;
}
}
return QAbstractItemModel::flags(index);
}
bool CustomSQLModel::compareDates(const QString date) const
{
int year, month, day, hour, minute, second;
year = date.mid(0,4).toInt();
month = date.mid(5,2).toInt();
day = date.mid(8,2).toInt();
hour = date.mid(11,2).toInt();
minute = date.mid(14,2).toInt();
second = date.mid(17,2).toInt();
QDate currentDate = QDate::currentDate();
QTime currentTime = QTime::currentTime();
QDateTime current(currentDate,currentTime);
QDate d(year,month,day);
QTime t(hour,minute,second);
QDateTime timestamp(d,t);
return (timestamp.daysTo(current) > 3);
}
bool CustomSQLModel::isReservat(QString name) const
{
QSqlQuery assignatures("SELECT * FROM assignatures WHERE agafats = 1;");
while(assignatures.next())
{
if(assignatures.record().value(0).toString() == name)
{
return true;
}
}
return false;
}