Skip to content

Commit ffd1a9c

Browse files
committed
Version 0.6:
* Added call details window to show details about what time a call was registered at. * Fixed bug when showing bars about weekly registered calls. * We're now using Widget::update() instead of QWidget::repaint() to let Qt optimize refreshes. * Optimized the way of saving calls. Now we just save unsaved calls instead of all of them.
1 parent 1d30f82 commit ffd1a9c

11 files changed

+261
-82
lines changed

CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.5)
22

3-
project(PerformanceMeasurer VERSION 0.5 LANGUAGES CXX)
3+
project(PerformanceMeasurer VERSION 0.6 LANGUAGES CXX)
44

55
set(CMAKE_AUTOUIC ON)
66
set(CMAKE_AUTOMOC ON)
@@ -10,7 +10,7 @@ set(CMAKE_CXX_STANDARD 17)
1010
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1111

1212
add_compile_definitions(PROGRAM_NAME="PerformanceMeasurer")
13-
add_compile_definitions(VERSION="0.5")
13+
add_compile_definitions(VERSION="0.6")
1414
add_compile_definitions(AUTHOR="Brayan MS")
1515
add_compile_definitions(THIS_PROGRAM_URL="https://github.com/brookiestein/PerformanceMeasurer")
1616
add_compile_definitions(OTHER_PROJECTS="https://github.com/brookiestein?tab=repositories")
@@ -30,6 +30,9 @@ set(PROJECT_SOURCES
3030
chart.hpp
3131
chart.cpp
3232
chart.ui
33+
calldetails.hpp
34+
calldetails.cpp
35+
calldetails.ui
3336
${TS_FILES}
3437
)
3538

calldetails.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "calldetails.hpp"
2+
#include "ui_calldetails.h"
3+
4+
CallDetails::CallDetails(Database& db, const QString& datetime, QWidget* parent) :
5+
QWidget(parent)
6+
, m_ui(new Ui::CallDetails)
7+
, m_db(db)
8+
, m_datetime(datetime)
9+
{
10+
m_ui->setupUi(this);
11+
m_tw = m_ui->tableWidget;
12+
13+
setWindowIcon(QIcon("assets/icon.ico"));
14+
prepareTableWidget();
15+
setCalls();
16+
}
17+
18+
CallDetails::~CallDetails()
19+
{
20+
delete m_ui;
21+
}
22+
23+
void CallDetails::closeEvent(QCloseEvent* event)
24+
{
25+
emit closed();
26+
QWidget::closeEvent(event);
27+
}
28+
29+
void CallDetails::prepareTableWidget()
30+
{
31+
QStringList headers;
32+
headers << "Calls" << "Date" << "Time";
33+
34+
m_tw->setColumnCount(headers.size());
35+
m_tw->setSelectionMode(QAbstractItemView::SingleSelection);
36+
m_tw->setSelectionBehavior(QAbstractItemView::SelectItems);
37+
m_tw->setEditTriggers(QTableWidget::NoEditTriggers);
38+
m_tw->setHorizontalHeaderLabels(headers);
39+
}
40+
41+
void CallDetails::setCalls()
42+
{
43+
if (not m_db.open()) {
44+
QMessageBox::critical(this, PROGRAM_NAME, tr("Database couldn't be opened."));
45+
return;
46+
}
47+
48+
auto statement = QString("SELECT calls, time FROM Calls WHERE date='%1'").arg(m_datetime);
49+
if (not m_db.exec(statement)) {
50+
QMessageBox::critical(this, PROGRAM_NAME, tr("Couldn't execute statement."));
51+
QMessageBox::critical(this, PROGRAM_NAME, m_db.query().lastError().text());
52+
m_db.close();
53+
return;
54+
}
55+
56+
auto& query { m_db.query() };
57+
auto record { query.record() };
58+
int callsID { record.indexOf("calls") };
59+
int timeID { record.indexOf("time") };
60+
61+
while (query.next()) {
62+
auto calls { query.value(callsID).toString() };
63+
auto time { query.value(timeID).toString() };
64+
auto rowCount { m_tw->rowCount() };
65+
66+
m_tw->insertRow(rowCount);
67+
m_tw->setItem(rowCount, 0, new QTableWidgetItem(calls));
68+
m_tw->setItem(rowCount, 1, new QTableWidgetItem(m_datetime));
69+
m_tw->setItem(rowCount, 2, new QTableWidgetItem(time));
70+
}
71+
72+
m_db.close();
73+
}

