-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cpp
131 lines (109 loc) · 3.24 KB
/
MainWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "MainWindow.h"
#include "MyTextEdit.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QFontDialog>
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
this->setCentralWidget(ui->tabWidget);
}
MainWindow::~MainWindow() {
delete ui;
}
// 新建文件
void MainWindow::on_new_file_triggered() {
// this->currentFile.clear();
// this->ui->textEdit->setText("");
auto *myTextEdit = new MyTextEdit(this);
this->ui->tabWidget->addTab(myTextEdit, "newFile.txt");
}
// 打开文件
void MainWindow::on_open_file_triggered() {
QString fileName = QFileDialog::getOpenFileName(this, "打开文件");
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, "警告", "文件无法打开" + file.errorString());
return;
}
this->currentFile = fileName;
qDebug() << "file name: " << fileName;
setWindowTitle(fileName);
QTextStream in(&file);
this->ui->textEdit->setPlainText(in.readAll());
file.close();
}
// 保存文件
void MainWindow::on_save_file_triggered() {
if (currentFile.isEmpty()) {
currentFile = QFileDialog::getSaveFileName(this, "保存文件");
}
QFile file(currentFile);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "警告", "无法保存文件:" + file.errorString());
return;
}
setWindowTitle(currentFile);
QTextStream out(&file);
out << this->ui->textEdit->toPlainText();
file.close();
}
// 另存为
void MainWindow::on_save_as_triggered() {
QString fileName = QFileDialog::getSaveFileName(this, "另存为");
QFile file(fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "警告", "无法保存文件:" + file.errorString());
return;
}
setWindowTitle(fileName);
QTextStream out(&file);
out << this->ui->textEdit->toPlainText();
file.close();
}
void MainWindow::on_paste_triggered() {
this->ui->textEdit->paste();
}
void MainWindow::on_cut_triggered() {
this->ui->textEdit->cut();
}
void MainWindow::on_copy_triggered() {
this->ui->textEdit->copy();
}
// 粗体
void MainWindow::on_bolder_triggered(bool bolder) {
this->ui->textEdit->setFontWeight(bolder ? QFont::Bold : QFont::Normal);
}
// 斜体
void MainWindow::on_italic_triggered(bool italic) {
this->ui->textEdit->setFontItalic(italic);
}
// 下划线
void MainWindow::on_underline_triggered(bool underline) {
this->ui->textEdit->setFontUnderline(underline);
}
// 撤销
void MainWindow::on_undo_triggered() {
this->ui->textEdit->undo();
}
// 取消撤销
void MainWindow::on_redo_triggered() {
this->ui->textEdit->redo();
}
// 字体
void MainWindow::on_font_triggered() {
bool fontSelected;
QFont font = QFontDialog::getFont(&fontSelected, this);
if (fontSelected) {
this->ui->textEdit->setFont(font);
}
}
// 关于
void MainWindow::on_about_triggered() {
QMessageBox::about(this, "关于", "这是我的文本编辑器,欢迎学习和使用!");
}
// 退出
void MainWindow::on_exit_triggered() {
QCoreApplication::exit();
}