Skip to content
This repository has been archived by the owner on Nov 25, 2023. It is now read-only.

Commit

Permalink
Merge pull request #23 from Hatsushigure:dev
Browse files Browse the repository at this point in the history
0.1.1
  • Loading branch information
Hatsushigure authored Nov 3, 2023
2 parents 4369c59 + ebcdbbe commit 6ed31ef
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 41 deletions.
2 changes: 1 addition & 1 deletion AlarmDelegate.qml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "HeAlarmJs.js" as HeAlarmJs
Item {
property int hour: 10
property int minute: 10
property bool isActive: true
property bool isActive
property string title: "闹铃"
property int activeDays: HeAlarm.All
signal clicked()
Expand Down
2 changes: 1 addition & 1 deletion AlarmModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void AlarmModel::setIsActive(int row, bool isActive)
if (m_data[row].isActive == isActive)
return;
m_data[row].isActive = isActive;
emit dataChanged(index(row), index(row), {AlarmDataRoles::IsActiveRole});
emit dataChanged(index(row), index(row), {AlarmModel::IsActiveRole});
}

void AlarmModel::remove(int row)
Expand Down
8 changes: 8 additions & 0 deletions AlarmPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ Item {
editBtn.visible = true
}
onIsActiveChanged: globalProp.alarmModel.setIsActive(index, isActive)
Connections {
target: globalProp.alarmModel
function onDataChanged(topLeft, bottomRight) {
if (index < topLeft.row || bottomRight.row < index)
return
isActive = globalProp.alarmModel.data(globalProp.alarmModel.index(index, 0), AlarmModel.IsActiveRole)
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
find_package(Qt6 6.5 REQUIRED COMPONENTS Widgets)

qt_standard_project_setup(REQUIRES 6.5)

Expand Down Expand Up @@ -57,6 +58,7 @@ set_target_properties(HeAlarm PROPERTIES

target_link_libraries(HeAlarm
PRIVATE Qt6::Quick
PRIVATE Qt6::Widgets
)

include(GNUInstallDirs)
Expand Down
7 changes: 5 additions & 2 deletions CoreNotifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <QDateTime>
#include "HeAlarm.h"

CoreNotifier::CoreNotifier(const AlarmModel* model, QObject *parent) :
CoreNotifier::CoreNotifier(AlarmModel* model, QObject *parent) :
QObject{parent},
m_model {model}
{
Expand All @@ -23,8 +23,9 @@ void CoreNotifier::restartTimers()
delete pt;
m_timerList.clear();
const auto& data = m_model->rawData();
for (const auto& alm : data)
for (int i = 0; i < data.size(); i++)
{
const auto& alm = data.at(i);
if (!alm.isActive)
continue;
auto current = QDateTime::currentDateTime();
Expand All @@ -38,6 +39,8 @@ void CoreNotifier::restartTimers()
auto timer = new QTimer(this);
m_timerList.push_back(timer);
connect(timer, &QTimer::timeout, this, [this, alm]() {emit this->alarmTriggered(QVariant::fromValue(alm));});
if (alm.activeDays == 0)
connect(timer, &QTimer::timeout, m_model, [this, i]() {this->m_model->setIsActive(i, false);});
timer->setInterval(duration);
timer->setSingleShot(true);
timer->start();
Expand Down
4 changes: 2 additions & 2 deletions CoreNotifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class CoreNotifier : public QObject
{
Q_OBJECT
private:
const AlarmModel* m_model;
AlarmModel* m_model;
QList<QTimer*> m_timerList;
public:
explicit CoreNotifier(const AlarmModel* model, QObject *parent = nullptr);
explicit CoreNotifier(AlarmModel* model, QObject *parent = nullptr);
signals:
void alarmTriggered(QVariant data);
private slots:
Expand Down
7 changes: 6 additions & 1 deletion HeAlarm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#include "AlarmFileManager.h"
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <format>

HeAlarmApp* HeAlarm::s_app = nullptr;
QQmlApplicationEngine* HeAlarm::s_qmlEngine = nullptr;
QObject* HeAlarm::s_trayIcon = nullptr;
AlarmFileManager* HeAlarm::s_alarmFileManager = nullptr;
AlarmModel* HeAlarm::s_alarmModel = nullptr;
CoreNotifier* HeAlarm::s_notifier = nullptr;
Expand All @@ -14,6 +14,11 @@ TrayNotificationSender* HeAlarm::s_notificationSender = nullptr;
HeAlarm::HeAlarm(QObject* parent)
: QObject {parent} {}

QString HeAlarm::versionString()
{
return QString::fromStdString(std::format("{}.{}.{}", s_versionMajor, s_versionMinor, s_versionPatch));
}

HeAlarmApp* HeAlarm::app()
{
return s_app;
Expand Down
6 changes: 5 additions & 1 deletion HeAlarm.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ class HeAlarm : public QObject
enum DayOfWeek {Monday = 0x1, Tuesday = 0x2, Wednesday = 0x4, Thursday = 0x8, Friday = 0x10, Saturday = 0x20, Sunday = 0x40, Weekday = 0x1F, Weekend = 0x60, All = 0x7F};
Q_ENUM(DayOfWeek)
private:
const static auto s_versionMajor = 0;
const static auto s_versionMinor = 1;
const static auto s_versionPatch = 1;
private:
static HeAlarmApp* s_app;
static QQmlApplicationEngine* s_qmlEngine;
static QObject* s_trayIcon;
static AlarmFileManager* s_alarmFileManager;
static AlarmModel* s_alarmModel;
static CoreNotifier* s_notifier;
Expand All @@ -31,6 +34,7 @@ class HeAlarm : public QObject
public:
explicit HeAlarm(QObject* parent = nullptr);
public:
static QString versionString();
static HeAlarmApp* app();
static QQmlApplicationEngine* qmlEngine();
static AlarmFileManager* alarmFileManager();
Expand Down
43 changes: 31 additions & 12 deletions HeAlarmApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "HeAlarm.h"
#include "TrayNotificationSender.h"
#include <QQmlApplicationEngine>
#include <QIcon>
#include <QPixmap>

HeAlarmApp::HeAlarmApp(int argc, char** argv) :
QGuiApplication {argc, argv}
Expand All @@ -14,16 +16,33 @@ HeAlarmApp::HeAlarmApp(int argc, char** argv) :

void HeAlarmApp::init()
{
HeAlarm::s_app = this;
HeAlarm::s_qmlEngine = new QQmlApplicationEngine;
HeAlarm::s_alarmModel = new AlarmModel;
HeAlarm::s_alarmFileManager = new AlarmFileManager(HeAlarm::alarmModel());
HeAlarm::s_notifier = new CoreNotifier(HeAlarm::alarmModel());
QObject::connect(HeAlarm::qmlEngine(), &QQmlApplicationEngine::objectCreationFailed,
HeAlarm::app(), []() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
HeAlarm::qmlEngine()->loadFromModule("HeAlarm", "Main");
HeAlarm::s_trayIcon = HeAlarm::qmlEngine()->rootObjects().at(0)->findChild<QObject*>("sysTrayIcon");
HeAlarm::s_notificationSender = new TrayNotificationSender(HeAlarm::s_trayIcon);
connect(HeAlarm::notifier(), &CoreNotifier::alarmTriggered, HeAlarm::s_notificationSender, &TrayNotificationSender::sendNotification);
fillAppInfo();
HeAlarm::s_app = this;
HeAlarm::s_qmlEngine = new QQmlApplicationEngine;
HeAlarm::s_alarmModel = new AlarmModel;
HeAlarm::s_alarmFileManager = new AlarmFileManager(HeAlarm::alarmModel());
HeAlarm::s_notifier = new CoreNotifier(HeAlarm::alarmModel());
HeAlarm::s_notificationSender = new TrayNotificationSender;
connect(HeAlarm::notifier(), &CoreNotifier::alarmTriggered, HeAlarm::s_notificationSender, &TrayNotificationSender::sendNotification);
QObject::connect(HeAlarm::qmlEngine(), &QQmlApplicationEngine::objectCreationFailed,
HeAlarm::app(), []() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
HeAlarm::qmlEngine()->loadFromModule("HeAlarm", "Main");
}

void HeAlarmApp::fillAppInfo()
{
setApplicationName("HeAlarm");
setApplicationDisplayName("HeAlarm");
setApplicationVersion(HeAlarm::versionString());
setOrganizationName("初時雨");
setOrganizationDomain("Hatsushigure.github.io");
auto pix = QPixmap(":/res/Logo.png");
auto icon = QIcon();
icon.addPixmap(pix);
icon.addPixmap(pix.scaled({64, 64}, Qt::KeepAspectRatio, Qt::SmoothTransformation));
icon.addPixmap(pix.scaled({48, 48}, Qt::KeepAspectRatio, Qt::SmoothTransformation));
icon.addPixmap(pix.scaled({32, 32}, Qt::KeepAspectRatio, Qt::SmoothTransformation));
icon.addPixmap(pix.scaled({16, 16}, Qt::KeepAspectRatio, Qt::SmoothTransformation));
setWindowIcon(QIcon(pix));
}
1 change: 1 addition & 0 deletions HeAlarmApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ class HeAlarmApp: public QGuiApplication
HeAlarmApp(int argc, char** argv);
private:
void init();
void fillAppInfo();
};

12 changes: 1 addition & 11 deletions Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import QtQuick.Controls.Material as Controls
Window {
width: 640; height: 724
visible: true
title: qsTr("HeAlarm 0.1.0")
color: "#f1f1f1"
minimumWidth: swpView.currentItem.implicitWidth; minimumHeight: 600

Expand All @@ -25,9 +24,7 @@ Window {
currentIndex: nvgBar.currentIndex
onCurrentIndexChanged: nvgBar.currentIndex = currentIndex

ClockPage {

}
ClockPage {}

AlarmPage {
editPage: editPge
Expand All @@ -48,11 +45,4 @@ Window {
visible: false
onCancled: visible = false
}

Platform.SystemTrayIcon {
objectName: "sysTrayIcon"
icon.source: "qrc:///res/Logo.png"
tooltip: "HeAlarm 0.1.0"
visible: true
}
}
16 changes: 8 additions & 8 deletions TrayNotificationSender.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include "TrayNotificationSender.h"
#include "AlarmData.h"
#include <QIcon>
#include <QSystemTrayIcon>

TrayNotificationSender::TrayNotificationSender(QObject* trayIcon, QObject *parent) :
QObject{parent},
m_trayIcon {trayIcon}
TrayNotificationSender::TrayNotificationSender(QObject *parent) :
QObject{parent}
{

m_trayIcon = new QSystemTrayIcon;
m_trayIcon->setIcon(QIcon(":/res/Logo.png"));
m_trayIcon->setToolTip("HeAlarm 0.1.1");
m_trayIcon->show();
}

void TrayNotificationSender::sendNotification(const QVariant& var)
{
auto alm = var.value<AlarmData>();
QMetaObject::invokeMethod(m_trayIcon, "showMessage",
alm.title,
QTime(alm.hour, alm.minute).toString("HH:mm")
);
m_trayIcon->showMessage(alm.title, QTime(alm.hour, alm.minute).toString("HH:mm"), m_trayIcon->icon(), 600000);
}
6 changes: 4 additions & 2 deletions TrayNotificationSender.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#pragma once
#include <QObject>

class QSystemTrayIcon;

class TrayNotificationSender : public QObject
{
Q_OBJECT
private:
static constexpr int duration = 600000;
private:
QObject* m_trayIcon;
QSystemTrayIcon* m_trayIcon;
public:
explicit TrayNotificationSender(QObject* trayIcon, QObject *parent = nullptr);
explicit TrayNotificationSender(QObject *parent = nullptr);
public slots:
void sendNotification(const QVariant& var);
};
Expand Down

0 comments on commit 6ed31ef

Please sign in to comment.