瀏覽代碼

ctkConsoleWidget uses ctkConsoleWidgetPrivate instead of pqImplementation

Jean-Christophe Fillion-Robin 14 年之前
父節點
當前提交
0a2b9b6a27
共有 2 個文件被更改,包括 70 次插入52 次删除
  1. 61 45
      Libs/Widgets/ctkConsoleWidget.cpp
  2. 9 7
      Libs/Widgets/ctkConsoleWidget.h

+ 61 - 45
Libs/Widgets/ctkConsoleWidget.cpp

@@ -64,16 +64,19 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 // CTK includes
 #include "ctkConsoleWidget.h"
 
-/////////////////////////////////////////////////////////////////////////
-// ctkConsoleWidget::pqImplementation
+//-----------------------------------------------------------------------------
+// ctkConsoleWidgetPrivate
 
-class ctkConsoleWidget::pqImplementation :
-  public QTextEdit
+//-----------------------------------------------------------------------------
+class ctkConsoleWidgetPrivate : public QTextEdit
 {
+  Q_DECLARE_PUBLIC(ctkConsoleWidget);
+protected:
+  ctkConsoleWidget* const q_ptr;
 public:
-  pqImplementation(ctkConsoleWidget& p) :
-    QTextEdit(&p),
-    Parent(p),
+  ctkConsoleWidgetPrivate(ctkConsoleWidget& object) :
+    QTextEdit(&object),
+    q_ptr(&object),
     InteractivePosition(documentEnd())
   {
     this->setTabChangesFocus(false);
@@ -285,11 +288,12 @@ public:
   /// else do nothing.
   void selectCompletion()
   {
-  if (this->Completer && this->Completer->completionCount() == 1)
-    {
-    this->Parent.insertCompletion(this->Completer->currentCompletion());
-    this->Completer->popup()->hide();
-    }
+    Q_Q(ctkConsoleWidget);
+    if (this->Completer && this->Completer->completionCount() == 1)
+      {
+      q->insertCompletion(this->Completer->currentCompletion());
+      this->Completer->popup()->hide();
+      }
   }
 
   void updateCompleter()
@@ -352,6 +356,8 @@ public:
   /// Implements command-execution
   void internalExecuteCommand()
     {
+    Q_Q(ctkConsoleWidget);
+
     // 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).
@@ -366,16 +372,18 @@ public:
     c.insertText("\n");
 
     this->InteractivePosition = this->documentEnd();
-    this->Parent.internalExecuteCommand(command);
+    q->internalExecuteCommand(command);
     }
 
   void setCompleter(ctkConsoleWidgetCompleter* completer)
     {
+    Q_Q(ctkConsoleWidget);
+
     if (this->Completer)
       {
       this->Completer->setWidget(0);
       QObject::disconnect(this->Completer, SIGNAL(activated(const QString&)),
-                        &this->Parent, SLOT(insertCompletion(const QString&)));
+                          q, SLOT(insertCompletion(const QString&)));
 
       }
     this->Completer = completer;
@@ -383,12 +391,9 @@ public:
       {
       this->Completer->setWidget(this);
       QObject::connect(this->Completer, SIGNAL(activated(const QString&)),
-                      &this->Parent, SLOT(insertCompletion(const QString&)));
+                       q, SLOT(insertCompletion(const QString&)));
       }
     }
-  
-  /// Stores a back-reference to our owner
-  ctkConsoleWidget& Parent;
 
   /// A custom completer
   QPointer<ctkConsoleWidgetCompleter> Completer;
@@ -402,46 +407,51 @@ public:
   int CommandPosition;
 };
 
-/////////////////////////////////////////////////////////////////////////
-// ctkConsoleWidget
+//-----------------------------------------------------------------------------
+// ctkConsoleWidget methods
 
