Skip to content

Commit

Permalink
新增另存功能
Browse files Browse the repository at this point in the history
  • Loading branch information
LC044 committed Aug 21, 2023
1 parent 2135b39 commit e46fcbf
Show file tree
Hide file tree
Showing 10 changed files with 605 additions and 568 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ set(PROJECT_SOURCES
src/common.h
src/common.cpp
src/menu.cpp
src/global.cpp
${TS_FILES}
${qrc_FILES}
)
Expand Down
1,074 changes: 538 additions & 536 deletions OneMarkdown_zh_CN.ts

Large diffs are not rendered by default.

Binary file added data/icon/btn_source_code.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions data/img.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<file>icon/btn_side_bar_hide.png</file>
<file>icon/btn_side_bar_hover.png</file>
<file>icon/btn_side_bar_show.png</file>
<file>icon/btn_source_code.png</file>
</qresource>
</RCC>
3 changes: 2 additions & 1 deletion include/global.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#ifndef GLOBAL_H
#define GLOBAL_H
#include <string>
#include <QString>
const int TAB_WIDTH = 4;
extern bool IS_FILE_SAVED;
// QString CURRENT_FILE="NULL";
extern QString CURRENT_FILE;
#endif // GLOBAL_H
3 changes: 3 additions & 0 deletions src/OneMarkdown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,6 @@ void OneMarkdown::on_btn_file_list_toggled(bool checked)






2 changes: 2 additions & 0 deletions src/OneMarkdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ private slots:

void on_action_save_file_triggered();

void on_action_save_as_triggered();

private:
int WORD_NUM = 0;
int CHAR_NUM = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/OneMarkdown.ui
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ QToolButton:hover {
<addaction name="action_open_recent_file"/>
<addaction name="separator"/>
<addaction name="action_save_file"/>
<addaction name="action_9"/>
<addaction name="action_save_as"/>
<addaction name="action_10"/>
<addaction name="separator"/>
<addaction name="action_12"/>
Expand Down Expand Up @@ -1013,7 +1013,7 @@ QToolButton:hover {
<string>Ctrl+S</string>
</property>
</action>
<action name="action_9">
<action name="action_save_as">
<property name="text">
<string>另存为...</string>
</property>
Expand Down
3 changes: 3 additions & 0 deletions src/global.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "global.h"
bool IS_FILE_SAVED = false;
QString CURRENT_FILE="";
82 changes: 53 additions & 29 deletions src/menu.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "OneMarkdown.h"
#include "./ui_OneMarkdown.h"
#include "global.h"
bool IS_FILE_SAVED = false;
QString CURRENT_FILE="NULL"

void OneMarkdown::on_action_new_file_triggered()
{
}
Expand All @@ -24,6 +23,7 @@ void OneMarkdown::on_action_open_file_triggered()
// ui->textEdit->append("文件路径:"+filePath);
// 文件信息//
QFileInfo info(filePath);
CURRENT_FILE = filePath;
// ui->textEdit->append(QString("文件大小:%1 byte").arg(info.size()));
// ui->textEdit->append(QString("文件名称:%1").arg(info.fileName()));
// ui->textEdit->append(QString("创建时间:%1").arg(info.created().toString("yyyy-MM-dd hh:mm:ss")));
Expand Down Expand Up @@ -52,44 +52,68 @@ void OneMarkdown::on_action_save_file_triggered()
{
if (!IS_FILE_SAVED) // r如果标记为1,证明有文件加载,不然没有不需要保存
{
QString filePath; // 存储保存路径
filePath = QFileDialog::getSaveFileName(this, "保存");
if (filePath.isEmpty())
{
QMessageBox::information(this, "信息", "保存失败");
}
else
QString filePath; // 存储保存路径
if (CURRENT_FILE.isEmpty())
{
// 内容保存到路径文件
QFile file(filePath);

// 以文本方式打开
if (file.open(QIODevice::WriteOnly | QIODevice::Text))
filePath = QFileDialog::getSaveFileName(this, "保存");
if (filePath.isEmpty())
{
// todo: 增量保存,优化保存性能
QTextStream out(&file); // IO设备对象的地址对其进行初始化

out << ui->textEdit->toPlainText() << endl; // 输出

QMessageBox::information(this, "文件保存成功", QString("文件保存在%2").arg(filePath));

file.close();
IS_FILE_SAVED = true;
QMessageBox::information(this, "信息", "保存失败");
return;
}
else
{
QMessageBox::warning(this, tr("Error"), tr("File to open file!"));
}
// QDir *temp = new QDir; // 声明文件对象
// QMessageBox::information(this, "文件保存成功", QString("文件保存在%2").arg(filePath));
}
else
{
filePath = CURRENT_FILE;
}
// 内容保存到路径文件
QFile file(filePath);
// 以文本方式打开
if (file.open(QIODevice::WriteOnly | QIODevice::Text))
{
// todo: 增量保存,优化保存性能
QTextStream out(&file); // IO设备对象的地址对其进行初始化
out << ui->textEdit->toPlainText() << endl; // 输出
file.close();
IS_FILE_SAVED = true;
}
else
{
QMessageBox::warning(this, tr("Error"), tr("File to open file!"));
}
}
else
{
QMessageBox::information(this, "错误", "保存失败");
}
}

void OneMarkdown::on_action_save_as_triggered()
{
QString filePath; // 存储保存路径
QString filter = "Markdown文档 (*.md);;纯文本文档 (*.txt)";
filePath = QFileDialog::getSaveFileName(this, "保存","Untitled.md",filter);
if (filePath.isEmpty())
{
QMessageBox::information(this, "错误", "未选择文件");
return;
}
// 内容保存到路径文件
QFile file(filePath);
// 以文本方式打开
if (file.open(QIODevice::WriteOnly | QIODevice::Text))
{
// todo: 增量保存,优化保存性能
QTextStream out(&file); // IO设备对象的地址对其进行初始化
out << ui->textEdit->toPlainText() << endl; // 输出
file.close();
IS_FILE_SAVED = true;
}
else
{
QMessageBox::warning(this, tr("Error"), tr("File to open file!"));
}
}
void OneMarkdown::on_action_new_window_triggered()
{
}
Expand Down

0 comments on commit e46fcbf

Please sign in to comment.