Skip to content

Commit

Permalink
Seperate class for checkbox delegate.
Browse files Browse the repository at this point in the history
  • Loading branch information
xpero39 committed Nov 30, 2016
1 parent 7e7d93b commit ecf1a0b
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 83 deletions.
Binary file modified Database/doro_data.db
Binary file not shown.
65 changes: 65 additions & 0 deletions checkbox_model.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <QtWidgets>
#include <QSql>
#include <QSqlQuery>
#include "checkbox_model.h"

checkbox_model::checkbox_model(QObject *parent) : QSqlQueryModel(parent)
{
}

Qt::ItemFlags checkbox_model::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = QSqlQueryModel::flags(index);
if(index.column() == 2)
return QSqlQueryModel::flags(index) | Qt::ItemIsUserCheckable;
return QSqlQueryModel::flags(index);
}

QVariant checkbox_model::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
//if(index.column() == 2 && role == Qt::CheckStateRole)
if(index.column() == 2 && role == Qt::DisplayRole)
{
//implement your logic to return the check state
//....
//if (index.column() == 2) //add a checkbox to cell(1,0)
if(value.toInt()==1){
return Qt::Checked;
}
else if(value.toInt()==0){
return Qt::Unchecked;
}
}
}

//setData() is called each time the user edits the table i.e. save data refresh view
bool checkbox_model::setData(const QModelIndex &index, const QVariant &value, int /*role*/ )
{
QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
int id = QSqlQueryModel::data(primaryKeyIndex).toInt();
clear();
//if(index.column() == 2 && role == Qt::CheckStateRole)
if(index.column() == 2)
{
bool ok;
ok = setCheckbox(id, value.toInt());
return ok;
}
else
{
return false;
//QSqlQueryModel::setData(index, value, role);
}
return true;
}


bool checkbox_model::setCheckbox(int task_id, int checkbox_value)
{
QSqlQuery query;
query.prepare("UPDATE task_list SET finished = :finished WHERE id = :id");
query.bindValue(":finished", checkbox_value);
query.bindValue(":id", task_id);
return query.exec();
}
22 changes: 22 additions & 0 deletions checkbox_model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef CHECKBOX_MODEL_H
#define CHECKBOX_MODEL_H

#include <QSqlQueryModel>

class checkbox_model : public QSqlQueryModel
{
Q_OBJECT

public:
checkbox_model(QObject *parent = 0);

Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
QVariant data(const QModelIndex &item, int role) const Q_DECL_OVERRIDE;
bool setData(const QModelIndex &index, const QVariant &value, int role) Q_DECL_OVERRIDE;
//~checkbox_model();
private:
bool setCheckbox(int task_id, int checkbox_value);
};

#endif // CHECKBOX_MODEL_H

112 changes: 50 additions & 62 deletions doro_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "checkbox_model.h"
#include "doro_manager.h"
#include "ui_doro_manager.h"

Expand All @@ -20,11 +21,12 @@ doro_manager::doro_manager(QWidget *parent) :
this->kaz = new QTimer(this);
connect(kaz, SIGNAL(timeout()), this, SLOT(countdown())); //inicialization of a pointer and links it to a function to countdown

this->sound = new QSound("C:/Faks/DIPLOMSKA/Doro_Manager/doro_manager/Sounds/sound_alert.wav");
//this->sound = new QSound("C:/Faks/DIPLOMSKA/Doro_Manager/doro_manager/Sounds/sound_alert.wav");
this->sound = new QSound(":Sounds/Sounds/sound_alert.wav");

//Setting current date to dateEdit widget in Task tab
QDate date = QDate::currentDate();
ui->dateEdit->setDate(date);
ui->dateEditTaskList->setDate(date);

//Setting Timer state to be primary Active Work
activeWork=true;
Expand All @@ -45,14 +47,20 @@ doro_manager::doro_manager(QWidget *parent) :
ui->label_tl_pic->setPixmap(image_logo);

//Database connection
QString dir_path = QCoreApplication::applicationDirPath(); //Gets directory of the executable
//dir_path.remove(position number,length number);
// dir_path.replace(45,5,"/Database/doro_data.db");
QSqlDatabase db_con = QSqlDatabase::addDatabase("QSQLITE");
db_con.setDatabaseName("C:/Faks/DIPLOMSKA/Doro_Manager/doro_manager/Database/doro_data.db"); //path
//db_con.setDatabaseName("C:/Faks/DIPLOMSKA/Doro_Manager/doro_manager/Database/doro_data.db"); //path
db_con.setDatabaseName(dir_path +"/Database/doro_data.db");

