@@ -1073,100 +1073,70 @@ void MainWindow::on_group_item_clicked(QListWidgetItem *item) {
10731073}
10741074
10751075void MainWindow::on_task_item_clicked (QListWidgetItem *item) {
1076+ if (item != nullptr ) {
1077+ unsigned int task_id = item->data (Qt::UserRole).toUInt ();
10761078
1077- unsigned int task_id;
1078-
1079- if (item != 0x0 ) {
1080- task_id = item->data (Qt::UserRole).toUInt ();
1081- } else {
1082- task_id = 0 ;
1083- }
1084-
1085- if (task_id > 0 ) {
1086- m_task_current_id = 0 ;
1079+ if (task_id > 0 ) {
1080+ m_task_current_id = 0 ;
10871081
1088- if (db_show_task (task_id)) {
1089- m_task_current_id = task_id;
1082+ if (db_show_task (task_id)) {
1083+ m_task_current_id = task_id;
10901084
1091- if (auto_save_status == false ) auto_save ();
1085+ if (auto_save_status == false ) {
1086+ setup_auto_save ();
1087+ auto_save_status = true ;
1088+ }
10921089
1093- ui->task_title ->setCursorPosition (0 );
1094- ui->task_view_frame ->show ();
1090+ ui->task_title ->setCursorPosition (0 );
1091+ ui->task_view_frame ->show ();
1092+ }
10951093 }
10961094 }
1097-
10981095}
10991096
1100- void MainWindow::auto_save () {
1101-
1102- connect (ui->task_title , &QLineEdit::textChanged, [this ]() {
1103-
1104- if (m_task_current_id == 0 ) {
1105- return ;
1106- }
1107-
1108- QDateTime data_time = QDateTime::currentDateTime ();
1109- int timestamp = data_time.toSecsSinceEpoch ();
1110-
1111- // Auto save QTextEdit
1112- QSqlQuery db_query;
1113-
1114- db_query.prepare (" UPDATE task_list SET title = :title, update_time = :update_time WHERE id = :id" );
1115- db_query.bindValue (" :id" , m_task_current_id);
1116- db_query.bindValue (" :title" , ui->task_title ->text ());
1117- db_query.bindValue (" :update_time" , timestamp);
1118-
1119- if (!db_query.exec ())
1120- {
1121- qInfo () << " An error occurred when executing the query related to autosave task title!" ;
1122- } else {
1123- window_update ();
1124-
1125- QDateTime t_update_time;
1126- t_update_time.setSecsSinceEpoch (timestamp);
1127- QString format_update_time = t_update_time.toString (" dd/MM/yyyy hh:mm" );
1128- ui->updated_data ->setText (format_update_time);
1129-
1130- ui->updated_text ->show ();
1131- ui->updated_data ->show ();
1132- }
1133-
1134- });
1135-
1136- connect (ui->task_edit , &QTextEdit::textChanged, [this ]() {
1137-
1138- if (m_task_current_id == 0 ) {
1139- return ;
1140- }
1097+ void MainWindow::setup_auto_save () {
1098+ connect (ui->task_title , &QLineEdit::textChanged, this , &MainWindow::save_task_title);
1099+ connect (ui->task_edit , &QTextEdit::textChanged, this , &MainWindow::save_task_text);
1100+ }
11411101
1142- QDateTime data_time = QDateTime::currentDateTime ();
1143- int timestamp = data_time.toSecsSinceEpoch ();
1102+ void MainWindow::save_task_title () {
1103+ if (m_task_current_id == 0 ) return ;
1104+ save_task_data (" title" , ui->task_title ->text ());
1105+ }
11441106
1145- // Auto save QTextEdit
1146- QSqlQuery db_query;
1107+ void MainWindow::save_task_text () {
1108+ if (m_task_current_id == 0 ) return ;
1109+ save_task_data (" text" , ui->task_edit ->toPlainText ());
1110+ }
11471111
1148- db_query.prepare (" UPDATE task_list SET text = :text, update_time = :update_time WHERE id = :id" );
1149- db_query.bindValue (" :id" , m_task_current_id);
1150- db_query.bindValue (" :text" , ui->task_edit ->toPlainText ());
1151- db_query.bindValue (" :update_time" , timestamp);
1112+ void MainWindow::save_task_data (const QString& column, const QString& value) {
1113+ QDateTime data_time = QDateTime::currentDateTime ();
1114+ int timestamp = data_time.toSecsSinceEpoch ();
11521115
1153- if (!db_query.exec ())
1154- {
1155- qInfo () << " An error occurred when executing the query related to autosave task text!" ;
1156- } else {
1157- QDateTime t_update_time;
1158- t_update_time.setSecsSinceEpoch (timestamp);
1159- QString format_update_time = t_update_time.toString (" dd/MM/yyyy hh:mm" );
1160- ui->updated_data ->setText (format_update_time);
1116+ QSqlQuery db_query;
1117+ QString queryStr = QString (" UPDATE task_list SET %1 = :value, update_time = :update_time WHERE id = :id" ).arg (column);
1118+ db_query.prepare (queryStr);
1119+ db_query.bindValue (" :id" , m_task_current_id);
1120+ db_query.bindValue (" :value" , value);
1121+ db_query.bindValue (" :update_time" , timestamp);
11611122
1162- ui->updated_text ->show ();
1163- ui->updated_data ->show ();
1164- }
1123+ if (db_query.exec ()) {
1124+ update_task_ui_after_save (timestamp);
1125+ } else {
1126+ qWarning () << " An error occurred when executing the query related to autosave task" << column << " !" ;
1127+ }
1128+ }
11651129
1166- });
1130+ void MainWindow::update_task_ui_after_save (int timestamp) {
1131+ window_update ();
11671132
1168- auto_save_status = true ;
1133+ QDateTime t_update_time;
1134+ t_update_time.setSecsSinceEpoch (timestamp);
1135+ QString format_update_time = t_update_time.toString (" dd/MM/yyyy hh:mm" );
11691136
1137+ ui->updated_data ->setText (format_update_time);
1138+ ui->updated_text ->show ();
1139+ ui->updated_data ->show ();
11701140}
11711141
11721142QVector<QMap<QString, QVariant>> MainWindow::get_task_data_by_id (unsigned int task_id) {
0 commit comments