]> git.sven.stormbind.net Git - sven/vym.git/blob - src/scripting.cpp
New upstream version 2.9.22
[sven/vym.git] / src / scripting.cpp
1 #include "scripting.h"
2
3 #include "branchitem.h"
4 #include "confluence-agent.h"
5 #include "imageitem.h"
6 #include "mainwindow.h"
7 #include "misc.h"
8 #include "vymmodelwrapper.h"
9 #include "vymtext.h"
10 #include "xlink.h"
11
12 extern Main *mainWindow;
13 extern QString vymVersion;
14
15 ///////////////////////////////////////////////////////////////////////////
16 void logError(QScriptContext *context, QScriptContext::Error error,
17               const QString &text)
18 {
19     if (context)
20         context->throwError(error, text);
21     else
22         qDebug() << "VymWrapper: " << text;
23 }
24
25 ///////////////////////////////////////////////////////////////////////////
26 VymScriptContext::VymScriptContext() {}
27
28 QString VymScriptContext::setResult(const QString &r)
29 {
30     context()->engine()->globalObject().setProperty("lastResult", r);
31     return r;
32 }
33
34 bool VymScriptContext::setResult(bool r)
35 {
36     context()->engine()->globalObject().setProperty("lastResult", r);
37     return r;
38 }
39
40 int VymScriptContext::setResult(int r)
41 {
42     context()->engine()->globalObject().setProperty("lastResult", r);
43     return r;
44 }
45
46 uint VymScriptContext::setResult(uint r)
47 {
48     context()->engine()->globalObject().setProperty("lastResult", r);
49     return r;
50 }
51
52 ///////////////////////////////////////////////////////////////////////////
53 VymWrapper::VymWrapper() {}
54
55 void VymWrapper::clearConsole() { mainWindow->clearScriptOutput(); }
56
57 bool VymWrapper::isConfluenceAgentAvailable()
58 {
59     return ConfluenceAgent::available();
60 }
61
62 QObject *VymWrapper::currentMap()
63 {
64     return mainWindow->getCurrentModelWrapper();
65 }
66
67 void VymWrapper::editHeading()
68 {
69     MapEditor *me = mainWindow->currentMapEditor();
70     if (me) me->editHeading();
71 }
72
73 bool VymWrapper::loadMap(const QString &filename)
74 {
75     bool r;
76     if (File::Success == mainWindow->fileLoad(filename, NewMap, VymMap))
77         r = true;
78     else
79         r = false;
80     return setResult(r);
81 }
82
83 int VymWrapper::mapCount()
84 {
85     context()->engine()->globalObject().setProperty("lastResult",
86                                                     mainWindow->modelCount());
87     return setResult(mainWindow->modelCount());
88 }
89
90 void VymWrapper::gotoMap(uint n)
91 {
92     if (!mainWindow->gotoWindow(n)) {
93         logError(context(), QScriptContext::RangeError,
94                  QString("Map '%1' not available.").arg(n));
95     }
96 }
97
98 bool VymWrapper::closeMapWithID(uint n)
99 {
100     bool r = mainWindow->closeModelWithID(n);
101     if (!r)
102         logError(context(), QScriptContext::RangeError,
103                  QString("Map '%1' not available.").arg(n));
104     return setResult(r);
105 }
106
107 void VymWrapper::selectQuickColor(int n)
108 {
109     mainWindow->selectQuickColor(n);
110 }
111
112 QString VymWrapper::currentColor()
113 {
114     return mainWindow->getCurrentColor().name();
115 }
116
117 uint VymWrapper::currentMapID()
118 {
119     uint id = mainWindow->currentMapID();
120     return setResult(id);
121 }
122
123 void VymWrapper::toggleTreeEditor() { mainWindow->windowToggleTreeEditor(); }
124
125 QString VymWrapper::loadFile(
126     const QString
127         &filename) // FIXME-3 error handling missing (in vymmodel and here)
128 {
129     QString s;
130     loadStringFromDisk(filename, s);
131     return s;
132 }
133
134 void VymWrapper::saveFile(
135     const QString &filename,
136     const QString &s) // FIXME-3 error handling missing (in vymmodel and here)
137 {
138     saveStringToDisk(filename, s);
139 }
140
141 QString VymWrapper::version() { return setResult(vymVersion); }
142
143 // See also http://doc.qt.io/qt-5/qscriptengine.html#newFunction
144 Selection::Selection() { modelWrapper = NULL; }
145
146 void Selection::test()
147 {
148     qDebug() << "Selection::testSelection called"; // TODO debug
149     if (modelWrapper)
150         modelWrapper->setHeadingPlainText("huhu!");
151 }
152
153 void Selection::setModel(VymModelWrapper *mw)
154 {
155     qDebug() << "Selection::setModel called: " << mw; // TODO debug
156     modelWrapper = mw;
157 }