ctkSimplePythonShellMain.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Qt includes
  2. #include <QApplication>
  3. #include <QTextStream>
  4. // CTK includes
  5. #include <ctkPythonConsole.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. ctkPythonConsole console;
  47. console.initialize(&pythonManager);
  48. console.setAttribute(Qt::WA_QuitOnClose, true);
  49. console.resize(600, 280);
  50. console.show();
  51. console.setProperty("isInteractive", parsedArgs.contains("interactive"));
  52. pythonManager.addObjectToPythonMain("_ctkPythonConsoleInstance", &console);
  53. ctkTestWrappedQProperty testWrappedQProperty;
  54. pythonManager.addObjectToPythonMain("_testWrappedQPropertyInstance", &testWrappedQProperty);
  55. ctkTestWrappedQInvokable testWrappedQInvokable;
  56. pythonManager.addObjectToPythonMain("_testWrappedQInvokableInstance", &testWrappedQInvokable);
  57. ctkTestWrappedSlot testWrappedSlot;
  58. pythonManager.addObjectToPythonMain("_testWrappedSlotInstance", &testWrappedSlot);
  59. #ifdef CTK_WRAP_PYTHONQT_USE_VTK
  60. ctkTestWrappedVTKQInvokable testWrappedVTKQInvokable;
  61. pythonManager.addObjectToPythonMain("_testWrappedVTKQInvokableInstance", &testWrappedVTKQInvokable);
  62. ctkTestWrappedVTKSlot testWrappedVTKSlot;
  63. pythonManager.addObjectToPythonMain("_testWrappedVTKSlotInstance", &testWrappedVTKSlot);
  64. ctkTestWrappedQListOfVTKObject testWrappedQListOfVTKObject;
  65. pythonManager.addObjectToPythonMain("_testWrappedQListOfVTKObjectInstance", &testWrappedQListOfVTKObject);
  66. #endif
  67. foreach(const QString& script, parser.unparsedArguments())
  68. {
  69. pythonManager.executeFile(script);
  70. }
  71. return app.exec();
  72. }