-
Notifications
You must be signed in to change notification settings - Fork 6
/
mainwindow.cpp
573 lines (471 loc) · 17.5 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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QHeaderView *header = ui->tableWidget->horizontalHeader();
header->setResizeMode(QHeaderView::Stretch);
makeDBConnection();
ui->keyboardFrame->setHidden(true);
initAlphabet();
on_ArButton_clicked(); // Load Arabic by default
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::makeDBConnection()
{
QString path = "data/univlexique.db";
if(QFile::exists(path))
{
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(path);
if(!db.open())
{
QMessageBox::critical(this, tr("Error"), tr("Could not open database!"));
return;
}
}
}
QString MainWindow::generateHeader()
{
QString html =
tr("<html>") +
"<head>"
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
"<title>[Nibras]</title>"
"</head>"
"<body >"
"<div id=\"header\">"
"<p align=\"center\">"
+ tr("Nibras search result for the term: ") + ui->searchLineEdit->text() +
" </p>"
"<br />"
"<br />"
"</div>"
" </body>"
"</html>";
return html;
}
void MainWindow::generateBody()
{
document = new QTextDocument(this);
QTextCursor cursor(document);
cursor.movePosition(QTextCursor::Start);
QString column1Title = ui->tableWidget->horizontalHeaderItem(0)->text();
QString column2Title = ui->tableWidget->horizontalHeaderItem(1)->text();
QTextTableFormat tableFormat;
tableFormat.setAlignment(Qt::AlignHCenter);
tableFormat.setCellPadding(5);
tableFormat.setCellSpacing(0);
tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
QBrush brush(Qt::SolidPattern);
brush.setColor(Qt::black);
tableFormat.setBorderBrush(brush);
tableFormat.setBorder(1);
QVector<QTextLength> columnWidth;
columnWidth.append(QTextLength(QTextLength::PercentageLength, 50));
columnWidth.append(QTextLength(QTextLength::PercentageLength, 50));
tableFormat.setColumnWidthConstraints(columnWidth);
QTextTable *tTable = cursor.insertTable(2, 2);
tTable->setFormat(tableFormat);
QTextBlockFormat centerAlignment;
centerAlignment.setAlignment(Qt::AlignHCenter);
QTextBlockFormat leftAlignment;
leftAlignment.setAlignment(Qt::AlignLeft);
QTextBlockFormat rightAlignment;
rightAlignment.setAlignment(Qt::AlignRight);
QTextCharFormat boldFormat;
boldFormat.setFontWeight(QFont::Bold);
cursor.setBlockFormat(centerAlignment);
cursor.insertText(column1Title, boldFormat);
cursor.movePosition(QTextCursor::NextCell);
cursor.setBlockFormat(centerAlignment);
cursor.insertText(column2Title, boldFormat);
for(int i = 0; i < ui->tableWidget->rowCount(); i++)
{
tTable->appendRows(1);
cursor.movePosition(QTextCursor::NextCell);
cursor.setBlockFormat(leftAlignment);
cursor.insertText(ui->tableWidget->item(i,0)->text());
cursor.movePosition(QTextCursor::NextCell);
cursor.setBlockFormat(leftAlignment);
cursor.insertText(ui->tableWidget->item(i,1)->text());
}
tTable->removeRows(tTable->rows()-1, 1);
cursor.movePosition(QTextCursor::End);
}
void MainWindow::on_actionExit_triggered()
{
exit(0);
}
void MainWindow::clearTable()
{
ui->tableWidget->clearContents();
ui->tableWidget->setRowCount(0);
}
void MainWindow::on_searchLineEdit_textChanged()
{
on_searchButton_clicked();
}
void MainWindow::on_searchButton_clicked()
{
clearTable();
QString arg = ui->searchLineEdit->text().trimmed();
if(arg.isEmpty()) return;
// don't change these lines unless you ensure that this file is encoded with UTF-8
QString fatha = QString::fromUtf8("َ");
QString damma = QString::fromUtf8("ُ");
QString fathatin = QString::fromUtf8("ً");
QString dammatin = QString::fromUtf8("ٌ");
QString kasra = QString::fromUtf8("ِ");
QString kasratin = QString::fromUtf8("ٍ");
QString sukun = QString::fromUtf8("ْ");
// remove tashkeel
arg = arg.remove(fatha);
arg.remove(damma);
arg.remove(fatha);
arg.remove(fathatin);
arg.remove(dammatin);
arg.remove(kasra);
arg.remove(kasratin);
arg.remove(sukun);
QSqlQuery query;
if(alphabetQuery) // have to be true when the user clicks on one of the virtual keyboard alphabet button
{
// if the user clicks a specific alphabet button, the Query has to show all words that *begin* with
// the clicked button's letter
query.exec(QString("SELECT FRENCH, ARABIC FROM univ "
"WHERE FRENCH LIKE \"%1%\" OR ARABIC LIKE \"%1%\" COLLATE NOCASE").arg(arg));
alphabetQuery = false; // back to normal mode
}
else
{
// the query here shows all words that *contains* the arg
query.exec(QString("SELECT FRENCH, ARABIC FROM univ "
"WHERE FRENCH LIKE \"%%1%\" OR ARABIC LIKE \"%%1%\" COLLATE NOCASE").arg(arg));
}
int i = 0;
// in case the user pressed on zoom in/out let's keep the same rows height
QFont font = ui->tableWidget->font();
while(query.next())
{
ui->tableWidget->insertRow(i);
// keep the row heigth to fit the font size
ui->tableWidget->setRowHeight(i, font.pointSize() * 3);
ui->tableWidget->setItem(i, 0, new QTableWidgetItem());
ui->tableWidget->item(i, 0)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
ui->tableWidget->item(i, 0)->setTextAlignment(Qt::AlignVCenter|Qt::AlignLeft);
ui->tableWidget->item(i, 0)->setText(query.value(0).toString());
ui->tableWidget->setItem(i, 1, new QTableWidgetItem());
ui->tableWidget->item(i, 1)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
ui->tableWidget->item(i, 1)->setTextAlignment(Qt::AlignVCenter|Qt::AlignRight);
ui->tableWidget->item(i, 1)->setText(query.value(1).toString());
i++;
}
}
void MainWindow::on_actionZoom_in_triggered()
{
QFont font = ui->tableWidget->font();
font.setPointSize(font.pointSize() + 1);
ui->tableWidget->setFont(font);
for(int i = 0; i < ui->tableWidget->rowCount(); i++)
ui->tableWidget->setRowHeight(i, font.pointSize() * 3);
}
void MainWindow::on_actionZoom_out_triggered()
{
QFont font = ui->tableWidget->font();
font.setPointSize(font.pointSize() - 1);
ui->tableWidget->setFont(font);
for(int i = 0; i < ui->tableWidget->rowCount(); i++)
ui->tableWidget->setRowHeight(i, font.pointSize() * 3);
}
void MainWindow::on_actionCheck_for_update_triggered()
{
Updates up;
up.exec();
}
void MainWindow::on_actionFonts_triggered()
{
bool ok;
QFont font = QFontDialog::getFont(
&ok, ui->tableWidget->font(), this);
if (ok) {
ui->tableWidget->setFont(font);
for(int i = 0; i < ui->tableWidget->rowCount(); i++)
ui->tableWidget->setRowHeight(i, font.pointSize() * 3);
}
}
void MainWindow::on_actionSuggest_New_triggered()
{
QDesktopServices::openUrl(QUrl("http://nibras.sourceforge.net/"));
}
void MainWindow::setButtonContentToSearch(QAbstractButton *button)
{
// The query concerns alphabet, it has to show results that *start* with the given letter
alphabetQuery = true;
/*
Clear the search field to ensure that a "text changed" signal will be emitted
even when the old content is the same as the new one.
*/
ui->searchLineEdit->clear();
// set the button text as the search line edit content, this will perform a search as the text is changed.
ui->searchLineEdit->setText(button->text());
}
void MainWindow::initAlphabet()
{
// Add all buttons inside the virtual keyboard frame
QList<QPushButton *> buttonsList = ui->keyboardFrame->findChildren<QPushButton *>();
foreach (QPushButton *button, buttonsList) {
buttonGroup.addButton(button);
}
// The alphabet is hidden by default
alphabetIsShown = false;
// the search query doesn't concern alphabets for the first time
alphabetQuery = false;
// Catch every alphabet button "clicked" signals
connect(&buttonGroup, SIGNAL(buttonClicked(QAbstractButton*)), SLOT(setButtonContentToSearch(QAbstractButton*)));
}
void MainWindow::on_showAlphabetButton_clicked()
{
if(alphabetIsShown) // then hide it
{
ui->keyboardFrame->setHidden(true);
ui->showAlphabetButton->setText(tr("Show alphabet +"));
alphabetIsShown = false;
}
else // then show it
{
ui->keyboardFrame->setHidden(false);
ui->showAlphabetButton->setText(tr("Hide alphabet -"));
alphabetIsShown= true;
}
}
void MainWindow::on_actionCopy_triggered()
{
QClipboard *clipboard = QApplication::clipboard();
QString terms;
int rows = ui->tableWidget->selectedItems().count();
// if the user has selected specific cells, then only copy selected cells
if(rows != 0)
{
for(int i = 0; i < ui->tableWidget->rowCount(); i++)
{
if(ui->tableWidget->item(i, 0)->isSelected() ||
ui->tableWidget->item(i, 1)->isSelected())
{
terms += ui->tableWidget->item(i, 0)->text();
terms += " " + ui->tableWidget->item(i, 1)->text() + "\r\n";
}
}
}
else // copy the whole table
{
for(int i = 0; i < ui->tableWidget->rowCount(); i++)
{
terms += ui->tableWidget->item(i, 0)->text();
terms += " ";
terms += ui->tableWidget->item(i, 1)->text();
terms += "\r\n";
}
}
clipboard->setText(terms);
}
void MainWindow::on_actionPrint_triggered()
{
textPrinter = new TextPrinter(this);
textPrinter->setHeaderSize(20);
textPrinter->setFooterSize(19);
textPrinter->setSpacing(0);
textPrinter->setPageSize(QPrinter::A4);
textPrinter->setLeftMargin(3);
textPrinter->setTopMargin(2);
textPrinter->setRightMargin(7);
textPrinter->setBottomMargin(5);
textPrinter->pageSize();
generateBody();
textPrinter->setHeaderText(generateHeader());
textPrinter->setFooterText(generateFooter());
textPrinter->print(document, tr("Preview before printing of the search result for the term: ")
+ ui->searchLineEdit->text().trimmed());
}
QString MainWindow::generateFooter()
{
QString html =
"<html>"
"<head></head>"
"<body>"
"<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
"<tr>"
"<td><p align=\"left\">" + QDate::currentDate().toString("ddd MMMM d yy") + "</p></td>"
"<td><p align=\"right\">" + tr("Page: ") + "&page;" + "</p></td>"
"</tr>"
"</table>"
"</body>"
"</html>";
return html;
}
void MainWindow::on_actionExport_To_HTML_triggered()
{
QString path = QFileDialog::getSaveFileName(this, tr("Export to HTML"), tr("[Nibras] ") +
ui->searchLineEdit->text().trimmed(), "HTML (*.html)");
if(!path.isEmpty())
{
// header
QString html =
tr("<html>") +
"<head>"
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1256\" />"
"<title>[Nibras]</title>"
"</head>"
"<body >"
"<div id=\"header\">"
"<p align=\"center\">"
+ tr("Nibras search result for the term: ") + ui->searchLineEdit->text() +
" </p>"
"<br />"
"<br />"
"</div>";
//body
generateBody();
QString body = document->toHtml();
body.remove(0, body.indexOf("<table"));
body.remove(body.indexOf("</body>"), body.length() - body.indexOf("</html>") + 7);
html += body.toUtf8();
// footer
html += "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
"<tr>"
"<td><p align=\"left\">" + QDate::currentDate().toString("ddd MMMM d yy") + "</p></td>"
"</tr>"
"</table>"
"</body>"
"</html>";
QFile file(path);
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
out << html;
}
}
void MainWindow::loadLanguage(const QString &rLanguage)
{
if(m_currLang != rLanguage)
{
m_currLang = rLanguage;
QLocale locale = QLocale(m_currLang);
QLocale::setDefault(locale);
switchTranslator(m_translator, QString("Daleel_%1.qm").arg(rLanguage));
switchTranslator(m_translatorQt, QString("qt_%1.qm").arg(rLanguage));
}
}
void MainWindow::switchTranslator(QTranslator &translator, const QString &filename)
{
// remove the old translator
qApp->removeTranslator(&translator);
// load the new translator
if(translator.load(filename))
qApp->installTranslator(&translator);
}
void MainWindow::changeEvent(QEvent *event)
{
if(0 != event)
{
if(event->type() == QEvent::LanguageChange)
{
// this event is send if a translator is loaded
ui->retranslateUi(this);
}
}
}
void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
if(ui->tableWidget->underMouse() && ui->tableWidget->rowCount() != 0)
{
QMenu context(ui->tableWidget);
QAction* copyAction = new QAction(tr("Copy"), &context);
connect(copyAction, SIGNAL(triggered()), this, SLOT(copyCell()));
QAction* copyRowAction = new QAction(tr("Copy Row"), &context);
connect(copyRowAction, SIGNAL(triggered()), this, SLOT(copyRow()));
QAction* copyTable = new QAction(tr("Copy the whole table"), &context);
connect(copyTable, SIGNAL(triggered()), this, SLOT(copyTable()));
context.addAction(copyAction);
context.addAction(copyRowAction);
context.addSeparator();
context.addAction(copyTable);
context.exec(event->globalPos());
}
}
void MainWindow::on_FRButton_clicked()
{
loadLanguage("fr");
qApp->setLayoutDirection(Qt::LeftToRight);
this->addToolBar(Qt::LeftToolBarArea, ui->mainToolBar);
ui->baseFrame->setStyleSheet("#baseFrame {"
"background: #414141;"
"border-left: 1px solid #4b4b4b;}");
}
void MainWindow::on_ArButton_clicked()
{
loadLanguage("ar");
qApp->setLayoutDirection(Qt::RightToLeft);
this->addToolBar(Qt::RightToolBarArea, ui->mainToolBar);
ui->baseFrame->setStyleSheet("#baseFrame {"
"background: #414141;"
"border-right: 1px solid #4b4b4b;}");
// keep the layout direction of the following widgets to LTR
ui->keyboardFrame->setLayoutDirection(Qt::LeftToRight);
ui->tableWidget->setLayoutDirection(Qt::LeftToRight);
}
void MainWindow::on_actionPrintPreview_triggered()
{
textPrinter = new TextPrinter(this);
textPrinter->setHeaderSize(20);
textPrinter->setFooterSize(19);
textPrinter->setSpacing(0);
textPrinter->setPageSize(QPrinter::A4);
textPrinter->setLeftMargin(3);
textPrinter->setTopMargin(2);
textPrinter->setRightMargin(7);
textPrinter->setBottomMargin(5);
textPrinter->pageSize();
generateBody();
textPrinter->setHeaderText(generateHeader());
textPrinter->setFooterText(generateFooter());
textPrinter->preview(document, tr("Preview before printing of the search result for the term: ")
+ ui->searchLineEdit->text().trimmed());
}
void MainWindow::on_actionAbout_the_program_triggered()
{
About about;
about.exec();
}
void MainWindow::copyCell()
{
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(ui->tableWidget->currentItem()->text());
}
void MainWindow::copyRow()
{
QClipboard *clipboard = QApplication::clipboard();
QString terms;
int i = ui->tableWidget->currentItem()->row();
terms += ui->tableWidget->item(i, 0)->text();
terms += " " + ui->tableWidget->item(i, 1)->text() + "\r\n";
clipboard->setText(terms);
}
void MainWindow::copyTable()
{
QClipboard *clipboard = QApplication::clipboard();
QString terms;
for(int i = 0; i < ui->tableWidget->rowCount(); i++)
{
terms += ui->tableWidget->item(i, 0)->text();
terms += " ";
terms += ui->tableWidget->item(i, 1)->text();
terms += "\r\n";
}
clipboard->setText(terms);
}