-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtdjmisc.cpp
796 lines (646 loc) · 22 KB
/
tdjmisc.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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/*
The Daily Journal - A PIM program
Qt version
begin : 12 July 2013
copyright : (C) Kartik Patel
email : letapk@gmail.com
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
*/
//Last modified 19 June 2022
#include "tdj.h"
#include <QFileDialog>
#include <QImageReader>
#include <QDesktopServices>
extern size_t txtLength; // string plus termination
extern size_t txtLengthpadded; // string plus padded zero characters
extern char *toencrypt, *encrypted, *todecrypt, *decrypted;
extern char iniVector[];
extern int aesdec (void);
extern void set_decrypt_variables(void);
extern void free_dec_strings (void);
extern int aesenc (void);
extern int close_gcrypt (void);
extern void crypt_error_notification (const char *errstr);
extern QString Lockfilename, userpath;
extern bool setpwd;
extern Note note[];
//used while reencrypting data files due to a change in password
QFileInfo reencfileInfo;
extern size_t szt;
void MainWindow::about()
//open a window to show program information and copyright license
{
QFile file(Gnugplfilename);
QTextStream in(&file);
QTextBrowser *gnugpl;
bool ok;
ok = file.open(QFile::ReadOnly);
if (ok == false)
return;
gnugpl = new QTextBrowser ();
gnugpl->setGeometry(10, 10, 800, 600);
gnugpl->setWindowTitle (QObject::tr("About The Daily Journal"));
gnugpl->setPlainText(in.readAll());
gnugpl->setAlignment(Qt::AlignLeft);
gnugpl->show();
}
void MainWindow::help()
//open and show the user manual
{
bool ok;
QString s1;
QMessageBox msgBox;
QFile file(Helpfilename);
ok = file.exists();
if (ok == false) {
s1 = QObject::tr("The help file was not found.");
s1.append (QObject::tr("Please make sure that it is present in the hidden tdj data directory."));
msgBox.setText(s1);
msgBox.exec();
}
else {
QDesktopServices::openUrl (QUrl (Helpfilename));
}
}
bool check_lockfile (void)
{
QMessageBox msgBox;
QString s1, s2, s3;
bool ok = false;
QFile file(Lockfilename);
ok = file.open(QFile::ReadOnly);
if (ok == true) {//lockfile present, close it and inform user
file.close();
s1 = QObject::tr("It seems that \"The Daily Journal\" is already running.");
s2 = QObject::tr("If this is not the case, click \"Continue\", else click \"Abort\". ");
s3 = QObject::tr("A lockfile has been found in the hidden tdj data-subdirectory. ");
s3.append (QObject::tr("The program may be currently running in another terminal, in which case click \"Abort\". "));
s3.append (QObject::tr("Alternatively, an earlier instance of the program may have failed to delete the lockfile. "));
s3.append (QObject::tr("If you are sure that tdj is not running in your account, click \"Continue\". "));
s3.append (QObject::tr("See the user manual about the risks of running two instances of the program at the same time."));
msgBox.setText(s1);
msgBox.setInformativeText(s2);
msgBox.setDetailedText(s3);
msgBox.addButton(QObject::tr("Continue"), QMessageBox::ApplyRole);
msgBox.addButton(QObject::tr("Abort"), QMessageBox::RejectRole);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::ApplyRole://continue
file.remove();
return true;
break;
case QMessageBox::RejectRole://abort
return false;
break;
}
}
return true;//lockfile absent
}
void create_lockfile ()
{
QFile file(Lockfilename);
file.open(QFile::WriteOnly);
file.close();
}
void delete_lockfile ()
{
QFile file(Lockfilename);
file.remove();
}
void check_qtdata_dir ()
{
QString qtpath, s1;
QDir qtdir;
QMessageBox msgBox;
qtpath.append (userpath);
qtdir = QDir (qtpath);
if (qtdir.exists() == false) {
s1.append (QObject::tr("The tdj data directory does not exist. "));
s1.append (QObject::tr("This is required to store your work.\n"));
s1.append (QObject::tr("Click OK to create a new, hidden subdirectory "));
s1.append (QObject::tr("in your area with the name :\n"));
s1.append (qtpath);
msgBox.setText(s1);
msgBox.exec();
qtdir.mkdir(qtpath);
s1.clear();
s1.append (QObject::tr("The program needs a password to encrypt the data. "));
s1.append (QObject::tr("This will not be stored anywhere, and thus should not be forgotten.\n"));
s1.append (QObject::tr("Click OK to create a new password in the next step.\n"));
msgBox.setText(s1);
msgBox.exec();
//ask for password
setpwd = true;
}
}
MainWindow::~MainWindow()
{
}
void MainWindow::closeEvent(QCloseEvent *event)
{
save_notes_and_appts();
save_other_data ();
writeprefs();
delete_lockfile ();
close_gcrypt();
event->accept();
}
void MainWindow::resizeEvent(QResizeEvent *event)
//move and shift the widgets when the window size changes
{
int w, h;
w = width();
h = height();
tb->resize(w - 320, 30);
//resize the trees
contree->resize(contree->width(), h - 340);
listree->resize(contree->width(), h - 340);
//resize the tabcontainer
tabcontainer->resize(w - 320, h - 100);
noteditor->setGeometry(10, 10, tabcontainer->width()- 20, tabcontainer->height()- 55);
contacteditor->setGeometry(10, 10, tabcontainer->width()- 20, tabcontainer->height()- 55);
listeditor->setGeometry(10, 10, tabcontainer->width()- 20, tabcontainer->height()- 55);
apptable->setGeometry(10, 10, tabcontainer->width()- 20, tabcontainer->height()- 55);
anntable->setGeometry(10, 10, tabcontainer->width()- 20, tabcontainer->height()- 55);
//resize and reposition the search widgets
searchtxtbox->setGeometry(120, 10, tabcontainer->width()-220, 25);
srchbut->setGeometry(tabcontainer->width()-90, 10, 80, 25);
//move the status text area
statustext->setGeometry(10, h - 30, w, 25);
srchresults->setGeometry(10, 45, tabcontainer->width() - 20, tabcontainer->height() - 65 - 25);
//this makes the font size box visible on expanding the window and fixes
//a bug which would prevent it showing if the user has clicked the extension
//button on the toolbar
comboFont->setVisible(true);
comboSize->setVisible(true);
//pass the event up the chain
QWidget::resizeEvent(event);
}
void MainWindow::make_password_test_file (QString phrase)
{
QString Testfilename, s;
int i, k;
FILE *testf;
i = phrase.toUtf8().size();
toencrypt = (char *) malloc (i + 1);
strcpy(toencrypt, "");
strcpy (toencrypt, phrase.toUtf8().data());
Testfilename.append (Homepath);
s = QString ("/Checkpwd.tdj");
Testfilename.append (s);
testf = fopen (Testfilename.toUtf8().data(), "w");
fwrite (&iniVector, sizeof (char), 16, testf);
fwrite (&i, sizeof (int), 1, testf);
k = aesenc ();
if (k == 1) {
crypt_error_notification ("Error in encryption of test data.");
}
//save the encrypted data
fwrite (encrypted, txtLengthpadded, 1, testf);
free ((char *)encrypted);
free ((char *)toencrypt);
fflush(testf);
fclose (testf);
}
void MainWindow::test_password ()
{
QString Testfilename, s, serr;
QString Decstr;
QMessageBox msgBox;
int k, testln;
FILE *testf;
Testfilename.append (Homepath);
s = QString ("/Checkpwd.tdj");
Testfilename.append (s);
testf = fopen (Testfilename.toUtf8().data(), "r");
szt = fread (&iniVector, sizeof (char), 16, testf);
szt = fread (&testln, sizeof (int), 1, testf);
txtLength = testln;
set_decrypt_variables();
//read the data to be decrypted
szt = fread (todecrypt, txtLengthpadded, 1, testf);
//decrypt the data and put it in decrypted
k = aesdec ();
if (k == 1) {
crypt_error_notification ("Error in decryption of test data.");
}
Decstr.clear();
Decstr.append(decrypted);
//display the string
Cnfdialog = new QDialog (this);
lbl4 = new QLabel(tr("Confirm encoded phrase"), Cnfdialog);
ledt4 = new QLineEdit (Cnfdialog);
ledt4->setText(Decstr);
lbl4->setBuddy(ledt4);
ok2but = new QPushButton(tr("&Confirm"));
cncl2but = new QPushButton(tr("&Reject"));
connect (ok2but, SIGNAL(clicked()), this, SLOT(confirm_phrase()));
connect (cncl2but, SIGNAL(clicked()), this, SLOT(reject_phrase()));
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(lbl4);
vbox->addWidget(ledt4);
vbox->addWidget(ok2but);
vbox->addWidget(cncl2but);
Cnfdialog->setLayout(vbox);
//execute the dialog
Cnfdialog->exec();
//check the result
k = Cnfdialog->result();
if (k == QDialog::Rejected) {
serr.clear();
serr.append (QObject::tr("The program will now terminate"));
msgBox.setText(serr);
msgBox.exec();
delete_lockfile();
std::exit (1);
}
//free the buffers
free_dec_strings ();
}
void MainWindow::re_encrypt_notes_appt_data ()
{
int i;
QDir dir;
QString path;
QStringList fltr;
QFileInfoList list;
int inivecflag = 1;
dir.setFilter(QDir::Files);
dir.setPath(Homepath);
//create list of Notes files
fltr << "Notes*.tdj";
dir.setNameFilters(fltr);
list = dir.entryInfoList();//list of Notes files
//read and re-encrypt each file
for (i = 0; i < list.size(); i++) {
reencfileInfo = list.at(i);//file at position i in the list
read_journal_file (inivecflag);
write_journal_file (inivecflag);
}
fltr.clear();
list.clear();
//create list of Appt files
fltr << "Appointments*.tdj";
dir.setNameFilters(fltr);
list = dir.entryInfoList();
//read and re-encrypt each file
for (i = 0; i < list.size(); i++) {
reencfileInfo = list.at(i);
read_appt_file (inivecflag);
write_appt_file (inivecflag);
}
}
void MainWindow::save_notes_as_text()
{
int i, j;
QDir dir;
QStringList fltr;
QFileInfoList list;
QString txtfile, s, fname, Year, Month;
QTextDocument *doc;
bool ok;
//file to put the exported text
txtfile.append (Homepath);
txtfile.append ("/Notes.txt");
QFile file(txtfile);
ok = file.open(QFile::WriteOnly);
if (ok == false)
return;
QTextStream out (&file);
//path for file list
dir.setFilter(QDir::Files);
dir.setPath(Homepath);
//create list of Notes files
fltr << "Notes*.tdj";
dir.setNameFilters(fltr);
list = dir.entryInfoList();//list of Notes files
//read and export each file as text
for (i = 0; i < list.size(); i++) {
reencfileInfo = list.at(i);//file at position i in the list
read_journal_file (1);
fname = reencfileInfo.baseName();//filename without path and extension : "Notes-xxxx-xx"
Month = fname.remove (0, 11);//remove leading part : "Notes-xxxx-". Only "xx" remains
fname = reencfileInfo.baseName();//filename without path and extension : "Notes-xxxx-xx"
Year = fname.remove(0, 6);//remove leading part of Notefilename : "Notes-". Only "xxxx-xx" remains
Year.truncate(4);//remove trailing part : "-xx". Only "xxxx" remains.
for (j = 1; j <= 31; j++) {
if (note[j].length > 0) {
out << j << " " << get_month_name(Month.toInt()) << " " << Year << "\n";
doc = new QTextDocument ();
doc->setHtml(note[j].data);
s = doc->toPlainText();
delete doc;
out << s;
out << "\n";
}
}
out << "\n";
}
file.close();
s = QString (tr("Notes data saved to %1")).arg(txtfile);
statustext->setText(s);
}
void MainWindow::save_lists_as_text()
{
QString txtfile, s;
QTreeWidgetItem *it;
QTextDocument *doc;
int i, toplevelcount;
bool ok;
txtfile.append (Homepath);
txtfile.append ("/Lists.txt");
QFile file(txtfile);
ok = file.open(QFile::WriteOnly);
if (ok == false)
return;
QTextStream out(&file);
//number of categories
toplevelcount = listree->topLevelItemCount();
out << "Number of lists:" << toplevelcount << "\n";
//loop over lists
for (i = 0; i < toplevelcount; i++){
it = listree->topLevelItem(i);
doc = new QTextDocument ();
doc->setHtml(it->text(1));
s = doc->toPlainText();
delete doc;
out << "\nName of list:";
out << s;
out << "\n";
}
file.close();
s = QString (tr("Lists data saved to %1")).arg(txtfile);
statustext->setText(s);
}
void MainWindow::confirm_phrase()
{
Cnfdialog->accept();
}
void MainWindow::reject_phrase()
{
Cnfdialog->reject();
}
void MainWindow::setuptoolbar()
{
//toolbar for the editor
tb = new QToolBar;
tb->setParent (this);
tb->setGeometry(320, 30, 470, 30);
tb->setFloatable (false);
tb->setMovable(false);
actionTextBold = new QAction(QIcon::fromTheme("", QIcon(":/images/textbold.png")), tr("&Bold"), this);
actionTextBold->setShortcut(Qt::CTRL + Qt::Key_B);
actionTextBold->setPriority(QAction::LowPriority);
QFont bold;
bold.setBold(true);
actionTextBold->setFont(bold);
connect(actionTextBold, SIGNAL(triggered()), this, SLOT(textBold()));
tb->addAction(actionTextBold);
actionTextBold->setCheckable(true);
actionTextItalic = new QAction(QIcon::fromTheme("", QIcon(":/images/textitalic.png")), tr("&Italic"), this);
actionTextItalic->setPriority(QAction::LowPriority);
actionTextItalic->setShortcut(Qt::CTRL + Qt::Key_I);
QFont italic;
italic.setItalic(true);
actionTextItalic->setFont(italic);
connect(actionTextItalic, SIGNAL(triggered()), this, SLOT(textItalic()));
tb->addAction(actionTextItalic);
actionTextItalic->setCheckable(true);
actionTextUnderline = new QAction(QIcon::fromTheme("", QIcon(":/images/textunder.png")), tr("&Underline"), this);
actionTextUnderline->setShortcut(Qt::CTRL + Qt::Key_U);
actionTextUnderline->setPriority(QAction::LowPriority);
QFont underline;
underline.setUnderline(true);
actionTextUnderline->setFont(underline);
connect(actionTextUnderline, SIGNAL(triggered()), this, SLOT(textUnderline()));
tb->addAction(actionTextUnderline);
actionTextUnderline->setCheckable(true);
QActionGroup *grp = new QActionGroup(this);
connect(grp, SIGNAL(triggered(QAction*)), this, SLOT(textAlign(QAction*)));
actionAlignLeft = new QAction(QIcon::fromTheme("", QIcon(":/images/textleft.png")),tr("&Left"), grp);
actionAlignCenter = new QAction(QIcon::fromTheme("",QIcon(":/images/textcenter.png")),tr("C&enter"), grp);
actionAlignRight = new QAction(QIcon::fromTheme("",QIcon(":/images/textright.png")),tr("&Right"), grp);
actionAlignJustify = new QAction(QIcon::fromTheme("",QIcon(":/images/textjustify.png")),tr("&Justify"), grp);
actionAlignLeft->setShortcut(Qt::CTRL + Qt::Key_L);
actionAlignLeft->setCheckable(true);
actionAlignLeft->setPriority(QAction::LowPriority);
actionAlignCenter->setShortcut(Qt::CTRL + Qt::Key_E);
actionAlignCenter->setCheckable(true);
actionAlignCenter->setPriority(QAction::LowPriority);
actionAlignRight->setShortcut(Qt::CTRL + Qt::Key_R);
actionAlignRight->setCheckable(true);
actionAlignRight->setPriority(QAction::LowPriority);
actionAlignJustify->setShortcut(Qt::CTRL + Qt::Key_J);
actionAlignJustify->setCheckable(true);
actionAlignJustify->setPriority(QAction::LowPriority);
tb->addActions(grp->actions());
QAction *actionInsertImage= new QAction(QIcon::fromTheme("", QIcon(":/images/insert-image.png")), tr("&Insert image"), this);
actionInsertImage->setPriority(QAction::LowPriority);
connect(actionInsertImage, SIGNAL(triggered()), this, SLOT(insertImage()));
actionInsertImage->setCheckable(true);
tb->addAction(actionInsertImage);
QPixmap pix(16, 16);
pix.fill(Qt::black);
actionTextColor = new QAction(pix, tr("&Color..."), this);
connect(actionTextColor, SIGNAL(triggered()), this, SLOT(textColor()));
tb->addAction(actionTextColor);
comboFont = new QFontComboBox(tb);
tb->addWidget(comboFont);
connect(comboFont, SIGNAL(activated(QString)), this, SLOT(textFamily(QString)));
comboSize = new QComboBox(tb);
comboSize->setObjectName("comboSize");
tb->addWidget(comboSize);
comboSize->setEditable(true);
QFontDatabase db;
foreach(int size, db.standardSizes())
comboSize->addItem(QString::number(size));
connect(comboSize, SIGNAL(activated(QString)), this, SLOT(textSize(QString)));
comboSize->setCurrentIndex(comboSize->findText(QString::number(QApplication::font().pointSize())));
}
void MainWindow::textBold()
{
QTextCharFormat fmt;
fmt.setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
mergeFormatOnWordOrSelection(fmt);
}
void MainWindow::textItalic()
{
QTextCharFormat fmt;
fmt.setFontItalic(actionTextItalic->isChecked());
mergeFormatOnWordOrSelection(fmt);
}
void MainWindow::textUnderline()
{
QTextCharFormat fmt;
fmt.setFontUnderline(actionTextUnderline->isChecked());
mergeFormatOnWordOrSelection(fmt);
}
void MainWindow::textAlign(QAction *a)
{
QTextEdit *editor;
int i = -1;
i = tabcontainer->currentIndex();
if (i == 0)
editor = noteditor;
else if (i == 2)
editor = contacteditor;
else if (i == 3)
editor = listeditor;
else
return;
if (a == actionAlignLeft)
editor->setAlignment(Qt::AlignLeft | Qt::AlignAbsolute);
else if (a == actionAlignCenter)
editor->setAlignment(Qt::AlignHCenter);
else if (a == actionAlignRight)
editor->setAlignment(Qt::AlignRight | Qt::AlignAbsolute);
else if (a == actionAlignJustify)
editor->setAlignment(Qt::AlignJustify);
}
void MainWindow::textColor()
{
QTextEdit *editor;
int i = -1;
i = tabcontainer->currentIndex();
if (i == 0)
editor = noteditor;
else if (i == 2)
editor = contacteditor;
else if (i == 3)
editor = listeditor;
else
return;
QColor col = QColorDialog::getColor(editor->textColor(), this);
if (!col.isValid())
return;
QTextCharFormat fmt;
fmt.setForeground(col);
mergeFormatOnWordOrSelection(fmt);
colorChanged(col);
}
void MainWindow::insertImage()
{
QString s, filters, fname;
QFileInfo fi;
QTextEdit *editor;
QMessageBox msgBox;
int i;
i = tabcontainer->currentIndex();
if (i == 0)
editor = noteditor;
else if (i == 2)
editor = contacteditor;
else if (i == 3)
editor = listeditor;
else
return;
filters += tr("Common Graphics (*.png *.jpg *.jpeg *.gif);;");
filters += tr("Portable Network Graphics (PNG) (*.png);;");
filters += tr("JPEG (*.jpg *.jpeg);;");
filters += tr("Graphics Interchange Format (*.gif);;");
filters += tr("All Files (*)");
QString file = QFileDialog::getOpenFileName(this, tr("Open image..."), QString(), filters);
if (file.isEmpty())
return;
if (!QFile::exists(file))
return;
fi = QFileInfo(file);
if (fi.path() != Homepath) {
//copy the file to the data subdirectory
fname.clear();
fname.append(Homepath);
fname.append("/");
fname.append(fi.fileName());
QFile::copy (fi.filePath(), fname);
s.append (QObject::tr("The image file has been copied to the tdj data directory "));
s.append (Homepath);
s.append (QObject::tr("\nClick OK to continue"));
msgBox.setText(s);
msgBox.exec();
}
editor->insertHtml(QString ("<img src=\"%1/%2\">").arg(Homepath).arg(fi.fileName()));
}
void MainWindow::cursorPositionChanged()
{
QTextEdit *editor;
int i = -1;
i = tabcontainer->currentIndex();
if (i == 0)
editor = noteditor;
else if (i == 2)
editor = contacteditor;
else if (i == 3)
editor = listeditor;
else
return;
alignmentChanged(editor->alignment());
}
void MainWindow::currentCharFormatChanged(const QTextCharFormat &format)
{
fontChanged(format.font());
colorChanged(format.foreground().color());
}
void MainWindow::fontChanged(const QFont &f)
{
comboFont->setCurrentIndex(comboFont->findText(QFontInfo(f).family()));
comboSize->setCurrentIndex(comboSize->findText(QString::number(f.pointSize())));
actionTextBold->setChecked(f.bold());
actionTextItalic->setChecked(f.italic());
actionTextUnderline->setChecked(f.underline());
}
void MainWindow::colorChanged(const QColor &c)
{
QPixmap pix(16, 16);
pix.fill(c);
actionTextColor->setIcon(pix);
}
void MainWindow::alignmentChanged(Qt::Alignment a)
{
if (a & Qt::AlignLeft)
actionAlignLeft->setChecked(true);
else if (a & Qt::AlignHCenter)
actionAlignCenter->setChecked(true);
else if (a & Qt::AlignRight)
actionAlignRight->setChecked(true);
else if (a & Qt::AlignJustify)
actionAlignJustify->setChecked(true);
}
void MainWindow::textFamily(const QString &f)
{
QTextCharFormat fmt;
fmt.setFontFamily(f);
mergeFormatOnWordOrSelection(fmt);
}
void MainWindow::textSize(const QString &p)
{
qreal pointSize = p.toFloat();
if (p.toFloat() > 0) {
QTextCharFormat fmt;
fmt.setFontPointSize(pointSize);
mergeFormatOnWordOrSelection(fmt);
}
}
void MainWindow::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
{
QTextEdit *editor;
int i = -1;
i = tabcontainer->currentIndex();
if (i == 0)
editor = noteditor;
else if (i == 2)
editor = contacteditor;
else if (i == 3)
editor = listeditor;
else
return;
QTextCursor cursor = editor->textCursor();
if (!cursor.hasSelection())
cursor.select(QTextCursor::WordUnderCursor);
cursor.mergeCharFormat(format);
editor->mergeCurrentCharFormat(format);
}