-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparadigmwindow.cpp
720 lines (609 loc) · 29.9 KB
/
paradigmwindow.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
#include "paradigmwindow.h"
#include "ui_paradigmwindow.h"
#include <QDebug>
#include <QMessageBox>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlQueryModel>
#include <QSqlTableModel>
#include <QSqlRelationalTableModel>
#include <QSqlRelationalDelegate>
#include <QFileDialog>
#include <QDateTime>
#include <QProcess>
#include "customquerydiagnostics.h"
#include "legendwindow.h"
#include "etimologywindow.h"
#include "phonologywindow.h"
#include "taleswindow.h"
#include "paradigmwindow.h"
#include "playwindow.h"
#include "utils.h"
ParadigmWindow::ParadigmWindow(DictGlobalAttributes _dictAttrs, QVariant _wordIdSelected, QVariant _wordSelected, QVariant _transcriptionSelected, QVariant _translationSelected, QWidget *parent) :
dictAttrs(_dictAttrs),
wordIdSelected(_wordIdSelected),
transcriptionSelectedID(_wordIdSelected),
wordSelected(_wordSelected),
transcriptionSelected(_transcriptionSelected),
translationSelected(_translationSelected),
QDialog(parent),
db(QSqlDatabase::database(dictAttrs.getFilename())),
ui(new Ui::ParadigmWindow),
soundChosen(false),
praatChosen(false),
praatRightChosen(false)
{
ui->setupUi(this);
if (!db.isValid()) {
qDebug() << "Error during tab creation: Database is " << db.isOpen() << " and it's because " << db.isOpenError();
return;
}
ui->labelWord->setText(wordSelected.toString());
ui->labelTrascription->setText(transcriptionSelected.toString());
ui->labelTranslation_3->setText(translationSelected.toString());
connect(ui->soundButton, SIGNAL(clicked()), this, SLOT(chooseSound()));
connect(ui->praatButton, SIGNAL(clicked()), this, SLOT(choosePraat()));
connect(ui->submitButton , SIGNAL(clicked()), this, SLOT(submitWord()));
connect(ui->playButton_2 , SIGNAL(clicked()), this, SLOT(showPlay()));
dictModel = new QSqlTableModel(this, db);
soundsModel = new QSqlTableModel(this, db);
praatModel = new QSqlTableModel(this, db);
initializeDictModel(dictModel);
ui->dictionaryTable->setModel(dictModel);
ui->dictionaryTable->setColumnHidden(0,true);
ui->dictionaryTable->setColumnHidden(2,true);
ui->dictionaryTable->setColumnHidden(5,true);
ui->dictionaryTable->setColumnHidden(6,true);
ui->dictionaryTable->setColumnHidden(7,true);
ui->dictionaryTable->setColumnHidden(8,true);
ui->dictionaryTable->setSortingEnabled(true);
ui->dictionaryTable->setColumnWidth(1, 100 );
ui->dictionaryTable->setColumnWidth(3, 200);
ui->dictionaryTable->setColumnWidth(4, 350);
initializeSoundsModel(soundsModel);
ui->soundsList_2->setModel(soundsModel);
ui->soundsList_2->setColumnHidden(0,true);
ui->soundsList_2->setColumnHidden(1,true);
ui->soundsList_2->setColumnHidden(4,true);
ui->soundsList_2->setColumnHidden(5,true);
ui->soundsList_2->verticalHeader()->setVisible(false);
// ui->soundsList->setModelColumn(2);
// ui->soundsList->setUpdatesEnabled(true);
initializePraatModel(praatModel);
ui->praatList_2->setModel(praatModel);
ui->praatList_2->setColumnHidden(0,true);
ui->praatList_2->setColumnHidden(1,true);
ui->praatList_2->setColumnHidden(4,true);
ui->praatList_2->setColumnHidden(5,true);
ui->praatList_2->verticalHeader()->setVisible(false);
// ui->praatList->setModelColumn(2);
connect(ui->dictionaryTable , SIGNAL(clicked(QModelIndex)), this, SLOT(filterBlobs()));
connect(ui->dictionaryTable, SIGNAL(activated(QModelIndex)), this, SLOT(filterBlobs()));
connect(ui->choosePraat_2, SIGNAL(clicked()), this, SLOT(choosePraatRight()));
connect(ui->praatDescription_3, SIGNAL(textChanged(QString)), this, SLOT(checkPraatRightDescription()));
connect(ui->submitPraat_2, SIGNAL(clicked()), this, SLOT(submitRightPraat()));
connect(ui->sendToPraat_2, SIGNAL(clicked()), this, SLOT(sendToPraat()));
connect(ui->saveChangedPraatButton, SIGNAL(clicked()), this, SLOT(saveChangesInPraat()));
// ui->deleteButton->setDisabled(true);
}
bool ParadigmWindow::sendToPraat() {
QItemSelectionModel *select = ui->praatList_2->selectionModel();
if (!select->hasSelection()) {
errorMsg("Вы не выбрали элементы для отправки в Praat");
return false;
}
QModelIndex index = ui->praatList_2->currentIndex();
QVariant praatblobid = index.sibling(index.row(),0).data();
QVariant praatname = index.sibling(index.row(),2).data();
qDebug() << praatblobid;
QSqlQuery query_blobs(db);
query_blobs.prepare("SELECT mainblob, secblob FROM blobs WHERE blobs.id = :blobid");
query_blobs.bindValue(":blobid", praatblobid);
query_blobs.exec();
qDebug() << getLastExecutedQuery(query_blobs);
// I do it because sendpraat is dumb: it doesn't understand paths with spaces and non-ASCII
QString dest_dir = QDir::tempPath();
qDebug() << "temp is here: " << dest_dir;
QString basename = QString::number(QDateTime::currentMSecsSinceEpoch());
while (query_blobs.next()) {
if (!extractSound(dest_dir, basename + QString(".wav"), query_blobs.value("mainblob").toByteArray())) {
qDebug() << "Can't decompress sound blobs";
errorMsg("Не удалось распаковать звук из праатовской пары файлов, так быть не должно. Либо у вас кончается место на диске, либо это ошибка -- напишите разработчику и не редактируйте словарь, пока он не ответит.");
return false;
} else {
if (!extractSound(dest_dir, basename + QString(".TextGrid"), query_blobs.value("secblob").toByteArray())) {
qDebug() << "Can't decompress praat markup";
errorMsg("Не удалось распаковать разметку из праатовской пары файлов, так быть не должно. Либо у вас кончается место на диске, либо это ошибка -- напишите разработчику и не редактируйте словарь, пока он не ответит.");
return false;
}
}
}
QProcess::startDetached("Praat.exe");
QStringList args;
QString arg1 = QString("praat");
QString arg2 = QString("sound = Read from file... %1/%2.wav").arg(dest_dir, basename);
QString arg3 = QString("markup = Read from file... %1/%2.TextGrid").arg(dest_dir, basename);
QString arg4 = QString("selectObject(sound,markup)");
QString arg5 = QString("View & Edit");
qDebug() << "args for praatsend: " << arg1 << arg2 << arg3 << arg4 << arg5;
args.append(arg1);
args.append(arg2);
args.append(arg3);
args.append(arg4);
args.append(arg5);
QProcess::execute("sendpraat.exe", args);
praatListToSave.append(QPair<QVariant, QString>(praatblobid, basename));
return true;
}
bool ParadigmWindow::saveChangesInPraat() {
QString temp_dir = QDir::tempPath();
QStringList args;
QString arg1 = QString("praat");
QString arg2 = QString("execute %1/scripts/saveallobjectstotemp.praat %2").arg(QDir::currentPath(), temp_dir);
qDebug() << "args for praatsend: " << arg1 << arg2;
args.append(arg1);
args.append(arg2);
QProcess::execute("sendpraat.exe", args);
for (int i=0; i < praatListToSave.size(); i++) {
QVariant blobid = praatListToSave[i].first;
QString basename = praatListToSave[i].second;
qDebug() << "temp is here: " << temp_dir;
QString markuppath = QDir::toNativeSeparators(temp_dir + QString("/") + basename + QString(".TextGrid"));
QString soundpath = QDir::toNativeSeparators(temp_dir + QString("/") + basename + QString(".wav"));
QByteArray praatMarkup;
QByteArray praatSound;
readAndCompress(markuppath, praatMarkup);
readAndCompress(soundpath, praatSound);
// here we need to push sound to blobs table, get id of blob and make a record in blobs_description_table
QSqlQuery praatBlobQuery(db);
praatBlobQuery.prepare("UPDATE blobs SET mainblob = :praatmarkupblob, secblob = :praatsoundblob WHERE id = :blobid");
praatBlobQuery.bindValue(":praatmarkupblob", praatMarkup);
praatBlobQuery.bindValue(":praatsoundblob", praatSound);
praatBlobQuery.bindValue(":blobid", blobid);
if (!praatBlobQuery.exec()) {
qDebug() << "Can't update praat blob";
qDebug() << "Query was the following: " << getLastExecutedQuery(praatBlobQuery);
qDebug() << db.lastError().text();
this->clearForms();
praatModel->select();
return false;
}
}
praatModel->select();
praatListToSave.clear();
return true;
}
bool ParadigmWindow::chooseSound() {
if (!this->soundChosen) {
this->soundFilename = QFileDialog::getOpenFileName(this, tr("Выберите звуковой файл для вставки в словарь"), "", tr("Звуковые файлы (*.mp3 *.wav)"));
qDebug() << "Sound file selected: " << this->soundFilename;
if (!this->soundFilename.isEmpty()) {
ui->soundButton->setText("Сбросить звук?");
this->soundChosen = true;
}
} else {
this->soundFilename.clear();
ui->soundButton->setText("Выберите звуковой файл");
this->soundChosen = false;
}
return true;
}
bool ParadigmWindow::choosePraatRight() {
QItemSelectionModel *select = ui->dictionaryTable->selectionModel();
if (!select->hasSelection()) {
errorMsg("Вы не выбрали слово, для которого хотите добавить разметку Praat");
return false;
}
if (!this->praatRightChosen) {
this->praatRightFormFilenameMarkup = QDir::toNativeSeparators (QFileDialog::getOpenFileName(this,
tr("Выберите файл разметки Praat для вставки в словарь"),
"",
tr("Файлы разметки Praat (*.TextGrid)")));
QFileInfo fileInfo(this->praatRightFormFilenameMarkup);
this->praatRightFormFilenameSound = QDir::toNativeSeparators (fileInfo.canonicalPath().append(fileInfo.completeBaseName().append(".wav").prepend("/")));
qDebug() << "Praat right markup is selected: " << this->praatRightFormFilenameMarkup;
qDebug() << "Praat right sound is selected: " << this->praatRightFormFilenameSound;
if (!this->praatRightFormFilenameMarkup.isEmpty()) {
ui->choosePraat_2->setText("Сбросить выбор?");
this->praatRightChosen = true;
}
} else {
this->praatRightFormFilenameMarkup.clear();
this->praatRightFormFilenameSound.clear();
ui->choosePraat_2->setText("Выберите файл разметки");
this->praatRightChosen = false;
}
return true;
}
bool ParadigmWindow::checkPraatRightDescription() {
QItemSelectionModel *select = ui->dictionaryTable->selectionModel();
if (!select->hasSelection()) {
errorMsg("Вы не выбрали слово, для которого хотите добавить разметку Praat");
return false;
}
if (ui->praatDescription_3->text().isEmpty()) {
return false;
} else if (!this->praatRightChosen) {
return false;
} else {
return true;
}
}
bool ParadigmWindow::submitRightPraat() {
QItemSelectionModel *select = ui->dictionaryTable->selectionModel();
if (!select->hasSelection()) {
errorMsg("Вы не выбрали слово, для которого хотите добавить разметку Praat");
return false;
}
QModelIndex index = ui->dictionaryTable->currentIndex();
QVariant wordid = index.sibling(index.row(),0).data();
qDebug() << wordid;
if (!this->praatRightChosen) {
errorMsg("Вы не выбрали файл разметки");
return false;
}
if (ui->praatDescription_3->text().isEmpty()) {
errorMsg("Вы не заполнили описание для разметки, без этого нельзя её добавить");
return false;
}
QByteArray praatMarkup;
QByteArray praatSound;
readAndCompress(this->praatRightFormFilenameMarkup, praatMarkup);
readAndCompress(this->praatRightFormFilenameSound, praatSound);
QFileInfo fileinfo = QFileInfo(this->praatRightFormFilenameSound);
qDebug() << praatSound.size();
QVariant praatblobid;
// here we need to push sound to blobs table, get id of blob and make a record in blobs_description_table
QSqlQuery praatBlobQuery(db);
praatBlobQuery.prepare("INSERT INTO blobs(mainblob,secblob) values (:praatmarkupblob, :praatsoundblob)");
praatBlobQuery.bindValue(":praatmarkupblob", praatMarkup);
praatBlobQuery.bindValue(":praatsoundblob", praatSound);
if (!praatBlobQuery.exec()) {
qDebug() << "Can't insert praat blob";
qDebug() << "Query was the following: " << getLastExecutedQuery(praatBlobQuery);
qDebug() << db.lastError().text();
this->clearForms();
return false;
}
praatblobid = praatBlobQuery.lastInsertId();
praatBlobQuery.clear();
QSqlQuery praatDescriptionQuery(db);
praatDescriptionQuery.prepare("INSERT INTO dict_blobs_description(type,name,description,wordid,blobid)"
"VALUES (2,:name,:description,:wordid,:blobid)");
praatDescriptionQuery.bindValue(":name", fileinfo.completeBaseName());
praatDescriptionQuery.bindValue(":description", ui->praatDescription_3->text());
praatDescriptionQuery.bindValue(":wordid", wordid);
praatDescriptionQuery.bindValue(":blobid", praatblobid);
if (!praatDescriptionQuery.exec()) {
qDebug() << "Can't insert praat blob description";
qDebug() << "Query was the following: " << getLastExecutedQuery(praatDescriptionQuery);
qDebug() << db.lastError().text();
this->clearForms();
return false;
}
qDebug() << "Successfully submitted praat";
this->clearForms();
this->praatModel->select();
return true;
}
bool ParadigmWindow::choosePraat() {
if (!this->praatChosen) {
this->praatFilenameMarkup = QDir::toNativeSeparators (QFileDialog::getOpenFileName(this,
tr("Выберите файл разметки Praat для вставки в словарь"),
"",
tr("Файлы разметки Praat (*.TextGrid)")));
QFileInfo fileInfo(this->praatFilenameMarkup);
if (!this->praatFilenameMarkup.isEmpty()) {
this->praatFilenameSound = QDir::toNativeSeparators (fileInfo.canonicalPath().append(fileInfo.completeBaseName().append(".wav").prepend("/")));
qDebug() << "Praat markup is selected: " << this->praatFilenameMarkup;
qDebug() << "Praat sound is selected: " << this->praatFilenameSound;
ui->praatButton->setText("Сбросить выбор?");
this->praatChosen = true;
}
} else {
this->praatFilenameMarkup.clear();
this->praatFilenameSound.clear();
ui->praatButton->setText("Выберите файл разметки");
this->praatChosen = false;
}
return true;
}
bool ParadigmWindow::showPlay() {
QStringList playlist;
QItemSelectionModel *select = ui->dictionaryTable->selectionModel();
if (!select->hasSelection()) {
errorMsg("Вы не выбрали слово, для которого хотите послушать/добавить аудиофайлы");
return false;
}
QModelIndex index = ui->dictionaryTable->currentIndex();
QVariant wordid = index.sibling(index.row(),0).data();
qDebug() << wordid;
QSqlQuery query_descriptions(db);
query_descriptions.prepare("SELECT blobid, name FROM dict_blobs_description WHERE dict_blobs_description.wordid = :wordid and dict_blobs_description.type = 1");
query_descriptions.bindValue(":wordid", wordid);
query_descriptions.exec();
QString dest_dir = createDirectory(dictAttrs.getDictname(), "audio");
while (query_descriptions.next()) {
QSqlQuery query_blobs(db);
query_blobs.prepare("SELECT mainblob FROM blobs WHERE blobs.id = :blobid");
query_blobs.bindValue(":blobid",query_descriptions.value("blobid"));
qDebug() << getLastExecutedQuery(query_blobs);
query_blobs.exec();
while (query_blobs.next()) {
if (!extractSound(dest_dir, query_descriptions.value("name").toString(), query_blobs.value("mainblob").toByteArray())) {
qDebug() << "Can't decompress sound blobs";
} else {
playlist.append(dest_dir + "/" + query_descriptions.value("name").toString());
}
}
}
PlayWindow play(playlist, db, dest_dir, wordid, this);
play.exec();
return true;
}
ParadigmWindow::~ParadigmWindow()
{
delete dictModel;
delete soundsModel;
delete praatModel;
delete ui;
}
void ParadigmWindow::initializeDictModel(QSqlTableModel *model) {
model->setTable("dictionary");
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->setHeaderData(0, Qt::Horizontal, QObject::tr("Номер"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Слово"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Регулярная форма"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("Транскрипция"));
model->setHeaderData(4, Qt::Horizontal, QObject::tr("Перевод"));
model->setFilter(QString("is_a_regular_form = 0 AND regular_form = \"") + transcriptionSelectedID.toString() + QString("\""));
qDebug() << model->query().lastQuery();
model->select();
qDebug() << model->query().lastQuery();
qDebug() << model->query().result();
}
bool ParadigmWindow::filterBlobs() {
qDebug() << "filtering blobs";
QString filterSoundsQuery = "type = 1 and wordid = ";
QString filterPraatQuery = "dict_blobs_description.type = 2 and dict_blobs_description.wordid = ";
QModelIndexList model_index = ui->dictionaryTable->selectionModel()->selection().indexes();
int row = model_index.at(0).row();
QString param = this->dictModel->data(this->dictModel->index(row,0)).toString();
filterSoundsQuery.append(param);
filterPraatQuery.append(param);
const QString filtersounds(filterSoundsQuery);
const QString filterpraat(filterPraatQuery);
soundsModel->setFilter(filtersounds);
praatModel->setFilter(filterpraat);
soundsModel->select();
praatModel->select();
qDebug() << soundsModel->query().lastQuery();
qDebug() << praatModel->query().lastQuery();
qDebug() << soundsModel->query().record().count();
qDebug() << soundsModel->rowCount();
qDebug() << soundsModel->query().result();
qDebug() << praatModel->query().result();
qDebug() << praatModel->query().record().count();
return true;
}
void ParadigmWindow::initializeSoundsModel(QSqlTableModel *model) {
model->setTable("dict_blobs_description");
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->setHeaderData(0, Qt::Horizontal, QObject::tr("Номер"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Тип"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Имя"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("Описание"));
model->setHeaderData(4, Qt::Horizontal, QObject::tr("Ссылка на слово"));
model->setHeaderData(5, Qt::Horizontal, QObject::tr("Ссылка на блоб"));
model->setFilter("type = 1");
model->select();
qDebug() << "initializing soundsmodel";
qDebug() << model->query().lastQuery();
qDebug() << model->query().result();
qDebug() << model->query().record().count();
qDebug() << model->rowCount();
}
void ParadigmWindow::initializePraatModel(QSqlTableModel *model) {
model->setTable("dict_blobs_description");
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->setHeaderData(0, Qt::Horizontal, QObject::tr("Номер"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Тип"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Имя"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("Описание"));
model->setHeaderData(4, Qt::Horizontal, QObject::tr("Ссылка на слово"));
model->setHeaderData(5, Qt::Horizontal, QObject::tr("Ссылка на блоб"));
model->setFilter("type = 2");
model->select();
}
bool ParadigmWindow::isValidInput(void) {
// 1 -- text inputs are blank
if (ui->wordForm->text().isEmpty() && ui->transcritptionForm->text().isEmpty() && ui->translationForm->text().isEmpty()) {
errorMsg("Вы должны заполнить хотя бы одно из текстовых полей для создания слова");
return false;
}
// 2 -- sound is selected, but no description
if (!this->soundFilename.isEmpty() && ui->soundDescription_2->text().isEmpty()) {
errorMsg("Вы выбрали звук, но не написали описание: так нельзя");
return false;
}
// 3 -- sound is not selected, but description is ready
if (this->soundFilename.isEmpty() && !ui->soundDescription_2->text().isEmpty()) {
errorMsg("Вы описали аудиозапись, но не выбрали её: так нельзя");
return false;
}
// 4 -- sound file doesn't exist
if (!this->soundFilename.isEmpty() && !QFile(this->soundFilename).exists()) {
errorMsg("Кажется, аудиофайл изчез, пока вы заполняли остальные поля. Сбросьте выбор и попробуйте ещё раз");
return false;
}
// 5 -- praat is selected, but no description
if ( !(this->praatFilenameSound.isEmpty() || this->praatFilenameMarkup.isEmpty()) && ui->praatDescription_4->text().isEmpty()) {
errorMsg("Вы выбрали файлы для Praat, но не описали их; так нельзя");
return false;
}
// 6 -- praat is not selected, but description is ready
if ((this->praatFilenameSound.isEmpty() || this->praatFilenameMarkup.isEmpty()) && !ui->praatDescription_4->text().isEmpty()) {
errorMsg("Вы описали файлы для Praat, но не выбрали их, это нужно сделать");
return false;
}
// 7 -- praat markup doesn't exist
if (!(this->praatFilenameMarkup.isEmpty()) && !QFile(this->praatFilenameMarkup).exists()) {
qDebug() << !(this->praatFilenameMarkup.isEmpty());
qDebug() << QFile(this->praatFilenameMarkup).exists();
errorMsg("Похоже, к файлу разметки Praat нет файла звука. Проверьте, что они называются одинаково (за исключением расширений .wav и .praat) и что оба файла лежат в одной и той же папке");
return false;
}
// 8 -- praat sound doesn't exist
if (!(this->praatFilenameSound.isEmpty()) && !QFile(this->praatFilenameSound).exists()) {
errorMsg("Похоже, вы выбрали файл звука, но парного файла разметки к нему не существует. Проверьте, что они называются одинаково (за исключением расширений .wav и .praat) и что оба файла лежат в одной и той же папке");
return false;
}
// 0 -- everything is ok
return true;
}
DictEntry ParadigmWindow::readFields()
{
QString word(ui->wordForm->text());
QString transcription(ui->transcritptionForm->text());
QString translation(ui->translationForm->text());
QString soundDescription;
QString praatDescription;
if (this->soundChosen) {
soundDescription = (ui->soundDescription_2->text());
} else {
this->soundFilename = "";
}
if (this->praatChosen) {
praatDescription = (ui->praatDescription_4->text());
} else {
this->praatFilenameMarkup = "";
this->praatFilenameSound = "";
praatDescription = "";
}
return DictEntry(this,
word,
transcription,
translation,
this->soundFilename,
soundDescription,
this->praatFilenameMarkup,
this->praatFilenameSound,
praatDescription);
}
bool ParadigmWindow::submitWord()
{
/* check that we have at least one filled field */
if (!this->isValidInput()) {
return false;
}
DictEntry entry(readFields());
qDebug() << "Now we should get an entry to sqlite";
QSqlRecord record = dictModel->record();
record.setValue("word", entry.getWord());
record.setValue("regular_form" , transcriptionSelectedID);
record.setValue("transcription" , entry.getTranscription());
record.setValue("translation", entry.getTranslation());
record.setValue("is_a_regular_form" , false);
dictModel->insertRecord(-1, record);
dictModel->submitAll();
qDebug() << "after set data: " << dictModel->query().lastQuery();
record.clear();
QVariant wordid = dictModel->query().lastInsertId();
if (this->soundChosen || this->praatChosen) {
QVariant soundblobid;
// here we need to push sound to blobs table, get id of blob and make a record in blobs_description_table
if (this->soundChosen) {
QSqlQuery soundBlobQuery(db);
soundBlobQuery.prepare("INSERT INTO blobs(mainblob) values (:soundblob)");
soundBlobQuery.bindValue(":soundblob", *(entry.getSoundPointer()));
if (!soundBlobQuery.exec()) {
//TODO: Cleanup
qDebug() << "Can't insert sound blob";
qDebug() << "Query was the following: " << getLastExecutedQuery(soundBlobQuery);
qDebug() << db.lastError().text();
this->clearForms();
return false;
}
soundblobid = soundBlobQuery.lastInsertId();
soundBlobQuery.clear();
QSqlQuery soundDescriptionQuery(db);
soundDescriptionQuery.prepare("INSERT INTO dict_blobs_description(type,name,description,wordid,blobid)"
"VALUES (1,:name,:description,:wordid,:blobid)");
soundDescriptionQuery.bindValue(":name", entry.getSoundFilename());
soundDescriptionQuery.bindValue(":description", entry.getSoundDescription());
soundDescriptionQuery.bindValue(":wordid", wordid);
soundDescriptionQuery.bindValue(":blobid", soundblobid);
if (!soundDescriptionQuery.exec()) {
//TODO: Cleanup
qDebug() << "Can't insert sound blob description";
qDebug() << "Query was the following: " << getLastExecutedQuery(soundDescriptionQuery);
qDebug() << db.lastError().text();
this->clearForms();
return false;
}
qDebug() << "Successfully submitted sound";
}
}
if (this->praatChosen) {
QVariant praatblobid;
QSqlQuery praatBlobQuery(db);
praatBlobQuery.prepare("INSERT INTO blobs(mainblob,secblob) values (:praatmarkupblob,:praatsoundblob)");
praatBlobQuery.bindValue(":praatmarkupblob", *(entry.getPraatMarkupPointer()));
praatBlobQuery.bindValue(":praatsoundblob", *(entry.getPraatSoundPointer()));
if (!praatBlobQuery.exec()) {
//TODO: Cleanup
qDebug() << "Can't insert praat blob";
qDebug() << "Query was the following: " << getLastExecutedQuery(praatBlobQuery);
qDebug() << db.lastError().text();
this->clearForms();
return false;
}
praatblobid = praatBlobQuery.lastInsertId();
praatBlobQuery.clear();
QSqlQuery praatDescriptionQuery(db);
praatDescriptionQuery.prepare("INSERT INTO dict_blobs_description(type,name,description,wordid,blobid)"
"VALUES (2,:name,:description,:wordid,:blobid)");
praatDescriptionQuery.bindValue(":name", entry.getBasePraatMarkupFilename());
praatDescriptionQuery.bindValue(":description", entry.getPraatDescription());
praatDescriptionQuery.bindValue(":wordid", wordid);
praatDescriptionQuery.bindValue(":blobid", praatblobid);
if (!praatDescriptionQuery.exec()) {
//TODO: Cleanup
qDebug() << "Can't insert sound blob description";
qDebug() << "Query was the following: " << getLastExecutedQuery(praatDescriptionQuery);
qDebug() << db.lastError().text();
this->clearForms();
return false;
}
qDebug() << "Successfully submitted praat blob";
}
// dictModel->database().commit();
qDebug() << "Executed the query on commit: " << dictModel->query().lastQuery();
dictModel->select();
while (dictModel->canFetchMore()) {
dictModel->fetchMore();
}
this->clearForms();
return true;
}
void ParadigmWindow::clearForms() {
ui->wordForm->clear();
ui->transcritptionForm->clear();
ui->translationForm->clear();
ui->soundDescription_2->clear();
ui->praatDescription_4->clear();
this->praatChosen = false;
ui->praatButton->setText("Выберите файл разметки");
this->soundChosen = false;
ui->soundButton->setText("Выберите звуковой файл");
this->soundFilename.clear();
this->praatFilenameMarkup.clear();
this->praatFilenameSound.clear();
this->praatRightFormFilenameMarkup.clear();
this->praatRightFormFilenameSound.clear();
this->praatRightFormDescription.clear();
this->praatRightChosen = false;
ui->choosePraat_2->setText("Выберите файл разметки");
ui->praatDescription_3->clear();
}