ui->textBrowser_Calander->setText(dir_path + "/Database/doro_data.db");

if (!db_con.open()){
ui->textBrowser_Calander->setText("Error: Connection with database failed.");
ui->textBrowser_Calander->append("Error: Connection with database failed.");
}
else {
ui->textBrowser_Calander->setText("Database: Connection successful.");
ui->textBrowser_Calander->append("Database: Connection successful.");
}

//Show data in Calender widget and in task list, dist. list
Expand All @@ -63,11 +71,14 @@ doro_manager::doro_manager(QWidget *parent) :
distmodel->setQuery(query);
ui->listView_distraction->setModel(distmodel);

calmodel = new QSqlQueryModel(this);
query.prepare("SELECT task, finished FROM task_list WHERE date = :date");
calmodel = new checkbox_model(this);
query.prepare("SELECT id,task, finished FROM task_list WHERE date = :date");
query.bindValue(":date", date);
query.exec();
calmodel->setQuery(query);
calmodel->setHeaderData(0, Qt::Horizontal, tr("ID"));
calmodel->setHeaderData(1, Qt::Horizontal, tr("TASK"));
calmodel->setHeaderData(2, Qt::Horizontal, tr("FINISHED"));
ui->tableView_Calendar->setModel(calmodel);

taskmodel = new QSqlQueryModel(this);
Expand All @@ -76,10 +87,10 @@ doro_manager::doro_manager(QWidget *parent) :
query.exec();
taskmodel->setQuery(query);
ui->listView_task->setModel(taskmodel);
//db_con.close();
}


//METHODS AND BUTTONS SIGNALS
void doro_manager::showDateTime() //method to show current date and time
{
QTime time = QTime::currentTime();
Expand All @@ -101,7 +112,7 @@ void doro_manager::showTime() //method to show current time

void doro_manager::countdown() //function sets countdown timer value depending on work/break interwall and subtracts time
{
if ((timeValueMin == 0) && (timeValueSec == 0)) //loop checks if the countdown ended to switch work and break time intervals
if ((timeValueMin == 0) && (timeValueSec == 0)) //loop checks if the countdown ended to switch work and break time intervals
{
if(activeWork==true){
activeWork=false;
Expand Down Expand Up @@ -193,11 +204,6 @@ void doro_manager::on_stopButton_clicked() //STOP button function stops
kaz->stop();
}

doro_manager::~doro_manager()
{
delete ui;
}

void doro_manager::on_pTimerButton_clicked() //Main tab button which shows Pomodoro timer tab
{
ui->tabWidget->setCurrentIndex(1);
Expand Down Expand Up @@ -255,13 +261,10 @@ void doro_manager::on_AddTaskButton_clicked()
//DOES NOTHING IF NOT MODIFIED
}
else {
// QSqlDatabase db_con = QSqlDatabase::addDatabase("QSQLITE");
// db_con.setDatabaseName("C:/Faks/DIPLOMSKA/Doro_Manager/doro_manager/Database/doro_data.db"); //path
// db_con.open();
QSqlQuery query;
query.prepare("INSERT INTO task_list(date,task) VALUES(:date,:task)");
query.bindValue(":task",ui->lineEdit_TaskList->text());
query.bindValue(":date",ui->dateEdit->date());
query.bindValue(":date",ui->dateEditTaskList->date());

if(!query.exec())
{
Expand All @@ -270,14 +273,13 @@ void doro_manager::on_AddTaskButton_clicked()
}
else
{
QDate date = QDate::currentDate();
QDate date = ui->dateEditTaskList->date();
query.prepare("SELECT task FROM task_list WHERE date = :date");
query.bindValue(":date", date);
query.exec();
taskmodel->setQuery(query);
ui->listView_task->setModel(taskmodel);
}
// db_con.close();
}
}

Expand All @@ -287,9 +289,6 @@ void doro_manager::on_addDistractButton_clicked()
//DOES NOTHING IF NOT MODIFIED
}
else{
// QSqlDatabase db_con = QSqlDatabase::addDatabase("QSQLITE");
// db_con.setDatabaseName("C:/Faks/DIPLOMSKA/Doro_Manager/doro_manager/Database/doro_data.db"); //path
// db_con.open();
QSqlQuery query;
query.prepare("INSERT INTO dist_list(distraction) VALUES(:distraction)");
query.bindValue(":distraction",ui->lineEdit_DistList->text());
Expand All @@ -306,67 +305,56 @@ void doro_manager::on_addDistractButton_clicked()
distmodel->setQuery(query);
ui->listView_distraction->setModel(distmodel);
}
// db_con.close();
}

}

