![]() |
Mixxx
|
00001 #ifdef __PYTHON__ 00002 #include "python/pythoninterface.h" 00003 #endif 00004 00005 #include "scriptengine.h" 00006 #include "scripttest.h" 00007 #include "scriptcontrolqueue.h" 00008 #include "scriptrecorder.h" 00009 #include "scriptstudio.h" 00010 00011 #include <qfile.h> 00012 #include <qdom.h> 00013 00014 ScriptEngine::ScriptEngine(MixxxApp* parent, Track* track) { 00015 ScriptControlQueue* q = new ScriptControlQueue(this); 00016 00017 m_pcount = 0; 00018 m_parent = parent; 00019 m_track = track; 00020 00021 m_pi = new PlayInterface(q); 00022 #ifdef __LUA__ 00023 m_lua = new LuaInterface(m_pi); 00024 #endif 00025 00026 #ifdef __PYTHON__ 00027 PythonInterface::initInterface(m_pi); 00028 #endif 00029 00030 m_qti = new QtScriptInterface(m_pi); 00031 00032 m_macros = new QList<Macro*>(); 00033 00034 loadMacros(); 00035 m_rec = new ScriptRecorder(track); 00036 00037 //ScriptTest* stest = new ScriptTest(this); 00038 m_studio = new ScriptStudio(this); 00039 00040 // A handy test call: 00041 //q->interpolate("[Master]", "crossfader", &QDateTime::currentDateTime(), 00042 // 5000, -1.0, 15000, 1.0, 1234); 00043 } 00044 00045 void ScriptEngine::playTrack(int channel, QString filename) { 00046 if (channel == 1) { 00047 m_track->slotLoadPlayer1(filename); 00048 } else if (channel == 2) { 00049 m_track->slotLoadPlayer2(filename); 00050 } else { 00051 // This shouldn't happen 00052 qDebug("Asked for channel %i which doesn't exist", channel); 00053 } 00054 } 00055 00056 ScriptStudio* ScriptEngine::getStudio() { 00057 return m_studio; 00058 } 00059 00060 void ScriptEngine::addMacro(Macro* macro) { 00061 m_macros->append(macro); 00062 } 00063 00064 void ScriptEngine::newMacro(int lang) { 00065 addMacro(new Macro(lang, "New Macro")); 00066 } 00067 00068 int ScriptEngine::macroCount() { 00069 return m_macros->count(); 00070 } 00071 00072 ScriptRecorder* ScriptEngine::getRecorder() { 00073 return m_rec; 00074 } 00075 00076 Macro* ScriptEngine::getMacro(int index) { 00077 return m_macros->at(index); 00078 } 00079 00080 ScriptEngine::~ScriptEngine() { 00081 } 00082 00083 void ScriptEngine::executeMacro(Macro* macro) { 00084 if (macro->getLang() == Macro::LANG_LUA) { 00085 #ifdef __LUA__ 00086 m_lua->executeScript(macro->getScript(), m_pcount); 00087 #else 00088 qDebug("Lua support not available!"); 00089 #endif 00090 } else if (macro->getLang() == Macro::LANG_PYTHON) { 00091 #ifdef __PYTHON__ 00092 PythonInterface::executeScript(macro->getScript(), m_pcount); 00093 #else 00094 qDebug("Python support not available!"); 00095 #endif 00096 } else if (macro->getLang() == Macro::LANG_QTSCRIPT) { 00097 m_qti->executeScript(macro->getScript(), m_pcount); 00098 } else { 00099 return; 00100 } 00101 m_pcount++; 00102 } 00103 00104 void ScriptEngine::executeScript(const char* script) { 00105 // Don't execute a null script 00106 //qDebug("This function is obsolete and breaks everything!"); 00107 if (script == 0x0) { 00108 return; 00109 } 00110 00111 // For now just call lua, but this layer is here in anticipation of not 00112 // necessarily wanting to use it 00113 //#ifdef __LUA__ 00114 // m_lua->executeScript(script, -1); 00115 //#else 00116 // qDebug("Lua support not available!"); 00117 //#endif 00118 } 00119 00120 void ScriptEngine::deleteMacro(Macro* macro) { 00121 if (!m_macros->remove(macro)) { 00122 qDebug("Something pretty bad happened deleting a macro"); 00123 } 00124 } 00125 00126 void ScriptEngine::loadMacros() { 00127 QDomDocument doc("MacroStore"); 00128 QFile* file = getMacroFile(); 00129 00130 00131 if (!file->open(IO_ReadOnly)) { 00132 qDebug("Problem loading macros from disk (file)"); 00133 return; 00134 } 00135 QString* errorMsg = new QString();; 00136 int line; 00137 int col; 00138 if (!doc.setContent(file, errorMsg, &line, &col)) { 00139 file->close(); 00140 qDebug("Problem loading macros from disk (xml)"); 00141 qDebug("At %i, %i: %s", line, col, (const char*)(*errorMsg)); 00142 delete file; 00143 return; 00144 } 00145 file->close(); 00146 delete file; 00147 delete errorMsg; 00148 QDomElement docElem = doc.documentElement(); 00149 00150 QDomNode n = docElem.firstChild(); 00151 while (!n.isNull()) { 00152 int lang = Macro::LANG_LUA; 00153 QDomNode lnode = n.firstChild(); 00154 QDomNode name = lnode.nextSibling(); 00155 if (lnode.nodeName() == "Name") { 00156 qDebug("Converting from old macro file format..."); 00157 name = lnode; 00158 } else { 00159 QDomText ltext = lnode.firstChild().toText(); 00160 QString lstr = ltext.data(); 00161 lang = lstr.toInt(); 00162 } 00163 QDomNode script = name.nextSibling(); 00164 QDomText ntext = name.firstChild().toText(); 00165 QDomText stext = script.firstChild().toText(); 00166 QString nstr = ntext.data(); 00167 QString sstr = stext.data(); 00168 Macro* macro = new Macro(lang, nstr, sstr); 00169 addMacro(macro); 00170 n = n.nextSibling(); 00171 } 00172 } 00173 00174 void ScriptEngine::saveMacros() { 00175 qDebug("Saving Macros to disk"); 00176 QFile* file = getMacroFile(); 00177 QDomDocument doc("MacroStore"); 00178 00179 QDomElement root = doc.createElement("Macros"); 00180 doc.appendChild(root); 00181 00182 for (int i = 0; i < macroCount(); i++) { 00183 Macro* m = getMacro(i); 00184 QDomElement macro = doc.createElement("Macro"); 00185 QDomElement lang = doc.createElement("Lang"); 00186 QDomElement name = doc.createElement("Name"); 00187 QDomElement script = doc.createElement("Script"); 00188 QDomText ntext = doc.createTextNode(m->getName()); 00189 QDomText stext = doc.createTextNode(m->getScript()); 00190 QDomText ltext = doc.createTextNode(QString::number(\ 00191 m->getLang())); 00192 macro.appendChild(lang); 00193 macro.appendChild(name); 00194 macro.appendChild(script); 00195 name.appendChild(ntext); 00196 script.appendChild(stext); 00197 lang.appendChild(ltext); 00198 root.appendChild(macro); 00199 } 00200 00201 QString xml = doc.toString(); 00202 00203 if (file->open(IO_WriteOnly)) { 00204 QTextStream stream(file); 00205 stream << xml << "\n"; 00206 file->close(); 00207 } else { 00208 qDebug("Everything went horribly wrong writing macros to disk"); 00209 } 00210 delete file; 00211 00212 } 00213 00214 QFile* ScriptEngine::getMacroFile() { 00215 QString path = QDir::homePath().append("/").append(SETTINGS_PATH).append("mixxxmacro.xml"); 00216 return new QFile(path); 00217 }