DailyBread/mainwindow.cpp
2024-01-04 16:32:53 -06:00

133 lines
2.5 KiB
C++

/*Author: Daniel Jones
*IDE: QT Creator
*Compiler: MinGW
*OS: Windows 10
*Purpose: QT Widget that displays the verse of the day upon Windows 10 bootup.
* Last edited: 1/3/2024
*/
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <chrono>
#include <ctime>
#include <iostream>
#include <QSqlDatabase>
#include <QSqlQuery>
//Function declarations
int current_day();
int current_month();
QString verse_grab();
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->verse->setText(verse_grab());
}
MainWindow::~MainWindow()
{
delete ui;
}
/*
* Function: verse_grab
* description: selects the verse based on the current day and returns the result
* Return type: string
* Param: none
*/
QString verse_grab(){
//gets current month and day and returns the result
int curmonth = current_month();
int curday = current_day();
std::string query_string = "select * from verse_list where day = " + std::to_string(curday) + " and month = " + std::to_string(curmonth);
QString q_query = QString::fromStdString(query_string);
QString my_verse;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("./verses.sqlite");
if(db.open()){
QSqlQuery query(db);
if (query.exec(q_query)){
query.next();
my_verse = query.value(1).toString();
db.close();
}
return my_verse;
}
return "error: sqlite database missing";
}
//UI function that exits when the "go in peace" button is pressed.
void MainWindow::on_exitButton_clicked()
{
QApplication::quit();
}
//Everything below this grabs the day and month.
typedef std::chrono::system_clock Clock;
/*
* Function: current_day
* description: queries the operating system for the current day
* Return type: integer
* Param: none
*/
int current_day(){
auto now = Clock::now();
std::time_t now_c = Clock::to_time_t(now);
struct tm *parts = std::localtime(&now_c);
int day = parts->tm_mday;
return day;
}
/*
* Function: current_month
* description: queries the operating system for the current month
* Return type: integer
* Param: none
*/
int current_month(){
auto now = Clock::now();
std::time_t now_c = Clock::to_time_t(now);
struct tm *parts = std::localtime(&now_c);
int month = 1 + parts->tm_mon;
return month;
}