Forráskód Böngészése

ctkPythonConsole - Add "initialize(ctkAbstractPythonManager*)" method

Jean-Christophe Fillion-Robin 14 éve
szülő
commit
96724f3064

+ 2 - 1
Applications/ctkSimplePythonShell/ctkSimplePythonShellMain.cpp

@@ -54,7 +54,8 @@ int main(int argc, char** argv)
   
   
   ctkSimplePythonManager pythonManager;
   ctkSimplePythonManager pythonManager;
   
   
-  ctkPythonConsole console(&pythonManager);
+  ctkPythonConsole console;
+  console.initialize(&pythonManager);
   console.setAttribute(Qt::WA_QuitOnClose, true);
   console.setAttribute(Qt::WA_QuitOnClose, true);
   console.resize(600, 280);
   console.resize(600, 280);
   console.show();
   console.show();

+ 3 - 1
Libs/Scripting/Python/Widgets/Testing/Cpp/ctkPythonConsoleTest1.cpp

@@ -38,7 +38,9 @@ int ctkPythonConsoleTest1(int argc, char * argv [] )
 
 
   QPushButton button("Show PythonConsole");
   QPushButton button("Show PythonConsole");
 
 
-  ctkPythonConsole pythonConsole(new ctkAbstractPythonManager);
+  ctkPythonConsole pythonConsole;
+  ctkAbstractPythonManager pythonManager;
+  pythonConsole.initialize(&pythonManager);
 
 
   QObject::connect(&button, SIGNAL(clicked()), &pythonConsole, SLOT(show()));
   QObject::connect(&button, SIGNAL(clicked()), &pythonConsole, SLOT(show()));
 
 

+ 36 - 17
Libs/Scripting/Python/Widgets/ctkPythonConsole.cpp

