Debian Codegraph @

src/pythonconsole.cpp

raw symbols SCIP
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 <qglobal.h>
24 #if defined(QCS_PYTHONQT) && defined(Q_OS_MACOS)
25 #include <Python.h> // necessary for pythonqt build on OSX
26 #endif
27
28 #include "pythonconsole.h"
29 #include "PythonQt.h"
30 //#include "PythonQtGui.h"
31 #include "PythonQt_QtAll.h"
32 #include "gui/PythonQtScriptingConsole.h"
33 #include "pyqcsobject.h"
34 #include "qutesheet.h"
35
36 #include "qutecsound.h"
37
38 PythonConsole::PythonConsole(QWidget *parent)
39 : QDockWidget(parent), m_pqcs(0), m_console(0)
40 {
41 // m_text = new QTextEdit(this);
42 // m_text->setReadOnly(true);
43
44 setWindowTitle(tr("Python Console"));
45 PythonQt::init(PythonQt::RedirectStdOut);
46 PythonQt_QtAll::init();
47 initializeInterpreter();
48 }
49
50 PythonConsole::~PythonConsole()
51 {
52 delete m_console;
53 delete m_pqcs;
54 PythonQt::cleanup();
55 }
56
57
58 void PythonConsole::evaluate(QString evalCode, bool notify)
59 {
60 PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
61 // PythonQtObjectPtr mainContext = m_pqcs->getMainModule();
62 mainContext.evalScript(evalCode.trimmed() + "\n");
63 if (notify) {
64 QString printScript = "print('Evaluated " + QString::number(countLines(evalCode)) + " lines.')";
65 mainContext.evalScript(printScript);
66 m_console->appendCommandPrompt();
67 }
68 }
69
70 void PythonConsole::runScript(QString fileName)
71 {
72 QDir dir = QDir::currentPath();
73 QDir newDir = QDir(fileName.mid(0, fileName.lastIndexOf(QDir::separator())));
74 qDebug() << newDir.absolutePath();
75 bool set = QDir::setCurrent(newDir.absolutePath());
76 PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
77 // mainContext.addVariable("currentScript", QVariant(fileName));
78 // PythonQtObjectPtr mainContext = m_pqcs->getMainModule();
79 QFile file(fileName);
80 if (!file.open(QIODevice::ReadOnly)) {
81 QString printScript = "print('Error opening file: " + fileName + "')";
82 mainContext.evalScript(printScript);
83 }
84 QString evalCode = QString(file.readAll());
85 evaluate(evalCode);
86 // mainContext.removeVariable("currentScript");
87 QDir::setCurrent (dir.absolutePath());
88 }
89
90 void PythonConsole::initializeInterpreter()
91 {
92 PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
93 if (m_console != 0) {
94 delete m_console;
95 }
96 if (m_pqcs != 0) {
97 delete m_pqcs;
98 }
99 m_console = new PythonQtScriptingConsole(this, mainContext);
100
101 // add a QObject to the namespace of the main python context
102 m_pqcs = new PyQcsObject() ;
103 m_pqcs->setCsoundQt(static_cast<CsoundQt *>(parentWidget()));
104 PythonQt::self()->registerCPPClass("QuteSheet", "","qs", PythonQtCreateObject<QuteSheet>);
105 mainContext.addObject("q", m_pqcs);
106 mainContext.evalScript("from PythonQt.qs import QuteSheet");
107 // mainContext.evalScript("print 'CsoundQt Python Interpreter Initialized.'");
108 // mainContext.evalScript("s = q.schedule");
109 // mainContext.evalScript("import os");
110
111 setWidget(m_console);
112 // m_console->setCurrentFont(QFont("Courier New"));
113 // m_console->setFontFamily("Courier New");
114 // m_console->setAcceptRichText (false);
115 m_console->show();
116 }
117
118 void PythonConsole::closeEvent(QCloseEvent * /*event*/)
119 {
120 emit Close(false);
121 }
122
123 int PythonConsole::countLines(QString evalCode)
124 {
125 int lines = evalCode.count("\n");
126 if (!evalCode.endsWith("\n")) {
127 lines++;
128 }
129 return lines;
130 }
131
132 //void PythonConsole::runCommand(QString command)
133 //{
134 // qDebug() << "PythonConsole::runCommand " << command;
135 //}
136
137 //void PythonConsole::keyPressEvent(QKeyEvent * event)
138 //{
139 // qDebug() << "PythonConsole::keyPressEvent";
140 // if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Return) {
141 // runCurrentCommand();
142 // }
143 // else {
144 // m_text->insertPlainText(event->text());
145 // m_command.append(event->text());
146 // }
147 //}
148
149 //void PythonConsole::runCurrentCommand()
150 //{
151 // runCommand(m_command);
152 // m_command = "";
153 //}