Skip to content

Commit

Permalink
点击分类时日期不同则更新待学习数
Browse files Browse the repository at this point in the history
  • Loading branch information
patchy1-admin committed Sep 4, 2024
1 parent e4e96a8 commit 166f6e6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
50 changes: 44 additions & 6 deletions learningdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ void LearningDialog::set_items_table(QString filter)
if(isSpeedLearn)
ui->comboBox->setCurrentIndex(1);
clear_question_display();
ui->tableView->sortByColumn(0,Qt::AscendingOrder);
on_comboBox_currentTextChanged(ui->comboBox->currentText());
}

void LearningDialog::set_question(int id)
Expand Down Expand Up @@ -177,18 +179,44 @@ void LearningDialog::on_pushButton_clicked()
//两次提交切换题目、生成随机排序数
if(submited)
{
int newCurrentId = tableModel->index(oldRow,0).data().toInt();

//更新随机排序数
if(ui->comboBox->currentText() == "随机排序")
if(ui->comboBox->currentText() == "随机排序" && newCurrentId == currentId)
{
tableModel->index(oldRow,0).data().toInt();
int randNum = QRandomGenerator::global()->bounded(32768);
questionSql->set_data(tableModel->index(oldRow,0).data().toInt(),"orderNum",randNum);
ui->tableView->sortByColumn(21,Qt::AscendingOrder);
questionSql->set_data(newCurrentId,"orderNum",randNum);
tableModel->select();
}else if(ui->comboBox->currentText() == "默认排序" && newCurrentId == currentId)
{
QString state = tableModel->index(oldRow,2).data().toString();
if(state == "learning")
{
int shortTimeCount = 0;
int row = oldRow + 1;
while(shortTimeCount < 30 && row < tableModel->rowCount())
{
QTime gTime = QTime().fromString(tableModel->index(oldRow,5).data().toString(),"mm'm':ss's'");
shortTimeCount += gTime.msecsSinceStartOfDay()/1000;
row++;
}
int orderNum1 = tableModel->index(row - 1,21).data().toInt();
int orderNum2 = 32767;
if(row < tableModel->rowCount())
{
orderNum2 = tableModel->index(row,21).data().toInt();
}
int newOrderNum = (orderNum1 + orderNum2) / 2;
questionSql->set_data(newCurrentId,"orderNum",newOrderNum);
tableModel->select();
}
}

newCurrentId = tableModel->index(oldRow,0).data().toInt();

//切换题目
int newRow = oldRow;
if(tableModel->index(oldRow,0).data().toInt() == currentId)
if(newCurrentId == currentId)
{
newRow = oldRow + 1;
if(newRow >= tableModel->rowCount())
Expand Down Expand Up @@ -344,13 +372,23 @@ void LearningDialog::on_comboBox_currentTextChanged(const QString &arg1)
int rowCount = tableModel->rowCount();
for(int i = 0;i < rowCount;i++)
{
ui->tableView->setSortingEnabled(false);
int randNum = QRandomGenerator::global()->bounded(32768);
int id = tableModel->index(i,0).data().toInt();
questionSql->set_data(id,"orderNum",randNum);
}
ui->tableView->setSortingEnabled(true);
ui->tableView->sortByColumn(21,Qt::AscendingOrder);
}else if(arg1 == "默认排序")
{
int rowCount = tableModel->rowCount();
int gap = 32767 / rowCount;
for(int i = 0;i < rowCount;i++)
{
int id = tableModel->index(i,0).data().toInt();
questionSql->set_data(id,"orderNum",i * gap);
}
ui->tableView->setSortingEnabled(true);
ui->tableView->sortByColumn(21,Qt::AscendingOrder);
}
ui->tableView->clearSelection();
clear_question_display();
Expand Down
27 changes: 25 additions & 2 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ MainWindow::MainWindow(QWidget *parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
currentDate = QDate::currentDate();
questionSql = new QuestionSql("question.db",this);

//init questionTableView
Expand Down Expand Up @@ -116,6 +117,23 @@ void MainWindow::reload_categoryTreeView()
ui->categoryTreeView->hideColumn(1);
}

void MainWindow::update_count_categoryTreeView()
{
QSqlQuery query(QSqlDatabase::database("connection1"));
query.prepare("SELECT * FROM categories");
query.exec();
while(query.next())
{
int id = query.value(0).toInt();
if(id == 0)
continue;
int qCount = questionSql->count_total_questions(id);
int qtoLearnCount = questionSql->count_total_questions_to_learn(id);
categoryItemLists[id][2]->setText(QString::number(qtoLearnCount));
categoryItemLists[id][3]->setText(QString::number(qCount));
}
}

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if (watched == ui->questionTableView) {
Expand Down Expand Up @@ -151,7 +169,7 @@ bool MainWindow::eventFilter(QObject *watched, QEvent *event)
void MainWindow::get_categoryItemTree(QStandardItem* parent,int parentId)
{
QSqlQuery query(QSqlDatabase::database("connection1"));
query.prepare("SELECT * FROM categories WHERE parentId = ?");
query.prepare("SELECT * FROM categories WHERE parentId = ? ORDER BY name");
query.bindValue(0,parentId);
query.exec();
while(query.next())
Expand Down Expand Up @@ -212,6 +230,12 @@ void MainWindow::on_categoryDelButton_clicked()

void MainWindow::on_categoryTreeView_clicked(const QModelIndex &index)
{
if(currentDate != QDate::currentDate())
{
update_count_categoryTreeView();
currentDate = QDate::currentDate();
}

int categoryId = index.siblingAtColumn(1).data().toInt();
QString condString = questionSql->get_category_condString(categoryId);
questionTableModel->setFilter(condString);
Expand Down Expand Up @@ -709,4 +733,3 @@ void MainWindow::on_htmlImgAddButton_clicked()
}
}


2 changes: 2 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class MainWindow : public QMainWindow
void save_answerList(int id);
void set_tableHeader();
void select_question_by_id(int id);
void update_count_categoryTreeView();
protected:
bool eventFilter(QObject *watched, QEvent *event);

Expand Down Expand Up @@ -106,5 +107,6 @@ private slots:
HtmlTableAddDialog *htmlTableAddDialog;
QuestionTagEditDialog *questionTagEditDialog;

QDate currentDate;
};
#endif // MAINWINDOW_H

0 comments on commit 166f6e6

Please sign in to comment.