]> Shamusworld >> Repos - architektonas/blob - src/base/rs_scriptlist.cpp
Changed RS_Graphic to Drawing; this is less confusing as a drawing is
[architektonas] / src / base / rs_scriptlist.cpp
1 // rs_scriptlist.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  06/02/2010  Added this text. :-)
13 //
14
15 #include "rs_scriptlist.h"
16
17 #include "rs_system.h"
18
19 RS_ScriptList * RS_ScriptList::uniqueInstance = NULL;
20
21 /**
22  * Default constructor.
23  */
24 RS_ScriptList::RS_ScriptList(): scriptIterator(scripts)
25 {
26 //Should be dealt with...
27 //#warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
28 //    scripts.setAutoDelete(true);
29     //init();
30     //scriptListListeners.setAutoDelete(false);
31     //activeScript = NULL;
32 }
33
34 /*virtual*/ RS_ScriptList::~RS_ScriptList()
35 {
36         while (!scripts.isEmpty())
37                 delete scripts.takeFirst();
38 }
39
40 /**
41  * @return Instance to the unique script list.
42  */
43 /*static*/ RS_ScriptList * RS_ScriptList::instance()
44 {
45         if (uniqueInstance == NULL)
46                 uniqueInstance = new RS_ScriptList();
47
48         return uniqueInstance;
49 }
50
51 /**
52  * Initializes the script list by creating RS_Script
53  * objects, one for each script that could be found.
54  */
55 void RS_ScriptList::init()
56 {
57         RS_DEBUG->print("RS_ScriptList::initScripts");
58
59         scripts.clear();
60         QStringList list = RS_SYSTEM->getScriptList();
61         RS_Script * script;
62
63         for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
64         {
65                 RS_DEBUG->print("script: %s:", (*it).toLatin1().data());
66
67                 QFileInfo fi(*it);
68                 script = new RS_Script(fi.baseName(), fi.absoluteFilePath());
69                 scripts.append(script);
70
71                 RS_DEBUG->print("base: %s", fi.baseName().toLatin1().data());
72                 RS_DEBUG->print("path: %s", fi.absoluteFilePath().toLatin1().data());
73         }
74
75         //RS_Script* f = new RS_Script("normal");
76         //scripts.append(f);
77 }
78
79 /**
80  * Removes all scripts in the scriptlist.
81  */
82 void RS_ScriptList::clearScripts()
83 {
84         scripts.clear();
85 }
86
87 int RS_ScriptList::countScripts()
88 {
89         return scripts.count();
90 }
91
92 /**
93  * Removes a script from the list.
94  * Listeners are notified after the script was removed from
95  * the list but before it gets deleted.
96  */
97 void RS_ScriptList::removeScript(RS_Script * script)
98 {
99     RS_DEBUG->print("RS_ScriptList::removeScript()");
100
101     // here the script is removed from the list but not deleted
102 //    scripts.remove(script);
103         // We have to delete it since AutoDelete does not exist in QList<T>
104         int i = scripts.indexOf(script);
105
106         if (i != -1)
107                 delete scripts.takeAt(i);
108
109     //for (uint i=0; i<scriptListListeners.count(); ++i) {
110     //    RS_ScriptListListener* l = scriptListListeners.at(i);
111     //    l->scriptRemoved(script);
112     //}
113
114     // activate an other script if necessary:
115     //if (activeScript==script) {
116     //    activateScript(scripts.first());
117     //}
118
119     // now it's save to delete the script
120     //delete script;
121 }
122
123 /**
124  * @return Pointer to the script with the given name or
125  * \p NULL if no such script was found. The script will be loaded into
126  * memory if it's not already.
127  */
128 RS_Script * RS_ScriptList::requestScript(const QString & name)
129 {
130         RS_DEBUG->print("RS_ScriptList::requestScript %s",  name.toLatin1().data());
131
132         QString name2 = name.toLower();
133         RS_Script * foundScript = NULL;
134
135         RS_DEBUG->print("name2: %s", name2.toLatin1().data());
136
137         // Search our list of available scripts:
138 //      for(RS_Script * s=scripts.first(); s!=NULL; s=scripts.next())
139         for(int i=0; i<scripts.size(); i++)
140         {
141                 RS_Script * s = scripts[i];
142
143                 if (s->getName() == name2)
144                 {
145                         foundScript = s;
146                         break;
147                 }
148         }
149
150         // Script not found:
151         return foundScript;
152 }
153
154 //! @return First script of the list.
155 RS_Script * RS_ScriptList::firstScript()
156 {
157 //      return scripts.first();
158         scriptIterator.toFront();
159         return scriptIterator.next();
160 }
161
162 /** @return Next script from the list after
163  * calling firstScript() or nextScript().
164  */
165 RS_Script * RS_ScriptList::nextScript()
166 {
167 //      return scripts.next();
168         return scriptIterator.next();
169 }
170
171 /**
172  * @return Pointer to the script with the given name or
173  * \p NULL if no such script was found.
174  */
175 //RS_Script* RS_ScriptList::loadScript(const QString& name) {
176 //}
177
178 /**
179  * Tests the script list and its ability to load scripts.
180  */
181 bool RS_ScriptList::test()
182 {
183     //RS_ScriptList* l = RS_ScriptList::instance();
184
185     //std::cout << "RS_ScriptList: " << *l << std::endl;
186
187     return true;
188 }