-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
109 lines (88 loc) · 2.89 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
#include "helpdialog.h"
#include "mainwindow.h"
#include "parser.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QPrinter>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle(APP_NAME);
QFont sourceTextareaFont("Consolas, 'Fira Mono', 'Source Code Pro', Monospace", 10);
QFont targetTextareaFont("'Linux Libertine', Georgia, sans-serif", 10);
ui->sourceTextarea->setFont(sourceTextareaFont);
ui->targetTextarea->setFont(targetTextareaFont);
ui->targetTextarea->setOpenExternalLinks(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionHelp_triggered()
{
HelpDialog *helpDialog = new HelpDialog(this);
helpDialog->show();
helpDialog->raise();
helpDialog->activateWindow();
}
void MainWindow::on_actionSave_HTML_triggered()
{
QString filePath = this->getSaveFilePath("html");
if (filePath == nullptr) {
return;
}
QFile fileToSave(filePath);
fileToSave.open(QIODevice::WriteOnly);
fileToSave.write(ui->targetTextarea->toHtml().toLocal8Bit());
fileToSave.close();
ui->statusBar->showMessage(tr("Parsed content saved to: %1").arg(filePath));
}
void MainWindow::on_actionSave_PDF_triggered()
{
QString filePath = this->getSaveFilePath("pdf");
if (filePath == nullptr) {
return;
}
QPrinter printer(QPrinter::PrinterResolution);
printer.setOutputFileName(filePath);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPageSize(QPrinter::A4);
printer.setPaperSize(QPrinter::A4);
QTextDocument doc;
doc.setHtml(ui->targetTextarea->toHtml());
doc.print(&printer);
ui->statusBar->showMessage(tr("Parsed content saved to: %1").arg(filePath));
}
void MainWindow::on_sourceTextarea_textChanged()
{
// update status bar
QString editorText = ui->sourceTextarea->toPlainText();
QRegExp separator("( |\n)");
QStringList words = editorText.split(separator);
words.removeAll("");
this->updateStatusBar(editorText.count(), words.count());
// update target editor text
Parser *parser = new Parser(editorText);
ui->targetTextarea->setHtml(parser->getFormattedText());
}
QString MainWindow::getSaveFilePath(QString ext)
{
QFileDialog saveDialog(this, tr("Choose location to save %1 file...").arg(ext.toUpper()));
saveDialog.setAcceptMode(QFileDialog::AcceptMode::AcceptSave);
saveDialog.setDefaultSuffix(ext);
saveDialog.setDirectory(QDir::home());
saveDialog.setNameFilter(tr("%1 file (*.%2)").arg(ext.toUpper()).arg(ext));
if (saveDialog.exec()) {
return saveDialog.selectedFiles().first();
} else {
return nullptr;
}
}
void MainWindow::updateStatusBar(int chars, int words)
{
ui->statusBar->showMessage(tr("Characters: %1 | Words: %2").arg(chars).arg(words));
}