-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotepad.cpp
More file actions
218 lines (183 loc) · 5.12 KB
/
notepad.cpp
File metadata and controls
218 lines (183 loc) · 5.12 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "notepad.h"
#include "ui_notepad.h"
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QMessageBox>
#include <QFontDialog>
#include <QColorDialog>
#include <QDate>
#include <QTime>
Notepad::Notepad(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Notepad)
{
ui->setupUi(this);
this->setCentralWidget(ui->textEdit);
}
Notepad::~Notepad()
{
delete ui;
}
//-----------------Action button triggers-----------------------
//new file method
void Notepad::on_actionNew_triggered()
{
file_path_ = "";//clear file path
ui-> textEdit->setText("");//clear text
}
//open file method
void Notepad::on_actionOpen_triggered()
{
QString file_name = QFileDialog::getOpenFileName(this,"Open the file");
QFile file(file_name);
file_path_ = file_name;
if(!file.open(QFile::ReadOnly| QFile::Text)){
QMessageBox::warning(this,"Error","Somthing went wrong!");
return;
}
QTextStream in(&file);
QString text = in.readAll();
ui->textEdit->setText(text);
file.close();
}
//save method
void Notepad::on_actionSave_triggered()
{
QFile file(file_path_);
if(!file.open(QFile::WriteOnly| QFile::Text)){
QMessageBox::warning(this,"Error","Somthing went wrong!");
return;
}
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.flush();
file.close();
}
//save as method
void Notepad::on_actionSave_As_triggered()
{
QString file_name = QFileDialog::getSaveFileName(this,"Open the file");
QFile file(file_name);
file_path_=file_name;
if(!file.open(QFile::WriteOnly| QFile::Text)){
QMessageBox::warning(this,"Error","Somthing went wrong!");
return;
}
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.flush();
file.close();
}
//window close method
void Notepad::on_actionExit_triggered()
{
this->close();
}
//selected text copy method
void Notepad::on_actionCopy_triggered()
{
ui->textEdit->copy();
}
//selected text cut method
void Notepad::on_actionCut_triggered()
{
ui->textEdit->cut();
}
//paste text method
void Notepad::on_actionPaste_triggered()
{
ui->textEdit->paste();
}
//clear text edit method
void Notepad::on_actionDelete_triggered()
{
ui->textEdit->clear();
}
//undo changes method
void Notepad::on_actionUndo_triggered()
{
ui->textEdit->undo();
}
//redo changes method
void Notepad::on_actionRedo_triggered()
{
ui->textEdit->redo();
}
//font properties method
void Notepad::on_actionFont_triggered()
{
bool ok;
QFont font = QFontDialog::getFont(
&ok,
QFont( "Arial", 9,50 ),
this,
tr("Pick a font") );
if( ok )
{
ui->textEdit->setFontPointSize(font.pointSize());//set font size
ui->textEdit->setFontFamily(font.family());//set font family
if (font.style() == 1) {
ui->textEdit->setFontItalic(true);//set font style
}else {
ui->textEdit->setFontItalic(false);
}
if (font.underline() == true) {
ui->textEdit->setFontUnderline(true);//Set text underline
}else {
ui->textEdit->setFontUnderline(false);
}
}
}
//change font color method
void Notepad::on_actionColor_triggered()
{
QColor color = QColorDialog::getColor(Qt::black, this );
if( color.isValid() )
{
ui->textEdit->setTextColor(color.name());
}
}
//zoom in method
void Notepad::on_actionZoom_In_triggered()
{
ui->textEdit->zoomIn(4);
}
//zoom out method
void Notepad::on_actionZoom_Out_triggered()
{
ui->textEdit->zoomOut(3);
}
//about section method
void Notepad::on_actionAbout_triggered()
{
QMessageBox::about(this, tr("About"), tr("Notepad v1.0 "
"\nAuthor : Lahiru Dananjaya Sudasinghe\n"
"Date : 01/17/2022\n"
"\nThis is a simple notepad application build for University project using C++ with QT \n"
"\n© 2022 Notepad. Alrights reserved. "));//qMessageBox will popup
}
//text align left method
void Notepad::on_actionAlign_left_triggered()
{
ui->textEdit->setAlignment(Qt::AlignLeft);
}
//text slign center method
void Notepad::on_actionAlign_Center_triggered()
{
ui->textEdit->setAlignment(Qt::AlignCenter);
}
//text align right method
void Notepad::on_actionAlign_Right_triggered()
{
ui->textEdit->setAlignment(Qt::AlignRight);
}
//add date and time method
void Notepad::on_actionAdd_Date_Time_triggered()
{
QDate cd = QDate::currentDate();//get current system date and assign it into cd
QTime ct = QTime::currentTime();//get current system time and assign it into ct
ui->textEdit->setText(cd.toString() +" "+ ct.toString());//set text into date and time
}