ctkSimplePythonShellMain.cpp 3.8 KB

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