-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
277 lines (239 loc) · 7.47 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
vblayout = new QVBoxLayout();
addCat = new QToolButton();
addCat->setText("+");
delCat = new QToolButton();
delCat->setText("-");
del = new QToolButton();
del->setText("-");
catList = new QComboBox();
catList->addItem("Категория");
saveList = new QComboBox();
saveList->addItem("Заметка");
text = new QPlainTextEdit();
hblayout1 = new QHBoxLayout();
hblayout1->addWidget(catList);
hblayout1->addWidget(addCat);
hblayout1->addWidget(delCat);
hblayout2 = new QHBoxLayout();
hblayout2->addWidget(saveList);
hblayout2->addWidget(del);
vblayout->addLayout(hblayout1);
vblayout->addLayout(hblayout2);
vblayout->addWidget(text);
testbtn = new QPushButton("Сохранить");
vblayout->addWidget(testbtn);
this->setWindowIcon(QIcon("/usr/share/pixmaps/saver.png"));
connect(testbtn, SIGNAL(clicked(bool)), this, SLOT(textSave()));
connect(addCat, SIGNAL(clicked(bool)), this, SLOT(categoryAdd()));
connect(catList, SIGNAL(currentIndexChanged(int)), this, SLOT(categoryChanche()));
connect(delCat, SIGNAL(clicked(bool)), this, SLOT(categoryRemove()));
connect(saveList, SIGNAL(currentIndexChanged(int)), this, SLOT(textListChanged()));
connect(del, SIGNAL(clicked(bool)), this, SLOT(textRemove()));
connect(text, SIGNAL(textChanged()), this, SLOT(editText()));
HomePath = QDir::homePath();
if(QApplication::platformName() == "android"){
HomePath += "/Saver";
}else{
if(QApplication::platformName() == "windows"){
//проверяем наличие конфига
HomePath += "/Saver";
}else{
//linux
HomePath += "/.local/share/Saver";
}
}
QDir *HPath = new QDir(HomePath);
if(!HPath->exists()){
HPath->mkdir(HomePath);
}
delete HPath;
listDirs(HomePath);
listFiles(HomePath);
}
MainWindow::~MainWindow()
{
delete catList;
delete addCat;
delete delCat;
delete saveList;
delete del;
delete text;
delete testbtn;
delete hblayout1;
delete hblayout2;
delete vblayout;
}
//добавление категории
void MainWindow::categoryAdd(){
QString str = QInputDialog::getText(0, "Добавление категории", "Название категории",
QLineEdit::Normal);
if(str == "") {
return;
}
QDir dir;
dir.mkdir(HomePath + "/" + str);
catList->addItem(str);
}
//удаление категории
void MainWindow::categoryRemove(){
if(catList->currentIndex() == 0){
return;
}
QMessageBox *msg = new QMessageBox("Удаление категории",
catList->currentText() + " удаляем?",
QMessageBox::Information,
QMessageBox::Yes,
QMessageBox::No,
QMessageBox::Cancel | QMessageBox::Escape);
int n = msg->exec();
delete msg;
if(n == QMessageBox::Yes){
QDir dir;
dir.setPath(HomePath + "/" + catList->currentText());
dir.removeRecursively();
catList->removeItem(catList->currentIndex());
}
}
//выбор категории
void MainWindow::categoryChanche() {
if(catList->currentIndex() == 0){
listFiles(HomePath);
}else{
listFiles(HomePath + "/" + catList->currentText());
}
}
//сохранение заметки
void MainWindow::textSave(){
QString path = HomePath;
QString filename = "";
bool newsave;
if(catList->currentIndex() == 0){
//если категория корневая
path += "/";
}else{
//если выбранная категория
path += "/" + catList->currentText() + "/";
}
if(saveList->currentIndex() == 0){
//если не выбрана заметка, спрашиваем имя заметки
filename = QInputDialog::getText(0, "Сохранение заметки", "Имя заметки", QLineEdit::Normal);
if(filename == "") return;
newsave = true;
}else{
//есть выбранная заметка, пишем в неё
filename = saveList->currentText();
newsave = false;
}
path += filename;
if(fWrite(path, text->toPlainText())){
if(newsave) saveList->addItem(filename);
}
text->clear();
}
//удаление заметки
void MainWindow::textRemove(){
if(saveList->currentIndex() == 0) return;
QMessageBox m;
m.setText("Удаляем " + saveList->currentText() + "?");
m.addButton(QMessageBox::Yes);
m.addButton(QMessageBox::No);
if(m.exec() == QMessageBox::No){
return;
}
QString path = HomePath;
if(catList->currentIndex() == 0){
path += "/";
}else{
path += "/" + catList->currentText() + "/";
}
path += saveList->currentText();
QFile file;
file.setFileName(path);
if(file.remove()){
saveList->removeItem(saveList->currentIndex());
}
}
//выбор заметки
void MainWindow::textListChanged(){
QString path = HomePath;
if(catList->currentIndex() != 0){
path += "/" + catList->currentText() + "/";
}else{
path += "/";
}
if(saveList->currentIndex() > 0){
path += saveList->currentText();
}else{
return;
}
if(text->toPlainText().count() > 0){
//нужно ли сохранить
QMessageBox m;
m.setText("Есть несохранённая заметка, открыть новую?");
m.addButton(QMessageBox::Yes);
m.addButton(QMessageBox::No);
if(m.exec() == QMessageBox::Yes){
text->setPlainText(fRead(path));
}else{
return;
}
}else{
text->setPlainText(fRead(path));
}
}
//список каталогов
void MainWindow::listDirs(QString path){
catList->clear();
QDir dir;
dir.setPath(path);
QStringList list = dir.entryList(QDir::Dirs);
catList->addItem("Категория");
foreach (QString item, list) {
if(item != "." && item != "..")
catList->addItem(item);
}
}
//список файлов/заметок
void MainWindow::listFiles(QString path){
saveList->clear();
QDir dir;
dir.setPath(path);
QStringList list = dir.entryList(QDir::Files);
saveList->addItem("Заметка");
foreach (QString item, list) {
if(item != "." && item != "..")
saveList->addItem(item);
}
}
void MainWindow::editText()
{
this->setWindowTitle(title + " (изменено)");
}
//запись
bool MainWindow::fWrite(QString path, QString tofile){
QFile f(path);
if(f.open(QIODevice::WriteOnly | QIODevice::Text)){
QTextStream wStream(&f);
wStream.setCodec("UTF-8");
wStream << tofile;
f.close();
return true;
}
return false;
}
//чтение
QString MainWindow::fRead(QString path){
QFile f(path);
if(!f.open(QIODevice::ReadOnly|QIODevice::Text)){
return "";
}
QByteArray data = f.readAll();
return QString(data);
}
void MainWindow::msg(QString text, QString title = QApplication::applicationName()){
QMessageBox(QMessageBox::Information, title, text, QMessageBox::Ok);
}