refactor: 新建 saveBook() 函数

This commit is contained in:
Calcitem 2019-09-28 11:38:15 +08:00
parent 9612c014cd
commit dd669091a0
2 changed files with 41 additions and 41 deletions

View File

@ -398,6 +398,35 @@ void MillGameWindow::ruleInfo()
#endif #endif
} }
void MillGameWindow::saveBook(const QString &path)
{
if (path.isEmpty()) {
return;
}
if (file.isOpen()) {
file.close();
}
// 文件对象
file.setFileName(path);
// 打开文件,只写方式打开
if (!(file.open(QFileDevice::WriteOnly | QFileDevice::Text))) {
return;
}
// 写文件
QTextStream textStream(&file);
auto *strlist = qobject_cast<QStringListModel *>(ui.listView->model());
for (const QString &cmd : strlist->stringList()) {
textStream << cmd << endl;
}
file.flush();
}
void MillGameWindow::on_actionLimited_T_triggered() void MillGameWindow::on_actionLimited_T_triggered()
{ {
/* /*
@ -513,25 +542,17 @@ void MillGameWindow::actionRules_triggered()
void MillGameWindow::on_actionNew_N_triggered() void MillGameWindow::on_actionNew_N_triggered()
{ {
if (file.isOpen())
file.close();
#ifdef SAVE_GAMEBOOK_WHEN_ACTION_NEW_TRIGGERED #ifdef SAVE_GAMEBOOK_WHEN_ACTION_NEW_TRIGGERED
QString path = QDir::currentPath() + "/" + tr("book_") + QString::number(QDateTime::currentDateTimeUtc().toTime_t()) + ".txt"; QString path = QDir::currentPath()
auto *strlist = qobject_cast<QStringListModel*>(ui.listView->model()); + "/" + tr("book_")
+ QString::number(QDateTime::currentDateTimeUtc().toTime_t())
+ ".txt";
if (!path.isEmpty() && strlist->stringList().size() > 18) { // 下了一定步数之后新建游戏时才保存棋谱
// 文件对象 auto *strlist = qobject_cast<QStringListModel *>(ui.listView->model());
file.setFileName(path);
// 打开文件,只写方式打开 if (strlist->stringList().size() > 18) {
if (file.open(QFileDevice::WriteOnly | QFileDevice::Text)) { saveBook(path);
// 写文件
QTextStream textStream(&file);
for (const QString &cmd : strlist->stringList())
textStream << cmd << endl;
file.flush();
}
} }
#endif /* SAVE_GAMEBOOK_WHEN_ACTION_NEW_TRIGGERED */ #endif /* SAVE_GAMEBOOK_WHEN_ACTION_NEW_TRIGGERED */
@ -635,31 +656,7 @@ void MillGameWindow::on_actionSaveAs_A_triggered()
tr("打开棋谱文件"), tr("打开棋谱文件"),
QDir::currentPath() + tr("棋谱_") + QString::number(QDateTime::currentDateTimeUtc().toTime_t())+ ".txt", "TXT(*.txt)"); QDir::currentPath() + tr("棋谱_") + QString::number(QDateTime::currentDateTimeUtc().toTime_t())+ ".txt", "TXT(*.txt)");
if (path.isEmpty()) { saveBook(path);
return;
}
if (file.isOpen()) {
file.close();
}
// 文件对象
file.setFileName(path);
// 打开文件,只写方式打开
if (!(file.open(QFileDevice::WriteOnly | QFileDevice::Text))) {
return;
}
// 写文件
QTextStream textStream(&file);
auto *strlist = qobject_cast<QStringListModel*>(ui.listView->model());
for (const QString &cmd : strlist->stringList()) {
textStream << cmd << endl;
}
file.flush();
} }
void MillGameWindow::on_actionEdit_E_toggled(bool arg1) void MillGameWindow::on_actionEdit_E_toggled(bool arg1)

View File

@ -106,6 +106,9 @@ private slots:
void on_actionWeb_W_triggered(); void on_actionWeb_W_triggered();
void on_actionAbout_A_triggered(); void on_actionAbout_A_triggered();
protected:
void saveBook(const QString &path);
private: private:
// 界面文件 // 界面文件
Ui::MillGameWindowClass ui {}; Ui::MillGameWindowClass ui {};