-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
174 lines (127 loc) · 3.68 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "mainwindow.h"
#include "./ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setCentralWidget(ui->textEdit);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionNew_triggered()
{
//Giving a file name to empty string
mFilename = "";
//Setting the text to empty string
ui->textEdit->setPlainText("");
}
void MainWindow::on_actionOpen_triggered()
{
//Taking input from user via a dialog box and store that file name
QString file = QFileDialog::getOpenFileName(this,"Open a file");
// If that file is given as a input
if(!file.isEmpty()){
QFile sFile(file);
if(sFile.open(QFile::ReadOnly | QFile::Text)){
mFilename = file;
QTextStream in(&sFile);
QString text = in.readAll();
sFile.close();
ui->textEdit->setPlainText(text);
}
}
}
void MainWindow::on_actionSave_triggered()
{
QFile sFile(mFilename);
if(sFile.open(QFile::WriteOnly | QFile::Text)){
QTextStream out(&sFile);
out << ui->textEdit->toPlainText();
sFile.flush();
sFile.close();
}
}
void MainWindow::on_actionSave_As_triggered()
{
QString file = QFileDialog::getSaveFileName(this,"Open a file");
// If that file is given as a input
if(!file.isEmpty()){
mFilename = file;
on_actionSave_triggered();
}
}
void MainWindow::on_actionCopy_triggered()
{
ui->textEdit->copy();
}
void MainWindow::on_actionCut_triggered()
{
ui->textEdit->cut();
}
void MainWindow::on_actionPaste_triggered()
{
ui->textEdit->paste();
}
void MainWindow::on_actionUndo_triggered()
{
ui->textEdit->undo();
}
void MainWindow::on_actionRedo_triggered()
{
ui->textEdit->redo();
}
void MainWindow::on_actionBold_triggered()
{
// Saving font weight in a local varaible
int font = ui->textEdit->fontWeight();
//First check if the font weight is not equal to the weight of BOLD
if(font != QFont::Bold){
// Then set the selected text to bold
ui->textEdit->setFontWeight(QFont::Bold);
}
// If it is equal
else{
// If it is equal then set text to normal {UnBold feature}
ui->textEdit->setFontWeight(QFont::Normal);
}
}
void MainWindow::on_actionSubScript_triggered()
{
//Added scripting to a local variable
//And then setting that formatting with the current format
auto isSubscript = ui->textEdit->currentCharFormat();
// Will check whether the current format is subscript
if(isSubscript.verticalAlignment() != QTextCharFormat::AlignSubScript){
// no
// turn current format to subscript
QTextCharFormat format;
format.setVerticalAlignment(QTextCharFormat::AlignSubScript);
ui->textEdit->setCurrentCharFormat(format);
}
else{
// yes
// turn current format back to normal
QTextCharFormat format;
format.setVerticalAlignment(QTextCharFormat::AlignNormal);
ui->textEdit->setCurrentCharFormat(format);
}
}
void MainWindow::on_actionSuperScript_triggered()
{
// Similar to subscript
// Current format will change
auto isSubscript = ui->textEdit->currentCharFormat();
if(isSubscript.verticalAlignment() != QTextCharFormat::AlignSuperScript){
QTextCharFormat format;
format.setVerticalAlignment(QTextCharFormat::AlignSuperScript);
ui->textEdit->setCurrentCharFormat(format);
}
else{
QTextCharFormat format;
format.setVerticalAlignment(QTextCharFormat::AlignNormal);
ui->textEdit->setCurrentCharFormat(format);
}
}