ctkSimplePythonShellMain.cpp 5.2 KB

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