Browse Source

Add createReport script method and supporting functionality.

Neal Wilson 11 years ago
parent
commit
e680190513
1 changed files with 43 additions and 3 deletions
  1. 43
    3
      src/typica.w

+ 43
- 3
src/typica.w View File

3877
 {\tt <window>} element into a window displayed on screen, the script in the
3877
 {\tt <window>} element into a window displayed on screen, the script in the
3878
 {\tt <program>} elements must call a function to display a specified window.
3878
 {\tt <program>} elements must call a function to display a specified window.
3879
 
3879
 
3880
+Report windows can be produced by scripts in a similar, but slightly different
3881
+manner.
3882
+
3880
 \danger This design works, but it'@q'@>s not particularly good design. It was written
3883
 \danger This design works, but it'@q'@>s not particularly good design. It was written
3881
 under severe time constraints and should be redesigned or at least cleaned up
3884
 under severe time constraints and should be redesigned or at least cleaned up
3882
 and reorganized.\endanger
3885
 and reorganized.\endanger
3883
 
3886
 
3884
 @<Function prototypes for scripting@>=
3887
 @<Function prototypes for scripting@>=
3885
 QScriptValue createWindow(QScriptContext *context, QScriptEngine *engine);
3888
 QScriptValue createWindow(QScriptContext *context, QScriptEngine *engine);
3889
+QScriptValue createReport(QScriptContext *context, QScriptEngine *engine);
3886
 void addLayoutToWidget(QDomElement element, QStack<QWidget*> *widgetStack,
3890
 void addLayoutToWidget(QDomElement element, QStack<QWidget*> *widgetStack,
3887
                        QStack<QLayout*> *layoutStack);
3891
                        QStack<QLayout*> *layoutStack);
3888
 void addLayoutToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
3892
 void addLayoutToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
3942
 void addSpinBoxToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
3946
 void addSpinBoxToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
3943
                         QStack<QLayout *> *layoutStack);
3947
                         QStack<QLayout *> *layoutStack);
3944
 
3948
 
3945
-@ The function for creating the window must be made available to the scripting
3949
+@ The functions for creating windows must be made available to the scripting
3946
 engine.
3950
 engine.
3947
 
3951
 
3948
 @<Set up the scripting engine@>=
3952
 @<Set up the scripting engine@>=
3949
 engine->globalObject().setProperty("createWindow",
3953
 engine->globalObject().setProperty("createWindow",
3950
                                    engine->newFunction(createWindow));
3954
                                    engine->newFunction(createWindow));
3955
+engine->globalObject().setProperty("createReport",
3956
+                                   engine->newFunction(createReport));
3951
 
3957
 
3952
 @ This function must examine the configuration document in search of the
3958
 @ This function must examine the configuration document in search of the
3953
 appropriate window element, parse the contents of that element, and create a
3959
 appropriate window element, parse the contents of that element, and create a
3967
 	return object;
3973
 	return object;
3968
 }
3974
 }
3969
 
3975
 
3976
+@ Report files are not part of the configuration document and must be created
3977
+differently. While there is a special menu type that handles all of this
3978
+without involving the host environment, scripted generation and manipulation of
3979
+report windows requires another function. This function will only work after a
3980
+window with a reports menu has been created.
3981
+
3982
+@<Functions for scripting@>=
3983
+QScriptValue createReport(QScriptContext *context, QScriptEngine *engine)
3984
+{
3985
+	QString targetID = argument<QString>(0, context);
3986
+	QFile file(QString("reports:%1").arg(targetID));
3987
+	QScriptValue object;
3988
+	if(file.open(QIODevice::ReadOnly))
3989
+	{
3990
+		QDomDocument document;
3991
+		document.setContent(&file, true);
3992
+		QDomElement element = document.documentElement();
3993
+		if(!element.isNull())
3994
+		{
3995
+			@<Display the window@>@;
3996
+		}
3997
+		file.close();
3998
+	}
3999
+	return object;
4000
+}
4001
+
3970
 @ First we must locate the {\tt <window>} element. The most sensible way to do
4002
 @ First we must locate the {\tt <window>} element. The most sensible way to do
3971
 this would require that each {\tt <window>} element has an ID attribute and
4003
 this would require that each {\tt <window>} element has an ID attribute and
3972
 search the DOM tree for that ID. Unfortunately, as of this writing,
4004
 search the DOM tree for that ID. Unfortunately, as of this writing,
13001
 type is highly inefficient. There are many optimizations available if this
13033
 type is highly inefficient. There are many optimizations available if this
13002
 becomes problematic.\endanger
13034
 becomes problematic.\endanger
13003
 
13035
 
13036
+When a report menu is generated, the directory used for this is added as a
13037
+search path for the |"reports"| prefix. This is used by the |createReport()|
13038
+script method and is intended to allow access to reports from outside of the
13039
+Report menu.
13040
+
13004
 @<Populate reports menu@>=
13041
 @<Populate reports menu@>=
13005
 QSettings settings;
13042
 QSettings settings;
13006
-QDir directory(QString("%1/%2").arg(settings.value("config").toString()).
13007
-                                arg(element.attribute("src")));
13043
+QString reportDirectory = QString("%1/%2").arg(settings.value("config").
13044
+                                               toString()).
13045
+                                           arg(element.attribute("src"));
13046
+QDir::addSearchPath("reports", reportDirectory);
13047
+QDir directory(reportDirectory);
13008
 directory.setFilter(QDir::Files);
13048
 directory.setFilter(QDir::Files);
13009
 directory.setSorting(QDir::Name);
13049
 directory.setSorting(QDir::Name);
13010
 QStringList nameFilter;
13050
 QStringList nameFilter;

Loading…
Cancel
Save