| 1 | /* |
| 2 | Copyright (C) 2010 Andres Cabrera |
| 3 | [email protected] |
| 4 | |
| 5 | This file is part of CsoundQt. |
| 6 | |
| 7 | CsoundQt is free software; you can redistribute it |
| 8 | and/or modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | CsoundQt is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with Csound; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include "basedocument.h" |
| 24 | |
| 25 | #include "widgetlayout.h" |
| 26 | #include "documentview.h" |
| 27 | #include "csoundengine.h" |
| 28 | #include "qutecsound.h" |
| 29 | #include "qutebutton.h" |
| 30 | #include "console.h" |
| 31 | |
| 32 | |
| 33 | BaseDocument::BaseDocument(QWidget *parent, OpEntryParser *opcodeTree, ConfigLists *configlists) : |
| 34 | QObject(parent), m_opcodeTree(opcodeTree), m_csEngine(0) |
| 35 | { |
| 36 | m_view = 0; |
| 37 | m_csEngine = new CsoundEngine(configlists); |
| 38 | //FIXME widgetlayout should have the chance of being empty |
| 39 | m_widgetLayouts.append(this->newWidgetLayout()); |
| 40 | m_csEngine->setWidgetLayout(m_widgetLayouts[0]); // Pass first widget layout to engine |
| 41 | // m_view->setOpcodeNameList(opcodeNameList); |
| 42 | // m_view->setOpcodeTree(m_opcodeTree); |
| 43 | m_console = new ConsoleWidget(0); |
| 44 | m_console->setReadOnly(true); |
| 45 | // Register the console with the engine for message printing |
| 46 | m_csEngine->registerConsole(m_console); |
| 47 | m_status = PlayStopStatus::Ok; |
| 48 | connect(m_console, SIGNAL(keyPressed(int)), |
| 49 | m_csEngine, SLOT(keyPressForCsound(int))); |
| 50 | connect(m_console, SIGNAL(keyReleased(int)), |
| 51 | m_csEngine, SLOT(keyReleaseForCsound(int))); |
| 52 | |
| 53 | acceptsMidiCC = true; |
| 54 | } |
| 55 | |
| 56 | BaseDocument::~BaseDocument() |
| 57 | { |
| 58 | disconnect(m_csEngine, 0,0,0); |
| 59 | // disconnect(m_widgetLayout, 0,0,0); |
| 60 | m_csEngine->stop(); |
| 61 | delete m_csEngine; |
| 62 | while (!m_widgetLayouts.isEmpty()) { |
| 63 | WidgetLayout *wl = m_widgetLayouts.takeLast(); |
| 64 | wl->hide(); |
| 65 | delete wl; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /* Returns the number of panels |
| 70 | */ |
| 71 | int BaseDocument::parseAndRemoveWidgetText(QString &text) |
| 72 | { |
| 73 | |
| 74 | QStringList xmlPanels; |
| 75 | while(true) { |
| 76 | auto panelStart = text.indexOf("<bsbPanel"); |
| 77 | if(panelStart < 0) { |
| 78 | QDEBUG "Didn't find any more panels"; |
| 79 | break; |
| 80 | } |
| 81 | auto panelEnd = text.indexOf("</bsbPanel>", panelStart); |
| 82 | if(panelEnd < 0) { |
| 83 | QDEBUG << "Did not find matching </bsbPanel> tag"; |
| 84 | return 0; |
| 85 | } |
| 86 | auto panel = text.mid(panelStart, panelEnd+11-panelStart); |
| 87 | xmlPanels << panel; |
| 88 | text.remove(panelStart, panelEnd+11-panelStart); |
| 89 | |
| 90 | } |
| 91 | /* |
| 92 | while (text.contains("<bsbPanel") && text.contains("</bsbPanel>")) { |
| 93 | QString panel = text.right(text.size()-text.indexOf("<bsbPanel")); |
| 94 | panel.resize(panel.indexOf("</bsbPanel>") + 11); |
| 95 | if (text.indexOf("</bsbPanel>") + 11 < text.size() && |
| 96 | text[text.indexOf("</bsbPanel>") + 13] == '\n') |
| 97 | text.remove(text.indexOf("</bsbPanel>") + 13, 1); //remove final line break |
| 98 | if (text.indexOf("<bsbPanel") > 0 && text[text.indexOf("<bsbPanel") - 1] == '\n') |
| 99 | text.remove(text.indexOf("<bsbPanel") - 1, 1); //remove initial line break |
| 100 | text.remove(text.indexOf("<bsbPanel"), panel.size()); |
| 101 | xmlPanels << panel; |
| 102 | // TODO enable creation of several panels |
| 103 | } |
| 104 | */ |
| 105 | if (!xmlPanels.isEmpty()) { |
| 106 | //FIXME allow multiple layouts |
| 107 | auto t0 = std::chrono::high_resolution_clock::now(); |
| 108 | m_widgetLayouts[0]->loadXmlWidgets(xmlPanels[0]); |
| 109 | auto t1 = std::chrono::high_resolution_clock::now(); |
| 110 | auto diff = std::chrono::duration<double, std::milli>(t1-t0).count(); |
| 111 | QDEBUG << "loadXmlWidgets" << diff << "ms"; |
| 112 | |
| 113 | m_widgetLayouts[0]->markHistory(); |
| 114 | auto presetsStart = text.indexOf("<bsbPresets>"); |
| 115 | if(presetsStart >= 0) { |
| 116 | auto presetsEnd = text.indexOf("</bsbPresets>", presetsStart); |
| 117 | if(presetsEnd < 0) { |
| 118 | QDEBUG << "Missing </bsbPresets> tag"; |
| 119 | } |
| 120 | else { |
| 121 | auto presetsSize = presetsEnd - presetsStart + 13; |
| 122 | auto presets = text.mid(presetsStart, presetsSize); |
| 123 | m_widgetLayouts[0]->loadXmlPresets(presets); |
| 124 | text.remove(presetsStart, presetsSize); |
| 125 | } |
| 126 | } |
| 127 | /* |
| 128 | if (text.contains("<bsbPresets>") && text.contains("</bsbPresets>")) { |
| 129 | QString presets = text.right(text.size()-text.indexOf("<bsbPresets>")); |
| 130 | presets.resize(presets.indexOf("</bsbPresets>") + 13); |
| 131 | if (text.indexOf("</bsbPresets>") + 13 < text.size() && |
| 132 | text[text.indexOf("</bsbPresets>") + 15] == '\n') { |
| 133 | //remove final line break |
| 134 | text.remove(text.indexOf("</bsbPresets>") + 15, 1); |
| 135 | } |
| 136 | if (text.indexOf("<bsbPresets>") > 0 |
| 137 | && text[text.indexOf("<bsbPresets>") - 1] == '\n') { |
| 138 | // remove initial line break |
| 139 | text.remove(text.indexOf("<bsbPresets>") - 1, 1); |
| 140 | } |
| 141 | text.remove(text.indexOf("<bsbPresets>"), presets.size()); |
| 142 | // FIXME allow multiple |
| 143 | m_widgetLayouts[0]->loadXmlPresets(presets); |
| 144 | } |
| 145 | */ |
| 146 | } |
| 147 | else { |
| 148 | QString defaultPanel = "<bsbPanel><visible>true</visible><x>100</x><y>100</y>" |
| 149 | "<width>320</width><height>240</height></bsbPanel>"; |
| 150 | m_widgetLayouts[0]->loadXmlWidgets(defaultPanel); |
| 151 | m_widgetLayouts[0]->markHistory(); |
| 152 | } |
| 153 | return xmlPanels.size(); |
| 154 | } |
| 155 | |
| 156 | WidgetLayout* BaseDocument::newWidgetLayout() |
| 157 | { |
| 158 | WidgetLayout* wl = new WidgetLayout(0); |
| 159 | wl->setWindowFlags(Qt::Window | wl->windowFlags()); |
| 160 | connect(wl, SIGNAL(queueEventSignal(QString)),this,SLOT(queueEvent(QString))); |
| 161 | connect(wl, SIGNAL(registerButton(QuteButton*)), |
| 162 | this, SLOT(registerButton(QuteButton*))); |
| 163 | return wl; |
| 164 | } |
| 165 | |
| 166 | void BaseDocument::widgetsVisible(bool visible) |
| 167 | { |
| 168 | for (int i = 0; i < m_widgetLayouts.size(); i++) { |
| 169 | m_widgetLayouts[i]->setVisible(visible); |
| 170 | m_widgetLayouts[i]->raise(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void BaseDocument::setFlags(int flags) |
| 175 | { |
| 176 | m_csEngine->setFlags((PerfFlags) flags); |
| 177 | } |
| 178 | |
| 179 | void BaseDocument::setAppProperties(AppProperties properties) |
| 180 | { |
| 181 | m_view->setAppProperties(properties); |
| 182 | } |
| 183 | |
| 184 | QString BaseDocument::getFullText() |
| 185 | { |
| 186 | QStringList parts; |
| 187 | parts << m_view->getFullText(); |
| 188 | if (fileName.endsWith(".csd",Qt::CaseInsensitive) || fileName == "") { |
| 189 | parts << getWidgetsText() ; |
| 190 | parts << getPresetsText() << "\n"; |
| 191 | } |
| 192 | else { // Not a csd file |
| 193 | for(auto wl: m_widgetLayouts) { |
| 194 | // foreach (WidgetLayout * wl, m_widgetLayouts) { |
| 195 | wl->clearWidgets(); // make sure no widgets are used. |
| 196 | } |
| 197 | } |
| 198 | return parts.join(""); |
| 199 | } |
| 200 | |
| 201 | QString BaseDocument::getBasicText() |
| 202 | { |
| 203 | QString text = m_view->getBasicText(); |
| 204 | return text; |
| 205 | } |
| 206 | |
| 207 | QString BaseDocument::getOrc() |
| 208 | { |
| 209 | QString text = m_view->getOrc(); |
| 210 | return text; |
| 211 | } |
| 212 | |
| 213 | QString BaseDocument::getSco() |
| 214 | { |
| 215 | QString text = m_view->getSco(); |
| 216 | return text; |
| 217 | } |
| 218 | |
| 219 | QString BaseDocument::getOptionsText() |
| 220 | { |
| 221 | QString text = m_view->getOptionsText(); |
| 222 | return text; |
| 223 | } |
| 224 | |
| 225 | QString BaseDocument::getWidgetsText() |
| 226 | { |
| 227 | //FIXME allow multiple |
| 228 | QString text = m_widgetLayouts[0]->getWidgetsText(); |
| 229 | QDomDocument d; |
| 230 | d.setContent(text); |
| 231 | // QDomElement n = d.firstChildElement("bsbPanel"); |
| 232 | // if (!n.isNull()) { |
| 233 | // } |
| 234 | return d.toString(); |
| 235 | } |
| 236 | |
| 237 | QString BaseDocument::getPresetsText() |
| 238 | { |
| 239 | //FIXME allow multiple |
| 240 | return m_widgetLayouts[0]->getPresetsText(); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | //void BaseDocument::setOpcodeNameList(QStringList opcodeNameList) |
| 245 | //{ |
| 246 | // m_view->setOpcodeNameList(opcodeNameList); |
| 247 | //} |
| 248 | |
| 249 | WidgetLayout *BaseDocument::getWidgetLayout() |
| 250 | { |
| 251 | //FIXME allow multiple layouts |
| 252 | return m_widgetLayouts[0]; |
| 253 | } |
| 254 | |
| 255 | ConsoleWidget *BaseDocument::getConsole() |
| 256 | { |
| 257 | return m_console; |
| 258 | } |
| 259 | |
| 260 | CsoundEngine *BaseDocument::getEngine() |
| 261 | { |
| 262 | return m_csEngine; |
| 263 | } |
| 264 | |
| 265 | int BaseDocument::play(CsoundOptions *options) |
| 266 | { |
| 267 | while(m_status == PlayStopStatus::Stopping) { |
| 268 | QDEBUG << "Stopping, waiting"; |
| 269 | QThread::msleep(100); |
| 270 | } |
| 271 | mutex.lock(); |
| 272 | m_status = PlayStopStatus::Starting; |
| 273 | if (!m_csEngine->isRunning()) { |
| 274 | foreach (WidgetLayout *wl, m_widgetLayouts) { |
| 275 | wl->flush(); // Flush accumulated values |
| 276 | wl->clearGraphs(); |
| 277 | } |
| 278 | } |
| 279 | m_status = PlayStopStatus::Ok; |
| 280 | mutex.unlock(); |
| 281 | return m_csEngine->play(options); |
| 282 | } |
| 283 | |
| 284 | void BaseDocument::pause() |
| 285 | { |
| 286 | m_csEngine->pause(); |
| 287 | } |
| 288 | |
| 289 | void BaseDocument::stop() |
| 290 | { |
| 291 | if (!m_csEngine->isRunning()) { |
| 292 | QDEBUG << "Csound is not running"; |
| 293 | return; |
| 294 | } |
| 295 | if(m_status == PlayStopStatus::Stopping) { |
| 296 | qDebug() << "Already stopping"; |
| 297 | return; |
| 298 | } |
| 299 | if(m_status == PlayStopStatus::Starting) { |
| 300 | QDEBUG << "Asked to stop, but we are already starting"; |
| 301 | return; |
| 302 | } |
| 303 | QDEBUG << "getting lock"; |
| 304 | mutex.lock(); |
| 305 | QDEBUG << "locked, stopping engine"; |
| 306 | m_status = PlayStopStatus::Stopping; |
| 307 | |
| 308 | m_csEngine->stop(); |
| 309 | QDEBUG << "Engine stopped, signaling widgets..."; |
| 310 | foreach (WidgetLayout *wl, m_widgetLayouts) { |
| 311 | // TODO only needed to flush graph buffer, but this should be moved to this class |
| 312 | wl->engineStopped(); |
| 313 | } |
| 314 | m_status = PlayStopStatus::Ok; |
| 315 | mutex.unlock(); |
| 316 | QDEBUG << "Stopped OK"; |
| 317 | } |
| 318 | |
| 319 | int BaseDocument::record(int format) |
| 320 | { |
| 321 | #ifdef PERFTHREAD_RECORD |
| 322 | return m_csEngine->startRecording(format, "output.wav"); |
| 323 | #else |
| 324 | (void) format; |
| 325 | QMessageBox::warning(NULL, tr("Recording not possible"), |
| 326 | tr("This version of CsoundQt was not built with recording support.")); |
| 327 | return 0; |
| 328 | #endif |
| 329 | } |
| 330 | |
| 331 | void BaseDocument::stopRecording() |
| 332 | { |
| 333 | m_csEngine->stopRecording(); |
| 334 | } |
| 335 | |
| 336 | void BaseDocument::queueEvent(QString eventLine, int delay) |
| 337 | { |
| 338 | m_csEngine->queueEvent(eventLine, delay); //TODO implement passing of timestamp |
| 339 | } |
| 340 | |
| 341 | void BaseDocument::loadTextString(QString &text) |
| 342 | { |
| 343 | setTextString(text); |
| 344 | m_view->clearUndoRedoStack(); |
| 345 | } |
| 346 | |
| 347 | void BaseDocument::setFileName(QString name) |
| 348 | { |
| 349 | fileName = name; |
| 350 | if (name.endsWith(".csd") || name.isEmpty()) { |
| 351 | m_view->setFileType(EDIT_CSOUND_MODE); |
| 352 | } |
| 353 | else if (name.endsWith(".py")) { |
| 354 | m_view->setFileType(EDIT_PYTHON_MODE); |
| 355 | } |
| 356 | else if (name.endsWith(".xml")) { |
| 357 | m_view->setFileType(EDIT_XML_MODE); |
| 358 | } |
| 359 | else if (name.endsWith(".orc")) { |
| 360 | m_view->setFileType(EDIT_ORC_MODE); |
| 361 | } |
| 362 | else if (name.endsWith(".udo")) { |
| 363 | m_view->setFileType(EDIT_ORC_MODE); |
| 364 | } |
| 365 | else if (name.endsWith(".sco")) { |
| 366 | m_view->setFileType(EDIT_SCO_MODE); |
| 367 | } |
| 368 | else if (name.endsWith(".inc")) { |
| 369 | m_view->setFileType(EDIT_INC_MODE); |
| 370 | } |
| 371 | else if (name.endsWith(".html")) { |
| 372 | m_view->setFileType(EDIT_HTML_MODE); |
| 373 | } |
| 374 | else { |
| 375 | m_view->setFileType(EDIT_GENERIC_MODE); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | AppProperties BaseDocument::getAppProperties() |
| 380 | { |
| 381 | return m_view->getAppProperties(); |
| 382 | } |