-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
95 lines (82 loc) · 2.14 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "about.h"
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
//*************************
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
fileLocation = "";
}
//*************************
MainWindow::~MainWindow()
{
delete ui;
}
//*************************
void MainWindow::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"));
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QString content = file.readAll();
file.close();
ui->textEdit->setText(content);
fileLocation = fileName;
MainWindow::setWindowTitle("qNotePad - " + fileName);
}
//*************************
void MainWindow::saveFile()
{
// If no file location set, use save as function instead.
if(fileLocation == "")
{
saveFileAs();
return;
}
QFile file(fileLocation);
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
fileContents = ui->textEdit->toPlainText();
out << fileContents;
file.close();
fileContents = "";
MainWindow::setWindowTitle("qNotePad - " + fileLocation);
}
//*************************
void MainWindow::saveFileAs()
{
QString filename = QFileDialog::getSaveFileName(this, tr("Save File"));
QFile file(filename);
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
fileContents = ui->textEdit->toPlainText();
out << fileContents;
file.close();
fileContents = "";
fileLocation = filename;
MainWindow::setWindowTitle("qNotePad - " + fileLocation);
}
//*************************
void MainWindow::openAbout()
{
About *a = new About();
a->show();
}
//*************************
void MainWindow::textChanged()
{
// If there is no file location, let user know they haven't
// saved their data yet.
if(fileLocation == "")
{
MainWindow::setWindowTitle("qNotePad - Unsaved Data");
return;
}
MainWindow::setWindowTitle("qNotePad - " + fileLocation + "*");
}