Quellcode durchsuchen

Add virtual reset() function to ctkConsole and ctkPythonConsole

Within ctkConsole,
this function clear the console and print the welcome message

Within ctkPythonConsole,
this function also make sure ps1 and ps2 are reset to ">>> " and "... "
Jean-Christophe Fillion-Robin vor 14 Jahren
Ursprung
Commit
232f67aa52

+ 20 - 10
Libs/Scripting/Python/Widgets/ctkPythonConsole.cpp

@@ -267,22 +267,21 @@ ctkPythonConsole::ctkPythonConsole(ctkAbstractPythonManager* pythonManager, QWid
   d->PythonManager->mainContext();
   d->initializeInteractiveConsole();
 
-  ctkPythonConsoleCompleter* completer = new ctkPythonConsoleCompleter(*d->PythonManager);
-  this->setCompleter(completer);
-
-  // Set primary and secondary prompt
-  this->setPs1(this->Superclass::ps1());
-  this->setPs2(this->Superclass::ps2());
-
-  d->printWelcomeMessage();
-  d->promptForInput();
-
   Q_ASSERT(PythonQt::self()); // PythonQt should be initialized
 
   this->connect(PythonQt::self(), SIGNAL(pythonStdOut(const QString&)),
                 d, SLOT(printOutputMessage(const QString&)));
   this->connect(PythonQt::self(), SIGNAL(pythonStdErr(const QString&)),
                 d, SLOT(printErrorMessage(const QString&)));
+
+  ctkPythonConsoleCompleter* completer = new ctkPythonConsoleCompleter(*d->PythonManager);
+  this->setCompleter(completer);
+
+  // Set primary and secondary prompt
+  this->setPs1(">>> ");
+  this->setPs2("... ");
+
+  this->reset();
 }
 
 //----------------------------------------------------------------------------
@@ -338,3 +337,14 @@ void ctkPythonConsole::executeCommand(const QString& command)
   Q_D(ctkPythonConsole);
   d->MultilineStatement = d->push(command);
 }
+
+//----------------------------------------------------------------------------
+void ctkPythonConsole::reset()
+{
+  // Set primary and secondary prompt
+  this->setPs1(">>> ");
+  this->setPs2("... ");
+
+  this->Superclass::reset();
+}
+

+ 3 - 0
Libs/Scripting/Python/Widgets/ctkPythonConsole.h

@@ -92,6 +92,9 @@ public slots:
 
 //  void executeScript(const QString&);
 
+  /// Reset ps1 and ps2, clear the console and print the welcome message
+  virtual void reset();
+
 protected:
   virtual void executeCommand(const QString& command);
 

+ 26 - 1
Libs/Widgets/ctkConsole.cpp

@@ -74,7 +74,7 @@ ctkConsolePrivate::ctkConsolePrivate(ctkConsole& object) :
   QTextEdit(0),
   q_ptr(&object),
   InteractivePosition(documentEnd()),
-  MultilineStatement(false), Ps1(">>> "), Ps2("... "), AutomaticIndentation(false)
+  MultilineStatement(false), Ps1("$ "), Ps2("> "),
   EditorHints(ctkConsole::AutomaticIndentation | ctkConsole::RemoveTrailingSpaces)
 {
 }
@@ -515,6 +515,16 @@ void ctkConsolePrivate::prompt(const QString& text)
   this->ensureCursorVisible();
 }
 
+//----------------------------------------------------------------------------
+void ctkConsolePrivate::printWelcomeMessage()
+{
+  Q_Q(ctkConsole);
+
+  q->printMessage(
+    QLatin1String("CTK Console"),
+    q->welcomeTextColor());
+}
+
 //-----------------------------------------------------------------------------
 void ctkConsolePrivate::insertCompletion(const QString& completion)
 {
@@ -650,3 +660,18 @@ void ctkConsole::clear()
   d->promptForInput();
 }
 
+//-----------------------------------------------------------------------------
+void ctkConsole::reset()
+{
+  Q_D(ctkConsole);
+
+  d->clear();
+
+  // For some reason the QCompleter tries to set the focus policy to
+  // NoFocus, set let's make sure we set it back to the default WheelFocus.
+  d->setFocusPolicy(Qt::WheelFocus);
+
+  d->printWelcomeMessage();
+  d->promptForInput();
+}
+

+ 3 - 0
Libs/Widgets/ctkConsole.h

@@ -153,6 +153,9 @@ public slots:
   /// Clears the contents of the console
   virtual void clear();
 
+  /// Clears the contents of the console and display welcome message
+  virtual void reset();
+
 protected:
 
   /// Called whenever the user enters a command

+ 3 - 0
Libs/Widgets/ctkConsole_p.h

@@ -90,6 +90,9 @@ public:
   /// line.
   void prompt(const QString& text);
 
+  /// Print welcome message
+  virtual void printWelcomeMessage();
+
 public slots:
 
   /// Inserts the given completion string at the cursor.  This will replace