浏览代码

Merge QProcess scripting from packaging branch

Neal Wilson 9 年前
父节点
当前提交
050e31d5c3
共有 1 个文件被更改,包括 124 次插入0 次删除
  1. 124
    0
      src/typica.w

+ 124
- 0
src/typica.w 查看文件

@@ -2077,6 +2077,130 @@ QScriptValue QIODevice_read(QScriptContext *context, QScriptEngine *engine)
2077 2077
 	return value;
2078 2078
 }
2079 2079
 
2080
+@* Scripting QProcess.
2081
+
2082
+\noindent Sometimes it is useful to have \pn work with an external program.
2083
+The initial use case was document generation by typesetting instructions to a
2084
+file and then running \TeX to generate a shelf sign or a sheet of labels.
2085
+Other likely use cases include interfacing with external programs that output
2086
+measurement streams. There are several methods which we may want to expose,
2087
+however this is being done only as needed.
2088
+
2089
+@<Function prototypes for scripting@>=
2090
+QScriptValue constructQProcess(QScriptContext *context, QScriptEngine *engine);
2091
+void setQProcessProperties(QScriptValue value, QScriptEngine *engine);
2092
+QScriptValue QProcess_execute(QScriptContext *context, QScriptEngine *engine);
2093
+QScriptValue QProcess_startDetached(QScriptContext *context, QScriptEngine *engine);
2094
+QScriptValue QProcess_setWorkingDirectory(QScriptContext *context, QScriptEngine *engine);
2095
+QScriptValue QProcess_start(QScriptContext *context, QScriptEngine *engine);
2096
+
2097
+@ We follow the same pattern with this as with many other types.
2098
+
2099
+@<Set up the scripting engine@>=
2100
+constructor = engine->newFunction(constructQProcess);
2101
+value = engine->newQMetaObject(&QProcess::staticMetaObject, constructor);
2102
+engine->globalObject().setProperty("QProcess", value);
2103
+
2104
+@ The constructor is trivial.
2105
+
2106
+@<Functions for scripting@>=
2107
+QScriptValue constructQProcess(QScriptContext *context, QScriptEngine *engine)
2108
+{
2109
+	QScriptValue object = engine->newQObject(new QProcess);
2110
+	setQProcessProperties(object, engine);
2111
+	return object;
2112
+}
2113
+
2114
+@ As |QProcess| is a |QIODevice| we inherit some properties from that. We also
2115
+expose some details that are specific to |QProcess|.
2116
+
2117
+@<Functions for scripting@>=
2118
+void setQProcessProperties(QScriptValue value, QScriptEngine *engine)
2119
+{
2120
+	setQIODeviceProperties(value, engine);
2121
+	value.setProperty("execute", engine->newFunction(QProcess_execute));
2122
+	value.setProperty("startDetached", engine->newFunction(QProcess_startDetached));
2123
+	value.setProperty("setWorkingDirectory", engine->newFunction(QProcess_setWorkingDirectory));
2124
+	value.setProperty("start", engine->newFunction(QProcess_start));
2125
+}
2126
+
2127
+@ The |execute()| method comes in two flavors: one with arguments and one without.
2128
+We always call the one with arguments and simply pass in an empty list if no
2129
+arguments are specified.
2130
+
2131
+@<Functions for scripting@>=
2132
+QScriptValue QProcess_execute(QScriptContext *context, QScriptEngine *engine)
2133
+{
2134
+	QProcess *self = getself<QProcess *>(context);
2135
+	QString program = argument<QString>(0, context);
2136
+	QStringList arguments = QStringList();
2137
+	if(context->argumentCount() > 1) {
2138
+		arguments = argument<QVariant>(1, context).toStringList();
2139
+	}
2140
+	int retval = self->execute(program, arguments);
2141
+	return QScriptValue(retval);
2142
+}
2143
+
2144
+@ Similarly |startDetached()| can be called in a few different ways.
2145
+
2146
+@<Functions for scripting@>=
2147
+QScriptValue QProcess_startDetached(QScriptContext *context, QScriptEngine *engine)
2148
+{
2149
+	QProcess *self = getself<QProcess *>(context);
2150
+	QString program = argument<QString>(0, context);
2151
+	QStringList arguments = QStringList();
2152
+	if(context->argumentCount() > 1) {
2153
+		arguments = argument<QVariant>(1, context).toStringList();
2154
+	}
2155
+	QString workingDirectory = "";
2156
+	if(context->argumentCount() > 2) {
2157
+		workingDirectory = argument<QString>(2, context);
2158
+	}
2159
+	bool retval;
2160
+	switch(context->argumentCount())
2161
+	{
2162
+		case 1:
2163
+			retval = self->startDetached(program);
2164
+			break;
2165
+		case 2:
2166
+			retval = self->startDetached(program, arguments);
2167
+			break;
2168
+		case 3:
2169
+			retval = self->startDetached(program, arguments, workingDirectory);
2170
+			break;
2171
+		default:
2172
+			retval = false;
2173
+	}
2174
+	return QScriptValue(retval);
2175
+}
2176
+
2177
+@ Sometimes we care about the working directory for our program.
2178
+
2179
+@<Functions for scripting@>=
2180
+QScriptValue QProcess_setWorkingDirectory(QScriptContext *context, QScriptEngine *engine)
2181
+{
2182
+	QProcess *self = getself<QProcess *>(context);
2183
+	QString directory = argument<QString>(0, context);
2184
+	self->setWorkingDirectory(directory);
2185
+	return QScriptValue();
2186
+}
2187
+
2188
+@ When using the |start()| method we always assume that we want read and write
2189
+access.
2190
+
2191
+@<Functions for scripting@>=
2192
+QScriptValue QProcess_start(QScriptContext *context, QScriptEngine *engine)
2193
+{
2194
+	QProcess *self = getself<QProcess *>(context);
2195
+	QString program = argument<QString>(0, context);
2196
+	QStringList arguments = QStringList();
2197
+	if(context->argumentCount() > 1) {
2198
+		arguments = argument<QVariant>(1, context).toStringList();
2199
+	}
2200
+	self->start(program, arguments);
2201
+	return QScriptValue();
2202
+}
2203
+
2080 2204
 @ In order to work with |QByteArray| this should also be exposed to the host
2081 2205
 environment.
2082 2206
 

正在加载...
取消
保存