-ctkConsoleWidget::ctkConsoleWidget(QWidget* Parent) :
-  QWidget(Parent),
-  Implementation(new pqImplementation(*this))
+//-----------------------------------------------------------------------------
+ctkConsoleWidget::ctkConsoleWidget(QWidget* parentObject) :
+  QWidget(parentObject),
+  d_ptr(new ctkConsoleWidgetPrivate(*this))
 {
+  Q_D(ctkConsoleWidget);
   QVBoxLayout* const l = new QVBoxLayout(this);
   l->setMargin(0);
-  l->addWidget(this->Implementation);
+  l->addWidget(d);
 }
 
 //-----------------------------------------------------------------------------
 ctkConsoleWidget::~ctkConsoleWidget()
 {
-  delete this->Implementation;
 }
 
 //-----------------------------------------------------------------------------
 QTextCharFormat ctkConsoleWidget::getFormat()
 {
-  return this->Implementation->currentCharFormat();
+  Q_D(ctkConsoleWidget);
+  return d->currentCharFormat();
 }
 
 //-----------------------------------------------------------------------------
 void ctkConsoleWidget::setFormat(const QTextCharFormat& Format)
 {
-  this->Implementation->setCurrentCharFormat(Format);
+  Q_D(ctkConsoleWidget);
+  d->setCurrentCharFormat(Format);
 }
 
 //-----------------------------------------------------------------------------
 void ctkConsoleWidget::setCompleter(ctkConsoleWidgetCompleter* completer)
 {
-  this->Implementation->setCompleter(completer);
+  Q_D(ctkConsoleWidget);
+  d->setCompleter(completer);
 }
 
 //-----------------------------------------------------------------------------
 void ctkConsoleWidget::insertCompletion(const QString& completion)
 {
-  QTextCursor tc = this->Implementation->textCursor();
+  Q_D(ctkConsoleWidget);
+  QTextCursor tc = d->textCursor();
   tc.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
   if (tc.selectedText()==".")
     {
@@ -449,35 +459,39 @@ void ctkConsoleWidget::insertCompletion(const QString& completion)
     }
   else
     {
-    tc = this->Implementation->textCursor();
+    tc = d->textCursor();
     tc.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
     tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
     tc.insertText(completion);
-    this->Implementation->setTextCursor(tc);
+    d->setTextCursor(tc);
     }
-  this->Implementation->updateCommandBuffer();
+  d->updateCommandBuffer();
 }
 
 //-----------------------------------------------------------------------------
 void ctkConsoleWidget::printString(const QString& Text)
 {
-  this->Implementation->textCursor().movePosition(QTextCursor::End);
-  this->Implementation->textCursor().insertText(Text);
-  this->Implementation->InteractivePosition = this->Implementation->documentEnd();
-  this->Implementation->ensureCursorVisible();
+  Q_D(ctkConsoleWidget);
+  d->textCursor().movePosition(QTextCursor::End);
+  d->textCursor().insertText(Text);
+  d->InteractivePosition = d->documentEnd();
+  d->ensureCursorVisible();
 }
 
 //-----------------------------------------------------------------------------
 void ctkConsoleWidget::printCommand(const QString& cmd)
 {
-  this->Implementation->textCursor().insertText(cmd);
-  this->Implementation->updateCommandBuffer();
+  Q_D(ctkConsoleWidget);
+  d->textCursor().insertText(cmd);
+  d->updateCommandBuffer();
 }
 
 //-----------------------------------------------------------------------------
 void ctkConsoleWidget::prompt(const QString& text)
 {
-  QTextCursor text_cursor = this->Implementation->textCursor();
+  Q_D(ctkConsoleWidget);
+
+  QTextCursor text_cursor = d->textCursor();
 
   // if the cursor is currently on a clean line, do nothing, otherwise we move
   // the cursor to a new line before showing the prompt.
@@ -487,22 +501,24 @@ void ctkConsoleWidget::prompt(const QString& text)
   int endpos = text_cursor.position();
   if (endpos != startpos)
     {
-    this->Implementation->textCursor().insertText("\n");
+    d->textCursor().insertText("\n");
     }
 
-  this->Implementation->textCursor().insertText(text);
-  this->Implementation->InteractivePosition = this->Implementation->documentEnd();
-  this->Implementation->ensureCursorVisible();
+  d->textCursor().insertText(text);
+  d->InteractivePosition = d->documentEnd();
+  d->ensureCursorVisible();
 }
 
 //-----------------------------------------------------------------------------
 void ctkConsoleWidget::clear()
 {
-  this->Implementation->clear();
+  Q_D(ctkConsoleWidget);
+
+  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.
-  this->Implementation->setFocusPolicy(Qt::WheelFocus);
+  d->setFocusPolicy(Qt::WheelFocus);
 }
 
 //-----------------------------------------------------------------------------

+ 9 - 7
Libs/Widgets/ctkConsoleWidget.h

@@ -60,6 +60,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 // CTK includes
 #include "ctkWidgetsExport.h"
 
+class ctkConsoleWidgetPrivate;
 class ctkConsoleWidgetCompleter;
 
 
@@ -73,7 +74,7 @@ class CTK_WIDGETS_EXPORT ctkConsoleWidget : public QWidget
   Q_OBJECT
   
 public:
-  ctkConsoleWidget(QWidget* parent = 0);
+  ctkConsoleWidget(QWidget* parentObject = 0);
   virtual ~ctkConsoleWidget();
 
   /// Returns the current formatting that will be used by printString
@@ -111,15 +112,16 @@ public slots:
   /// Determines the word using QTextCursor::StartOfWord, EndOfWord.
   void insertCompletion(const QString& text);
 
+protected:
+
+protected:
+  QScopedPointer<ctkConsoleWidgetPrivate> d_ptr;
+
 private:
-  ctkConsoleWidget(const ctkConsoleWidget&);
-  ctkConsoleWidget& operator=(const ctkConsoleWidget&);
+  Q_DECLARE_PRIVATE(ctkConsoleWidget);
+  Q_DISABLE_COPY(ctkConsoleWidget);
 
   void internalExecuteCommand(const QString& Command);
-
-  class pqImplementation;
-  pqImplementation* const Implementation;
-  friend class pqImplementation;
 };
 
 //-----------------------------------------------------------------------------