calldetails.hpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef CALLDETAILS_HPP
2+
#define CALLDETAILS_HPP
3+
4+
#include <QCloseEvent>
5+
#include <QIcon>
6+
#include <QMessageBox>
7+
#include <QStringList>
8+
#include <QTableWidget>
9+
#include <QTableWidgetItem>
10+
#include <QWidget>
11+
12+
#include "database.hpp"
13+
14+
namespace Ui { class CallDetails; }
15+
16+
class CallDetails : public QWidget
17+
{
18+
Q_OBJECT
19+
Ui::CallDetails *m_ui;
20+
QTableWidget* m_tw;
21+
Database& m_db;
22+
QString m_datetime;
23+
24+
void prepareTableWidget();
25+
void setCalls();
26+
public:
27+
CallDetails(Database& db, const QString& datetime, QWidget* parent = nullptr);
28+
~CallDetails();
29+
protected:
30+
void closeEvent(QCloseEvent* event);
31+
signals:
32+
void closed();
33+
};
34+
35+
#endif // CALLDETAILS_HPP

calldetails.ui

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>CallDetails</class>
4+
<widget class="QWidget" name="CallDetails">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>680</width>
10+
<height>340</height>
11+
</rect>
12+
</property>
13+
<property name="minimumSize">
14+
<size>
15+
<width>680</width>
16+
<height>340</height>
17+
</size>
18+
</property>
19+
<property name="windowTitle">
20+
<string>Today's Registered Calls</string>
21+
</property>
22+
<widget class="QTableWidget" name="tableWidget">
23+
<property name="geometry">
24+
<rect>
25+
<x>20</x>
26+
<y>50</y>
27+
<width>641</width>
28+
<height>271</height>
29+
</rect>
30+
</property>
31+
</widget>
32+
<widget class="QLabel" name="label">
33+
<property name="geometry">
34+
<rect>
35+
<x>210</x>
36+
<y>20</y>
37+
<width>271</width>
38+
<height>31</height>
39+
</rect>
40+
</property>
41+
<property name="font">
42+
<font>
43+
<pointsize>12</pointsize>
44+
<bold>true</bold>
45+
</font>
46+
</property>
47+
<property name="text">
48+
<string>Today's Registered Calls</string>
49+
</property>
50+
<property name="alignment">
51+
<set>Qt::AlignCenter</set>
52+
</property>
53+
</widget>
54+
</widget>
55+
<resources/>
56+
<connections/>
57+
</ui>

chart.cpp

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Chart::Chart(Database& db, const QString& username, QWidget* parent) :
3838
setRenderHint(QPainter::Antialiasing);
3939

4040
connect(&m_set, &QBarSet::hovered, this, &Chart::hovered);
41+
connect(&m_set, &QBarSet::clicked, this, &Chart::clicked);
4142
}
4243

4344
Chart::~Chart()
@@ -53,52 +54,38 @@ void Chart::setValues()
5354
}
5455

5556
QDate currentDate { QDate::currentDate() };
56-
auto today { currentDate.toString("dd-MM-yyyy") };
57-
auto oneWeekBefore { currentDate.addDays(-7).toString("dd-MM-yyyy") };
58-
auto statement = QString("SELECT calls, date FROM Calls WHERE date BETWEEN '%1' AND '%2'").arg(oneWeekBefore, today);
59-
if (not m_db.exec(statement)) {
60-
m_db.close();
61-
emit error(tr("[%1]: Couldn't execute statement.").arg(Q_FUNC_INFO));
62-
return;
63-
}
64-
65-
auto& query { m_db.query() };
66-
auto record { query.record() };
67-
int callsID { record.indexOf("calls") };
68-
int dateID { record.indexOf("date") };
69-
70-
//: If we're starting the week, don't show any other day.
71-
if (query.first()) {
72-
int day { currentDate.dayOfWeek() };
73-
if (day == 1) {
74-
int calls {};
75-
int dbDay { QDate::fromString(query.value(dateID).toString()).dayOfWeek() };
76-
do {
77-
calls = query.value(callsID).toInt();
78-
} while (day != dbDay);
79-
m_set << calls;
80-
m_db.close();
81-
return;
82-
}
83-
} else { // Nothing else to do.
84-
return;
85-
}
57+
int dayOfWeek { currentDate.dayOfWeek() };
58+
// We're going to iterate until daysToSubstract gets to -1.
59+
// We want to start adding calls from Monday (0) to today.
60+
int daysToSubstract { dayOfWeek - 1 };
8661

8762
for (int i {}; i < 6; ++i)
8863
m_set << 1;
8964

