Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
LaLa-HaHa-Hei committed Aug 2, 2024
0 parents commit fce2c50
Show file tree
Hide file tree
Showing 14 changed files with 1,018 additions and 0 deletions.
1 change: 1 addition & 0 deletions Path.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$env:Path += ";D:\PortableApp\PortableGit\bin"
44 changes: 44 additions & 0 deletions ScreenTime.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
main.cpp \
mainwindow.cpp \
viewhistorywindow.cpp

HEADERS += \
mainwindow.h \
viewhistorywindow.h

FORMS += \
mainwindow.ui \
viewhistorywindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

LIBS += -luser32 -lpsapi

RESOURCES += \
ScreenTime.qrc

RC_FILE = logo.rc

DISTFILES += \
logo.rc
319 changes: 319 additions & 0 deletions ScreenTime.pro.user

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions ScreenTime.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<RCC>
<qresource prefix="/img">
<file>img/32.ico</file>
<file>img/unknowfile.png</file>
</qresource>
</RCC>
Binary file added img/32.ico
Binary file not shown.
Binary file added img/unknowfile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions logo.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IDI_ICON1 ICON DISCARDABLE ".\\img\\32.ico"
10 changes: 10 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
return a.exec();
}
127 changes: 127 additions & 0 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
today = QDate::currentDate();
//相对路径转绝对路径
QString exeDirPath = QCoreApplication::applicationDirPath().replace('/', '\\');
if (settings.ExeIconFolderPath.startsWith(".\\"))
{
absluteExeIconFolderPath = exeDirPath + settings.ExeIconFolderPath.mid(1);
}
if (settings.JsonDataFolderPath.startsWith(".\\"))
{
absluteJsonDataFolderPath = exeDirPath + settings.JsonDataFolderPath.mid(1);
}
absluteJsonDataFilePath = QString("%1\\%2.json").arg(absluteJsonDataFolderPath, today.toString("yyyy-MM-dd"));
//创建存数据的文件夹
QDir dir(exeDirPath+"\\user_data");
if (!dir.exists())
dir.mkdir(exeDirPath+"\\user_data");
QDir dir1(absluteExeIconFolderPath);
if (!dir1.exists())
dir1.mkdir(absluteExeIconFolderPath);
QDir dir2(absluteJsonDataFolderPath);
if (!dir2.exists())
dir2.mkdir(absluteJsonDataFolderPath);
//是否有今天的数据,如果就就加载
QFile file(absluteJsonDataFilePath);
if (file.exists())
{
QFile file(absluteJsonDataFilePath);
file.open((QIODevice::ReadOnly));
QByteArray jsonData = file.readAll();
file.close();
QJsonDocument jsonDoc(QJsonDocument::fromJson(jsonData));
QJsonArray jsonArray = jsonDoc.array();
for (const QJsonValue& value : jsonArray)
{
QJsonObject jsonObject = value.toObject();
ExeItem* exeItem = new ExeItem();
exeItem->Name = jsonObject["Name"].toString();
exeItem->ExePath = jsonObject["ExePath"].toString();
exeItem->IconPath = jsonObject["IconPath"].toString();
exeItem->Seconds = jsonObject["Seconds"].toInt();
totalSeconds += exeItem->Seconds;
exeItemList.append(exeItem);
}
}
//托盘
InitializeTray();
//Timer绑定
connect(&getTopWindowTimer, &QTimer::timeout, this, &MainWindow::GetTopWindow_slot);
getTopWindowTimer.setInterval(settings.GetTopWindowInterval_s * 1000);
getTopWindowTimer.setTimerType(Qt::PreciseTimer);
getTopWindowTimer.start();
connect(&refreshListWidgetTimer, &QTimer::timeout, this, &MainWindow::RefreshListWidget_slot);
refreshListWidgetTimer.setInterval(settings.RefreshListWidgetIntervalList_s * 1000);
//检测是否开机自启动
QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
if (settings.contains("ScreenTime"))
ui->actionSelfStarting->setChecked(true);
}

MainWindow::~MainWindow()
{
delete ui;
}


void MainWindow::on_actionOpenAppDir_triggered()
{
const wchar_t* wPath = reinterpret_cast<const wchar_t *>(QString("/select,%1").arg(QCoreApplication::applicationFilePath().replace("/", "\\")).utf16());
ShellExecuteW(NULL, L"open", L"explorer.exe", wPath, NULL, SW_SHOW);
}


void MainWindow::on_actionExitApp_triggered(){ExitApp();}

void MainWindow::on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
case QSystemTrayIcon::Trigger:
if (this->isVisible())
{
this->HideWindow();
}
else
{
this->ShowWindow();
}
default:
break;
}
}

void MainWindow::on_actionSelfStarting_triggered()
{
if (!ui->actionSelfStarting->isChecked()) //点击后的状态所以取反
{
//删除注册表
QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
settings.remove("ScreenTime");
}
else
{
//添加注册表
QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
settings.setValue("ScreenTime", '"' + QCoreApplication::applicationFilePath().replace('/', '\\') + '"');
}
}

void MainWindow::on_actionViewHistory_triggered()
{
QString jsonFilePath = QFileDialog::getOpenFileName(nullptr, "选择 JSON 文件",absluteJsonDataFolderPath,"JSON Files (*.json);;All Files (*)");
if (jsonFilePath.isEmpty())
return;
else
{
ViewHistoryWindow* w = new ViewHistoryWindow(jsonFilePath);
w->show();
}
}
Loading

0 comments on commit fce2c50

Please sign in to comment.