Quellcode durchsuchen

Expose QProcess to host environment

Neal Wilson vor 10 Jahren
Ursprung
Commit
69ca2ac567
1 geänderte Dateien mit 124 neuen und 0 gelöschten Zeilen
  1. 124
    0
      src/typica.w

+ 124
- 0
src/typica.w Datei anzeigen

@@ -2038,6 +2038,130 @@ QScriptValue QIODevice_writeBytes(QScriptContext *context, QScriptEngine *)
2038 2038
 	return QScriptValue();
2039 2039
 }
2040 2040
 
2041
+@* Scripting QProcess.
2042
+
2043
+\noindent Sometimes it is useful to have \pn work with an external program.
2044
+The initial use case was document generation by typesetting instructions to a
2045
+file and then running \TeX to generate a shelf sign or a sheet of labels.
2046
+Other likely use cases include interfacing with external programs that output
2047
+measurement streams. There are several methods which we may want to expose,
2048
+however this is being done only as needed.
2049
+
2050
+@<Function prototypes for scripting@>=
2051
+QScriptValue constructQProcess(QScriptContext *context, QScriptEngine *engine);
2052
+void setQProcessProperties(QScriptValue value, QScriptEngine *engine);
2053
+QScriptValue QProcess_execute(QScriptContext *context, QScriptEngine *engine);
2054
+QScriptValue QProcess_startDetached(QScriptContext *context, QScriptEngine *engine);
2055
+QScriptValue QProcess_setWorkingDirectory(QScriptContext *context, QScriptEngine *engine);
2056
+QScriptValue QProcess_start(QScriptContext *context, QScriptEngine *engine);
2057
+
2058
+@ We follow the same pattern with this as with many other types.
2059
+
2060
+@<Set up the scripting engine@>=
2061
+constructor = engine->newFunction(constructQProcess);
2062
+value = engine->newQMetaObject(&QProcess::staticMetaObject, constructor);
2063
+engine->globalObject().setProperty("QProcess", value);
2064
+
2065
+@ The constructor is trivial.
2066
+
2067
+@<Functions for scripting@>=
2068
+QScriptValue constructQProcess(QScriptContext *context, QScriptEngine *engine)
2069
+{
2070
+	QScriptValue object = engine->newQObject(new QProcess);
2071
+	setQProcessProperties(object, engine);
2072
+	return object;
2073
+}
2074
+
2075
+@ As |QProcess| is a |QIODevice| we inherit some properties from that. We also
2076
+expose some details that are specific to |QProcess|.
2077
+
2078
+@<Functions for scripting@>=
2079
+void setQProcessProperties(QScriptValue value, QScriptEngine *engine)
2080
+{
2081
+	setQIODeviceProperties(value, engine);
2082
+	value.setProperty("execute", engine->newFunction(QProcess_execute));
2083
+	value.setProperty("startDetached", engine->newFunction(QProcess_startDetached));
2084
+	value.setProperty("setWorkingDirectory", engine->newFunction(QProcess_setWorkingDirectory));
2085
+	value.setProperty("start", engine->newFunction(QProcess_start));
2086
+}
2087
+
2088
+@ The |execute()| method comes in two flavors: one with arguments and one without.
2089
+We always call the one with arguments and simply pass in an empty list if no
2090
+arguments are specified.
2091
+
2092
+@<Functions for scripting@>=
2093
+QScriptValue QProcess_execute(QScriptContext *context, QScriptEngine *engine)
2094
+{
2095
+	QProcess *self = getself<QProcess *>(context);
2096
+	QString program = argument<QString>(0, context);
2097
+	QStringList arguments = QStringList();
2098
+	if(context->argumentCount() > 1) {
2099
+		arguments = argument<QVariant>(1, context).toStringList();
2100
+	}
2101
+	int retval = self->execute(program, arguments);
2102
+	return QScriptValue(retval);
2103
+}
2104
+
2105
+@ Similarly |startDetached()| can be called in a few different ways.
2106
+
2107
+@<Functions for scripting@>=
2108
+QScriptValue QProcess_startDetached(QScriptContext *context, QScriptEngine *engine)
2109
+{
2110
+	QProcess *self = getself<QProcess *>(context);
2111
+	QString program = argument<QString>(0, context);
2112
+	QStringList arguments = QStringList();
2113
+	if(context->argumentCount() > 1) {
2114
+		arguments = argument<QVariant>(1, context).toStringList();
2115
+	}
2116
+	QString workingDirectory = "";
2117
+	if(context->argumentCount() > 2) {
2118
+		workingDirectory = argument<QString>(2, context);
2119
+	}
2120
+	bool retval;
2121
+	switch(context->argumentCount())
2122
+	{
2123
+		case 1:
2124
+			retval = self->startDetached(program);
2125
+			break;
2126
+		case 2:
2127
+			retval = self->startDetached(program, arguments);
2128
+			break;
2129
+		case 3:
2130
+			retval = self->startDetached(program, arguments, workingDirectory);
2131
+			break;
2132
+		default:
2133
+			retval = false;
2134
+	}
2135
+	return QScriptValue(retval);
2136
+}
2137
+
2138
+@ Sometimes we care about the working directory for our program.
2139
+
2140
+@<Functions for scripting@>=
2141
+QScriptValue QProcess_setWorkingDirectory(QScriptContext *context, QScriptEngine *engine)
2142
+{
2143
+	QProcess *self = getself<QProcess *>(context);
2144
+	QString directory = argument<QString>(0, context);
2145
+	self->setWorkingDirectory(directory);
2146
+	return QScriptValue();
2147
+}
2148
+
2149
+@ When using the |start()| method we always assume that we want read and write
2150
+access.
2151
+
2152
+@<Functions for scripting@>=
2153
+QScriptValue QProcess_start(QScriptContext *context, QScriptEngine *engine)
2154
+{
2155
+	QProcess *self = getself<QProcess *>(context);
2156
+	QString program = argument<QString>(0, context);
2157
+	QStringList arguments = QStringList();
2158
+	if(context->argumentCount() > 1) {
2159
+		arguments = argument<QVariant>(1, context).toStringList();
2160
+	}
2161
+	self->start(program, arguments);
2162
+	return QScriptValue();
2163
+}
2164
+
2041 2165
 @ In order to work with |QByteArray| this should also be exposed to the host
2042 2166
 environment.
2043 2167
 

Laden…
Abbrechen
Speichern