@@ -151,7 +151,7 @@ class ctkPythonConsolePrivate : public ctkConsolePrivate
 {
 {
   Q_DECLARE_PUBLIC(ctkPythonConsole);
   Q_DECLARE_PUBLIC(ctkPythonConsole);
 public:
 public:
-  ctkPythonConsolePrivate(ctkPythonConsole& object, ctkAbstractPythonManager* pythonManager);
+  ctkPythonConsolePrivate(ctkPythonConsole& object);
   ~ctkPythonConsolePrivate();
   ~ctkPythonConsolePrivate();
 
 
   void initializeInteractiveConsole();
   void initializeInteractiveConsole();
@@ -172,10 +172,8 @@ public:
 // ctkPythonConsolePrivate methods
 // ctkPythonConsolePrivate methods
 
 
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
-ctkPythonConsolePrivate::ctkPythonConsolePrivate(
-  ctkPythonConsole& object, ctkAbstractPythonManager* pythonManager)
-  : ctkConsolePrivate(object), PythonManager(pythonManager),
-    InteractiveConsole(0)
+ctkPythonConsolePrivate::ctkPythonConsolePrivate(ctkPythonConsole& object)
+  : ctkConsolePrivate(object), PythonManager(0), InteractiveConsole(0)
 {
 {
 }
 }
 
 
@@ -187,6 +185,8 @@ ctkPythonConsolePrivate::~ctkPythonConsolePrivate()
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
 void ctkPythonConsolePrivate::initializeInteractiveConsole()
 void ctkPythonConsolePrivate::initializeInteractiveConsole()
 {
 {
+  Q_ASSERT(this->PythonManager);
+
   // set up the code.InteractiveConsole instance that we'll use.
   // set up the code.InteractiveConsole instance that we'll use.
   const char* code =
   const char* code =
     "import code\n"
     "import code\n"
@@ -207,6 +207,8 @@ void ctkPythonConsolePrivate::initializeInteractiveConsole()
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
 bool ctkPythonConsolePrivate::push(const QString& code)
 bool ctkPythonConsolePrivate::push(const QString& code)
 {
 {
+  Q_ASSERT(this->PythonManager);
+
   bool ret_value = false;
   bool ret_value = false;
 
 
   QString buffer = code;
   QString buffer = code;
@@ -256,10 +258,9 @@ void ctkPythonConsolePrivate::printWelcomeMessage()
 // ctkPythonConsole methods
 // ctkPythonConsole methods
 
 
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
-ctkPythonConsole::ctkPythonConsole(ctkAbstractPythonManager* pythonManager, QWidget* parentObject):
-  Superclass(new ctkPythonConsolePrivate(*this, pythonManager), parentObject)/*, d_ptr(new ctkPythonConsolePrivate(*this, pythonManager))*/
+ctkPythonConsole::ctkPythonConsole(QWidget* parentObject):
+  Superclass(new ctkPythonConsolePrivate(*this), parentObject)
 {
 {
-  Q_D(ctkPythonConsole);
   this->setObjectName("pythonConsole");
   this->setObjectName("pythonConsole");
 
 
   // Disable RemoveTrailingSpaces and AutomaticIndentation
   // Disable RemoveTrailingSpaces and AutomaticIndentation
@@ -268,11 +269,35 @@ ctkPythonConsole::ctkPythonConsole(ctkAbstractPythonManager* pythonManager, QWid
   // Enable SplitCopiedTextByLine
   // Enable SplitCopiedTextByLine
   this->setEditorHints(this->editorHints() | SplitCopiedTextByLine);
   this->setEditorHints(this->editorHints() | SplitCopiedTextByLine);
 
 
+  this->setDisabled(true);
+}
+
+//----------------------------------------------------------------------------
+ctkPythonConsole::~ctkPythonConsole()
+{
+}
+
+////----------------------------------------------------------------------------
+void ctkPythonConsole::initialize(ctkAbstractPythonManager* newPythonManager)
+{
+  Q_D(ctkPythonConsole);
+
+  if (d->PythonManager)
+    {
+    qWarning() << "ctkPythonConsole already initialized !";
+    return;
+    }
+
   // The call to mainContext() ensures that python has been initialized.
   // The call to mainContext() ensures that python has been initialized.
-  Q_ASSERT(d->PythonManager);
-  d->PythonManager->mainContext();
+  Q_ASSERT(newPythonManager);
+  newPythonManager->mainContext();
   Q_ASSERT(PythonQt::self()); // PythonQt should be initialized
   Q_ASSERT(PythonQt::self()); // PythonQt should be initialized
 
 
+  ctkPythonConsoleCompleter* completer = new ctkPythonConsoleCompleter(*newPythonManager);
+  this->setCompleter(completer);
+
+  d->PythonManager = newPythonManager;
+
   d->initializeInteractiveConsole();
   d->initializeInteractiveConsole();
 
 
   this->connect(PythonQt::self(), SIGNAL(pythonStdOut(const QString&)),
   this->connect(PythonQt::self(), SIGNAL(pythonStdOut(const QString&)),
@@ -280,9 +305,6 @@ ctkPythonConsole::ctkPythonConsole(ctkAbstractPythonManager* pythonManager, QWid
   this->connect(PythonQt::self(), SIGNAL(pythonStdErr(const QString&)),
   this->connect(PythonQt::self(), SIGNAL(pythonStdErr(const QString&)),
                 d, SLOT(printErrorMessage(const QString&)));
                 d, SLOT(printErrorMessage(const QString&)));
 
 
-  ctkPythonConsoleCompleter* completer = new ctkPythonConsoleCompleter(*d->PythonManager);
-  this->setCompleter(completer);
-
   // Set primary and secondary prompt
   // Set primary and secondary prompt
   this->setPs1(">>> ");
   this->setPs1(">>> ");
   this->setPs2("... ");
   this->setPs2("... ");
@@ -295,11 +317,8 @@ ctkPythonConsole::ctkPythonConsole(ctkAbstractPythonManager* pythonManager, QWid
   helpImportCode << "help = pydoc.help.help";
   helpImportCode << "help = pydoc.help.help";
   helpImportCode << "del pydoc";
   helpImportCode << "del pydoc";
   d->PythonManager->executeString(helpImportCode.join("\n"));
   d->PythonManager->executeString(helpImportCode.join("\n"));
-}
 
 
-//----------------------------------------------------------------------------
-ctkPythonConsole::~ctkPythonConsole()
-{
+  this->setDisabled(false);
 }
 }
 
 
 ////----------------------------------------------------------------------------
 ////----------------------------------------------------------------------------

+ 4 - 1
Libs/Scripting/Python/Widgets/ctkPythonConsole.h

@@ -73,9 +73,12 @@ class CTK_SCRIPTING_PYTHON_WIDGETS_EXPORT ctkPythonConsole : public ctkConsole
   
   
 public:
 public:
   typedef ctkConsole Superclass;
   typedef ctkConsole Superclass;
-  ctkPythonConsole(ctkAbstractPythonManager* pythonManager, QWidget* parentObject = 0);
+  ctkPythonConsole(QWidget* parentObject = 0);
   virtual ~ctkPythonConsole();
   virtual ~ctkPythonConsole();
 
 
+  /// Initialize
+  void initialize(ctkAbstractPythonManager* newPythonManager);
+
   /// Returns the string used as primary prompt
   /// Returns the string used as primary prompt
   virtual QString ps1() const;
   virtual QString ps1() const;