-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
133 lines (115 loc) · 3.58 KB
/
Copy pathmainwindow.cpp
File metadata and controls
133 lines (115 loc) · 3.58 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDir>
#include <QSettings>
#include <QWebEngineView>
#include <mecab.h>
#include "autocursor.h"
#include "preferencedialog.h"
#include "speechcounter.h"
#include "speechdialog.h"
#include "utility.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
#if defined(Q_OS_LINUX)
settings(new QSettings()),
#elif defined(Q_OS_WIN32) || defined(Q_OS_CYGWIN) || defined(Q_OS_MAC)
settings(new QSettings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName())),
#endif
preference(new PreferenceDialog(this->settings)),
tagger(nullptr),
speechCounter(),
speechDialog(new SpeechDialog)
{
this->ui->setupUi(this);
this->initializeTagger(QString());
}
MainWindow::~MainWindow()
{
if ( this->tagger ) {
MeCab::deleteTagger(this->tagger);
}
}
void MainWindow::initializeTagger(QString dictPath, QString userDictPath)
{
if ( this->tagger ) {
this->speechDialog->close();
this->speechCounter.reset();
this->speechDialog->setSpeechCounter(this->speechCounter);
MeCab::deleteTagger(this->tagger);
}
if ( dictPath.isEmpty() ) {
dictPath = this->preference->getSysDictDir();
}
if ( userDictPath.isEmpty() ) {
userDictPath = this->preference->getUserDictFile();
}
QDir appDir(QCoreApplication::applicationDirPath());
#if defined(Q_OS_MAC)
appDir.cdUp(); // MacOS
appDir.cdUp(); // Contents
appDir.cdUp(); // *.app
#endif
// build args
QStringList taggerArgs;
taggerArgs.append(QCoreApplication::applicationFilePath()); // argv[0]
taggerArgs.append("-U");
taggerArgs.append("Unknown");
taggerArgs.append("-r");
taggerArgs.append(appDir.filePath("mecabrc"));
taggerArgs.append("-d");
taggerArgs.append(dictPath + "/");
if ( ! userDictPath.isEmpty() ) {
taggerArgs.append("-u");
taggerArgs.append(userDictPath);
}
// unicode args to raw args
QList<QByteArray> args;
for ( const QString &arg : taggerArgs ) {
args.append(toLocalEncoding(arg));
}
// raw args to argv
QScopedPointer<char *> argv(new char *[args.size()]);
int argc = 0;
for ( QByteArray &arg : args ) {
argv.data()[argc++] = arg.data();
}
// create tagger
this->tagger = MeCab::Tagger::create(argc, argv.data());
}
void MainWindow::on_actionShowRuby_triggered()
{
if ( ! this->speechCounter ) {
return;
}
this->speechDialog->setSpeechCounter(this->speechCounter);
this->speechDialog->show();
}
void MainWindow::on_actionPreference_triggered()
{
if ( this->preference->exec() == QDialog::Accepted ) {
this->preference->saveSettings();
this->initializeTagger(QString()); // from new settings
}
}
void MainWindow::setTextInformation(int charCount, double speechCount)
{
QString message;
if ( speechCount < 0 ) {
message = tr("%1 chars").arg(charCount);
} else {
message = tr("%1 chars, %2 syllable").arg(charCount).arg(speechCount);
}
this->ui->statusBar->showMessage(message, 0);
}
void MainWindow::on_textEdit_textChanged()
{
BusyAutoCursor cursor(this);
Q_UNUSED(cursor);
QString text = this->ui->textEdit->toPlainText().normalized(QString::NormalizationForm_KC);
QString serialized(text);
serialized.remove(QRegExp("\\s+"));
this->speechCounter = QSharedPointer<SpeechCounter>(new SpeechCounter(this->tagger, text));
this->setTextInformation(serialized.size(), this->speechCounter->getSpeechCount());
}