void doro_manager::on_calendarWidget_clicked(const QDate &date)
void doro_manager::on_calendarWidget_clicked(const QDate &date) //WHEN DATE CHANGED SHOW TASKS FOR SELECTED DATE
{
//WHEN DATE CHANGED SHOW TASK FOR SELECTED DATE
//QDate date = ui->calendarWidget->selectedDate();
//db_con.open();
QSqlQuery query;
query.prepare("SELECT task, finished FROM task_list WHERE date = :date");
query.prepare("SELECT id, task, finished FROM task_list WHERE date = :date");
query.bindValue(":date", date);
query.exec();
calmodel->setQuery(query);
ui->tableView_Calendar->setModel(calmodel);
//db_con.close();
//calmodel->setQuery(query);
//ui->tableView_Calendar->setModel(calmodel);
}

/* void doro_manager::on_dateEdit_dateChanged(const QDate &date)
void doro_manager::on_dateEditTaskList_dateChanged(const QDate &date)
{
QSqlQuery query;
query.prepare("SELECT task FROM task_list WHERE date = :date");
query.bindValue(":date", date);
query.exec();
if(!query.exec()){
QMessageBox::critical(0,"Database error",query.lastError().text());
qDebug() << query.lastQuery();
}
else {
taskmodel->setQuery(query);
ui->listView_task->setModel(taskmodel);
}*/
}
}

class MyModel : public QSqlQueryModel
void doro_manager::on_clearTheListButton_clicked()
{
public:

Qt::ItemFlags flags(const QModelIndex & index) const
{
if(index.column() == 0)
return QSqlQueryModel::flags(index) | Qt::ItemIsUserCheckable;
return QSqlQueryModel::flags(index);
}
QSqlQuery query;
query.prepare("DELETE FROM dist_list");

QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const
if(!query.exec())
{
if(index.column() == 0 && role == Qt::CheckStateRole)
{
//implement your logic to return the check state
//....
}
else
return QSqlQueryModel::data(index, role);
QMessageBox::critical(0,"Database error",query.lastError().text());
qDebug() << query.lastQuery();
}

bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole)
else
{
if(index.column() == 0 && role == Qt::CheckStateRole)
{
//implement your logic to set the check state
//....
}
else
QSqlQueryModel::setData(index, value, role);
query.prepare("SELECT distraction FROM dist_list");
query.exec();
distmodel->setQuery(query);
ui->listView_distraction->setModel(distmodel);
}
};

}

doro_manager::~doro_manager()
{
delete ui;
}
17 changes: 9 additions & 8 deletions doro_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include <QtDebug>
#include <QMessageBox>
#include <QFile>
#include <QVariant>
#include <QModelIndex>
#include <QCheckBox>
#include "checkbox_model.h"

namespace Ui {
class doro_manager;
Expand All @@ -29,7 +33,6 @@ class doro_manager : public QMainWindow
int temporaryMin;
int temporarySec;


public:
explicit doro_manager(QWidget *parent = 0);
~doro_manager();
Expand Down Expand Up @@ -70,7 +73,9 @@ private slots: //declared private functions

void on_calendarWidget_clicked(const QDate &date);

// void on_dateEdit_dateChanged(const QDate &date);
void on_dateEditTaskList_dateChanged(const QDate &date);

void on_clearTheListButton_clicked();

private:
Ui::doro_manager *ui;
Expand All @@ -81,12 +86,8 @@ private slots: //declared private functions
QSound *sound;
QSqlQueryModel *distmodel;
QSqlQueryModel *taskmodel;
QSqlQueryModel *calmodel; //Calander tab task model
//QStringListModel *listmodel;

//QSqlDatabase db_con;
//QSqlQuery query;

checkbox_model *calmodel;
// QSqlQueryModel *calmodel; //Calander tab task model

};

Expand Down
6 changes: 4 additions & 2 deletions doro_manager.pro
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ TEMPLATE = app


SOURCES += main.cpp\
doro_manager.cpp
doro_manager.cpp \
checkbox_model.cpp

HEADERS += doro_manager.h
HEADERS += doro_manager.h \
checkbox_model.h

FORMS += doro_manager.ui

Expand Down
2 changes: 1 addition & 1 deletion doro_manager.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.1.0, 2016-11-14T21:22:15. -->
<!-- Written by QtCreator 4.1.0, 2016-11-29T21:06:27. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
Loading

0 comments on commit ecf1a0b

Please sign in to comment.