ctkSimplePythonShellMain.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <QLabel>
  17. #include <QMainWindow>
  18. #include <QStatusBar>
  19. #include <QTextStream>
  20. #include <QTimer>
  21. // CTK includes
  22. #include <ctkCallback.h>
  23. #include <ctkPythonConsole.h>
  24. #include <ctkCommandLineParser.h>
  25. #include "ctkSimplePythonManager.h"
  26. #include "ctkTestWrappedQProperty.h"
  27. #include "ctkTestWrappedQInvokable.h"
  28. #include "ctkTestWrappedSlot.h"
  29. #include "ctkSimplePythonShellConfigure.h" // For CTK_WRAP_PYTHONQT_USE_VTK
  30. #ifdef CTK_WRAP_PYTHONQT_USE_VTK
  31. # include "ctkTestWrappedQListOfVTKObject.h"
  32. # include "ctkTestWrappedVTKObserver.h"
  33. # include "ctkTestWrappedVTKQInvokable.h"
  34. # include "ctkTestWrappedVTKSlot.h"
  35. # include <vtkDebugLeaks.h>
  36. #endif
  37. namespace
  38. {
  39. //-----------------------------------------------------------------------------
  40. void executeScripts(void * data)
  41. {
  42. ctkSimplePythonManager * pythonManager = reinterpret_cast<ctkSimplePythonManager*>(data);
  43. QStringList scripts = pythonManager->property("scripts").toStringList();
  44. foreach(const QString& script, scripts)
  45. {
  46. pythonManager->executeFile(script);
  47. if (pythonManager->pythonErrorOccured())
  48. {
  49. QApplication::exit(EXIT_FAILURE);
  50. }
  51. }
  52. }
  53. //-----------------------------------------------------------------------------
  54. void onCursorPositionChanged(void *data)
  55. {
  56. ctkPythonConsole* pythonConsole = reinterpret_cast<ctkPythonConsole*>(data);
  57. QMainWindow* mainWindow = qobject_cast<QMainWindow*>(
  58. pythonConsole->parentWidget());
  59. QLabel * label = mainWindow->statusBar()->findChild<QLabel*>();
  60. label->setText(QString("Position %1, Column %2, Line %3")
  61. .arg(pythonConsole->cursorPosition())
  62. .arg(pythonConsole->cursorColumn())
  63. .arg(pythonConsole->cursorLine()));
  64. }
  65. } // end of anonymous namespace
  66. //-----------------------------------------------------------------------------
  67. int main(int argc, char** argv)
  68. {
  69. #ifdef CTK_WRAP_PYTHONQT_USE_VTK
  70. vtkDebugLeaks::SetExitError(true);
  71. ctkTestWrappedVTKObserver testWrappedVTKObserver;
  72. #endif
  73. int exitCode = EXIT_FAILURE;
  74. {
  75. QApplication app(argc, argv);
  76. ctkCommandLineParser parser;
  77. // Use Unix-style argument names
  78. parser.setArgumentPrefix("--", "-");
  79. // Add command line argument names
  80. parser.addArgument("help", "h", QVariant::Bool, "Print usage information and exit.");
  81. parser.addArgument("interactive", "I", QVariant::Bool, "Enable interactive mode");
  82. // Parse the command line arguments
  83. bool ok = false;
  84. QHash<QString, QVariant> parsedArgs = parser.parseArguments(QCoreApplication::arguments(), &ok);
  85. if (!ok)
  86. {
  87. QTextStream(stderr, QIODevice::WriteOnly) << "Error parsing arguments: "
  88. << parser.errorString() << "\n";
  89. return EXIT_FAILURE;
  90. }
  91. // Show a help message
  92. if (parsedArgs.contains("help") || parsedArgs.contains("h"))
  93. {
  94. QTextStream(stdout, QIODevice::WriteOnly) << "ctkSimplePythonShell\n"
  95. << "Usage\n\n"
  96. << " ctkSimplePythonShell [options] [<path-to-python-script> ...]\n\n"
  97. << "Options\n"
  98. << parser.helpText();
  99. return EXIT_SUCCESS;
  100. }
  101. ctkSimplePythonManager pythonManager;
  102. ctkPythonConsole console;
  103. console.initialize(&pythonManager);
  104. QMainWindow mainWindow;
  105. mainWindow.setCentralWidget(&console);
  106. mainWindow.resize(600, 280);
  107. mainWindow.show();
  108. QLabel cursorPositionLabel;
  109. mainWindow.statusBar()->addWidget(&cursorPositionLabel);
  110. ctkCallback cursorPositionChangedCallback;
  111. cursorPositionChangedCallback.setCallbackData(&console);
  112. cursorPositionChangedCallback.setCallback(onCursorPositionChanged);
  113. QObject::connect(&console, SIGNAL(cursorPositionChanged()),
  114. &cursorPositionChangedCallback, SLOT(invoke()));
  115. console.setProperty("isInteractive", parsedArgs.contains("interactive"));
  116. QStringList list;
  117. list << "qt.QPushButton";
  118. console.completer()->setAutocompletePreferenceList(list);
  119. pythonManager.addObjectToPythonMain("_ctkPythonConsoleInstance", &console);
  120. pythonManager.addObjectToPythonMain("_ctkPythonManagerInstance", &pythonManager);
  121. ctkTestWrappedQProperty testWrappedQProperty;
  122. pythonManager.addObjectToPythonMain("_testWrappedQPropertyInstance", &testWrappedQProperty);
  123. ctkTestWrappedQInvokable testWrappedQInvokable;
  124. pythonManager.addObjectToPythonMain("_testWrappedQInvokableInstance", &testWrappedQInvokable);
  125. ctkTestWrappedSlot testWrappedSlot;
  126. pythonManager.addObjectToPythonMain("_testWrappedSlotInstance", &testWrappedSlot);
  127. #ifdef CTK_WRAP_PYTHONQT_USE_VTK
  128. pythonManager.addObjectToPythonMain("_testWrappedVTKObserverInstance", &testWrappedVTKObserver);
  129. ctkTestWrappedVTKQInvokable testWrappedVTKQInvokable;
  130. pythonManager.addObjectToPythonMain("_testWrappedVTKQInvokableInstance", &testWrappedVTKQInvokable);
  131. ctkTestWrappedVTKSlot testWrappedVTKSlot;
  132. pythonManager.addObjectToPythonMain("_testWrappedVTKSlotInstance", &testWrappedVTKSlot);
  133. // ctkTestWrappedQListOfVTKObject testWrappedQListOfVTKObject;
  134. // pythonManager.addObjectToPythonMain("_testWrappedQListOfVTKObjectInstance", &testWrappedQListOfVTKObject);
  135. #endif
  136. ctkCallback callback;
  137. callback.setCallbackData(&pythonManager);
  138. pythonManager.setProperty("scripts", parser.unparsedArguments());
  139. callback.setCallback(executeScripts);
  140. QTimer::singleShot(0, &callback, SLOT(invoke()));
  141. exitCode = app.exec();
  142. }
  143. #ifdef CTK_WRAP_PYTHONQT_USE_VTK
  144. testWrappedVTKObserver.getTable()->Modified();
  145. #endif
  146. return exitCode;
  147. }