Explorar el Código

Simplify ctkConsole/ctkPythonConsole handling of command execution

Object extending from ctkConsole will have to implement
the virtual function "executeCommand(const QString& command)"
Jean-Christophe Fillion-Robin hace 14 años
padre
commit
0be6040fff

+ 6 - 31
Libs/Scripting/Python/Widgets/ctkPythonConsole.cpp

@@ -159,8 +159,6 @@ public:
 
   void resetBuffer();
 
-  void executeCommand(const QString& command);
-
   void promptForInput(const QString& indent = QString());
 
   ctkAbstractPythonManager* PythonManager;
@@ -246,17 +244,6 @@ void ctkPythonConsolePrivate::resetBuffer()
 }
 
 //----------------------------------------------------------------------------
-void ctkPythonConsolePrivate::executeCommand(const QString& command)
-{
-  this->MultilineStatement = this->push(command);
-//  if (command.length())
-//    {
-//    Q_ASSERT(this->PythonManager);
-//    this->PythonManager->executeString(command);
-//    }
-}
-
-//----------------------------------------------------------------------------
 void ctkPythonConsolePrivate::promptForInput(const QString& indent)
 {
   Q_Q(ctkPythonConsole);
@@ -295,10 +282,6 @@ ctkPythonConsole::ctkPythonConsole(ctkAbstractPythonManager* pythonManager, QWid
   ctkPythonConsoleCompleter* completer = new ctkPythonConsoleCompleter(*this);
   this->setCompleter(completer);
 
-  QObject::connect(
-    this, SIGNAL(executeCommand(const QString&)),
-    this, SLOT(onExecutePythonCommand(const QString&)));
-
   // The call to mainContext() ensures that python has been initialized.
   Q_ASSERT(d->PythonManager);
   d->PythonManager->mainContext();
@@ -442,29 +425,21 @@ void ctkPythonConsole::printStderr(const QString& text)
 }
 
 //----------------------------------------------------------------------------
-void ctkPythonConsole::onExecutePythonCommand(const QString& Command)
+void ctkPythonConsole::executeCommand(const QString& command)
 {
   Q_D(ctkPythonConsole);
 
-  QString command = Command;
-  command.replace(QRegExp("\\s*$"), "");
-  this->internalExecuteCommand(command);
+  QString commandUpdated = command;
+  commandUpdated.replace(QRegExp("\\s*$"), "");
+
+  d->MultilineStatement = d->push(commandUpdated);
 
   // Find the indent for the command.
   QRegExp regExp("^(\\s+)");
   QString indent;
-  if (regExp.indexIn(command) != -1)
+  if (regExp.indexIn(commandUpdated) != -1)
     {
     indent = regExp.cap(1);
     }
   d->promptForInput(indent);
 }
-
-//----------------------------------------------------------------------------
-void ctkPythonConsole::internalExecuteCommand(const QString& command)
-{
-  Q_D(ctkPythonConsole);
-  emit this->executing(true);
-  d->executeCommand(command);
-  emit this->executing(false);
-}

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

@@ -82,24 +82,20 @@ public:
   /// Given a python variable name, lookup its attributes and return them in a string list.
   QStringList pythonAttributes(const QString& pythonVariableName) const;
 
-signals:
-  void executing(bool);
-
 public slots:
-  void clear();
+  virtual void clear();
   void executeScript(const QString&);
 
+protected:
+  virtual void executeCommand(const QString& command);
+
 protected slots:
   void printStderr(const QString&);
   void printStdout(const QString&);
 
-  void onExecutePythonCommand(const QString&);
-
 private:
   Q_DECLARE_PRIVATE(ctkPythonConsole);
   Q_DISABLE_COPY(ctkPythonConsole);
-
-  void internalExecuteCommand(const QString&);
 };
 
 #endif

+ 12 - 1
Libs/Widgets/ctkConsole.cpp

@@ -59,6 +59,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <QTextCursor>
 #include <QVBoxLayout>
 #include <QScrollBar>
+#include <QDebug>
 
 // CTK includes
 #include "ctkConsole.h"
@@ -372,7 +373,10 @@ void ctkConsolePrivate::internalExecuteCommand()
   c.insertText("\n");
 
   this->InteractivePosition = this->documentEnd();
-  emit q->executeCommand(command);
+
+  emit q->executing(true);
+  q->executeCommand(command);
+  emit q->executing(false);
 }
 
 //-----------------------------------------------------------------------------
@@ -469,6 +473,13 @@ void ctkConsole::insertCompletion(const QString& completion)
 }
 
 //-----------------------------------------------------------------------------
+void ctkConsole::executeCommand(const QString& command)
+{
+  qWarning() << "ctkConsole::executeCommand not implemented !";
+  qWarning() << "command:" << command;
+}
+
+//-----------------------------------------------------------------------------
 void ctkConsole::printString(const QString& Text)
 {
   Q_D(ctkConsole);

+ 9 - 3
Libs/Widgets/ctkConsole.h

@@ -84,8 +84,9 @@ public:
   void setCompleter(ctkConsoleCompleter* completer);
 
 signals:
-  /// Signal emitted whenever the user enters a command
-  void executeCommand(const QString& Command);
+
+  /// This signal emitted before and after a command is executed
+  void executing(bool);
 
 public slots:
   /// Writes the supplied text to the console
@@ -96,7 +97,7 @@ public slots:
   void printCommand(const QString& cmd);
 
   /// Clears the contents of the console
-  void clear();
+  virtual void clear();
 
   /// Puts out an input accepting prompt.
   /// It is recommended that one uses prompt instead of printString() to print
@@ -110,6 +111,11 @@ public slots:
   void insertCompletion(const QString& text);
 
 protected:
+
+  /// Called whenever the user enters a command
+  virtual void executeCommand(const QString& Command);
+
+protected:
   ctkConsole(ctkConsolePrivate * pimpl, QWidget* parentObject);
 
   QScopedPointer<ctkConsolePrivate> d_ptr;