Repository navigation
Expand file tree
/
Copy pathmainw.cpp
More file actions
463 lines (414 loc) · 15.4 KB
/
Copy pathmainw.cpp
File metadata and controls
463 lines (414 loc) · 15.4 KB
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
#include "mainw.h"
void MainW::createMenus()
{
this->fileMenu = new QMenu(tr("File"), this);
this->newFileAct = new QAction(tr("New File"), this);
this->newFileAct->setShortcut(QKeySequence(QKeySequence::New));
connect(this->newFileAct, SIGNAL(triggered(bool)), this, SLOT(newFileSlot()));
this->fileMenu->addAction(this->newFileAct);
this->openFileAct = new QAction(tr("Open File"), this);
this->openFileAct->setShortcut(QKeySequence(QKeySequence::Open));
connect(this->openFileAct, SIGNAL(triggered(bool)), this, SLOT(openFileSlot()));
this->fileMenu->addAction(this->openFileAct);
this->saveFileAct = new QAction(tr("Save"), this);
this->saveFileAct->setShortcut(QKeySequence(QKeySequence::Save));
connect(this->saveFileAct, SIGNAL(triggered(bool)), this, SLOT(saveFileSlot()));
this->fileMenu->addAction(this->saveFileAct);
this->menuBar()->addMenu(this->fileMenu);
this->windowMenu = new QMenu(tr("Window"), this);
this->wordListWindowAct = new QAction(tr("Word List"), this);
this->wordListWindowAct->setShortcut(QKeySequence("Alt+L"));
this->wordListWindowAct->setCheckable(true);
connect(this->wordListWindowAct, SIGNAL(triggered(bool)), this, SLOT(wordListWindowSlot(bool)));
this->windowMenu->addAction(this->wordListWindowAct);
this->wordPadWindowAct = new QAction(tr("Word Pad"), this);
this->wordPadWindowAct->setShortcut(QKeySequence("Alt+W"));
this->wordPadWindowAct->setCheckable(true);
connect(this->wordPadWindowAct, SIGNAL(triggered(bool)), this, SLOT(wordPadWindowSlot(bool)));
this->windowMenu->addAction(this->wordPadWindowAct);
this->kanaPadWindowAct = new QAction(tr("Kana Pad"), this);
this->kanaPadWindowAct->setShortcut(QKeySequence("Alt+K"));
this->kanaPadWindowAct->setCheckable(true);
connect(this->kanaPadWindowAct, SIGNAL(triggered(bool)), this, SLOT(kanaPadWindowSlot(bool)));
this->windowMenu->addAction(this->kanaPadWindowAct);
this->menuBar()->addMenu(this->windowMenu);
}
MainW::MainW(QWidget *parent)
: QMainWindow(parent)
{
this->resize(1024, 768);
this->currentFocusWordPadLineEdit = nullptr;
this->wordListWidget = new WordlistWidget(this);
this->wordPadWidget = new WordPadWidget(this);
this->kanaPadWidget = new KanaPadWidget(this);
this->previewWidget = new PreviewWidget(this);
this->setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
this->setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
this->setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
this->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
this->wordListDockWidget = new QDockWidget(tr("Word List"), this);
wordListDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
wordListDockWidget->setWidget(this->wordListWidget);
wordListDockWidget->setFeatures(/*QDockWidget::DockWidgetClosable |*/ QDockWidget::DockWidgetMovable);
this->addDockWidget(Qt::LeftDockWidgetArea, wordListDockWidget);
connect(this->wordListWidget, SIGNAL(hideSig()), this, SLOT(wordListCloseSlot()));
this->wordPadDockWidget = new QDockWidget(tr("Word"), this);
wordPadDockWidget->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
wordPadDockWidget->setWidget(this->wordPadWidget);
wordPadDockWidget->setFeatures(/*QDockWidget::DockWidgetClosable |*/ QDockWidget::DockWidgetMovable);
this->addDockWidget(Qt::TopDockWidgetArea, wordPadDockWidget);
connect(this->wordPadWidget, SIGNAL(lostFocus(QLineEdit*)), this, SLOT(wordPadLostFocus(QLineEdit*)));
connect(this->wordPadWidget, SIGNAL(hideSig()), this, SLOT(wordPadCloseSlot()));
this->kanaPadDockWidget = new QDockWidget(tr("Kana Pad"), this);
kanaPadDockWidget->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
kanaPadDockWidget->setWidget(this->kanaPadWidget);
kanaPadDockWidget->setFeatures(/*QDockWidget::DockWidgetClosable |*/ QDockWidget::DockWidgetMovable);
this->addDockWidget(Qt::BottomDockWidgetArea, kanaPadDockWidget);
connect(this->kanaPadWidget, SIGNAL(kanaClicked(QString,bool)), this, SLOT(kanaPadClickedSlot(QString,bool)));
connect(this->kanaPadWidget, SIGNAL(hideSig()), this, SLOT(kanaPadCloseSlot()));
this->setCentralWidget(this->previewWidget);
this->createMenus();
#ifdef __DEBUG__
//QWidget *w = new QWidget(this);
//this->setCentralWidget(w);
//connect(this->kanaPadWidget, SIGNAL(kanaClicked(QString,bool)), this, SLOT(kanaClickedSlot(QString,bool)));
//connect(this->wordPadWidget, SIGNAL(lostFocus(QLineEdit*)), this, SLOT(wordLostFocus(QLineEdit*)));
#endif
this->fileNotOpenStatement();
this->wordListWidget->clearList();
connect(this->wordListWidget, SIGNAL(clicked(int)), this, SLOT(wordListClicked(int)));
connect(this->wordListWidget, SIGNAL(removed(int)), this, SLOT(wordListRemoved(int)));
connect(this->wordListWidget, SIGNAL(movedUp(int)), this, SLOT(wordListMovedUp(int)));
connect(this->wordListWidget, SIGNAL(movedDown(int)), this, SLOT(wordListMovedDown(int)));
connect(this->wordListWidget, SIGNAL(draged(int,int)), this, SLOT(wordListDraged(int,int)));
connect(this->wordListWidget, SIGNAL(add(int)), this, SLOT(wordListAdded(int)));
connect(this->wordListWidget, SIGNAL(listEmpty()), this, SLOT(wordListEmpty()));
connect(this->wordPadWidget, SIGNAL(saveButtonClicked()), this, SLOT(saveToList()));
connect(this->wordPadWidget, SIGNAL(textChangedSig(QString)), this->previewWidget, SLOT(setKanjiSlot(QString)));
}
MainW::~MainW()
{
}
void MainW::wordPadLostFocus(QLineEdit *w)
{
this->currentFocusWordPadLineEdit = w;
}
void MainW::kanaPadClickedSlot(QString kana, bool e)
{
Q_UNUSED(e);
if(this->currentFocusWordPadLineEdit != nullptr){
this->currentFocusWordPadLineEdit->insert(kana);
}
}
#ifdef __DEBUG__
void MainW::kanaClickedSlot(QString kana, bool e)
{
Q_UNUSED(e);
QMessageBox::information(this, "KanaPad Clicked get in Main", QString("You Clicked this char just now:%1").arg(kana), QMessageBox::Ok);
}
void MainW::wordLostFocus(QLineEdit *w)
{
w->insert("R");
QMessageBox::information(this, "WordPad Lost Focus in Main", QString("QLineEdit's Text:%1").arg(w->text()), QMessageBox::Ok);
}
#endif
void MainW::newFileSlot()
{
if(this->statement & FileModified){
int cancelled = 0;
int result = QMessageBox::question(this, "Kanji Maker", tr("You have modified the project, are you really want to create new file?"), QMessageBox::Yes | QMessageBox::Save | QMessageBox::Cancel);
switch(result){
case QMessageBox::Yes:
case QMessageBox::Discard: cancelled = 0; break;
case QMessageBox::Save:
//save file
{
bool isSaved = this->saveFileSlot();
if(isSaved == false){
cancelled = 1;
}
}
break;
case QMessageBox::No:
case QMessageBox::Cancel: cancelled = 1; break;
}
if(cancelled == 1){
return;
}
}
QString headName = QInputDialog::getText(this, tr("Kanji Maker"), tr("Please enter this project's name:"));
if(headName.isEmpty()){
QMessageBox::warning(this, "Kanji Maker", tr("Please enter an name for this project."), QMessageBox::Ok);
return;
}
this->head.name = headName;
this->word.clear();
this->wordListWidget->clearList();
this->wordListWidget->setList(this->word);
this->wordListWidget->setHead(this->head);
this->previewWidget->setFontSize(30);
this->previewWidget->setKanji(tr("新しいエントリの左のリストをクリックします"));
this->fileOpenStatement();
}
bool MainW::openFileSlot()
{
if(this->statement & FileModified){
int cancelled = 0;
int result = QMessageBox::question(this, "Kanji Maker", tr("You have modified the project, are you really want to open file?"), QMessageBox::Yes | QMessageBox::Save | QMessageBox::Cancel);
switch(result){
case QMessageBox::Yes:
case QMessageBox::Discard: cancelled = 0; break;
case QMessageBox::Save:
//save file
{
bool isSaved = this->saveFileSlot();
if(isSaved == false){
cancelled = 1;
}
}
break;
case QMessageBox::No:
case QMessageBox::Cancel: cancelled = 1; break;
}
if(cancelled == 1){
return false;
}
}
this->head.name.clear();
this->word.clear();
this->wordListWidget->clearList();
QString filename = QFileDialog::getOpenFileName(this, "Open Word File", QDir::currentPath(), "Kanji Word File(*.kji);;Kanji Json File(*.json);;All Files(*.*)");
if(filename.isEmpty()){
return false;
}
ReadSaveFile readFile(filename);
if(!readFile.loadFile(&this->word, &this->head)){
QMessageBox::critical(this, "Kanji Maker", "This file is not vaild.", QMessageBox::Ok);
return false;
}
this->fileOpenStatement();
this->wordListWidget->setHead(this->head);
this->wordListWidget->setList(this->word);
this->previewWidget->setFontSize(30);
this->previewWidget->setKanji(tr("エントリを表示するには、左側のをクリックして"));
return true;
}
bool MainW::saveFileSlot()
{
if(this->word.isEmpty()){
QMessageBox::information(this, "Kanji Maker", "Your word list is empty...", QMessageBox::Ok);
return false;
}
if(this->statement & FileModified){
QString filename = QFileDialog::getSaveFileName(this, "Save Word File", QDir::currentPath(), "Kanji Word File(*.kji);;Kanji Json File(*.json);;All Files(*.*)");
ReadSaveFile saveFile(filename);
if(!saveFile.saveFile(this->word, this->head)){
QMessageBox::critical(this, "Kanji Maker", "Save Failed...", QMessageBox::Ok);
return false;
}else{
QMessageBox::information(this, "Kanji Maker", "Word File Saved.", QMessageBox::Ok);
this->fileOpenStatement();
return true;
}
}
return false;
}
void MainW::wordListCloseSlot()
{
this->wordListDockWidget->hide();
this->wordListWindowAct->setChecked(false);
}
void MainW::wordPadCloseSlot()
{
this->wordPadDockWidget->hide();
this->wordPadWindowAct->setChecked(false);
}
void MainW::kanaPadCloseSlot()
{
this->kanaPadDockWidget->hide();
this->kanaPadWindowAct->setChecked(false);
}
void MainW::wordListWindowSlot(bool e)
{
if(e){
this->wordListDockWidget->show();
}else{
this->wordListDockWidget->hide();
}
}
void MainW::wordPadWindowSlot(bool e)
{
if(e){
this->wordPadDockWidget->show();
}else{
this->wordPadDockWidget->hide();
}
}
void MainW::kanaPadWindowSlot(bool e)
{
if(e){
this->kanaPadDockWidget->show();
}else{
this->kanaPadDockWidget->hide();
}
}
void MainW::fileNotOpenStatement()
{
this->saveFileAct->setEnabled(false);
this->wordListDockWidget->hide();
this->wordListWindowAct->setChecked(false);
this->wordPadDockWidget->hide();
this->wordPadWindowAct->setChecked(false);
this->kanaPadDockWidget->hide();
this->kanaPadWindowAct->setChecked(false);
this->statement = FileNotOpen;
}
void MainW::fileOpenStatement()
{
this->saveFileAct->setEnabled(false);
this->wordListDockWidget->show();
this->wordListWindowAct->setChecked(true);
this->wordPadDockWidget->hide();
this->wordPadWindowAct->setChecked(false);
this->kanaPadDockWidget->show();
this->kanaPadWindowAct->setChecked(true);
this->statement = FileOpen;
}
void MainW::fileModifiedStatement()
{
this->saveFileAct->setEnabled(true);
this->statement = FileModified;
}
void MainW::wordModifiedStatement(){
this->wordPadDockWidget->show();
this->saveFileAct->setEnabled(true);
this->statement = WordModified;
}
void MainW::wordListClicked(int index)
{
qDebug() << "[DEBUG]MainW::wordListClicked word[" << index << "]";
if(index >= this->word.length()){
qDebug() << "Word List: index:" << index << "not found, will update word list.";
this->wordListWidget->setList(this->word);
return;
}
KanjiWord w = this->word.at(index);
this->wordPadWidget->setKana(w.kana);
this->wordPadWidget->setKanji(w.kanji);
this->wordPadWidget->setChinese(w.chinese);
this->wordPadWidget->setEnglish(w.english);
this->previewWidget->setFontSize(40);
this->previewWidget->setKana(w.kana);
this->previewWidget->setKanji(w.kanji);
this->wordPadDockWidget->show();
this->wordPadWindowAct->setChecked(true);
this->currentWordIndex = index;
}
void MainW::wordListRemoved(int index)
{
qDebug() << "[DEBUG]MainW::wordListRemoved word[" << index << "]";
if(index >= this->word.length()){
qDebug() << "Word List: index:" << index << "not found, will update word list.";
this->wordListWidget->setList(this->word);
return;
}
KanjiWord w = this->word.at(index);
if(w.kana != this->wordPadWidget->getKana()){
qDebug() << "Kanji Word: Kana:" << w.kana << "is not same to" << this->wordPadWidget->getKana();
return;
}
if(w.kanji != this->wordPadWidget->getKanji()){
qDebug() << "Kanji Word: Kanji:" << w.kanji << "is not same to" << this->wordPadWidget->getKanji();
return;
}
if(w.chinese != this->wordPadWidget->getChinese()){
qDebug() << "Kanji Word: Chinese:" << w.chinese << "is not same to" << this->wordPadWidget->getChinese();
return;
}
if(w.english != this->wordPadWidget->getEnglish()){
qDebug() << "Kanji Word: English:" << w.english << "is not same to" << this->wordPadWidget->getEnglish();
return;
}
this->word.removeAt(index);
this->wordListWidget->setList(this->word);
}
void MainW::wordListMovedUp(int index)
{
qDebug() << "[DEBUG]MainW::wordListMovedUp word[" << index << "]";
if(index >= this->word.length()){
qDebug() << "Word List: index:" << index << "not found, will update word list.";
this->wordListWidget->setList(this->word);
return;
}
if(index == 0){
qDebug() << "Word List: index:" << index << "is the first one. cannot move up.";
return;
}
this->word.swap(index, index -1);
this->wordListWidget->setList(this->word);
}
void MainW::wordListMovedDown(int index)
{
qDebug() << "[DEBUG]MainW::wordListMovedDown word[" << index << "]";
if(index >= this->word.length()){
qDebug() << "Word List: index:" << index << "not found, will update word list.";
this->wordListWidget->setList(this->word);
return;
}
if(index == this->word.length() -1){
qDebug() << "Word List: index:" << index << "is the last one. cannot move down.";
return;
}
this->word.swap(index, index +1);
this->wordListWidget->setList(this->word);
}
void MainW::wordListDraged(int from, int to)
{
qDebug() << "[DEBUG]MainW::wordListDraged word[" << from << "] swapped with [" << to << "].";
if(from >= this->word.length() || to >= this->word.length()){
qDebug() << "Word List: index:" << from << "and:" << to << "not found, will update word list.";
this->wordListWidget->setList(this->word);
return;
}
this->word.swap(from, to);
this->wordListWidget->setList(this->word);
}
void MainW::wordListAdded(int index)
{
qDebug() << "[DEBUG]MainW::wordListAdded word[" << index << "]";
KanjiWord w;
if(index >= this->word.length()){
this->word.append(w);
this->wordListWidget->setList(this->word);
}else{
this->word.insert(index, w);
this->wordListWidget->setList(this->word);
}
}
void MainW::wordListEmpty()
{
this->currentWordIndex = -1;
this->wordPadWidget->setKana("");
this->wordPadWidget->setKanji("");
this->wordPadWidget->setChinese("");
this->wordPadWidget->setEnglish("");
this->previewWidget->setKana("");
this->previewWidget->setKanji(tr("リストは空です"));
this->wordPadDockWidget->hide();
this->wordPadWindowAct->setChecked(false);
this->kanaPadDockWidget->hide();
this->kanaPadWindowAct->setChecked(false);
}
void MainW::saveToList()
{
qDebug() << "[DEBUG]MainW::saveToList word[" << this->currentWordIndex << "]";
if(this->currentWordIndex == -1){
return;
}
this->word[this->currentWordIndex].kana = this->wordPadWidget->getKana();
this->word[this->currentWordIndex].kanji = this->wordPadWidget->getKanji();
this->word[this->currentWordIndex].chinese = this->wordPadWidget->getChinese();
this->word[this->currentWordIndex].english = this->wordPadWidget->getEnglish();
this->wordListWidget->setList(this->word);
this->wordModifiedStatement();
}