ctkSimplePythonShellMain.cpp 3.9 KB

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