Kstars

scriptfunction.cpp
1/*
2 SPDX-FileCopyrightText: 2003 Jason Harris <kstars@30doradus.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "scriptfunction.h"
8
9#include <QDebug>
10#include <QStringList>
11
12ScriptFunction::ScriptFunction(const QString &name, const QString &desc, bool clockfcn, const QString &at1,
13 const QString &an1, const QString &at2, const QString &an2, const QString &at3,
14 const QString &an3, const QString &at4, const QString &an4, const QString &at5,
15 const QString &an5, const QString &at6, const QString &an6)
16 : INDIProp(QString())
17{
18 Name = name;
19 ClockFunction = clockfcn;
20
21 ArgType[0] = at1;
22 ArgDBusType[0] = DBusType(at1);
23 ArgName[0] = an1;
24 ArgType[1] = at2;
25 ArgDBusType[1] = DBusType(at2);
26 ArgName[1] = an2;
27 ArgType[2] = at3;
28 ArgDBusType[2] = DBusType(at3);
29 ArgName[2] = an3;
30 ArgType[3] = at4;
31 ArgDBusType[3] = DBusType(at4);
32 ArgName[3] = an4;
33 ArgType[4] = at5;
34 ArgDBusType[4] = DBusType(at5);
35 ArgName[4] = an5;
36 ArgType[5] = at6;
37 ArgDBusType[5] = DBusType(at6);
38 ArgName[5] = an6;
39
40 //Construct a richtext description of the function
41 QString nameStyle = "<span style=\"font-family:monospace;font-weight:600\">%1</span>"; //bold
42 QString typeStyle = "<span style=\"font-family:monospace;color:#009d00\">%1</span>"; //green
43 QString paramStyle = "<span style=\"font-family:monospace;color:#00007f\">%1</span>"; //blue
44
45 Description = "<html><head><meta name=\"qrichtext\" content=\"1\" /></head>";
46 Description += "<body style=\"font-size:11pt;font-family:sans\">";
47 Description += "<p>" + nameStyle.arg(Name + '(');
48
49 NumArgs = 0;
50 if (!at1.isEmpty() && !an1.isEmpty())
51 {
52 Description += ' ' + typeStyle.arg(at1);
53 Description += ' ' + paramStyle.arg(an1);
54 NumArgs++;
55 }
56
57 if (!at2.isEmpty() && !an2.isEmpty())
58 {
59 Description += ", " + typeStyle.arg(at2);
60 Description += ' ' + paramStyle.arg(an2);
61 NumArgs++;
62 }
63
64 if (!at3.isEmpty() && !an3.isEmpty())
65 {
66 Description += ", " + typeStyle.arg(at3);
67 Description += ' ' + paramStyle.arg(an3);
68 NumArgs++;
69 }
70
71 if (!at4.isEmpty() && !an4.isEmpty())
72 {
73 Description += ", " + typeStyle.arg(at4);
74 Description += ' ' + paramStyle.arg(an4);
75 NumArgs++;
76 }
77
78 if (!at5.isEmpty() && !an5.isEmpty())
79 {
80 Description += ", " + typeStyle.arg(at5);
81 Description += ' ' + paramStyle.arg(an5);
82 NumArgs++;
83 }
84
85 if (!at6.isEmpty() && !an6.isEmpty())
86 {
87 Description += ", " + typeStyle.arg(at6);
88 Description += ' ' + paramStyle.arg(an6);
89 NumArgs++;
90 }
91
92 //Set Valid=false if there are arguments (indicates that this fcn's args must be filled in)
93 Valid = true;
94 if (NumArgs)
95 Valid = false;
96
97 //Finish writing function prototype
98 if (NumArgs)
99 Description += ' ';
100 Description += nameStyle.arg(")") + "</p><p>";
101
102 //Add description
103 Description += desc;
104
105 //Finish up
106 Description += "</p></body></html>";
107}
108
109//Copy constructor
110ScriptFunction::ScriptFunction(ScriptFunction *sf)
111{
112 Name = sf->name();
113 Description = sf->description();
114 ClockFunction = sf->isClockFunction();
115 NumArgs = sf->numArgs();
116 INDIProp = sf->INDIProperty();
117 Valid = sf->valid();
118
119 for (unsigned int i = 0; i < 6; i++)
120 {
121 ArgType[i] = sf->argType(i);
122 ArgName[i] = sf->argName(i);
123 ArgDBusType[i] = sf->argDBusType(i);
124 //ArgVal[i] .clear();
125 // JM: Some default argument values might be passed from another object as well
126 ArgVal[i] = sf->argVal(i);
127 }
128}
129
130QString ScriptFunction::DBusType(const QString &type)
131{
132 if (type == QString("int"))
133 return QString("int32");
134 else if (type == QString("uint"))
135 return QString("uint32");
136 else if (type == QString("double"))
137 return type;
138 else if (type == QString("QString"))
139 return QString("string");
140 else if (type == QString("bool"))
141 return QString("boolean");
142
143 return nullptr;
144}
145
146QString ScriptFunction::prototype() const
147{
148 QString p = Name + '(';
149
150 bool args(false);
151 if (!ArgType[0].isEmpty() && !ArgName[0].isEmpty())
152 {
153 p += ' ' + ArgType[0];
154 p += ' ' + ArgName[0];
155 args = true; //assume that if any args are present, 1st arg is present
156 }
157
158 if (!ArgType[1].isEmpty() && !ArgName[1].isEmpty())
159 {
160 p += ", " + ArgType[1];
161 p += ' ' + ArgName[1];
162 }
163
164 if (!ArgType[2].isEmpty() && !ArgName[2].isEmpty())
165 {
166 p += ", " + ArgType[2];
167 p += ' ' + ArgName[2];
168 }
169
170 if (!ArgType[3].isEmpty() && !ArgName[3].isEmpty())
171 {
172 p += ", " + ArgType[3];
173 p += ' ' + ArgName[3];
174 }
175
176 if (!ArgType[4].isEmpty() && !ArgName[4].isEmpty())
177 {
178 p += ", " + ArgType[4];
179 p += ' ' + ArgName[4];
180 }
181
182 if (!ArgType[5].isEmpty() && !ArgName[5].isEmpty())
183 {
184 p += ", " + ArgType[5];
185 p += ' ' + ArgName[5];
186 }
187
188 if (args)
189 p += ' ';
190 p += ')';
191
192 return p;
193}
194
195QString ScriptFunction::scriptLine() const
196{
197 QString out(Name);
198 unsigned int i = 0;
199
200 while (i < 6 && !ArgName[i].isEmpty())
201 {
202 //Make sure strings are quoted
203 QString value = ArgVal[i];
204 if (ArgDBusType[i] == "string")
205 {
206 if (value.isEmpty())
207 {
208 value = "\"\"";
209 }
210 else
211 {
212 if (value.at(0) != '\"' && value.at(0) != '\'')
213 {
214 value = '\"' + value;
215 }
216 if (value.right(1) != "\"" && value.right(1) != "\'")
217 {
218 value = value + '\"';
219 }
220 }
221 }
222
223 // Write DBus style prototype compatible with dbus-send format
224 out += ' ' + ArgDBusType[i] + ':' + value;
225 ++i;
226 }
227
228 return out;
229}
Jason Harris.
QString name(GameStandardAction id)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
QString arg(Args &&... args) const const
const QChar at(qsizetype position) const const
bool isEmpty() const const
QString right(qsizetype n) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:11:16 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.