/
filecontroller.cpp
141 lines (109 loc) · 3.09 KB
/
filecontroller.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
#include "filecontroller.h"
FileController::FileController(QObject *parent) : QObject(parent)
{
fileOpenned = 0;
}
void FileController::clear() {
src.clear();
lines.clear();
line.clear();
path.clear();
}
QString FileController::getPathString() {
return path;
}
void FileController::openDialogEnable() {
QString pathTmp = QFileDialog::getOpenFileName(0, "Открыть файл...", "", "*.stur");
if(!pathTmp.isEmpty()) {
clear();
path = pathTmp;
file = new QFile(path);
if(file->open(QIODevice::ReadWrite)) {
copyDataFromFile();
}
emit opennedFile(src, line);
emit sendPath(path);
}
}
void FileController::copyDataFromFile() {
QTextStream tStream(file);
QString fileData = tStream.readAll();
QStringList list = fileData.split('\n');
for(int i = 0; i < list.size(); ++i) {
lines.push_back(list.at(i));
}
if(lines.size() >= 1) {
line = lines[0];
for(int i = 1; i < lines.size(); ++i) {
if(i != 1) {src.push_back('\n');}
src.push_back(lines[i]);
}
}
fileOpenned = 1;
}
void FileController::openFromPath(QString p) {
if(!p.isEmpty()) {
clear();
path = p;
file = new QFile(path);
if(file->open(QIODevice::ReadWrite)) {
copyDataFromFile();
}
emit opennedFile(src, line);
emit sendPath(path);
}
}
void FileController::saveFile(QString src, QString line) {
if(path.isEmpty()) {
saveDialogEnable();
}
else {
QFile file(path);
file.open(QIODevice::ReadWrite);
QByteArray srcBytes;
QByteArray lineBytes;
string lineStd = line.toStdString();
for(int i = 0; i < lineStd.size(); ++i) {
lineBytes.push_back(lineStd[i]);
}
string srcStd = src.toStdString();
for(int i = 0; i < srcStd.size(); ++i) {
srcBytes.push_back(srcStd[i]);
}
file.resize(0);
file.write(lineBytes);
file.write("\n");
file.write(srcBytes);
}
}
QString FileController::getPath(QString p) {
if(p.size() >= 5) {
if(p[p.size() - 1] == 'r' && p[p.size() - 2] == 'u' && p[p.size() - 3] == 't' && p[p.size() - 4] == 's' && p[p.size() - 5] == '.') {
return p;
} else {return p + ".stur";}
} else {return p + ".stur";}
}
void FileController::saveDialogEnable() {
QFileDialog sDialog;
QString pathTmp = sDialog.getSaveFileName(0, tr("Сохранить файл как..."), "", tr("STuring files (*.stur)"));
if(!pathTmp.isEmpty()) {
lines.clear();
path = pathTmp;
emit saveFileSign();
file = new QFile(path);
QFileInfo info(*file);
if(info.suffix().isEmpty()) {
info.suffix() = ".stur";
}
file->open(QIODevice::ReadWrite);
emit sendPath(path);
}
}
void FileController::createNewFile() {
clear();
emit opennedFile(src, line);
emit sendPath("Новый");
}
FileController::~FileController() {
delete file;
}