ctkSimplePythonShellMain.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. QStringList list;
  53. list << "qt.QPushButton";
  54. console.completer()->setAutocompletePreferenceList(list);
  55. pythonManager.addObjectToPythonMain("_ctkPythonConsoleInstance", &console);
  56. ctkTestWrappedQProperty testWrappedQProperty;
  57. pythonManager.addObjectToPythonMain("_testWrappedQPropertyInstance", &testWrappedQProperty);
  58. ctkTestWrappedQInvokable testWrappedQInvokable;
  59. pythonManager.addObjectToPythonMain("_testWrappedQInvokableInstance", &testWrappedQInvokable);
  60. ctkTestWrappedSlot testWrappedSlot;
  61. pythonManager.addObjectToPythonMain("_testWrappedSlotInstance", &testWrappedSlot);
  62. #ifdef CTK_WRAP_PYTHONQT_USE_VTK
  63. ctkTestWrappedVTKQInvokable testWrappedVTKQInvokable;
  64. pythonManager.addObjectToPythonMain("_testWrappedVTKQInvokableInstance", &testWrappedVTKQInvokable);
  65. ctkTestWrappedVTKSlot testWrappedVTKSlot;
  66. pythonManager.addObjectToPythonMain("_testWrappedVTKSlotInstance", &testWrappedVTKSlot);
  67. ctkTestWrappedQListOfVTKObject testWrappedQListOfVTKObject;
  68. pythonManager.addObjectToPythonMain("_testWrappedQListOfVTKObjectInstance", &testWrappedQListOfVTKObject);
  69. #endif
  70. foreach(const QString& script, parser.unparsedArguments())
  71. {
  72. pythonManager.executeFile(script);
  73. }
  74. return app.exec();
  75. }