#include "customsqlmodel.h" #include #include #include #include #include #include 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; }