90-
query.previous();
91-
while (query.next()) {
92-
auto dateStr { query.value(dateID).toString() };
93-
auto date { QDate::fromString(dateStr, "dd-MM-yyyy") };
94-
int index { date.dayOfWeek() };
95-
96-
int calls { query.value(callsID).toInt() };
97-
qDebug() << tr("Setting barset #%1 to %2.").arg(QString::number(index - 1), QString::number(calls));
98-
//: If we're in the same day of week, but it isn't actually today, don't show that set until we setValue().
99-
if (date.dayOfWeek() == currentDate.dayOfWeek() and date != QDate::currentDate())
100-
calls = 0;
65+
while (daysToSubstract != -1) {
66+
auto date { currentDate.addDays(-daysToSubstract).toString("dd-MM-yyyy") };
67+
auto statement = QString("SELECT MAX(calls) AS calls FROM Calls WHERE date = '%1'").arg(date);
68+
if (not m_db.exec(statement)) {
69+
qCritical() << "Couldn't execute statement:" << statement;
70+
if (daysToSubstract == 0) {
71+
break;
72+
} else {
73+
--daysToSubstract;
74+
continue;
75+
}
76+
}
77+
78+
auto& query { m_db.query() };
79+
auto record { query.record() };
80+
if (not query.first() and daysToSubstract == 0)
81+
break;
82+
83+
int callsID { record.indexOf("calls") };
84+
auto calls { query.value(callsID).toInt() };
85+
86+
int index { dayOfWeek - daysToSubstract-- };
10187
m_set.replace(index - 1, calls);
88+
m_dates[index - 1] = date;
10289
}
10390

10491
for (int i {}; i < m_set.count(); ++i)
@@ -111,7 +98,7 @@ void Chart::setValues()
11198
void Chart::setValue(int day, int value)
11299
{
113100
m_set.replace(day, value);
114-
repaint();
101+
update();
115102
}
116103

117104
void Chart::hovered(bool status, int index)
@@ -145,3 +132,13 @@ void Chart::hovered(bool status, int index)
145132

146133
emit updateHoveredLabel(tr("%1 registered calls on %2.").arg(calls, dayName));
147134
}
135+
136+
void Chart::clicked(int index)
137+
{
138+
auto datetime { m_dates[index] };
139+
CallDetails cd(m_db, datetime);
140+
QEventLoop loop;
141+
connect(&cd, &CallDetails::closed, &loop, &QEventLoop::quit);
142+
cd.show();
143+
loop.exec();
144+
}

chart.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
#include <QChart>
88
#include <QChartView>
99
#include <QDate>
10+
#include <QEventLoop>
1011
#include <QIcon>
12+
#include <QMap>
1113
#include <QPainter>
1214
#include <QStringList>
1315
#include <QSqlRecord>
1416
#include <QValueAxis>
1517
#include <QWidget>
1618

19+
#include "calldetails.hpp"
1720
#include "database.hpp"
1821

1922
namespace Ui { class Chart; }
@@ -31,6 +34,7 @@ class Chart : public QChartView
3134
QBarSet m_set;
3235
QStringList m_categories;
3336
Database& m_db;
37+
QMap<quint8, QString> m_dates;
3438

3539
void setValues();
3640
public:
@@ -42,6 +46,7 @@ class Chart : public QChartView
4246
void updateHoveredLabel(const QString& label);
4347
private slots:
4448
void hovered(bool status, int index);
49+
void clicked(int index);
4550
};
4651

4752
#endif // CHART_HPP

database.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Database::Database(QObject *parent)
44
: QObject{parent}
5+
, m_dbFilename("pm_db.sqlite3")
56
{
67
m_db = QSqlDatabase::addDatabase("QSQLITE");
78
m_query = QSqlQuery(m_db);
@@ -20,7 +21,7 @@ void Database::setDBEnvironmentUp()
2021
{
2122
auto actualDir { QStandardPaths::displayName(QStandardPaths::StandardLocation::DocumentsLocation) };
2223
auto parentDir = QString("%1%2%3%4%5").arg(QDir::homePath(), QDir::separator(), actualDir, QDir::separator(), PROGRAM_NAME);
23-
auto fullPath = QString("%1%2%3").arg(parentDir, QDir::separator(), "calls.sqlite3");
24+
auto fullPath = QString("%1%2%3").arg(parentDir, QDir::separator(), m_dbFilename);
2425

2526
if (QDir dir(parentDir); not dir.exists()) {
2627
dir.mkdir(parentDir);
@@ -39,7 +40,8 @@ void Database::createDB()
3940
QString statement = "CREATE TABLE IF NOT EXISTS Calls (\
4041
id INTEGER PRIMARY KEY AUTOINCREMENT,\
4142
calls INTERGER NOT NULL,\
42-
date DATETIME NOT NULL UNIQUE,\
43+
date DATETIME NOT NULL,\
44+
time DATETIME NOT NULL,\
4345
username TEXT NOT NULL)";
4446

4547
if (not m_query.exec(statement)) {

database.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Database : public QObject
1414
Q_OBJECT
1515
QSqlDatabase m_db;
1616
QSqlQuery m_query;
17+
const QString m_dbFilename;
1718

1819
void setDBEnvironmentUp();
1920
void createDB();

0 commit comments

Comments
 (0)