-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathmathassistant.cpp
87 lines (78 loc) · 2.73 KB
/
mathassistant.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
#include "mathassistant.h"
#include "smallUsefulFunctions.h"
#include <QMutex>
MathAssistant *MathAssistant::m_Instance = nullptr;
MathAssistant::MathAssistant() :
QObject()
{
connect(&process, SIGNAL(errorOccurred(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
}
void MathAssistant::exec()
{
if (process.state() == QProcess::Running) return;
// return value of TexTablet is always zero, so we have to decide on the clipboard contents, if TexTablet was aborted
lastClipboardText = QApplication::clipboard()->text();
QApplication::clipboard()->clear();
//TODO This is the default installation path of TexTablet. Make it configurabe.
QString texTablet = QCoreApplication::applicationDirPath() + "/TexTablet/TeXTablet.exe";
if (!QFileInfo(texTablet).exists()) {
QString msg = QString(tr("TexTablet not found."));
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Math Assistant"));
msgBox.setTextFormat(Qt::RichText);
msgBox.setText(msg);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec();
return;
}
process.setWorkingDirectory(QFileInfo(texTablet).absolutePath());
process.start("\"" + texTablet + "\"",QStringList());
}
void MathAssistant::processError(QProcess::ProcessError err)
{
switch (err) {
case QProcess::FailedToStart:
QMessageBox::information(nullptr, "FailedToStart", "FailedToStart");
break;
case QProcess::Crashed:
QMessageBox::information(nullptr, "Crashed", "Crashed");
break;
case QProcess::Timedout:
QMessageBox::information(nullptr, "Timedout", "Timedout");
break;
case QProcess::WriteError:
QMessageBox::information(nullptr, "WriteError", "WriteError");
break;
case QProcess::ReadError:
QMessageBox::information(nullptr, "ReadError", "ReadError");
break;
case QProcess::UnknownError:
QMessageBox::information(nullptr, "UnknownError", "UnknownError");
break;
default:
QMessageBox::information(nullptr, "default", "default");
break;
}
}
void MathAssistant::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
Q_UNUSED(exitCode)
if (exitStatus != QProcess::NormalExit) {
UtilsUi::txsCritical(tr("TexTablet crashed."));
}
// exit code is always zero for TexTablet
QString text = QApplication::clipboard()->text();
if (text.startsWith("$"))
emit formulaReceived(text);
QApplication::clipboard()->setText(lastClipboardText);
}
MathAssistant *MathAssistant::instance()
{
static QMutex mutex;
mutex.lock();
if (!m_Instance)
m_Instance = new MathAssistant();
mutex.unlock();
return m_Instance;
}