ctkSimplePythonShellMain.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // Qt includes
  15. #include <QApplication>
  16. #include <QTextStream>
  17. #include <QTimer>
  18. // CTK includes
  19. #include <ctkCallback.h>
  20. #include <ctkPythonConsole.h>
  21. #include <ctkCommandLineParser.h>
  22. #include "ctkSimplePythonManager.h"
  23. #include "ctkTestWrappedQProperty.h"
  24. #include "ctkTestWrappedQInvokable.h"
  25. #include "ctkTestWrappedSlot.h"
  26. #include "ctkSimplePythonShellConfigure.h" // For CTK_WRAP_PYTHONQT_USE_VTK
  27. #ifdef CTK_WRAP_PYTHONQT_USE_VTK
  28. # include "ctkTestWrappedQListOfVTKObject.h"
  29. # include "ctkTestWrappedVTKSlot.h"
  30. # include "ctkTestWrappedVTKQInvokable.h"
  31. # include <vtkDebugLeaks.h>
  32. #endif
  33. namespace
  34. {
  35. //-----------------------------------------------------------------------------
  36. void executeScripts(void * data)
  37. {
  38. ctkSimplePythonManager * pythonManager = reinterpret_cast<ctkSimplePythonManager*>(data);
  39. QStringList scripts = pythonManager->property("scripts").toStringList();
  40. foreach(const QString& script, scripts)
  41. {
  42. pythonManager->executeFile(script);
  43. if (pythonManager->pythonErrorOccured())
  44. {
  45. QApplication::exit(EXIT_FAILURE);
  46. }
  47. }
  48. }
  49. } // end of anonymous namespace
  50. //-----------------------------------------------------------------------------
  51. int main(int argc, char** argv)
  52. {
  53. #ifdef CTK_WRAP_PYTHONQT_USE_VTK
  54. vtkDebugLeaks::SetExitError(true);
  55. #endif
  56. QApplication app(argc, argv);
  57. ctkCommandLineParser parser;
  58. // Use Unix-style argument names
  59. parser.setArgumentPrefix("--", "-");
  60. // Add command line argument names
  61. parser.addArgument("help", "h", QVariant::Bool, "Print usage information and exit.");
  62. parser.addArgument("interactive", "I", QVariant::Bool, "Enable interactive mode");
  63. // Parse the command line arguments
  64. bool ok = false;
  65. QHash<QString, QVariant> parsedArgs = parser.parseArguments(QCoreApplication::arguments(), &ok);
  66. if (!ok)
  67. {
  68. QTextStream(stderr, QIODevice::WriteOnly) << "Error parsing arguments: "
  69. << parser.errorString() << "\n";
  70. return EXIT_FAILURE;
  71. }
  72. // Show a help message
  73. if (parsedArgs.contains("help") || parsedArgs.contains("h"))
  74. {
  75. QTextStream(stdout, QIODevice::WriteOnly) << "ctkSimplePythonShell\n"
  76. << "Usage\n\n"
  77. << " ctkSimplePythonShell [options] [<path-to-python-script> ...]\n\n"
  78. << "Options\n"
  79. << parser.helpText();
  80. return EXIT_SUCCESS;
  81. }
  82. ctkSimplePythonManager pythonManager;
  83. ctkPythonConsole console;
  84. console.initialize(&pythonManager);
  85. console.setAttribute(Qt::WA_QuitOnClose, true);
  86. console.resize(600, 280);
  87. console.show();
  88. console.setProperty("isInteractive", parsedArgs.contains("interactive"));
  89. QStringList list;
  90. list << "qt.QPushButton";
  91. console.completer()->setAutocompletePreferenceList(list);
  92. pythonManager.addObjectToPythonMain("_ctkPythonConsoleInstance", &console);
  93. ctkTestWrappedQProperty testWrappedQProperty;
  94. pythonManager.addObjectToPythonMain("_testWrappedQPropertyInstance", &testWrappedQProperty);
  95. ctkTestWrappedQInvokable testWrappedQInvokable;
  96. pythonManager.addObjectToPythonMain("_testWrappedQInvokableInstance", &testWrappedQInvokable);
  97. ctkTestWrappedSlot testWrappedSlot;
  98. pythonManager.addObjectToPythonMain("_testWrappedSlotInstance", &testWrappedSlot);
  99. #ifdef CTK_WRAP_PYTHONQT_USE_VTK
  100. ctkTestWrappedVTKQInvokable testWrappedVTKQInvokable;
  101. pythonManager.addObjectToPythonMain("_testWrappedVTKQInvokableInstance", &testWrappedVTKQInvokable);
  102. ctkTestWrappedVTKSlot testWrappedVTKSlot;
  103. pythonManager.addObjectToPythonMain("_testWrappedVTKSlotInstance", &testWrappedVTKSlot);
  104. ctkTestWrappedQListOfVTKObject testWrappedQListOfVTKObject;
  105. pythonManager.addObjectToPythonMain("_testWrappedQListOfVTKObjectInstance", &testWrappedQListOfVTKObject);
  106. #endif
  107. ctkCallback callback;
  108. callback.setCallbackData(&pythonManager);
  109. pythonManager.setProperty("scripts", parser.unparsedArguments());
  110. callback.setCallback(executeScripts);
  111. QTimer::singleShot(0, &callback, SLOT(invoke()));
  112. return app.exec();
  113. }