Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Habbit1
Submodule Habbit1 added at f605a8
157 changes: 157 additions & 0 deletions src/DBLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ DBLayer::DBLayer(std::string db_file_name) : db_file_name_(std::move(db_file_nam
qDebug() << "UserSettingsTable 创建成功";
}

// 创建 PomodoroStateTable 用于存储番茄钟状态
QString pomodoroStateSql = "CREATE TABLE IF NOT EXISTS PomodoroStateTable ("
"userId INTEGER PRIMARY KEY, "
"state INTEGER, "
"totalSeconds INTEGER, "
"remainingSeconds INTEGER, "
"remark TEXT, "
"startTime TEXT)";
if (!query.exec(pomodoroStateSql))
{
qDebug() << "PomodoroStateTable 创建失败:" << query.lastError().text();
}
else
{
qDebug() << "PomodoroStateTable 创建成功";
}

// 构造函数中初始化完后关闭数据库
closeDatabase();
}
Expand Down Expand Up @@ -816,3 +833,143 @@ bool DBLayer::setActiveHabit(std::size_t habit_id)
closeDatabase();
return true;
}

bool DBLayer::savePomodoroState(int state, int total_seconds, int remaining_seconds, const std::string& remark, const std::string& start_time)
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法保存番茄钟状态";
return false;
}

std::size_t currentUserId = getCurrentUserID();
if (currentUserId == 0)
{
// 如果没有登录用户,使用默认用户ID 1
currentUserId = 1;
//qDebug() << "没有当前登录用户,使用默认用户ID:" << currentUserId;
}

// 确保数据库连接仍然打开
if (!db_.isOpen()) {
//qDebug() << "数据库连接已关闭,重新打开";
if (!openDatabase()) {
//qDebug() << "重新打开数据库失败";
return false;
}
}

QSqlQuery query(db_);
query.prepare("INSERT OR REPLACE INTO PomodoroStateTable "
"(userId, state, totalSeconds, remainingSeconds, remark, startTime) "
"VALUES (:userId, :state, :totalSeconds, :remainingSeconds, :remark, :startTime)");

query.bindValue(":userId", static_cast<int>(currentUserId));
query.bindValue(":state", state);
query.bindValue(":totalSeconds", total_seconds);
query.bindValue(":remainingSeconds", remaining_seconds);
query.bindValue(":remark", QString::fromStdString(remark));
query.bindValue(":startTime", QString::fromStdString(start_time));

if (!query.exec())
{
qDebug() << "保存番茄钟状态失败:" << query.lastError().text();
closeDatabase();
return false;
}

closeDatabase();
return true;
}

bool DBLayer::loadPomodoroState(int& state, int& total_seconds, int& remaining_seconds, std::string& remark, std::string& start_time)
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法加载番茄钟状态";
return false;
}

std::size_t currentUserId = getCurrentUserID();
if (currentUserId == 0)
{
// 如果没有登录用户,使用默认用户ID 1
currentUserId = 1;
//qDebug() << "没有当前登录用户,使用默认用户ID:" << currentUserId;
}

// 确保数据库连接仍然打开
if (!db_.isOpen()) {
//qDebug() << "数据库连接已关闭,重新打开";
if (!openDatabase()) {
//qDebug() << "重新打开数据库失败";
return false;
}
}

QSqlQuery query(db_);
query.prepare("SELECT state, totalSeconds, remainingSeconds, remark, startTime "
"FROM PomodoroStateTable WHERE userId = :userId");
query.bindValue(":userId", static_cast<int>(currentUserId));

if (!query.exec())
{
qDebug() << "加载番茄钟状态失败:" << query.lastError().text();
closeDatabase();
return false;
}

if (query.next())
{
state = query.value("state").toInt();
total_seconds = query.value("totalSeconds").toInt();
remaining_seconds = query.value("remainingSeconds").toInt();
remark = query.value("remark").toString().toStdString();
start_time = query.value("startTime").toString().toStdString();
closeDatabase();
return true;
}

closeDatabase();
return false; // 没有找到状态记录
}

bool DBLayer::clearPomodoroState()
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法清除番茄钟状态";
return false;
}

std::size_t currentUserId = getCurrentUserID();
if (currentUserId == 0)
{
// 如果没有登录用户,使用默认用户ID 1
currentUserId = 1;
//qDebug() << "没有当前登录用户,使用默认用户ID:" << currentUserId;
}

// 确保数据库连接仍然打开
if (!db_.isOpen()) {
//qDebug() << "数据库连接已关闭,重新打开";
if (!openDatabase()) {
//qDebug() << "重新打开数据库失败";
return false;
}
}

QSqlQuery query(db_);
query.prepare("DELETE FROM PomodoroStateTable WHERE userId = :userId");
query.bindValue(":userId", static_cast<int>(currentUserId));

if (!query.exec())
{
qDebug() << "清除番茄钟状态失败:" << query.lastError().text();
closeDatabase();
return false;
}

