build: 🎉 begin!

This commit is contained in:
ivaquero
2023-07-30 12:34:38 +08:00
commit 7e0c3d136e
11 changed files with 1772 additions and 0 deletions
+1135
View File
File diff suppressed because it is too large Load Diff
+25
View File
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.5)
project(clock)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_PREFIX_PATH "/opt/homebrew/opt/qt@5")
find_package(Qt5 COMPONENTS Core Gui Widgets REQUIRED)
include_directories(
.
)
set (SRCS
clock.cpp
clock.h
main.cpp
)
add_executable(${CMAKE_PROJECT_NAME} ${SRCS})
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Qt5::Core Qt5::Gui Qt5::Widgets)
+39
View File
@@ -0,0 +1,39 @@
# ⏰ Floating Clock
![code size](https://img.shields.io/github/languages/code-size/ivaquero/floating-clock.svg)
![repo size](https://img.shields.io/github/repo-size/ivaquero/floating-clock.svg)
This project aims to build a cross-platform floating clock based on PySide6 & Qt5.
## Motivation
When coding in full screen mode, I often forget the time.
When on macOS / Linux, the clock on the menu bar is too small to be noticeable, so is the clock on Windows.
![clock](clock.png)
## Roadmap
- [ ] Main Window
- [x] Frameless
- [x] Drag and Move
- [x] Right Click Menu
- [x] Always On Top
- [ ] Show in Fullscreen Mode
- [x] Font
- [x] Set Color
- [x] Set Background Color
- [x] Change Size
- [x] Configuratin File
- [x] Read Settings
- [x] Write Settings
- [x] Reset Settings
- [ ] Time
- [ ] Select Time Zone
- [ ] Set Alarms
## For Testing
```python
python -m pip install -r requirements.txt
```
+169
View File
@@ -0,0 +1,169 @@
#include "clock.h"
Clock::Clock(QWidget *parent) : QWidget{parent}, pressed(false) {
// geometry of Main Window (x, y, width, height)
setGeometry(1100, 800, 180, 60);
// hide frame
setWindowFlag(Qt::WindowStaysOnTopHint, true);
setWindowFlag(Qt::FramelessWindowHint, true);
setAttribute(Qt::WA_TranslucentBackground);
// clock object
QTimer *clock = new QTimer(this);
connect(clock, &QTimer::timeout, this, &Clock::showTime);
clock->start(1000); // update the clock per second
show();
settings = new QSettings(
QString("%1/config.ini").arg(QCoreApplication::applicationDirPath()),
QSettings::IniFormat);
font = QFont();
font.setFamily(settings->value("USER/FONT_FAMILY").toString());
font.setPointSize(settings->value("USER/FONT_SIZE").toInt());
// load settings
settings = new QSettings(
QString("%1/config.ini").arg(QCoreApplication::applicationDirPath()),
QSettings::IniFormat);
// font object
font = QFont();
font.setFamily(settings->value("DEFAULT/FONT_FAMILY").toString());
font_size_step = settings->value("USER/FONT_SIZE_STEP").toInt();
// label object
label = new QLabel();
label->setAlignment(Qt::AlignCenter);
// layout
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(label);
setLayout(layout);
reloadUI();
}
void Clock::showTime() {
QString current_time = QTime::currentTime().toString("hh:mm");
label->setText(current_time);
}
void Clock::reloadUI() {
font_size = settings->value("USER/FONT_SIZE").toInt();
font.setPointSize(font_size);
label->setFont(font);
label->setStyleSheet(QString("color: %1; background-color: %2")
.arg(settings->value("USER/FONT_COLOR").toString(),
settings->value("USER/BG_COLOR").toString()));
}
void Clock::resetUI() {
QString default_bg_color = settings->value("DEFAULT/BG_COLOR").toString();
QString default_font_color = settings->value("DEFAULT/FONT_COLOR").toString();
QString default_font_size = settings->value("DEFAULT/FONT_SIZE").toString();
settings->setValue("USER/BG_COLOR", default_bg_color);
settings->setValue("USER/FONT_COLOR", default_font_color);
settings->setValue("USER/FONT_SIZE", default_font_size);
reloadUI();
}
void Clock::increaseFontSize() {
font_size += font_size_step;
changeFontSize();
}
void Clock::decreaseFontSize() {
font_size -= font_size_step;
changeFontSize();
}
void Clock::changeFontSize() {
font.setPointSize(font_size);
label->setFont(font);
}
void Clock::setFontColor(const QString &color) {
settings->setValue("USER/FONT_COLOR", color);
reloadUI();
}
void Clock::setBackgroundColor(const QString &color) {
settings->setValue("USER/BG_COLOR", color);
reloadUI();
}
void Clock::mousePressEvent(QMouseEvent *event) {
pressed = true;
oldPos = event->pos();
QWidget::mousePressEvent(event);
}
void Clock::mouseMoveEvent(QMouseEvent *event) {
if (pressed) {
move(pos() + (event->pos() - oldPos));
}
QWidget::mouseMoveEvent(event);
}
void Clock::mouseReleaseEvent(QMouseEvent *event) {
pressed = false;
oldPos = event->pos();
QWidget::mouseReleaseEvent(event);
}
void Clock::mouseRightMenu(const QPoint &pos) {
QMenu menu(this);
// font size
QMenu *font_size_menu = menu.addMenu("font size");
QAction *larger_action = new QAction("larger", this);
QAction *smaller_action = new QAction("smaller", this);
connect(larger_action, &QAction::triggered, this, &Clock::increaseFontSize);
connect(smaller_action, &QAction::triggered, this, &Clock::decreaseFontSize);
font_size_menu->addAction(larger_action);
font_size_menu->addAction(smaller_action);
QStringList ft_colors = {"none", "blue", "orange", "green",
"red", "yellow", "white"};
QStringList bg_colors = {"none", "blue", "orange", "green",
"red", "yellow", "white"};
// font color
QMenu *font_color_menu = menu.addMenu("font color");
QList<QAction *> font_color_actions;
for (const QString &color : ft_colors) {
QAction *action = new QAction(color, this);
connect(action, &QAction::triggered, [=]() { setFontColor(color); });
font_color_actions.append(action);
}
font_color_menu->addActions(font_color_actions);
// background color
QMenu *bg_color_menu = menu.addMenu("bg color");
QList<QAction *> bg_color_actions;
for (const QString &color : bg_colors) {
QAction *action = new QAction(color, this);
connect(action, &QAction::triggered, [=]() { setBackgroundColor(color); });
bg_color_actions.append(action);
}
bg_color_menu->addActions(bg_color_actions);
// reset
QAction *reset_action = new QAction("reset", this);
connect(reset_action, &QAction::triggered, this, &Clock::resetUI);
menu.addAction(reset_action);
// quit
QAction *quit_action = new QAction("quit", this);
connect(quit_action, &QAction::triggered, this, &Clock::close);
menu.addAction(quit_action);
menu.exec(mapToGlobal(pos));
}
+53
View File
@@ -0,0 +1,53 @@
#ifndef CLOCK_H
#define CLOCK_H
#include <QApplication>
#include <QLabel>
#include <QLocale>
#include <QMenu>
#include <QMouseEvent>
#include <QPoint>
#include <QSettings>
#include <QTime>
#include <QTimer>
#include <QTranslator>
#include <QVBoxLayout>
#include <QWidget>
class Clock : public QWidget {
Q_OBJECT
public:
explicit Clock(QWidget *parent = nullptr);
void reloadUI();
void resetUI();
void increaseFontSize();
void decreaseFontSize();
void changeFontSize();
void setFontColor(const class QString &color);
void setBackgroundColor(const class QString &color);
protected:
void mousePressEvent(QMouseEvent *event);
// void mousePressEvent(QMouseEvent *event) ;
void mouseMoveEvent(QMouseEvent *event);
// void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
// void mouseReleaseEvent(QMouseEvent *event) ;
void mouseRightMenu(const QPoint &pos);
signals:
public slots:
void showTime();
private:
bool pressed;
QPoint oldPos;
QSettings *settings;
QFont font;
int font_size_step;
int font_size;
QLabel *label;
};
#endif // CLOCK_H
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

+166
View File
@@ -0,0 +1,166 @@
import sys
from PySide6.QtCore import QPoint, QSettings, Qt, QTime, QTimer
from PySide6.QtGui import QAction, QFont, QMouseEvent
from PySide6.QtWidgets import QApplication, QLabel, QMenu, QVBoxLayout, QWidget
class Clock(QWidget):
def __init__(self, parent=None) -> None:
super().__init__(parent)
# geometry of Mmin window (x, y, width, height)
self.setGeometry(1100, 800, 180, 60)
# hide frame
self.setWindowFlag(Qt.WindowStaysOnTopHint, True)
self.setWindowFlag(Qt.FramelessWindowHint, True)
self.setAttribute(Qt.WA_TranslucentBackground)
# mouse & menu
self.pressed = False
self.oldPos = QPoint(0, 0)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.mouseRightMenu)
# clock object
clock = QTimer(self)
clock.timeout.connect(self.showTime)
clock.start(1000) # update the clock per second
self.show()
self.settings = QSettings(f"{sys.argv[0]}/../config.ini", QSettings.IniFormat)
font = QFont()
font.setFamily(self.settings.value("USER/FONT_FAMILY"))
font.setPointSize(int(self.settings.value("USER/FONT_SIZE")))
# load settings
self.settings = QSettings(f"{sys.argv[0]}/../config.ini", QSettings.IniFormat)
# # font object
self.font = QFont()
self.font.setFamily(self.settings.value("DEFAULT/FONT_FAMILY"))
self.font_size_step = int(self.settings.value("USER/FONT_SIZE_STEP"))
# label object
self.label = QLabel()
self.label.setAlignment(Qt.AlignCenter)
# layout
layout = QVBoxLayout(self, spacing=0)
layout.addWidget(self.label)
self.setLayout(layout)
self.reloadUI()
def reloadUI(self):
self.font_size = int(self.settings.value("USER/FONT_SIZE"))
self.font.setPointSize(self.font_size)
self.label.setFont(self.font)
self.label.setStyleSheet(
f"color: {self.settings.value('USER/FONT_COLOR')}; "
f"background-color: {self.settings.value('USER/BG_COLOR')}"
)
def resetUI(self):
default_bg_color = self.settings.value("DEFAULT/BG_COLOR")
default_font_color = self.settings.value("DEFAULT/FONT_COLOR")
default_font_size = self.settings.value("DEFAULT/FONT_SIZE")
self.settings.setValue("USER/BG_COLOR", default_bg_color)
self.settings.setValue("USER/FONT_COLOR", default_font_color)
self.settings.setValue("USER/FONT_SIZE", default_font_size)
self.reloadUI()
def increaseFontSize(self):
self.font_size += self.font_size_step
self.changeFontSize()
def decreaseFontSize(self):
self.font_size -= self.font_size_step
self.changeFontSize()
def changeFontSize(self):
font = self.label.font()
font.setPointSize(self.font_size)
self.label.setFont(font)
def setFontColor(self, color: str) -> None:
self.settings.setValue("USER/FONT_COLOR", color)
self.reloadUI()
def setBackgroundColor(self, color: str) -> None:
self.settings.setValue("USER/BG_COLOR", color)
self.reloadUI()
def mousePressEvent(self, event: QMouseEvent) -> None:
self.pressed = True
self.oldPos = event.pos()
super().mousePressEvent(event)
def mouseMoveEvent(self, event: QMouseEvent) -> None:
if self.pressed:
self.move(self.pos() + (event.pos() - self.oldPos))
super().mouseMoveEvent(event)
def mouseReleaseEvent(self, event: QMouseEvent) -> None:
self.pressed = False
self.oldPos = event.pos()
super().mouseReleaseEvent(event)
def mouseRightMenu(self, pos) -> None:
menu = QMenu(self)
# font size
font_size_menu = menu.addMenu("font size")
larger_action = QAction("larger", self)
smaller_action = QAction("smaller", self)
larger_action.triggered.connect(self.increaseFontSize)
smaller_action.triggered.connect(self.decreaseFontSize)
font_size_menu.addAction(larger_action)
font_size_menu.addAction(smaller_action)
ft_colors = ["none", "blue", "orange", "green", "red", "yellow", "white"]
bg_colors = ["none", "blue", "orange", "green", "red", "yellow", "white"]
# font color
font_color_menu = menu.addMenu("font color")
font_color_actions = [
QAction(color, self, triggered=lambda c=color: self.setFontColor(c))
for color in ft_colors
]
font_color_menu.addActions(font_color_actions)
# background color
bg_color_menu = menu.addMenu("bg color")
bg_color_actions = [
QAction(color, self, triggered=lambda c=color: self.setBackgroundColor(c))
for color in bg_colors
]
bg_color_menu.addActions(bg_color_actions)
# reset
reset_action = QAction("reset", self)
reset_action.triggered.connect(self.resetUI)
menu.addAction(reset_action)
# quit
quit_action = QAction("quit", self)
quit_action.triggered.connect(self.close)
menu.addAction(quit_action)
menu.exec(self.mapToGlobal(pos))
def showTime(self):
current_time = QTime.currentTime().toString("hh:mm")
self.label.setText(current_time)
if __name__ == "__main__":
app = QApplication(sys.argv)
clock = Clock()
clock.show() # show all the widgets
app.exit(app.exec()) # start the app
+161
View File
@@ -0,0 +1,161 @@
[
{
"arguments": [
"clang",
"-Wno-documentation-unknown-command",
"-Wno-unknown-warning-option",
"-Wno-unknown-pragmas",
"-nostdinc",
"-nostdinc++",
"-DQT_QML_DEBUG",
"-g",
"-std=gnu++17",
"-arch",
"arm64",
"-isysroot",
"/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk",
"-fPIC",
"-fsyntax-only",
"--target=arm64-apple-darwin22.5.0",
"-DQT_CORE_LIB",
"-DQT_GUI_LIB",
"-DQT_WIDGETS_LIB",
"-DQ_CREATOR_RUN",
"-I/Applications/Qt Creator.app/Contents/Resources/cplusplus/wrappedQtHeaders",
"-I/Applications/Qt Creator.app/Contents/Resources/cplusplus/wrappedQtHeaders/QtCore",
"-I/Users/integzz/Documents/proj-clock/build-clock-Desktop_arm_darwin_generic_mach_o_64bit-Debug/clock_autogen/include",
"-I/opt/homebrew/opt/qt@5/include",
"-I/opt/homebrew/opt/qt@5/include/QtWidgets",
"-I/opt/homebrew/opt/qt@5/include/QtGui",
"-I/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/System/Library/Frameworks/OpenGL.framework/Headers",
"-I/opt/homebrew/opt/qt@5/include/QtCore",
"-I/opt/homebrew/opt/qt@5/include/mkspecs/macx-clang",
"-F",
"/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/System/Library/Frameworks",
"-isystem",
"/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1",
"-isystem",
"/Applications/Qt Creator.app/Contents/Resources/libexec/clang/lib/clang/16/include",
"-isystem",
"/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include",
"-isystem",
"/Library/Developer/CommandLineTools/usr/include",
"-fmessage-length=0",
"-fdiagnostics-show-note-include-stack",
"-fretain-comments-from-system-headers",
"-fmacro-backtrace-limit=0",
"-ferror-limit=1000",
"-x",
"c++",
"/Users/integzz/Documents/proj-clock/main.cpp"
],
"directory": "/usr/bin/clangd",
"file": "/Users/integzz/Documents/proj-clock/main.cpp"
},
{
"arguments": [
"clang",
"-Wno-documentation-unknown-command",
"-Wno-unknown-warning-option",
"-Wno-unknown-pragmas",
"-nostdinc",
"-nostdinc++",
"-DQT_QML_DEBUG",
"-g",
"-std=gnu++17",
"-arch",
"arm64",
"-isysroot",
"/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk",
"-fPIC",
"-fsyntax-only",
"--target=arm64-apple-darwin22.5.0",
"-DQT_CORE_LIB",
"-DQT_GUI_LIB",
"-DQT_WIDGETS_LIB",
"-DQ_CREATOR_RUN",
"-I/Applications/Qt Creator.app/Contents/Resources/cplusplus/wrappedQtHeaders",
"-I/Applications/Qt Creator.app/Contents/Resources/cplusplus/wrappedQtHeaders/QtCore",
"-I/Users/integzz/Documents/proj-clock/build-clock-Desktop_arm_darwin_generic_mach_o_64bit-Debug/clock_autogen/include",
"-I/opt/homebrew/opt/qt@5/include",
"-I/opt/homebrew/opt/qt@5/include/QtWidgets",
"-I/opt/homebrew/opt/qt@5/include/QtGui",
"-I/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/System/Library/Frameworks/OpenGL.framework/Headers",
"-I/opt/homebrew/opt/qt@5/include/QtCore",
"-I/opt/homebrew/opt/qt@5/include/mkspecs/macx-clang",
"-F",
"/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/System/Library/Frameworks",
"-isystem",
"/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1",
"-isystem",
"/Applications/Qt Creator.app/Contents/Resources/libexec/clang/lib/clang/16/include",
"-isystem",
"/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include",
"-isystem",
"/Library/Developer/CommandLineTools/usr/include",
"-fmessage-length=0",
"-fdiagnostics-show-note-include-stack",
"-fretain-comments-from-system-headers",
"-fmacro-backtrace-limit=0",
"-ferror-limit=1000",
"-x",
"c++",
"/Users/integzz/Documents/proj-clock/clock.cpp"
],
"directory": "/usr/bin/clangd",
"file": "/Users/integzz/Documents/proj-clock/clock.cpp"
},
{
"arguments": [
"clang",
"-Wno-documentation-unknown-command",
"-Wno-unknown-warning-option",
"-Wno-unknown-pragmas",
"-nostdinc",
"-nostdinc++",
"-DQT_QML_DEBUG",
"-g",
"-std=gnu++17",
"-arch",
"arm64",
"-isysroot",
"/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk",
"-fPIC",
"-fsyntax-only",
"--target=arm64-apple-darwin22.5.0",
"-DQT_CORE_LIB",
"-DQT_GUI_LIB",
"-DQT_WIDGETS_LIB",
"-DQ_CREATOR_RUN",
"-I/Applications/Qt Creator.app/Contents/Resources/cplusplus/wrappedQtHeaders",
"-I/Applications/Qt Creator.app/Contents/Resources/cplusplus/wrappedQtHeaders/QtCore",
"-I/Users/integzz/Documents/proj-clock/build-clock-Desktop_arm_darwin_generic_mach_o_64bit-Debug/clock_autogen/include",
"-I/opt/homebrew/opt/qt@5/include",
"-I/opt/homebrew/opt/qt@5/include/QtWidgets",
"-I/opt/homebrew/opt/qt@5/include/QtGui",
"-I/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/System/Library/Frameworks/OpenGL.framework/Headers",
"-I/opt/homebrew/opt/qt@5/include/QtCore",
"-I/opt/homebrew/opt/qt@5/include/mkspecs/macx-clang",
"-F",
"/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/System/Library/Frameworks",
"-isystem",
"/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1",
"-isystem",
"/Applications/Qt Creator.app/Contents/Resources/libexec/clang/lib/clang/16/include",
"-isystem",
"/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include",
"-isystem",
"/Library/Developer/CommandLineTools/usr/include",
"-fmessage-length=0",
"-fdiagnostics-show-note-include-stack",
"-fretain-comments-from-system-headers",
"-fmacro-backtrace-limit=0",
"-ferror-limit=1000",
"-x",
"c++-header",
"/Users/integzz/Documents/proj-clock/clock.h"
],
"directory": "/usr/bin/clangd",
"file": "/Users/integzz/Documents/proj-clock/clock.h"
}
]
+13
View File
@@ -0,0 +1,13 @@
[DEFAULT]
BG_COLOR=NONE
FONT_COLOR=blue
FONT_FAMILY=Arial
FONT_SIZE=50
FONT_SIZE_STEP=10
[USER]
BG_COLOR=NONE
FONT_COLOR=blue
FONT_FAMILY=Arial
FONT_SIZE=50
FONT_SIZE_STEP=10
+10
View File
@@ -0,0 +1,10 @@
#include <QApplication>
#include "clock.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
Clock clock;
return app.exec();
}
+1
View File
@@ -0,0 +1 @@
pyside6