Skip to content

Commit fb181bc

Browse files
Merge pull request #56 from DeepCodingInTuringAcademy/dev-yaoyuan
Merging changes from yaoyuan
2 parents 629aa99 + 9f97039 commit fb181bc

File tree

12 files changed

+719
-77
lines changed

12 files changed

+719
-77
lines changed

Habbit1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit f605a8c944ca265c3e98e1e92d71d6f2d21acc9b

src/DBLayer.cpp

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ DBLayer::DBLayer(std::string db_file_name) : db_file_name_(std::move(db_file_nam
115115
qDebug() << "UserSettingsTable 创建成功";
116116
}
117117

118+
// 创建 PomodoroStateTable 用于存储番茄钟状态
119+
QString pomodoroStateSql = "CREATE TABLE IF NOT EXISTS PomodoroStateTable ("
120+
"userId INTEGER PRIMARY KEY, "
121+
"state INTEGER, "
122+
"totalSeconds INTEGER, "
123+
"remainingSeconds INTEGER, "
124+
"remark TEXT, "
125+
"startTime TEXT)";
126+
if (!query.exec(pomodoroStateSql))
127+
{
128+
qDebug() << "PomodoroStateTable 创建失败:" << query.lastError().text();
129+
}
130+
else
131+
{
132+
qDebug() << "PomodoroStateTable 创建成功";
133+
}
134+
118135
// 构造函数中初始化完后关闭数据库
119136
closeDatabase();
120137
}
@@ -816,3 +833,143 @@ bool DBLayer::setActiveHabit(std::size_t habit_id)
816833
closeDatabase();
817834
return true;
818835
}
836+
837+
bool DBLayer::savePomodoroState(int state, int total_seconds, int remaining_seconds, const std::string& remark, const std::string& start_time)
838+
{
839+
if (!openDatabase())
840+
{
841+
qDebug() << "数据库打开失败,无法保存番茄钟状态";
842+
return false;
843+
}
844+
845+
std::size_t currentUserId = getCurrentUserID();
846+
if (currentUserId == 0)
847+
{
848+
// 如果没有登录用户,使用默认用户ID 1
849+
currentUserId = 1;
850+
//qDebug() << "没有当前登录用户,使用默认用户ID:" << currentUserId;
851+
}
852+
853+
// 确保数据库连接仍然打开
854+
if (!db_.isOpen()) {
855+
//qDebug() << "数据库连接已关闭,重新打开";
856+
if (!openDatabase()) {
857+
//qDebug() << "重新打开数据库失败";
858+
return false;
859+
}
860+
}
861+
862+
QSqlQuery query(db_);
863+
query.prepare("INSERT OR REPLACE INTO PomodoroStateTable "
864+
"(userId, state, totalSeconds, remainingSeconds, remark, startTime) "
865+
"VALUES (:userId, :state, :totalSeconds, :remainingSeconds, :remark, :startTime)");
866+
867+
query.bindValue(":userId", static_cast<int>(currentUserId));
868+
query.bindValue(":state", state);
869+
query.bindValue(":totalSeconds", total_seconds);
870+
query.bindValue(":remainingSeconds", remaining_seconds);
871+
query.bindValue(":remark", QString::fromStdString(remark));
872+
query.bindValue(":startTime", QString::fromStdString(start_time));
873+
874+
if (!query.exec())
875+
{
876+
qDebug() << "保存番茄钟状态失败:" << query.lastError().text();
877+
closeDatabase();
878+
return false;
879+
}
880+
881+
closeDatabase();
882+
return true;
883+
}
884+
885+
bool DBLayer::loadPomodoroState(int& state, int& total_seconds, int& remaining_seconds, std::string& remark, std::string& start_time)
886+
{
887+
if (!openDatabase())
888+
{
889+
qDebug() << "数据库打开失败,无法加载番茄钟状态";
890+
return false;
891+
}
892+
893+
std::size_t currentUserId = getCurrentUserID();
894+
if (currentUserId == 0)
895+
{
896+
// 如果没有登录用户,使用默认用户ID 1
897+
currentUserId = 1;
898+
//qDebug() << "没有当前登录用户,使用默认用户ID:" << currentUserId;
899+
}
900+
901+
// 确保数据库连接仍然打开
902+
if (!db_.isOpen()) {
903+
//qDebug() << "数据库连接已关闭,重新打开";
904+
if (!openDatabase()) {
905+
//qDebug() << "重新打开数据库失败";
906+
return false;
907+
}
908+
}
909+
910+
QSqlQuery query(db_);
911+
query.prepare("SELECT state, totalSeconds, remainingSeconds, remark, startTime "
912+
"FROM PomodoroStateTable WHERE userId = :userId");
913+
query.bindValue(":userId", static_cast<int>(currentUserId));
914+
915+
if (!query.exec())
916+
{
917+
qDebug() << "加载番茄钟状态失败:" << query.lastError().text();
918+
closeDatabase();
919+
return false;
920+
}
921+
922+
if (query.next())
923+
{
924+
state = query.value("state").toInt();
925+
total_seconds = query.value("totalSeconds").toInt();
926+
remaining_seconds = query.value("remainingSeconds").toInt();
927+
remark = query.value("remark").toString().toStdString();
928+
start_time = query.value("startTime").toString().toStdString();
929+
closeDatabase();
930+
return true;
931+
}
932+
933+
closeDatabase();
934+
return false; // 没有找到状态记录
935+
}
936+
937+
bool DBLayer::clearPomodoroState()
938+
{
939+
if (!openDatabase())
940+
{
941+
qDebug() << "数据库打开失败,无法清除番茄钟状态";
942+
return false;
943+
}
944+
945+
std::size_t currentUserId = getCurrentUserID();
946+
if (currentUserId == 0)
947+
{
948+
// 如果没有登录用户,使用默认用户ID 1
949+
currentUserId = 1;
950+
//qDebug() << "没有当前登录用户,使用默认用户ID:" << currentUserId;
951+
}
952+
953+
// 确保数据库连接仍然打开
954+
if (!db_.isOpen()) {
955+
//qDebug() << "数据库连接已关闭,重新打开";
956+
if (!openDatabase()) {
957+
//qDebug() << "重新打开数据库失败";
958+
return false;
959+
}
960+
}
961+
962+
QSqlQuery query(db_);
963+
query.prepare("DELETE FROM PomodoroStateTable WHERE userId = :userId");
964+
query.bindValue(":userId", static_cast<int>(currentUserId));
965+
966+
if (!query.exec())
967+
{
968+
qDebug() << "清除番茄钟状态失败:" << query.lastError().text();
969+
closeDatabase();
970+
return false;
971+
}
972+
973+
closeDatabase();
974+
return true;
975+
}

