-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotepad.cpp
182 lines (158 loc) · 4.02 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
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
#include "gotodialog.h"
#include "notepad.h"
#include "ui_notepad.h"
#include <QFontDialog>
#include <QFileDialog>
#include <QFile>
#include <qDebug>
#include <QTextStream>
#include <QString>
#include <QDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setCentralWidget(ui->plainTextEdit);
LineNumber = new QLabel(this);
ui->statusBar->addPermanentWidget(LineNumber);
// defaults
ui->plainTextEdit->setWordWrapMode(QTextOption::NoWrap);
ui->plainTextEdit->setFont(QFont("Consolas",11));
QWidget::setWindowTitle("Untitled - Notepad");
LineNumber->setText("Ln 1, Col 1");
// Default file value
main_file="";
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionNew_Window_triggered()
{
main_file="";
ui->plainTextEdit->clear();
}
void MainWindow::on_actionOpen_triggered()
{
read_file();
}
void MainWindow::on_actionFont_triggered()
{
ui->plainTextEdit->setFont(QFontDialog::getFont(0,ui->plainTextEdit->font()));
}
void MainWindow::on_actionExit_triggered()
{
exit(0);
}
void MainWindow::on_actionSave_triggered()
{
if(main_file.isEmpty())
save_file(true);
else
save_file(false);
}
// state = true.. if you want for prompt , else no if you want to save to existing file
void MainWindow::save_file(bool state)
{
QString file_Name;
if(state)
{
file_Name = QFileDialog::getSaveFileName(this, tr("Save As"),NULL,tr("Text files (*.txt)"));
main_file=file_Name;
}
else
file_Name=main_file;
QFile mfile(file_Name);
if(mfile.open(QFile::WriteOnly|QFile::Text))
{
QTextStream in(&mfile);
in << ui->plainTextEdit->toPlainText();
}
mfile.flush();
mfile.close();
QWidget::setWindowTitle(file_Name);
}
void MainWindow::read_file()
{
QString file_Name = QFileDialog::getOpenFileName(this, tr("Open"),NULL,tr("Text files (*.txt)"));
main_file=file_Name;
QWidget::setWindowTitle(file_Name);
QFile mfile(file_Name);
if(mfile.open(QFile::ReadOnly|QFile::Text))
{
QTextStream in(&mfile);
QString line=in.readAll();
ui->plainTextEdit->clear();
ui->plainTextEdit->appendPlainText(line);
}
}
void MainWindow::on_actionSave_As_triggered()
{
save_file(true);
}
void MainWindow::on_actionPaste_triggered()
{
ui->plainTextEdit->paste();
}
void MainWindow::on_actionCopy_triggered()
{
ui->plainTextEdit->copy();
}
void MainWindow::on_actionCut_triggered()
{
ui->plainTextEdit->cut();
}
void MainWindow::on_actionDelete_triggered()
{
QTextCursor curs=ui->plainTextEdit->textCursor();
if(curs.hasSelection())
qDebug() << "There is selection";
else
qDebug() << "There is no selection";
//curs.removeSelectedText();
//qDebug() <<"Anchor : " << curs.anchor();
//qDebug() <<"Position pos: "<< curs.position();
//qDebug() << "Selection star: "<<curs.selectionStart();
//qDebug() << "Select End: " << curs.selectionEnd();
ui->plainTextEdit->moveCursor(QTextCursor::Up);
}
void MainWindow::on_actionWord_Wrap_toggled(bool checked)
{
if(checked)
{
ui->plainTextEdit->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
}
else
{
ui->plainTextEdit->setWordWrapMode(QTextOption::NoWrap);
}
}
void MainWindow::on_actionUndo_triggered()
{
ui->plainTextEdit->undo();
}
void MainWindow::on_plainTextEdit_cursorPositionChanged()
{
QTextCursor curPos = ui->plainTextEdit->textCursor();
int row=curPos.blockNumber();
int col=curPos.positionInBlock();
LineNumber->setText("Ln "+QString::number(row+1)+", Col "+QString::number(col+1));
}
void MainWindow::on_actionStatus_Bar_toggled(bool toogle)
{
if(toogle)
ui->statusBar->show();
else
ui->statusBar->hide();
}
void MainWindow::on_actionGo_To_triggered()
{
GotoDialog myDialog;
myDialog.setModal(true);
myDialog.exec();
}
void MainWindow::on_actionSelect_All_triggered()
{
ui->plainTextEdit->selectAll();
}