浏览代码

ctkConsole - Introduce EditorHints property

This set of flag holding the following option:
 - AutomaticIndentation - Align cursor based an indentation of the previous command
 - RemoveTrailingSpaces - Remove trailing spaces of the entered command
Jean-Christophe Fillion-Robin 14 年之前
父节点
当前提交
264cb402c9

+ 1 - 5
Libs/Scripting/Python/Widgets/ctkPythonConsole.cpp

@@ -336,9 +336,5 @@ void ctkPythonConsole::setPs2(const QString& newPs2)
 void ctkPythonConsole::executeCommand(const QString& command)
 {
   Q_D(ctkPythonConsole);
-
-  QString commandUpdated = command;
-  commandUpdated.replace(QRegExp("\\s*$"), ""); // Remove trailing spaces
-
-  d->MultilineStatement = d->push(commandUpdated);
+  d->MultilineStatement = d->push(command);
 }

+ 12 - 4
Libs/Widgets/ctkConsole.cpp

@@ -75,6 +75,7 @@ ctkConsolePrivate::ctkConsolePrivate(ctkConsole& object) :
   q_ptr(&object),
   InteractivePosition(documentEnd()),
   MultilineStatement(false), Ps1(">>> "), Ps2("... "), AutomaticIndentation(false)
+  EditorHints(ctkConsole::AutomaticIndentation | ctkConsole::RemoveTrailingSpaces)
 {
 }
 
@@ -399,10 +400,17 @@ void ctkConsolePrivate::internalExecuteCommand()
 {
   Q_Q(ctkConsole);
 
+  QString command = this->commandBuffer();
+
+  if (this->EditorHints & ctkConsole::RemoveTrailingSpaces)
+    {
+    command.replace(QRegExp("\\s*$"), ""); // Remove trailing spaces
+    this->commandBuffer() = command; // Update buffer
+    }
+
   // First update the history cache. It's essential to update the
   // this->CommandPosition before calling internalExecuteCommand() since that
   // can result in a clearing of the current command (BUG #8765).
-  QString command = this->commandBuffer();
   if (!command.isEmpty()) // Don't store empty commands in the history
     {
     this->CommandHistory.push_back("");
@@ -421,7 +429,7 @@ void ctkConsolePrivate::internalExecuteCommand()
 
   // Find the indent for the command.
   QString indent;
-  if (this->AutomaticIndentation)
+  if (this->EditorHints & ctkConsole::AutomaticIndentation)
     {
     QRegExp regExp("^(\\s+)");
     if (regExp.indexIn(command) != -1)
@@ -607,8 +615,8 @@ CTK_GET_CPP(ctkConsole, QString, ps2, Ps2);
 CTK_SET_CPP(ctkConsole, const QString&, setPs2, Ps2);
 
 //-----------------------------------------------------------------------------
-CTK_GET_CPP(ctkConsole, bool, automaticIndentation, AutomaticIndentation);
-CTK_SET_CPP(ctkConsole, bool, setAutomaticIndentation, AutomaticIndentation);
+CTK_GET_CPP(ctkConsole, ctkConsole::EditorHints, editorHints, EditorHints);
+CTK_SET_CPP(ctkConsole, const ctkConsole::EditorHints&, setEditorHints, EditorHints);
 
 //-----------------------------------------------------------------------------
 void ctkConsole::executeCommand(const QString& command)

+ 14 - 5
Libs/Widgets/ctkConsole.h

@@ -74,9 +74,19 @@ class CTK_WIDGETS_EXPORT ctkConsole : public QWidget
   Q_PROPERTY(QColor welcomeTextColor READ welcomeTextColor WRITE setWelcomeTextColor)
   Q_PROPERTY(QString ps1 READ ps1 WRITE setPs1)
   Q_PROPERTY(QString ps2 READ ps2 WRITE setPs2)
-  Q_PROPERTY(bool automaticIndentation READ automaticIndentation WRITE setAutomaticIndentation)
+  Q_FLAGS(EditorHint EditorHints)
+  Q_PROPERTY(EditorHints editorHints READ editorHints WRITE setEditorHints)
   
 public:
+
+  enum EditorHint
+  {
+    NoHints = 0x00,
+    AutomaticIndentation = 0x01, /*!< Align cursor based an indentation of the previous command */
+    RemoveTrailingSpaces = 0x02  /*!< Remove trailing spaces of the entered command */
+  };
+  Q_DECLARE_FLAGS(EditorHints, EditorHint)
+
   ctkConsole(QWidget* parentObject = 0);
   virtual ~ctkConsole();
 
@@ -114,8 +124,10 @@ public:
   /// \sa welcomeTextColor()
   void setWelcomeTextColor(const QColor& newColor);
 
-  bool automaticIndentation()const;
+  EditorHints editorHints()const;
 
+  /// \sa editorHints()
+  void setEditorHints(const EditorHints& newEditorHints);
   /// Prints text on the console
   void printMessage(const QString& message, const QColor& color);
 
@@ -138,9 +150,6 @@ signals:
 
 public slots:
 
-  /// \sa automaticIndentation()
-  void setAutomaticIndentation(bool value);
-
   /// Clears the contents of the console
   virtual void clear();
 

+ 1 - 1
Libs/Widgets/ctkConsole_p.h

@@ -144,7 +144,7 @@ public:
   /// Secondary prompt
   QString Ps2;
 
-  bool AutomaticIndentation;
+  ctkConsole::EditorHints EditorHints;
 
 };