-
Notifications
You must be signed in to change notification settings - Fork 1
/
text.cpp
81 lines (59 loc) · 1.35 KB
/
text.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
#include "text.h"
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>
#include <QTextStream>
Text::Text(QString text):
text_(text){}
Text::Text(QString file, QString text):
file_(file), text_(text){}
Text::Text(const Text &other):
file_(other.file_), text_(other.text_){}
void Text::readFromFile(QString fileName)
{
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly | QFile::Text)) {
throw FileOpenningException();
}
file_ = fileName;
QTextStream in(&file);
in >> (*this);
isSaved_ = true;
file.close();
}
void Text::saveToFile(QString fileName)
{
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly | QFile::Text)) {
throw FileSavingException();
}
file_ = fileName;
QTextStream out(&file);
out << *(this);
isSaved_ = true;
file.close();
}
void Text::clearAll()
{
text_.clear();
file_.clear();
isSaved_ = false;
}
/*Friend functions*/
void operator>>(QTextEdit *textEdit, Text &text){
text.text_ = textEdit->toPlainText();
}
void operator<<(QTextEdit *textEdit, Text &text){
textEdit->setText(text.text_);
}
QTextStream &operator<<(QTextStream &out, Text &text)
{
out << text.text_;
return out;
}
QTextStream &operator>>(QTextStream &in, Text &txt)
{
txt.text_ = in.readAll();
txt.isSaved_ = true;
return in;
}