-
Notifications
You must be signed in to change notification settings - Fork 0
/
notepad.cpp
155 lines (119 loc) · 2.91 KB
/
notepad.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
#include "notepad.h"
#include "ui_notepad.h"
#include "mainwindow.h"
#include <QMessageBox>
#include <QApplication>
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QFontDialog>
#include <QColorDialog>
#include <QDebug>
NotePad::NotePad(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::NotePad)
{
ui->setupUi(this);
this->setWindowTitle("Note Pad");
}
NotePad::~NotePad()
{
delete ui;
}
void NotePad::on_actionQuit_2_triggered()
{
this->close();
this->destroy();
QApplication::quit();
}
void NotePad::on_actionCopy_triggered()
{
ui->textEdit->copy();
}
void NotePad::on_actionCut_triggered()
{
ui->textEdit->cut();
}
void NotePad::on_actionPaste_triggered()
{
ui->textEdit->paste();
}
void NotePad::on_actionUndo_triggered()
{
ui->textEdit->undo();
}
void NotePad::on_actionRedo_triggered()
{
ui->textEdit->redo();
}
void NotePad::on_actionAbout_triggered()
{
QMessageBox::about(this,"Message","This is a simple notepad application with contains basic notepad functions.");
}
void NotePad::on_actionAbout_Qt_triggered()
{
QApplication::aboutQt();
}
void NotePad::on_actionSave_triggered()
{
QString fileName = QFileDialog::getSaveFileName(this,"Save As");
if(fileName.isEmpty()){
return;
}
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)){
return;
}
QTextStream out(&file);
out << ui->textEdit->toPlainText() << "\n";
file.close();
}
void NotePad::on_actionOpen_triggered()
{
QString fileContent;
QString fileName = QFileDialog::getOpenFileName(this,"Open File");
if(fileName.isEmpty()){
return;
}
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
return;
}
QTextStream in(&file);
QString text = in.readLine();
while(!text.isEmpty()){
fileContent.append(text);
text = in.readLine();
}
file.close();
ui->textEdit->clear();
ui->textEdit->setPlainText(fileContent);
}
void NotePad::on_actionFont_triggered()
{
bool ok;
QFont font = QFontDialog::getFont(&ok,QFont("Helvetica [Cronyx]", 10),this);
if(ok){
ui->textEdit->setFont(font);
} else {
QMessageBox::information(this,"Message","Font Selection Failed");
}
}
void NotePad::on_actionColor_triggered()
{
QPalette pallete = ui->textEdit->palette();
QColor color = pallete.color(QPalette::WindowText);
QColor choosenColor = QColorDialog::getColor(color,this,"Choose Color");
if(choosenColor.isValid()){
ui->textEdit->setTextColor(choosenColor);
qDebug() << "user choosen a valid color";
} else {
qDebug() << "user choosen is valid color";
}
}
void NotePad::on_actionSet_HTML_triggered()
{
QString plain = ui->textEdit->toPlainText();
QString html = plain.toHtmlEscaped();
ui->textEdit->setHtml(html);
}