forked from vnotex/vnote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvgraphvizhelper.cpp
More file actions
144 lines (118 loc) · 4.44 KB
/
vgraphvizhelper.cpp
File metadata and controls
144 lines (118 loc) · 4.44 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
134
135
136
137
138
139
140
141
142
143
144
#include "vgraphvizhelper.h"
#include <QDebug>
#include <QThread>
#include "vconfigmanager.h"
#include "utils/vprocessutils.h"
extern VConfigManager *g_config;
#define TaskIdProperty "GraphvizTaskId"
#define TaskFormatProperty "GraphvizTaskFormat"
#define TaskTimeStampProperty "GraphvizTaskTimeStamp"
VGraphvizHelper::VGraphvizHelper(QObject *p_parent)
: QObject(p_parent)
{
prepareCommand(m_program, m_args);
}
void VGraphvizHelper::processAsync(int p_id, TimeStamp p_timeStamp, const QString &p_format, const QString &p_text)
{
QProcess *process = new QProcess(this);
process->setProperty(TaskIdProperty, p_id);
process->setProperty(TaskTimeStampProperty, p_timeStamp);
process->setProperty(TaskFormatProperty, p_format);
connect(process, SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(handleProcessFinished(int, QProcess::ExitStatus)));
QStringList args(m_args);
args << ("-T" + p_format);
qDebug() << m_program << args;
process->start(m_program, args);
if (process->write(p_text.toUtf8()) == -1) {
qWarning() << "fail to write to QProcess:" << process->errorString();
}
process->closeWriteChannel();
}
void VGraphvizHelper::prepareCommand(QString &p_program, QStringList &p_args) const
{
const QString &dot = g_config->getGraphvizDot();
if (dot.isEmpty()) {
p_program = "dot";
} else {
p_program = dot;
}
p_args.clear();
}
void VGraphvizHelper::handleProcessFinished(int p_exitCode, QProcess::ExitStatus p_exitStatus)
{
QProcess *process = static_cast<QProcess *>(sender());
int id = process->property(TaskIdProperty).toInt();
QString format = process->property(TaskFormatProperty).toString();
TimeStamp timeStamp = process->property(TaskTimeStampProperty).toULongLong();
qDebug() << QString("Graphviz finished: id %1 timestamp %2 format %3 exitcode %4 exitstatus %5")
.arg(id)
.arg(timeStamp)
.arg(format)
.arg(p_exitCode)
.arg(p_exitStatus);
bool failed = true;
if (p_exitStatus == QProcess::NormalExit) {
if (p_exitCode < 0) {
qWarning() << "Graphviz fail" << p_exitCode;
} else {
failed = false;
QByteArray outBa = process->readAllStandardOutput();
if (format == "svg") {
emit resultReady(id, timeStamp, format, QString::fromLocal8Bit(outBa));
} else {
emit resultReady(id, timeStamp, format, QString::fromLocal8Bit(outBa.toBase64()));
}
}
} else {
qWarning() << "fail to start Graphviz process" << p_exitCode << p_exitStatus;
}
QByteArray errBa = process->readAllStandardError();
if (!errBa.isEmpty()) {
QString errStr(QString::fromLocal8Bit(errBa));
if (failed) {
qWarning() << "Graphviz stderr:" << errStr;
} else {
qDebug() << "Graphviz stderr:" << errStr;
}
}
if (failed) {
emit resultReady(id, timeStamp, format, "");
}
process->deleteLater();
}
bool VGraphvizHelper::testGraphviz(const QString &p_dot, QString &p_msg)
{
QString program(p_dot);
QStringList args;
args << "-Tsvg";
QString testGraph("digraph G {VNote->Markdown}");
int exitCode = -1;
QByteArray out, err;
int ret = VProcessUtils::startProcess(program, args, testGraph.toUtf8(), exitCode, out, err);
p_msg = QString("Command: %1 %2\nExitCode: %3\nOutput: %4\nError: %5")
.arg(program)
.arg(args.join(' '))
.arg(exitCode)
.arg(QString::fromLocal8Bit(out))
.arg(QString::fromLocal8Bit(err));
return ret == 0 && exitCode == 0;
}
QByteArray VGraphvizHelper::process(const QString &p_format, const QString &p_text)
{
VGraphvizHelper inst;
int exitCode = -1;
QByteArray out, err;
QStringList args(inst.m_args);
args << ("-T" + p_format);
int ret = VProcessUtils::startProcess(inst.m_program,
args,
p_text.toUtf8(),
exitCode,
out,
err);
if (ret != 0 || exitCode < 0) {
qWarning() << "Graphviz fail" << ret << exitCode << QString::fromLocal8Bit(err);
}
return out;
}