closeDatabase();
return true;
}
31 changes: 31 additions & 0 deletions src/DBLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,37 @@ class DBLayer
* @details 从数据库中启用指定ID的习惯
*/
bool setActiveHabit(std::size_t habit_id);

/**
* @brief 保存番茄钟状态到数据库
* @author 遥远
* @param state 番茄钟状态 (0=IDLE, 1=RUNNING, 2=PAUSED)
* @param total_seconds 总秒数
* @param remaining_seconds 剩余秒数
* @param remark 备注
* @param start_time 开始时间戳
* @return 保存是否成功
*/
bool savePomodoroState(int state, int total_seconds, int remaining_seconds, const std::string& remark, const std::string& start_time);

/**
* @brief 从数据库加载番茄钟状态
* @author 遥远
* @param state 输出:番茄钟状态
* @param total_seconds 输出:总秒数
* @param remaining_seconds 输出:剩余秒数
* @param remark 输出:备注
* @param start_time 输出:开始时间戳
* @return 加载是否成功
*/
bool loadPomodoroState(int& state, int& total_seconds, int& remaining_seconds, std::string& remark, std::string& start_time);

/**
* @brief 清除番茄钟状态
* @author 遥远
* @return 清除是否成功
*/
bool clearPomodoroState();
};

#endif // DBLAYER_H
23 changes: 15 additions & 8 deletions src/PomodoroWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,26 +500,33 @@ QString PomodoroWidget::getTimeDisplayText() const
return formatTime(remaining_seconds_);
}

void PomodoroWidget::restoreState(int state, int total_seconds, int remaining_seconds, const QString& remark, const QString& /*start_time*/)
void PomodoroWidget::restoreState(int state, int total_seconds, int remaining_seconds, const QString& remark, const QString& start_time)
{
if (timer_) timer_->stop();
state_ = static_cast<State>(state);
total_seconds_ = total_seconds;
remaining_seconds_ = remaining_seconds;
remaining_seconds_ = remaining_seconds; // 这里已经是计算好的剩余时间
remark_ = remark;
start_time_ = QTime::currentTime(); // 仅用于兼容,不参与倒计时计算

if (state_ == IDLE) {
control_button_->setText("▶");
time_edit_->setText("00 : 00 : 00");
time_edit_->show();
time_display_->hide();
} else {
control_button_->setText(state_ == RUNNING ? "⏸" : "▶");
time_edit_->hide();
time_display_->show();
time_display_->setText(formatTime(remaining_seconds_));
if (state_ == RUNNING) timer_->start();
// 对于运行中或暂停的状态
if (state_ == RUNNING) {
control_button_->setText("⏸");
time_edit_->hide();
time_display_->show();
time_display_->setText(formatTime(remaining_seconds_));
timer_->start();
} else if (state_ == PAUSED) {
control_button_->setText("▶");
time_edit_->hide();
time_display_->show();
time_display_->setText(formatTime(remaining_seconds_));
}
}
updateRemarkDisplay();
update();
Expand Down
15 changes: 15 additions & 0 deletions src/ServiceLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,19 @@ QJsonObject ServiceLayer::getThemeConfig(const QString &theme_name)

const QJsonDocument doc = QJsonDocument::fromJson(data);
return doc.object();
}

bool ServiceLayer::savePomodoroState(int state, int total_seconds, int remaining_seconds, const std::string& remark, const std::string& start_time)
{
return db_layer.savePomodoroState(state, total_seconds, remaining_seconds, remark, start_time);
}

bool ServiceLayer::loadPomodoroState(int& state, int& total_seconds, int& remaining_seconds, std::string& remark, std::string& start_time)
{
return db_layer.loadPomodoroState(state, total_seconds, remaining_seconds, remark, start_time);
}

bool ServiceLayer::clearPomodoroState()
{
return db_layer.clearPomodoroState();
}
8 changes: 8 additions & 0 deletions src/ServiceLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ class ServiceLayer
* @return 主题配置的JSON对象
*/
static QJsonObject getThemeConfig(const QString &theme_name);

bool savePomodoroState(int state, int total_seconds, int remaining_seconds, const std::string &remark,
const std::string &start_time);

bool loadPomodoroState(int &state, int &total_seconds, int &remaining_seconds, std::string &remark,
std::string &start_time);

bool clearPomodoroState();
};

inline const QString ServiceLayer::THEMES_PATH = ":/themes/themes.json"; // 主题列表配置文件路径
Expand Down
2 changes: 1 addition & 1 deletion src/SimpleAuthSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void SimpleAuthSystem::sendHttpRequest(const QString &endpoint, const QString &d

statusLabel->setText("正在发送请求...");

connect(process, &QProcess::finished, this, [=](int exitCode, QProcess::ExitStatus exitStatus)
connect(process, &QProcess::finished, this, [=, this](int exitCode, QProcess::ExitStatus exitStatus)
{
QString response = QString::fromUtf8(process->readAllStandardOutput());
QString error = QString::fromUtf8(process->readAllStandardError());
Expand Down
Loading