Initial commit.

master
kalle 2020-11-15 16:26:59 +01:00
commit cf0bfa7fe2
9 changed files with 296 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/cmake-build-debug/
/.idea/

20
CMakeLists.txt Normal file
View File

@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.17)
project(Timer)
set(CMAKE_CXX_STANDARD 14)
# Find includes in the build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Turn on automatic invocation of the MOC, UIC & RCC
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
find_package(Qt5 REQUIRED COMPONENTS Widgets)
add_executable(Timer src/main.cpp src/MainWindow.cpp src/MainWindow.h src/TimerThread.cpp src/TimerThread.h src/Structs.h src/config.h)
target_link_libraries(Timer Qt5::Widgets)

75
src/MainWindow.cpp Normal file
View File

@ -0,0 +1,75 @@
//
// Created by kalle on 10/30/20.
//
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QLabel>
#include <iomanip>
#include <iostream>
#include <QtCore/QCoreApplication>
#include "MainWindow.h"
#include "TimerThread.h"
#include "config.h"
MainWindow::MainWindow() {
centralWidget = new QWidget();
layout = new QVBoxLayout();
label = new QLabel();
labelStyle = new QString("font: 40pt; color: %1;");
labelString = new QString("%1:%2:%3");
label->setStyleSheet(labelStyle->arg(PAUSED_COLOR));
layout->addWidget(label);
this->setWindowFlag(Qt::FramelessWindowHint, true);
this->setAttribute(Qt::WA_TranslucentBackground);
centralWidget->setLayout(layout);
this->setCentralWidget(centralWidget);
this->timerThread = new TimerThread(QCoreApplication::arguments().at(1).toInt(),
QCoreApplication::arguments().at(2).toInt(),
QCoreApplication::arguments().at(3).toInt());
connect(timerThread, &TimerThread::timeUpdated, this, &MainWindow::onTimeUpdated);
connect(timerThread, &TimerThread::changeColor, this, &MainWindow::onChangeColor);
connect(this, &MainWindow::pauseTimer, timerThread, &TimerThread::onPauseChanged);
connect(this, &MainWindow::resetTimer, timerThread, &TimerThread::onReset);
timerThread->start();
}
MainWindow::~MainWindow() {
this->close();
}
void MainWindow::on_actionExit_triggered() {
this->close();
}
void MainWindow::onTimeUpdated(TimeData timeData) {
this->label->setText(QString::fromStdString(padStringLeft(std::to_string(timeData.hours), 2) + ":" + padStringLeft(std::to_string(timeData.minutes), 2) + ":" + padStringLeft(std::to_string(timeData.seconds), 2)));
}
void MainWindow::onChangeColor(char* color) {
this->label->setStyleSheet(labelStyle->arg(color));
}
void MainWindow::mousePressEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
emit pauseTimer();
event->accept();
} else if (event->button() == Qt::RightButton){
emit resetTimer();
event->accept();
}
}
std::string MainWindow::padStringLeft(const std::string& string, int amount) {
std::stringstream ss;
ss << std::setfill('0') << std::setw (amount) << string;
return ss.str();
}

44
src/MainWindow.h Normal file
View File

@ -0,0 +1,44 @@
//
// Created by kalle on 10/30/20.
//
#ifndef TIMER_MAINWINDOW_H
#define TIMER_MAINWINDOW_H
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QLabel>
#include <QMouseEvent>
#include "TimerThread.h"
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow();
~MainWindow() override;
void mousePressEvent(QMouseEvent *event);
private:
QWidget *centralWidget;
QVBoxLayout *layout;
QLabel *label;
TimerThread *timerThread;
QString *labelStyle;
QString *labelString;
private slots:
void on_actionExit_triggered();
void onTimeUpdated(TimeData);
void onChangeColor(char*);
static std::string padStringLeft(const std::string&, int);
signals:
void pauseTimer();
void resetTimer();
};
#endif //TIMER_MAINWINDOW_H

14
src/Structs.h Normal file
View File

@ -0,0 +1,14 @@
//
// Created by kalle on 11/15/20.
//
#ifndef TIMER_STRUCTS_H
#define TIMER_STRUCTS_H
struct TimeData {
int seconds;
int minutes;
int hours;
};
#endif //TIMER_STRUCTS_H

75
src/TimerThread.cpp Normal file
View File

@ -0,0 +1,75 @@
//
// Created by kalle on 11/15/20.
//
#include "TimerThread.h"
#include "config.h"
TimerThread::TimerThread(int hours, int minutes, int seconds) {
initialTimeData.seconds = seconds;
initialTimeData.minutes = minutes;
initialTimeData.hours = hours;
timeDate.seconds = seconds;
timeDate.minutes = minutes;
timeDate.hours = hours;
paused = true;
}
void TimerThread::onPauseChanged() {
paused = !paused;
if (paused)
emit changeColor(PAUSED_COLOR);
else
emit changeColor(RUNNING_COLOR);
}
void TimerThread::onReset() {
paused = true;
emit changeColor(PAUSED_COLOR);
timeDate.seconds = initialTimeData.seconds;
timeDate.minutes = initialTimeData.minutes;
timeDate.hours = initialTimeData.hours;
emit timeUpdated(timeDate);
}
[[noreturn]] void TimerThread::run() {
while (true) {
emit timeUpdated(timeDate);
sleep(1);
if (paused)
continue;
if (timeDate.seconds != 0) {
timeDate.seconds--;
continue;
}
timeDate.seconds = 59;
if (timeDate.minutes != 0) {
timeDate.minutes--;
continue;
}
timeDate.minutes = 59;
if (timeDate.hours != 0) {
timeDate.hours--;
continue;
}
paused = true;
timeDate.seconds = 0;
timeDate.minutes = 0;
timeDate.hours = 0;
emit changeColor(DONE_COLOR);
}
}

34
src/TimerThread.h Normal file
View File

@ -0,0 +1,34 @@
//
// Created by kalle on 11/15/20.
//
#ifndef TIMER_TIMERTHREAD_H
#define TIMER_TIMERTHREAD_H
#include <QtCore/QThread>
#include "Structs.h"
class TimerThread : public QThread {
Q_OBJECT
public:
explicit TimerThread(int hours, int minutes, int seconds);
[[noreturn]] void run() override;
public slots:
void onPauseChanged();
void onReset();
private:
TimeData initialTimeData{};
TimeData timeDate{};
bool paused;
signals:
void timeUpdated(TimeData);
void changeColor(char*);
};
#endif //TIMER_TIMERTHREAD_H

12
src/config.h Normal file
View File

@ -0,0 +1,12 @@
//
// Created by kalle on 10/30/20.
//
#ifndef TIMER_CONFIG_H
#define TIMER_CONFIG_H
#define PAUSED_COLOR "#B30000"
#define RUNNING_COLOR "#AAAAAA"
#define DONE_COLOR "#40FF00"
#endif //TIMER_CONFIG_H

20
src/main.cpp Normal file
View File

@ -0,0 +1,20 @@
#include <QtWidgets/QApplication>
#include <iostream>
#include "MainWindow.h"
int main(int argc, char *argv[]) {
if (argc != 4) {
std::cout << "Usage: Timer <hours> <minutes> <seconds>" << std::endl;
return 1;
}
qRegisterMetaType<TimeData>("TimeData");
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}