moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kstars / kstars / tools / scriptfunction.cpp
blob060ed2bcd370e21afc9800a295128fed3080b1b1
1 /***************************************************************************
2 scriptfunction.cpp - description
3 -------------------
4 begin : Thu Apr 17 2003
5 copyright : (C) 2003 by Jason Harris
6 email : kstars@30doradus.org
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include <kdebug.h>
20 #include "scriptfunction.h"
23 ScriptFunction::ScriptFunction( QString name, QString desc, bool clockfcn,
24 QString at1, QString an1, QString at2, QString an2, QString at3, QString an3,
25 QString at4, QString an4, QString at5, QString an5, QString at6, QString an6 ) : INDIProp("") {
26 Name = name;
27 ClockFunction = clockfcn;
29 ArgType[0] = at1; ArgName[0] = an1;
30 ArgType[1] = at2; ArgName[1] = an2;
31 ArgType[2] = at3; ArgName[2] = an3;
32 ArgType[3] = at4; ArgName[3] = an4;
33 ArgType[4] = at5; ArgName[4] = an5;
34 ArgType[5] = at6; ArgName[5] = an6;
36 //Construct a richtext description of the function
37 QString nameStyle = "<span style=\"font-family:courier;font-weight:600\">%1</span>"; //bold
38 QString typeStyle = "<span style=\"font-family:courier;color:#009d00\">%1</span>"; //green
39 QString paramStyle = "<span style=\"font-family:courier;color:#00007f\">%1</span>"; //blue
41 Description = "<html><head><meta name=\"qrichtext\" content=\"1\" /></head>";
42 Description += "<body style=\"font-size:11pt;font-family:Verdana\">";
43 Description += "<p>" + nameStyle.arg( Name + "(" );
45 NumArgs = 0;
46 if ( ! at1.isEmpty() && ! an1.isEmpty() ) {
47 Description += " " + typeStyle.arg( at1 );
48 Description += " " + paramStyle.arg( an1 );
49 NumArgs++;
52 if ( ! at2.isEmpty() && ! an2.isEmpty() ) {
53 Description += ", " + typeStyle.arg( at2 );
54 Description += " " + paramStyle.arg( an2 );
55 NumArgs++;
58 if ( ! at3.isEmpty() && ! an3.isEmpty() ) {
59 Description += ", " + typeStyle.arg( at3 );
60 Description += " " + paramStyle.arg( an3 );
61 NumArgs++;
64 if ( ! at4.isEmpty() && ! an4.isEmpty() ) {
65 Description += ", " + typeStyle.arg( at4 );
66 Description += " " + paramStyle.arg( an4 );
67 NumArgs++;
70 if ( ! at5.isEmpty() && ! an5.isEmpty() ) {
71 Description += ", " + typeStyle.arg( at5 );
72 Description += " " + paramStyle.arg( an5 );
73 NumArgs++;
76 if ( ! at6.isEmpty() && ! an6.isEmpty() ) {
77 Description += ", " + typeStyle.arg( at6 );
78 Description += " " + paramStyle.arg( an6 );
79 NumArgs++;
82 //Set Valid=false if there are arguments (indicates that this fcn's args must be filled in)
83 Valid = true;
84 if ( NumArgs ) Valid = false;
86 //Finish writing function prototype
87 if ( NumArgs ) Description += " ";
88 Description += nameStyle.arg( ")" ) + "</p><p>";
90 //before adding description, replace any '%n' instances with the corresponding
91 //argument name in color. For now, assume that the %n's occur in order, with no skips.
92 //Also assume that '%' is *only* used to indicate argument instances
93 int narg = desc.contains( '%' );
94 switch (narg ) {
95 case 1:
96 Description += desc.arg( paramStyle.arg( an1 ) );
97 break;
98 case 2:
99 Description +=
100 desc.arg( paramStyle.arg( an1 ) )
101 .arg( paramStyle.arg( an2 ) );
102 break;
103 case 3:
104 Description +=
105 desc.arg( paramStyle.arg( an1 ) )
106 .arg( paramStyle.arg( an2 ) )
107 .arg( paramStyle.arg( an3 ) );
108 break;
109 case 4:
110 Description +=
111 desc.arg( paramStyle.arg( an1 ) )
112 .arg( paramStyle.arg( an2 ) )
113 .arg( paramStyle.arg( an3 ) )
114 .arg( paramStyle.arg( an4 ) );
115 break;
116 case 5:
117 Description +=
118 desc.arg( paramStyle.arg( an1 ) )
119 .arg( paramStyle.arg( an2 ) )
120 .arg( paramStyle.arg( an3 ) )
121 .arg( paramStyle.arg( an4 ) )
122 .arg( paramStyle.arg( an5 ) );
123 break;
124 case 6:
125 Description +=
126 desc.arg( paramStyle.arg( an1 ) )
127 .arg( paramStyle.arg( an2 ) )
128 .arg( paramStyle.arg( an3 ) )
129 .arg( paramStyle.arg( an4 ) )
130 .arg( paramStyle.arg( an5 ) )
131 .arg( paramStyle.arg( an6 ) );
132 break;
133 default:
134 Description += desc;
135 break;
138 //Finish up
139 Description += "</p></body></html>";
142 //Copy constructor
143 ScriptFunction::ScriptFunction( ScriptFunction *sf )
145 Name = sf->name();
146 Description = sf->description();
147 ClockFunction = sf->isClockFunction();
148 NumArgs = sf->numArgs();
149 INDIProp = sf->INDIProperty();
150 Valid = sf->valid();
152 for ( unsigned int i=0; i<6; i++ ) {
153 ArgType[i] = sf->argType(i);
154 ArgName[i] = sf->argName(i);
155 ArgVal[i] = "";
159 ScriptFunction::~ScriptFunction()
163 QString ScriptFunction::prototype() const {
164 QString p = Name + "(";
166 bool args( false );
167 if ( ! ArgType[0].isEmpty() && ! ArgName[0].isEmpty() ) {
168 p += " " + ArgType[0];
169 p += " " + ArgName[0];
170 args = true; //assume that if any args are present, 1st arg is present
173 if ( ! ArgType[1].isEmpty() && ! ArgName[1].isEmpty() ) {
174 p += ", " + ArgType[1];
175 p += " " + ArgName[1];
178 if ( ! ArgType[2].isEmpty() && ! ArgName[2].isEmpty() ) {
179 p += ", " + ArgType[2];
180 p += " " + ArgName[2];
183 if ( ! ArgType[3].isEmpty() && ! ArgName[3].isEmpty() ) {
184 p += ", " + ArgType[3];
185 p += " " + ArgName[3];
188 if ( ! ArgType[4].isEmpty() && ! ArgName[4].isEmpty() ) {
189 p += ", " + ArgType[4];
190 p += " " + ArgName[4];
193 if ( ! ArgType[5].isEmpty() && ! ArgName[5].isEmpty() ) {
194 p += ", " + ArgType[5];
195 p += " " + ArgName[5];
198 if ( args ) p += " ";
199 p += ")";
201 return p;
204 QString ScriptFunction::scriptLine() const {
205 QString out( Name );
206 unsigned int i=0;
207 while ( ! ArgName[i].isEmpty() && i < 6 ) {
208 // Wrap arg in quotes if it contains a space
209 if ( ArgVal[i].contains(" ") ) {
210 out += " \"" + ArgVal[i] + "\"";
211 } else {
212 out += " " + ArgVal[i];
214 ++i;
217 return out;