-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtexedit.cpp
129 lines (111 loc) · 3.34 KB
/
texedit.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
#include "texedit.h"
#include "ui_texedit.h"
TexEdit::TexEdit(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::TexEdit)
{
ui->setupUi(this);
this->setCentralWidget(ui->u_Text);
// connect the ui buttons with their functions
connect(ui->actionNew, &QAction::triggered, this, &TexEdit::newFile);
connect(ui->actionOpen, &QAction::triggered, this, &TexEdit::open);
connect(ui->actionSave, &QAction::triggered, this, &TexEdit::save);
connect(ui->actionSave_As, &QAction::triggered, this, &TexEdit::saveAs);
connect(ui->actionQuit, &QAction::triggered, this, &TexEdit::quit);
connect(ui->actionUndo, &QAction::triggered, this, &TexEdit::undo);
connect(ui->actionRedo, &QAction::triggered, this, &TexEdit::redo);
connect(ui->actionFont, &QAction::triggered, this, &TexEdit::changeFont);
connect(ui->actionAbout, &QAction::triggered, this, &TexEdit::about);
// shortcuts
ui->actionNew->setShortcut(QKeySequence("Ctrl+n"));
ui->actionOpen->setShortcut(QKeySequence("Ctrl+o"));
ui->actionSave->setShortcut(QKeySequence("Ctrl+s"));
ui->actionSave_As->setShortcut(QKeySequence("Ctrl+Shift+s"));
ui->actionUndo->setShortcut(QKeySequence("Ctrl+z"));
ui->actionRedo->setShortcut(QKeySequence("Ctrl+y"));
}
TexEdit::~TexEdit()
{
delete ui;
}
void TexEdit::newFile() {
currentFile.clear();
ui->u_Text->setText(QString());
}
void TexEdit::save()
{
QString fileName;
// If we don't have a filename from before, get one.
if (currentFile.isEmpty()) {
fileName = QFileDialog::getSaveFileName(this, "Save");
currentFile = fileName;
}
else {
fileName = currentFile;
}
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
return;
}
setWindowTitle(fileName);
QTextStream out(&file);
QString text = ui->u_Text->toPlainText();
out << text;
file.close();
}
void TexEdit::quit()
{
QCoreApplication::quit();
}
void TexEdit::open()
{
QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
QFile file(fileName);
currentFile = fileName;
if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, "Warning", "Cannot open file: " + file.errorString());
return;
}
setWindowTitle(fileName);
QTextStream in(&file);
QString text = in.readAll();
ui->u_Text->setText(text);
file.close();
}
void TexEdit::saveAs()
{
QString fileName = QFileDialog::getSaveFileName(this, "Save as");
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
return;
}
currentFile = fileName;
setWindowTitle(fileName);
QTextStream out(&file);
QString text = ui->u_Text->toPlainText();
out << text;
file.close();
}
void TexEdit::undo()
{
ui->u_Text->undo();
}
void TexEdit::redo()
{
ui->u_Text->redo();
}
void TexEdit::changeFont()
{
bool fontSelected;
QFont font = QFontDialog::getFont(&fontSelected, this);
if (fontSelected)
ui->u_Text->setFont(font);
}
void TexEdit::about()
{
QMessageBox::about(this, "About",
"TODO Message"
);
}