-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnotepad.cpp
More file actions
executable file
·86 lines (77 loc) · 2.25 KB
/
notepad.cpp
File metadata and controls
executable file
·86 lines (77 loc) · 2.25 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
#include "notepad.h"
notepad::notepad()
{
tEditor = new QTextEdit;
quitButton = new QPushButton;
openAction = new QAction(tr("Open"), this);
saveAction = new QAction(tr("Save"), this);
quitAction = new QAction(tr("Quit"), this);
QObject::connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
QObject::connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
QObject::connect(quitAction, SIGNAL(triggered()), this, SLOT(Quit()));
fileMenu = menuBar()->addMenu(tr("File"));
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(quitAction);
setWindowTitle(tr("Notepad"));
setCentralWidget(tEditor);
}
void notepad::open()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Select a file... "), "", tr("Text Files (*.txt);;c++ source files (*.cpp);;All Files (*.*)"));
if(fileName!="")
{
QFile fIn(fileName);
if(!fIn.open(QIODevice::ReadOnly))
{
QMessageBox::critical(this, tr("Error"), tr("File could not opened"));
return;
}
QTextStream in(&fIn);
tEditor->setText(in.readAll());
fIn.close();
}
else
{
QMessageBox confirm;
confirm.setText(tr("No file has bee selected"));
confirm.setStandardButtons(QMessageBox::Retry | QMessageBox::Ok);
confirm.setDefaultButton(QMessageBox::Ok);
if(confirm.exec()==QMessageBox::Retry)
{
open();
}
}
}
void notepad::save()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Text Files (*.txt);;C++ Source Files (*.cpp);;All files(*.*)"));
QFile fOut(fileName);
if(fileName!="")
{
if(fOut.open(QIODevice::WriteOnly))
{
QTextStream out(&fOut);
out<<tEditor->toPlainText();
out.flush();
fOut.close();
}
else
{
QMessageBox::critical(this, tr("Error !!!"), tr("File could not be saved"));
return;
}
}
}
void notepad::Quit()
{
QMessageBox confirmQuit;
confirmQuit.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
confirmQuit.setDefaultButton(QMessageBox::No);
confirmQuit.setText(tr("Do you really want to quit ?"));
confirmQuit.setWindowTitle(tr("Close the Notepad"));
if(confirmQuit.exec() == QMessageBox::Yes)
{
qApp->quit();
}
}