ctkPythonConsole.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. /*=========================================================================
  15. Program: ParaView
  16. Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
  17. All rights reserved.
  18. ParaView is a free software; you can redistribute it and/or modify it
  19. under the terms of the ParaView license version 1.2.
  20. See http://www.paraview.org/paraview/project/license.html for the full ParaView license.
  21. A copy of this license can be obtained by contacting
  22. Kitware Inc.
  23. 28 Corporate Drive
  24. Clifton Park, NY 12065
  25. USA
  26. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
  30. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. =========================================================================*/
  38. // Qt includes
  39. #include <QAbstractItemView>
  40. #include <QCoreApplication>
  41. #include <QIcon>
  42. #include <QResizeEvent>
  43. #include <QScrollBar>
  44. #include <QStringListModel>
  45. #include <QTextCharFormat>
  46. #include <QVBoxLayout>
  47. // PythonQt includes
  48. #include <PythonQt.h>
  49. #include <PythonQtObjectPtr.h>
  50. // CTK includes
  51. #include <ctkConsole.h>
  52. #include <ctkConsole_p.h>
  53. #include <ctkAbstractPythonManager.h>
  54. #include "ctkPythonConsole.h"
  55. #ifdef __GNUC__
  56. // Disable warnings related to Python macros and functions
  57. // See http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
  58. // Note: Ideally the incriminated functions and macros should be fixed upstream ...
  59. #pragma GCC diagnostic ignored "-Wold-style-cast"
  60. #endif
  61. //----------------------------------------------------------------------------
  62. // ctkPythonConsoleCompleter
  63. //----------------------------------------------------------------------------
  64. class ctkPythonConsoleCompleter : public ctkConsoleCompleter
  65. {
  66. public:
  67. ctkPythonConsoleCompleter(ctkAbstractPythonManager& pythonManager);
  68. virtual int cursorOffset(const QString& completion);
  69. virtual void updateCompletionModel(const QString& completion);
  70. static QString searchUsableCharForCompletion(const QString& completion);
  71. protected:
  72. bool isInUserDefinedClass(const QString &pythonFunctionPath);
  73. bool isUserDefinedFunction(const QString &pythonFunctionName);
  74. bool isBuiltInFunction(const QString &pythonFunctionName);
  75. int parameterCountUserDefinedClassFunction(const QString &pythonFunctionName);
  76. int parameterCountBuiltInFunction(const QString& pythonFunctionName);
  77. int parameterCountUserDefinedFunction(const QString& pythonFunctionName);
  78. int parameterCountFromDocumentation(const QString& pythonFunctionPath);
  79. ctkAbstractPythonManager& PythonManager;
  80. };
  81. //----------------------------------------------------------------------------
  82. ctkPythonConsoleCompleter::ctkPythonConsoleCompleter(ctkAbstractPythonManager& pythonManager)
  83. : PythonManager(pythonManager)
  84. {
  85. this->setParent(&pythonManager);
  86. }
  87. //----------------------------------------------------------------------------
  88. int ctkPythonConsoleCompleter::cursorOffset(const QString& completion)
  89. {
  90. QString allTextFromShell = completion;
  91. int parameterCount = 0;
  92. int cursorOffset = 0;
  93. if (allTextFromShell.contains("()"))
  94. {
  95. allTextFromShell.replace("()", "");
  96. // Search backward through the string for usable characters
  97. QString currentCompletionText;
  98. for (int i = allTextFromShell.length()-1; i >= 0; --i)
  99. {
  100. QChar c = allTextFromShell.at(i);
  101. if (c.isLetterOrNumber() || c == '.' || c == '_')
  102. {
  103. currentCompletionText.prepend(c);
  104. }
  105. else
  106. {
  107. break;
  108. }
  109. }
  110. QStringList lineSplit = currentCompletionText.split(".", QString::KeepEmptyParts);
  111. QString functionName = lineSplit.at(lineSplit.length()-1);
  112. QStringList builtinFunctionPath = QStringList() << "__main__" << "__builtins__";
  113. QStringList userDefinedFunctionPath = QStringList() << "__main__";
  114. if (this->isBuiltInFunction(functionName))
  115. {
  116. parameterCount = this->parameterCountBuiltInFunction(QStringList(builtinFunctionPath+lineSplit).join("."));
  117. }
  118. else if (this->isUserDefinedFunction(functionName))
  119. {
  120. parameterCount = this->parameterCountUserDefinedFunction(QStringList(userDefinedFunctionPath+lineSplit).join("."));
  121. }
  122. else if (this->isInUserDefinedClass(currentCompletionText))
  123. {
  124. // "self" parameter can be ignored
  125. parameterCount = this->parameterCountUserDefinedClassFunction(QStringList(userDefinedFunctionPath+lineSplit).join(".")) - 1;
  126. }
  127. else
  128. {
  129. QStringList variableNameAndFunctionList = userDefinedFunctionPath + lineSplit;
  130. QString variableNameAndFunction = variableNameAndFunctionList.join(".");
  131. parameterCount = this->parameterCountFromDocumentation(variableNameAndFunction);
  132. }
  133. }
  134. if (parameterCount > 0)
  135. {
  136. cursorOffset = 1;
  137. }
  138. return cursorOffset;
  139. }
  140. //---------------------------------------------------------------------------
  141. bool ctkPythonConsoleCompleter::isInUserDefinedClass(const QString &pythonFunctionPath)
  142. {
  143. return this->PythonManager.pythonAttributes(pythonFunctionPath).contains("__func__");
  144. }
  145. //---------------------------------------------------------------------------
  146. bool ctkPythonConsoleCompleter::isUserDefinedFunction(const QString &pythonFunctionName)
  147. {
  148. return this->PythonManager.pythonAttributes(pythonFunctionName).contains("__call__");
  149. }
  150. //---------------------------------------------------------------------------
  151. bool ctkPythonConsoleCompleter::isBuiltInFunction(const QString &pythonFunctionName)
  152. {
  153. return this->PythonManager.pythonAttributes(pythonFunctionName, QLatin1String("__main__.__builtins__")).contains("__call__");
  154. }
  155. //---------------------------------------------------------------------------
  156. int ctkPythonConsoleCompleter::parameterCountBuiltInFunction(const QString& pythonFunctionName)
  157. {
  158. int parameterCount = 0;
  159. PyObject* pFunction = this->PythonManager.pythonModule(pythonFunctionName);
  160. if (pFunction && PyObject_HasAttrString(pFunction, "__doc__"))
  161. {
  162. PyObject* pDoc = PyObject_GetAttrString(pFunction, "__doc__");
  163. QString docString = PyString_AsString(pDoc);
  164. QString argumentExtract = docString.mid(docString.indexOf("(")+1, docString.indexOf(")") - docString.indexOf("(")-1);
  165. QStringList arguments = argumentExtract.split(",", QString::SkipEmptyParts);
  166. parameterCount = arguments.count();
  167. Py_DECREF(pDoc);
  168. Py_DECREF(pFunction);
  169. }
  170. return parameterCount;
  171. }
  172. //----------------------------------------------------------------------------
  173. int ctkPythonConsoleCompleter::parameterCountUserDefinedFunction(const QString& pythonFunctionName)
  174. {
  175. int parameterCount = 0;
  176. PyObject* pFunction = this->PythonManager.pythonModule(pythonFunctionName);
  177. if (PyCallable_Check(pFunction))
  178. {
  179. PyObject* fc = PyObject_GetAttrString(pFunction, "func_code");
  180. if (fc)
  181. {
  182. PyObject* ac = PyObject_GetAttrString(fc, "co_argcount");
  183. if (ac)
  184. {
  185. parameterCount = PyInt_AsLong(ac);
  186. Py_DECREF(ac);
  187. }
  188. Py_DECREF(fc);
  189. }
  190. }
  191. return parameterCount;
  192. }
  193. //----------------------------------------------------------------------------
  194. int ctkPythonConsoleCompleter::parameterCountUserDefinedClassFunction(const QString& pythonFunctionName)
  195. {
  196. int parameterCount = 0;
  197. PyObject* pFunction = this->PythonManager.pythonObject(pythonFunctionName);
  198. if (PyCallable_Check(pFunction))
  199. {
  200. PyObject* fc = PyObject_GetAttrString(pFunction, "func_code");
  201. if (fc)
  202. {
  203. PyObject* ac = PyObject_GetAttrString(fc, "co_argcount");
  204. if (ac)
  205. {
  206. parameterCount = PyInt_AsLong(ac);
  207. Py_DECREF(ac);
  208. }
  209. Py_DECREF(fc);
  210. }
  211. }
  212. return parameterCount;
  213. }
  214. //----------------------------------------------------------------------------
  215. int ctkPythonConsoleCompleter::parameterCountFromDocumentation(const QString& pythonFunctionPath)
  216. {
  217. int parameterCount = 0;
  218. PyObject* pFunction = this->PythonManager.pythonObject(pythonFunctionPath);
  219. if (pFunction)
  220. {
  221. if (PyObject_HasAttrString(pFunction, "__call__"))
  222. {
  223. PyObject* pDoc = PyObject_GetAttrString(pFunction, "__doc__");
  224. if (PyString_Check(pDoc))
  225. {
  226. QString docString = PyString_AsString(pDoc);
  227. QString argumentExtract = docString.mid(docString.indexOf("(")+1, docString.indexOf(")") - docString.indexOf("(")-1);
  228. QStringList arguments = argumentExtract.split(",", QString::SkipEmptyParts);
  229. parameterCount = arguments.count();
  230. }
  231. }
  232. Py_DECREF(pFunction);
  233. }
  234. return parameterCount;
  235. }
  236. //----------------------------------------------------------------------------
  237. QString ctkPythonConsoleCompleter::searchUsableCharForCompletion(const QString& completion)
  238. {
  239. bool betweenSingleQuotes = false;
  240. bool betweenDoubleQuotes = false;
  241. int numberOfParenthesisClosed = 0;
  242. // Search backward through the string for usable characters
  243. QString textToComplete;
  244. for (int i = completion.length()-1; i >= 0; --i)
  245. {
  246. QChar c = completion.at(i);
  247. if (c == '\'' && !betweenDoubleQuotes)
  248. {
  249. betweenSingleQuotes = !betweenSingleQuotes;
  250. }
  251. if (c == '"' && !betweenSingleQuotes)
  252. {
  253. betweenDoubleQuotes = !betweenDoubleQuotes;
  254. }
  255. // Stop the completion if c is not a letter,number,.,_,(,) and outside parenthesis
  256. if (c.isLetterOrNumber() || c == '.' || c == '_' || c == '(' || c == ')'
  257. || numberOfParenthesisClosed)
  258. {
  259. // Keep adding caractere to the completion if
  260. // the number of '(' is always <= to the number of ')'
  261. // note that we must not count parenthesis if they are between quote...
  262. if (!betweenSingleQuotes && !betweenDoubleQuotes)
  263. {
  264. if (c == '(')
  265. {
  266. if (numberOfParenthesisClosed>0)
  267. {
  268. numberOfParenthesisClosed--;
  269. }
  270. else
  271. break; // stop to prepend
  272. }
  273. if (c == ')')
  274. {
  275. numberOfParenthesisClosed++;
  276. }
  277. }
  278. textToComplete.prepend(c);
  279. }
  280. else
  281. {
  282. break;
  283. }
  284. }
  285. return textToComplete;
  286. }
  287. //----------------------------------------------------------------------------
  288. void ctkPythonConsoleCompleter::updateCompletionModel(const QString& completion)
  289. {
  290. // Start by clearing the model
  291. this->setModel(0);
  292. // Don't try to complete the empty string
  293. if (completion.isEmpty())
  294. {
  295. return;
  296. }
  297. bool appendParenthesis = true;
  298. // Search backward through the string for usable characters
  299. QString textToComplete = searchUsableCharForCompletion(completion);
  300. // Split the string at the last dot, if one exists
  301. QString lookup;
  302. QString compareText = textToComplete;
  303. int dot = compareText.lastIndexOf('.');
  304. if (dot != -1)
  305. {
  306. lookup = compareText.mid(0, dot);
  307. compareText = compareText.mid(dot+1);
  308. }
  309. // Lookup python names
  310. QStringList attrs;
  311. if (!lookup.isEmpty() || !compareText.isEmpty())
  312. {
  313. QString module = "__main__";
  314. attrs = this->PythonManager.pythonAttributes(lookup, module.toLatin1(), appendParenthesis);
  315. module = "__main__.__builtins__";
  316. attrs << this->PythonManager.pythonAttributes(lookup, module.toLatin1(),
  317. appendParenthesis);
  318. attrs.removeDuplicates();
  319. }
  320. // Initialize the completion model
  321. if (!attrs.isEmpty())
  322. {
  323. this->setCompletionMode(QCompleter::PopupCompletion);
  324. this->setModel(new QStringListModel(attrs, this));
  325. this->setCaseSensitivity(Qt::CaseInsensitive);
  326. this->setCompletionPrefix(compareText.toLower());
  327. // If a dot as been entered and if an item of possible
  328. // choices matches one of the preference list, it will be selected.
  329. QModelIndex preferredIndex = this->completionModel()->index(0, 0);
  330. int dotCount = completion.count('.');
  331. if (dotCount == 0 || completion.at(completion.count() - 1) == '.')
  332. {
  333. foreach(const QString& pref, this->AutocompletePreferenceList)
  334. {
  335. //qDebug() << "pref" << pref;
  336. int dotPref = pref.count('.');
  337. // Skip if there are dots in pref and if the completion has already more dots
  338. // than the pref
  339. if ((dotPref != 0) && (dotCount > dotPref))
  340. {
  341. continue;
  342. }
  343. // Extract string before the last dot
  344. int lastDot = pref.lastIndexOf('.');
  345. QString prefBeforeLastDot;
  346. if (lastDot != -1)
  347. {
  348. prefBeforeLastDot = pref.left(lastDot);
  349. }
  350. //qDebug() << "prefBeforeLastDot" << prefBeforeLastDot;
  351. if (!prefBeforeLastDot.isEmpty() && QString::compare(prefBeforeLastDot, lookup) != 0)
  352. {
  353. continue;
  354. }
  355. QString prefAfterLastDot = pref;
  356. if (lastDot != -1 )
  357. {
  358. prefAfterLastDot = pref.right(pref.size() - lastDot - 1);
  359. }
  360. //qDebug() << "prefAfterLastDot" << prefAfterLastDot;
  361. QModelIndexList list = this->completionModel()->match(
  362. this->completionModel()->index(0, 0), Qt::DisplayRole, QVariant(prefAfterLastDot));
  363. if (list.count() > 0)
  364. {
  365. preferredIndex = list.first();
  366. break;
  367. }
  368. }
  369. }
  370. this->popup()->setCurrentIndex(preferredIndex);
  371. }
  372. }
  373. //----------------------------------------------------------------------------
  374. // ctkPythonConsolePrivate
  375. //----------------------------------------------------------------------------
  376. class ctkPythonConsolePrivate : public ctkConsolePrivate
  377. {
  378. Q_DECLARE_PUBLIC(ctkPythonConsole);
  379. public:
  380. ctkPythonConsolePrivate(ctkPythonConsole& object);
  381. ~ctkPythonConsolePrivate();
  382. void initializeInteractiveConsole();
  383. bool push(const QString& code);
  384. /// Reset the input buffer of the interactive console
  385. // void resetInputBuffer();
  386. void printWelcomeMessage();
  387. ctkAbstractPythonManager* PythonManager;
  388. PyObject* InteractiveConsole;
  389. };
  390. //----------------------------------------------------------------------------
  391. // ctkPythonConsolePrivate methods
  392. //----------------------------------------------------------------------------
  393. ctkPythonConsolePrivate::ctkPythonConsolePrivate(ctkPythonConsole& object)
  394. : ctkConsolePrivate(object), PythonManager(0), InteractiveConsole(0)
  395. {
  396. }
  397. //----------------------------------------------------------------------------
  398. ctkPythonConsolePrivate::~ctkPythonConsolePrivate()
  399. {
  400. }
  401. //----------------------------------------------------------------------------
  402. void ctkPythonConsolePrivate::initializeInteractiveConsole()
  403. {
  404. Q_ASSERT(this->PythonManager);
  405. // set up the code.InteractiveConsole instance that we'll use.
  406. const char* code =
  407. "import code\n"
  408. "__ctkConsole = code.InteractiveConsole(locals())\n";
  409. PyRun_SimpleString(code);
  410. // Now get the reference to __ctkConsole and save the pointer.
  411. PyObject* main_module = PyImport_AddModule("__main__");
  412. PyObject* global_dict = PyModule_GetDict(main_module);
  413. this->InteractiveConsole = PyDict_GetItemString(
  414. global_dict, "__ctkConsole");
  415. if (!this->InteractiveConsole)
  416. {
  417. qCritical("Failed to locate the InteractiveConsole object.");
  418. }
  419. }
  420. //----------------------------------------------------------------------------
  421. bool ctkPythonConsolePrivate::push(const QString& code)
  422. {
  423. Q_ASSERT(this->PythonManager);
  424. bool ret_value = false;
  425. QString buffer = code;
  426. // The embedded python interpreter cannot handle DOS line-endings, see
  427. // http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=1167922
  428. buffer.remove('\r');
  429. PyObject *res = PyObject_CallMethod(this->InteractiveConsole,
  430. const_cast<char*>("push"),
  431. const_cast<char*>("z"),
  432. buffer.toLatin1().data());
  433. if (res)
  434. {
  435. int status = 0;
  436. if (PyArg_Parse(res, "i", &status))
  437. {
  438. ret_value = (status > 0);
  439. }
  440. Py_DECREF(res);
  441. }
  442. return ret_value;
  443. }
  444. ////----------------------------------------------------------------------------
  445. //void ctkPythonConsolePrivate::resetInputBuffer()
  446. //{
  447. // if (this->InteractiveConsole)
  448. // {
  449. // //this->MakeCurrent();
  450. // const char* code = "__ctkConsole.resetbuffer()\n";
  451. // PyRun_SimpleString(code);
  452. // //this->ReleaseControl();
  453. // }
  454. //}
  455. //----------------------------------------------------------------------------
  456. void ctkPythonConsolePrivate::printWelcomeMessage()
  457. {
  458. Q_Q(ctkPythonConsole);
  459. q->printMessage(
  460. QString("Python %1 on %2\n").arg(Py_GetVersion()).arg(Py_GetPlatform()),
  461. q->welcomeTextColor());
  462. }
  463. //----------------------------------------------------------------------------
  464. // ctkPythonConsole methods
  465. //----------------------------------------------------------------------------
  466. ctkPythonConsole::ctkPythonConsole(QWidget* parentObject):
  467. Superclass(new ctkPythonConsolePrivate(*this), parentObject)
  468. {
  469. this->setObjectName("pythonConsole");
  470. this->setWindowIcon(QIcon(":/python-icon.png"));
  471. // Disable RemoveTrailingSpaces and AutomaticIndentation
  472. this->setEditorHints(this->editorHints() ^ (RemoveTrailingSpaces | AutomaticIndentation));
  473. // Enable SplitCopiedTextByLine
  474. this->setEditorHints(this->editorHints() | SplitCopiedTextByLine);
  475. this->setDisabled(true);
  476. }
  477. //----------------------------------------------------------------------------
  478. ctkPythonConsole::~ctkPythonConsole()
  479. {
  480. }
  481. ////----------------------------------------------------------------------------
  482. void ctkPythonConsole::initialize(ctkAbstractPythonManager* newPythonManager)
  483. {
  484. Q_D(ctkPythonConsole);
  485. if (d->PythonManager)
  486. {
  487. qWarning() << "ctkPythonConsole already initialized !";
  488. return;
  489. }
  490. // The call to mainContext() ensures that python has been initialized.
  491. Q_ASSERT(newPythonManager);
  492. newPythonManager->mainContext();
  493. Q_ASSERT(PythonQt::self()); // PythonQt should be initialized
  494. ctkPythonConsoleCompleter* completer = new ctkPythonConsoleCompleter(*newPythonManager);
  495. this->setCompleter(completer);
  496. d->PythonManager = newPythonManager;
  497. d->initializeInteractiveConsole();
  498. this->connect(PythonQt::self(), SIGNAL(pythonStdOut(QString)),
  499. d, SLOT(printOutputMessage(QString)));
  500. this->connect(PythonQt::self(), SIGNAL(pythonStdErr(QString)),
  501. d, SLOT(printErrorMessage(QString)));
  502. PythonQt::self()->setRedirectStdInCallback(
  503. ctkConsole::stdInRedirectCallBack, reinterpret_cast<void*>(this));
  504. // Set primary and secondary prompt
  505. this->setPs1(">>> ");
  506. this->setPs2("... ");
  507. this->reset();
  508. // Expose help() function
  509. QStringList helpImportCode;
  510. helpImportCode << "from pydoc import help";
  511. d->PythonManager->executeString(helpImportCode.join("\n"));
  512. this->setDisabled(false);
  513. }
  514. ////----------------------------------------------------------------------------
  515. //void ctkPythonConsole::executeScript(const QString& script)
  516. //{
  517. // Q_D(ctkPythonConsole);
  518. // Q_UNUSED(script);
  519. // d->printOutputMessage("\n");
  520. // emit this->executing(true);
  521. //// d->Interpreter->RunSimpleString(
  522. //// script.toLatin1().data());
  523. // emit this->executing(false);
  524. // d->promptForInput();
  525. //}
  526. //----------------------------------------------------------------------------
  527. QString ctkPythonConsole::ps1() const
  528. {
  529. PyObject * ps1 = PySys_GetObject(const_cast<char*>("ps1"));
  530. const char * ps1_str = PyString_AsString(ps1);
  531. return QLatin1String(ps1_str);
  532. }
  533. //----------------------------------------------------------------------------
  534. void ctkPythonConsole::setPs1(const QString& newPs1)
  535. {
  536. PySys_SetObject(const_cast<char*>("ps1"), PyString_FromString(newPs1.toLatin1().data()));
  537. }
  538. //----------------------------------------------------------------------------
  539. QString ctkPythonConsole::ps2() const
  540. {
  541. PyObject * ps2 = PySys_GetObject(const_cast<char*>("ps2"));
  542. const char * ps2_str = PyString_AsString(ps2);
  543. return QLatin1String(ps2_str);
  544. }
  545. //----------------------------------------------------------------------------
  546. void ctkPythonConsole::setPs2(const QString& newPs2)
  547. {
  548. PySys_SetObject(const_cast<char*>("ps2"), PyString_FromString(newPs2.toLatin1().data()));
  549. }
  550. //----------------------------------------------------------------------------
  551. void ctkPythonConsole::executeCommand(const QString& command)
  552. {
  553. Q_D(ctkPythonConsole);
  554. d->MultilineStatement = d->push(command);
  555. }
  556. //----------------------------------------------------------------------------
  557. void ctkPythonConsole::reset()
  558. {
  559. // Set primary and secondary prompt
  560. this->setPs1(">>> ");
  561. this->setPs2("... ");
  562. this->Superclass::reset();
  563. }