ctkSimplePythonShellMain.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Qt includes
  2. #include <QApplication>
  3. #include <QTextStream>
  4. // CTK includes
  5. #include <ctkPythonShell.h>
  6. #include <ctkCommandLineParser.h>
  7. #include "ctkSimplePythonManager.h"
  8. #include "ctkTestWrappedQProperty.h"
  9. #include "ctkTestWrappedQInvokable.h"
  10. #include "ctkTestWrappedSlot.h"
  11. #include "ctkSimplePythonShellConfigure.h" // For CTK_WRAP_PYTHONQT_USE_VTK
  12. #ifdef CTK_WRAP_PYTHONQT_USE_VTK
  13. # include "ctkTestWrappedQListOfVTKObject.h"
  14. # include "ctkTestWrappedVTKSlot.h"
  15. # include "ctkTestWrappedVTKQInvokable.h"
  16. #endif
  17. int main(int argc, char** argv)
  18. {
  19. QApplication app(argc, argv);
  20. ctkCommandLineParser parser;
  21. // Use Unix-style argument names
  22. parser.setArgumentPrefix("--", "-");
  23. // Add command line argument names
  24. parser.addArgument("help", "h", QVariant::Bool, "Print usage information and exit.");
  25. parser.addArgument("interactive", "I", QVariant::Bool, "Enable interactive mode");
  26. // Parse the command line arguments
  27. bool ok = false;
  28. QHash<QString, QVariant> parsedArgs = parser.parseArguments(QCoreApplication::arguments(), &ok);
  29. if (!ok)
  30. {
  31. QTextStream(stderr, QIODevice::WriteOnly) << "Error parsing arguments: "
  32. << parser.errorString() << "\n";
  33. return EXIT_FAILURE;
  34. }
  35. // Show a help message
  36. if (parsedArgs.contains("help") || parsedArgs.contains("h"))
  37. {
  38. QTextStream(stdout, QIODevice::WriteOnly) << "ctkSimplePythonShell\n"
  39. << "Usage\n\n"
  40. << " ctkSimplePythonShell [options] [<path-to-python-script> ...]\n\n"
  41. << "Options\n"
  42. << parser.helpText();
  43. return EXIT_SUCCESS;
  44. }
  45. ctkSimplePythonManager pythonManager;
  46. ctkPythonShell shell(&pythonManager);
  47. shell.setAttribute(Qt::WA_QuitOnClose, true);
  48. shell.resize(600, 280);
  49. shell.show();
  50. shell.setProperty("isInteractive", parsedArgs.contains("interactive"));
  51. pythonManager.addObjectToPythonMain("_ctkPythonShellInstance", &shell);
  52. ctkTestWrappedQProperty testWrappedQProperty;
  53. pythonManager.addObjectToPythonMain("_testWrappedQPropertyInstance", &testWrappedQProperty);
  54. ctkTestWrappedQInvokable testWrappedQInvokable;
  55. pythonManager.addObjectToPythonMain("_testWrappedQInvokableInstance", &testWrappedQInvokable);
  56. ctkTestWrappedSlot testWrappedSlot;
  57. pythonManager.addObjectToPythonMain("_testWrappedSlotInstance", &testWrappedSlot);
  58. #ifdef CTK_WRAP_PYTHONQT_USE_VTK
  59. ctkTestWrappedVTKQInvokable testWrappedVTKQInvokable;
  60. pythonManager.addObjectToPythonMain("_testWrappedVTKQInvokableInstance", &testWrappedVTKQInvokable);
  61. ctkTestWrappedVTKSlot testWrappedVTKSlot;
  62. pythonManager.addObjectToPythonMain("_testWrappedVTKSlotInstance", &testWrappedVTKSlot);
  63. ctkTestWrappedQListOfVTKObject testWrappedQListOfVTKObject;
  64. pythonManager.addObjectToPythonMain("_testWrappedQListOfVTKObjectInstance", &testWrappedQListOfVTKObject);
  65. #endif
  66. foreach(const QString& script, parser.unparsedArguments())
  67. {
  68. pythonManager.executeFile(script);
  69. }
  70. return app.exec();
  71. }