| 1 | /* |
| 2 | Copyright (C) 2010 Andres Cabrera |
| 3 | [email protected] |
| 4 | |
| 5 | This file is part of QuteCsound. |
| 6 | |
| 7 | QuteCsound 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 | QuteCsound 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 <QtGui> |
| 24 | #include "quteapp.h" |
| 25 | #include "types.h" |
| 26 | #include "csoundoptions.h" |
| 27 | #include "simpledocument.h" |
| 28 | // To get AppProperties: |
| 29 | #include "documentview.h" |
| 30 | #include "opentryparser.h" |
| 31 | #include "widgetlayout.h" |
| 32 | #include "console.h" |
| 33 | #include "aboutwidget.h" |
| 34 | #include "settingsdialog.h" |
| 35 | #include "csound.h" |
| 36 | #include "csoundengine.h" |
| 37 | #include "types.h" |
| 38 | |
| 39 | #define CSD_NAME "quteapp.csd" |
| 40 | |
| 41 | QuteApp::QuteApp(QWidget *parent) |
| 42 | : QMainWindow(parent) |
| 43 | { |
| 44 | m_options = new CsoundOptions; |
| 45 | m_options->fileName1 = CSD_NAME; |
| 46 | m_options->rtAudioModule = _configlists.rtAudioNames.indexOf("portaudio"); |
| 47 | OpEntryParser *m_opcodeTree = new OpEntryParser(":/main/opcodes.xml"); |
| 48 | m_doc = new SimpleDocument(this, m_opcodeTree); |
| 49 | m_doc->getEngine()->setThreaded(true); |
| 50 | m_console = m_doc->getConsole(); |
| 51 | m_console->setWindowTitle(tr("Csound Console")); |
| 52 | m_console->setWindowFlags(Qt::Window | Qt::WindowStaysOnTopHint); |
| 53 | m_aboutWidget = new AboutWidget(this); |
| 54 | } |
| 55 | |
| 56 | QuteApp::~QuteApp() |
| 57 | { |
| 58 | m_doc->stop(); |
| 59 | if (m_saveState && QFile::exists(CSD_NAME)) { |
| 60 | QFile f(CSD_NAME); |
| 61 | if (f.open(QIODevice::WriteOnly)) { |
| 62 | QString fullText = m_doc->getFullText(); |
| 63 | f.write(fullText.toLocal8Bit()); |
| 64 | f.close(); |
| 65 | } else { |
| 66 | QMessageBox::critical(this, tr("Error"), tr("Error saving file!")); |
| 67 | } |
| 68 | } |
| 69 | delete m_doc; |
| 70 | delete m_options; |
| 71 | // delete m_opcodeTree; |
| 72 | } |
| 73 | |
| 74 | void QuteApp::setRunPath(QString path) |
| 75 | { |
| 76 | #ifdef Q_OS_MAC |
| 77 | path = path.left(path.lastIndexOf("/")); |
| 78 | #ifdef USE_DOUBLE |
| 79 | QString opcodeDir = path + QDir::separator() + "../Frameworks/CsoundLib64.framework/Resources/Opcodes64"; |
| 80 | #else |
| 81 | QString opcodeDir = path + QDir::separator() + "../Frameworks/CsoundLib.framework/Resources/Opcodes"; |
| 82 | #endif |
| 83 | QDir::setCurrent(path + QDir::separator() + "../Resources/"); |
| 84 | #else //not Q_OS_MAC |
| 85 | QDir::setCurrent(QDir::current().absolutePath() + QDir::separator() + "data"); |
| 86 | QString opcodeDir = QDir::currentPath(); |
| 87 | #endif |
| 88 | qDebug() << QDir::current() << qApp->applicationFilePath(); |
| 89 | qDebug() << "opcodeDir" << opcodeDir; |
| 90 | csoundSetGlobalEnv("OPCODEDIR", opcodeDir.toLocal8Bit().data()); |
| 91 | csoundSetGlobalEnv("OPCODEDIR64", opcodeDir.toLocal8Bit().data()); |
| 92 | |
| 93 | loadCsd(); |
| 94 | setAboutTexts(); |
| 95 | createMenus(); |
| 96 | setCentralWidget((QWidget *) m_doc->getWidgetLayout()); |
| 97 | // m_aboutWidget->setWindowFlags(Qt::Window | Qt::WindowStaysOnTopHint); |
| 98 | // m_console->show(); |
| 99 | // start(); |
| 100 | setWindowTitle(m_appName); |
| 101 | // setAttribute(Qt::WA_DeleteOnClose); // Will cause crashing! |
| 102 | if (m_autorun) { |
| 103 | start(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void QuteApp::createMenus() |
| 108 | { |
| 109 | QMenu *menu = menuBar()->addMenu(tr("&File")); |
| 110 | if (m_showRunOptions) { |
| 111 | menu->addAction(tr("Run"), this, SLOT(start()), QKeySequence("Ctrl+R")); |
| 112 | menu->addAction(tr("Stop"), this, SLOT(stop()), QKeySequence("Ctrl+.")); |
| 113 | } |
| 114 | menu->addAction(tr("Configure..."), this, SLOT(configure())); |
| 115 | menu->addAction(tr("Show Console Output"), this, SLOT(showConsole())); |
| 116 | menu->addSeparator(); |
| 117 | menu->addAction(tr("Quit"), qApp, SLOT(quit())); |
| 118 | QMenu *about = menuBar()->addMenu(tr("&Help")); |
| 119 | about->addAction(tr("About ..."), m_aboutWidget, SLOT(open())); |
| 120 | about->addAction(tr("Info"), this, SLOT(info())); |
| 121 | } |
| 122 | |
| 123 | void QuteApp::start() |
| 124 | { |
| 125 | m_doc->play(m_options); |
| 126 | } |
| 127 | |
| 128 | void QuteApp::pause() |
| 129 | { |
| 130 | m_doc->pause(); |
| 131 | } |
| 132 | |
| 133 | void QuteApp::stop() |
| 134 | { |
| 135 | m_doc->stop(); |
| 136 | } |
| 137 | |
| 138 | void QuteApp::save() |
| 139 | { |
| 140 | qDebug() << "Not implemented yet."; |
| 141 | } |
| 142 | |
| 143 | void QuteApp::showConsole() |
| 144 | { |
| 145 | m_console->setVisible(!m_console->isVisible()); |
| 146 | } |
| 147 | |
| 148 | void QuteApp::configure() |
| 149 | { |
| 150 | SettingsDialog d(this, m_options); |
| 151 | d.exec(); |
| 152 | if (d.result() == QDialog::Accepted) { |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void QuteApp::info() |
| 157 | { |
| 158 | QString message = tr("This application was built using CsoundQt\n"); |
| 159 | bool doubles; |
| 160 | #ifdef USE_DOUBLE |
| 161 | doubles = true; |
| 162 | #else |
| 163 | doubles = false; |
| 164 | #endif |
| 165 | message += tr("Using Csound %1 version %2\n").arg(doubles ? "doubles" : "floats").arg(csoundGetVersion()); |
| 166 | QMessageBox::information(this, tr("CsoundQt Application"), message); |
| 167 | } |
| 168 | |
| 169 | bool QuteApp::loadCsd() |
| 170 | { |
| 171 | QString fileName = m_options->fileName1; |
| 172 | QFile file(fileName); |
| 173 | if (!file.open(QFile::ReadOnly)) { |
| 174 | QMessageBox::warning(this, tr("QuteApp"), |
| 175 | tr("Application not built correctly.\ncsd file not in bundle.\n%1").arg(QDir::currentPath())); |
| 176 | return false; |
| 177 | } |
| 178 | // QApplication::setOverrideCursor(Qt::WaitCursor); |
| 179 | |
| 180 | QString text; |
| 181 | bool inEncFile = false; |
| 182 | while (!file.atEnd()) { |
| 183 | QByteArray line = file.readLine(); |
| 184 | if (line.contains("<CsFileB ")) { |
| 185 | inEncFile = true; |
| 186 | } |
| 187 | if (!inEncFile) { |
| 188 | while (line.contains("\r\n")) { |
| 189 | line.replace("\r\n", "\n"); //Change Win returns to line endings |
| 190 | } |
| 191 | while (line.contains("\r")) { |
| 192 | line.replace("\r", "\n"); //Change Mac returns to line endings |
| 193 | } |
| 194 | QTextDecoder decoder(QTextCodec::codecForLocale()); |
| 195 | text = text + decoder.toUnicode(line); |
| 196 | if (!line.endsWith("\n")) |
| 197 | text += "\n"; |
| 198 | } |
| 199 | else { |
| 200 | text += line; |
| 201 | if (line.contains("</CsFileB>" && !line.contains("<CsFileB " )) ) { |
| 202 | inEncFile = false; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // m_doc->setInitialDir(initialDir); |
| 208 | // m_doc->showLiveEventPanels(false); |
| 209 | |
| 210 | // setCurrentOptionsForPage(m_doc); |
| 211 | // setCurrentFile(fileName); |
| 212 | |
| 213 | // m_doc->setFileName(fileName); // Must set before sending text to set highlighting mode |
| 214 | |
| 215 | // connectActions(); |
| 216 | // connect(documentPages[curPage], SIGNAL(currentTextUpdated()), this, SLOT(markInspectorUpdate())); |
| 217 | // connect(documentPages[curPage], SIGNAL(modified()), this, SLOT(documentWasModified())); |
| 218 | // connect(documentPages[curPage], SIGNAL(currentLineChanged(int)), this, SLOT(showLineNumber(int))); |
| 219 | // connect(documentPages[curPage], SIGNAL(setWidgetClipboardSignal(QString)), |
| 220 | // this, SLOT(setWidgetClipboard(QString))); |
| 221 | // connect(documentPages[curPage], SIGNAL(setCurrentAudioFile(QString)), |
| 222 | // this, SLOT(setCurrentAudioFile(QString))); |
| 223 | // connect(documentPages[curPage]->getView(), SIGNAL(lineNumberSignal(int)), |
| 224 | // this, SLOT(showLineNumber(int))); |
| 225 | // connect(documentPages[curPage], SIGNAL(evaluatePythonSignal(QString)), |
| 226 | // this, SLOT(evaluatePython(QString))); |
| 227 | |
| 228 | m_doc->setTextString(text); |
| 229 | |
| 230 | AppProperties props = m_doc->getAppProperties(); |
| 231 | m_appName = props.appName; |
| 232 | m_author = props.author; |
| 233 | m_version = props.version; |
| 234 | m_email = props.email; |
| 235 | m_website = props.website; |
| 236 | m_date = props.date; |
| 237 | m_instructions = props.instructions; |
| 238 | m_autorun = props.autorun; |
| 239 | |
| 240 | this->resize(m_doc->getWidgetLayout()->size()); |
| 241 | // m_doc->readOnly = true; |
| 242 | // QApplication::restoreOverrideCursor(); |
| 243 | |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | void QuteApp::setAboutTexts() |
| 248 | { |
| 249 | QString templateText = "<h1>%1<h1><h2>%2</h2><h2>%3</h2>%4<br />%5<br />%6<br />"; |
| 250 | QString intro = templateText.arg(m_appName).arg(m_author).arg(m_version) |
| 251 | .arg(m_date).arg(m_website).arg(m_email); |
| 252 | m_aboutWidget->setIntroText(intro); |
| 253 | m_aboutWidget->setInstructions(m_instructions); |
| 254 | } |