From cf0bfa7fe2fc055d67ef586ae12a061a2cc93d13 Mon Sep 17 00:00:00 2001 From: kalle Date: Sun, 15 Nov 2020 16:26:59 +0100 Subject: [PATCH] Initial commit. --- .gitignore | 2 ++ CMakeLists.txt | 20 ++++++++++++ src/MainWindow.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++ src/MainWindow.h | 44 ++++++++++++++++++++++++++ src/Structs.h | 14 +++++++++ src/TimerThread.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++ src/TimerThread.h | 34 ++++++++++++++++++++ src/config.h | 12 ++++++++ src/main.cpp | 20 ++++++++++++ 9 files changed, 296 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 src/MainWindow.cpp create mode 100644 src/MainWindow.h create mode 100644 src/Structs.h create mode 100644 src/TimerThread.cpp create mode 100644 src/TimerThread.h create mode 100644 src/config.h create mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae6b19c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/cmake-build-debug/ +/.idea/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..11ab6e7 --- /dev/null +++ b/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp new file mode 100644 index 0000000..e627854 --- /dev/null +++ b/src/MainWindow.cpp @@ -0,0 +1,75 @@ +// +// Created by kalle on 10/30/20. +// + +#include +#include +#include +#include +#include +#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(); +} diff --git a/src/MainWindow.h b/src/MainWindow.h new file mode 100644 index 0000000..800dffd --- /dev/null +++ b/src/MainWindow.h @@ -0,0 +1,44 @@ +// +// Created by kalle on 10/30/20. +// + +#ifndef TIMER_MAINWINDOW_H +#define TIMER_MAINWINDOW_H + + +#include +#include +#include +#include +#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 diff --git a/src/Structs.h b/src/Structs.h new file mode 100644 index 0000000..555394b --- /dev/null +++ b/src/Structs.h @@ -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 diff --git a/src/TimerThread.cpp b/src/TimerThread.cpp new file mode 100644 index 0000000..8a55af3 --- /dev/null +++ b/src/TimerThread.cpp @@ -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); + } +} + + + + diff --git a/src/TimerThread.h b/src/TimerThread.h new file mode 100644 index 0000000..e342eef --- /dev/null +++ b/src/TimerThread.h @@ -0,0 +1,34 @@ +// +// Created by kalle on 11/15/20. +// + +#ifndef TIMER_TIMERTHREAD_H +#define TIMER_TIMERTHREAD_H + + +#include +#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 diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..67848c2 --- /dev/null +++ b/src/config.h @@ -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 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..4b1e10e --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,20 @@ +#include +#include +#include "MainWindow.h" + +int main(int argc, char *argv[]) { + if (argc != 4) { + std::cout << "Usage: Timer " << std::endl; + return 1; + } + + qRegisterMetaType("TimeData"); + + QApplication app(argc, argv); + MainWindow window; + window.show(); + + return app.exec(); +} + +