src/DBLayer.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,37 @@ class DBLayer
235235
* @details 从数据库中启用指定ID的习惯
236236
*/
237237
bool setActiveHabit(std::size_t habit_id);
238+
239+
/**
240+
* @brief 保存番茄钟状态到数据库
241+
* @author 遥远
242+
* @param state 番茄钟状态 (0=IDLE, 1=RUNNING, 2=PAUSED)
243+
* @param total_seconds 总秒数
244+
* @param remaining_seconds 剩余秒数
245+
* @param remark 备注
246+
* @param start_time 开始时间戳
247+
* @return 保存是否成功
248+
*/
249+
bool savePomodoroState(int state, int total_seconds, int remaining_seconds, const std::string& remark, const std::string& start_time);
250+
251+
/**
252+
* @brief 从数据库加载番茄钟状态
253+
* @author 遥远
254+
* @param state 输出:番茄钟状态
255+
* @param total_seconds 输出:总秒数
256+
* @param remaining_seconds 输出:剩余秒数
257+
* @param remark 输出:备注
258+
* @param start_time 输出:开始时间戳
259+
* @return 加载是否成功
260+
*/
261+
bool loadPomodoroState(int& state, int& total_seconds, int& remaining_seconds, std::string& remark, std::string& start_time);
262+
263+
/**
264+
* @brief 清除番茄钟状态
265+
* @author 遥远
266+
* @return 清除是否成功
267+
*/
268+
bool clearPomodoroState();
238269
};
239270

