1 // SPDX-FileCopyrightText: 2004 Pino Toscano <toscano.pino@tiscali.it>
2 
3 // SPDX-License-Identifier: GPL-2.0-or-later
4 
5 #include "script-common.h"
6 #include "../objects/text_imp.h"
7 
8 #include <QString>
9 
10 #include <QDebug>
11 
12 struct script_prop
13 {
14   const char* fillCodeStatement;
15   const char* icon;
16   const char* highlightStyle;
17 };
18 
19 static const script_prop scripts_properties[] =
20 {
21   { I18N_NOOP( "Now fill in the code:" ), "application-x-thoshellscript", 0 },
22   { I18N_NOOP( "Now fill in the Python code:" ), "text-x-python", "Python-Kig" }
23 };
24 
fillCodeStatement(ScriptType::Type type)25 QString ScriptType::fillCodeStatement( ScriptType::Type type )
26 {
27   return i18n( scripts_properties[type].fillCodeStatement );
28 }
29 
templateCode(ScriptType::Type type,std::list<ObjectHolder * > args)30 QString ScriptType::templateCode( ScriptType::Type type, std::list<ObjectHolder*> args )
31 {
32   if ( type == Python )
33   {
34     QString tempcode { ScriptType::scriptFunctionDefinition( type, args ) };
35     tempcode +=
36       "\n"
37       "\t# Calculate whatever you want to show here, and return it.\n";
38     if ( args.empty() )
39     {
40       tempcode +=
41         "\t# For example, to return the number pi, you would put\n"
42         "\t# this code here:\n"
43         "\t#\treturn DoubleObject( 4*atan(1.0) )\n";
44     } else {
45       if ( ! args.empty() && (*args.begin())->imp()->inherits( NumericTextImp::stype() ) )
46       {
47         tempcode +=
48           "\t# For example, to return one half of the input number,\n"
49           "\t# you would put this code here:\n"
50           "\t#\treturn DoubleObject( arg1.value()/ 2 )\n";
51       } else if ( ! args.empty() && (*args.begin())->imp()->inherits( BoolTextImp::stype() ) )
52       {
53         tempcode +=
54           "\t# For example, to return a string based on the test result,\n"
55           "\t# you would put this code here:\n"
56           "\t#\tif arg1.value():\n"
57           "\t#\t\treturn StringObject( \"TRUE!\" )\n"
58           "\t#\telse:\n"
59           "\t#\t\treturn StringObject( \"FALSE!\" )\n";
60       } else {
61         tempcode +=
62           "\t# For example, to implement a mid point, you would put\n"
63           "\t# this code here:\n"
64           "\t#\treturn Point( ( arg1.coordinate() + arg2.coordinate() ) / 2 )\n";
65       }
66     }
67     tempcode +=
68       "\t# Please refer to the manual for more information.\n"
69       "\n";
70     return tempcode;
71   }
72 
73   qDebug() << "No such script type: " << type;
74   return QLatin1String("");
75 }
76 
scriptFunctionDefinition(ScriptType::Type type,std::list<ObjectHolder * > args)77 QString ScriptType::scriptFunctionDefinition( ScriptType::Type type, std::list<ObjectHolder*> args )
78 {
79   if ( type == Python )
80   {
81     QString newHeader {QStringLiteral( "def calc( " )};
82     bool firstarg {true};
83     KLocalizedString temparg {ki18nc( "Note to translators: this should be a default "
84                                       "name for an argument in a Python function. The "
85                                       "default is \"arg%1\" which would become arg1, "
86                                       "arg2, etc. Give something which seems "
87                                       "appropriate for your language.", "arg%1" )};
88 
89     uint id { 1 };
90     for ( auto i { args.begin() }; i != args.end(); ++i )
91     {
92       if ( !firstarg ) newHeader += QLatin1String( ", " );
93       else firstarg = false;
94       QString n = ( *i )->name();
95       newHeader += n.isEmpty() ? temparg.subs( id ).toString() : n;
96       id++;
97     };
98     newHeader += " ):";
99 
100     return newHeader;
101   }
102 
103   qDebug() << "No such script type: " << type;
104   return QLatin1String("");
105 }
106 
updateCodeFunction(ScriptType::Type type,std::list<ObjectHolder * > args,QString & script)107 void ScriptType::updateCodeFunction( ScriptType::Type type, std::list<ObjectHolder*> args, QString & script )
108 {
109   if ( type == Python )
110   {
111     QString newHeader { ScriptType::scriptFunctionDefinition( type, args ) };
112     size_t newlinePos = script.toStdString().find_first_of( '\n', 0 );
113 
114     script.remove( 0, newlinePos );
115     script.insert( 0, newHeader );
116   }
117 }
118 
icon(ScriptType::Type type)119 const char* ScriptType::icon( ScriptType::Type type )
120 {
121   return scripts_properties[type].icon;
122 }
123 
highlightStyle(ScriptType::Type type)124 QString ScriptType::highlightStyle( ScriptType::Type type )
125 {
126   return scripts_properties[type].highlightStyle
127          ? QString::fromLatin1( scripts_properties[type].highlightStyle )
128          : QString();
129 }
130 
intToType(int type)131 ScriptType::Type ScriptType::intToType( int type )
132 {
133   if ( type == 1 )
134     return Python;
135 
136   return Unknown;
137 }
138