240271
#endif // DBLAYER_H

src/PomodoroWidget.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -500,26 +500,33 @@ QString PomodoroWidget::getTimeDisplayText() const
500500
return formatTime(remaining_seconds_);
501501
}
502502

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

512511
if (state_ == IDLE) {
513512
control_button_->setText("");
514513
time_edit_->setText("00 : 00 : 00");
515514
time_edit_->show();
516515
time_display_->hide();
517516
} else {
518-
control_button_->setText(state_ == RUNNING ? "" : "");
519-
time_edit_->hide();
520-
time_display_->show();
521-
time_display_->setText(formatTime(remaining_seconds_));
522-
if (state_ == RUNNING) timer_->start();
517+
// 对于运行中或暂停的状态
518+
if (state_ == RUNNING) {
519+
control_button_->setText("");
520+
time_edit_->hide();
521+
time_display_->show();
522+
time_display_->setText(formatTime(remaining_seconds_));
523+
timer_->start();
524+
} else if (state_ == PAUSED) {
525+
control_button_->setText("");
526+
time_edit_->hide();
527+
time_display_->show();
528+
time_display_->setText(formatTime(remaining_seconds_));
529+
}
523530
}
524531
updateRemarkDisplay();
525532
update();

src/ServiceLayer.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,19 @@ QJsonObject ServiceLayer::getThemeConfig(const QString &theme_name)
353353

354354
const QJsonDocument doc = QJsonDocument::fromJson(data);
355355
return doc.object();
356+
}
357+
358+
bool ServiceLayer::savePomodoroState(int state, int total_seconds, int remaining_seconds, const std::string& remark, const std::string& start_time)
359+
{
360+
return db_layer.savePomodoroState(state, total_seconds, remaining_seconds, remark, start_time);
361+
}
362+
363+
bool ServiceLayer::loadPomodoroState(int& state, int& total_seconds, int& remaining_seconds, std::string& remark, std::string& start_time)
364+
{
365+
return db_layer.loadPomodoroState(state, total_seconds, remaining_seconds, remark, start_time);
366+
}
367+
368+
bool ServiceLayer::clearPomodoroState()
369+
{
370+
return db_layer.clearPomodoroState();
356371
}

src/ServiceLayer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,14 @@ class ServiceLayer
241241
* @return 主题配置的JSON对象
242242
*/
243243
static QJsonObject getThemeConfig(const QString &theme_name);
244+
245+
bool savePomodoroState(int state, int total_seconds, int remaining_seconds, const std::string &remark,
246+
const std::string &start_time);
247+
248+
bool loadPomodoroState(int &state, int &total_seconds, int &remaining_seconds, std::string &remark,
249+
std::string &start_time);
250+
251+
bool clearPomodoroState();
244252
};
245253

246254
inline const QString ServiceLayer::THEMES_PATH = ":/themes/themes.json"; // 主题列表配置文件路径

src/SimpleAuthSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void SimpleAuthSystem::sendHttpRequest(const QString &endpoint, const QString &d
8484

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

87-
connect(process, &QProcess::finished, this, [=](int exitCode, QProcess::ExitStatus exitStatus)
87+
connect(process, &QProcess::finished, this, [=, this](int exitCode, QProcess::ExitStatus exitStatus)
8888
{
8989
QString response = QString::fromUtf8(process->readAllStandardOutput());
9090
QString error = QString::fromUtf8(process->readAllStandardError());

0 commit comments

Comments
 (0)