From 9d16a6ec567de91e081c5e3a4bc680113594252c Mon Sep 17 00:00:00 2001
From: Andreas Schenk <andreas_schenk@hms.harvard.edu>
Date: Mon, 27 Jun 2011 13:50:12 -0400
Subject: [PATCH] readded BZDNG-155 that was reverted by BZDNG-256 / fixed
 BZDNG-238

BZDNG-238 was not completely fixed before. We have to get the position of the
edit start block before we delete anything, as the bug in QT messes up the
edit start block and its position information
---
 .../src/python_shell/python_shell_widget.cc   | 23 +++++++++++++++++--
 .../src/python_shell/python_shell_widget.hh   |  1 +
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/modules/gui/src/python_shell/python_shell_widget.cc b/modules/gui/src/python_shell/python_shell_widget.cc
index dfd9cc702..e1891ae2c 100644
--- a/modules/gui/src/python_shell/python_shell_widget.cc
+++ b/modules/gui/src/python_shell/python_shell_widget.cc
@@ -328,6 +328,10 @@ void PythonShellWidget::setup_state_machine_()
   single_line->addTransition(new KeyEventTransition(Qt::Key_Up,DNG_ARROW_MODIFIERS,history_up));
   single_line->addTransition(new KeyEventTransition(Qt::Key_Down,DNG_ARROW_MODIFIERS,history_down));
 
+  KeyEventTransition* clear_all_tr_sl=new KeyEventTransition(Qt::Key_D,Qt::ControlModifier,single_line);
+  single_line->addTransition(clear_all_tr_sl);
+  connect(clear_all_tr_sl,SIGNAL(triggered()),this,SLOT(handle_clear_all_()));
+
   //multi line inactive transitions
   multi_line_inactive->addTransition(new KeyEventTransition(Qt::Key_Return,
                                                             Qt::NoModifier,
@@ -343,6 +347,10 @@ void PythonShellWidget::setup_state_machine_()
   multi_line_inactive->addTransition(tr6);
   connect(tr6,SIGNAL(triggered()),this,SLOT(NewLineAtEnd()));
 
+  KeyEventTransition* clear_all_tr_mli=new KeyEventTransition(Qt::Key_D,Qt::ControlModifier,single_line);
+  multi_line_inactive->addTransition(clear_all_tr_mli);
+  connect(clear_all_tr_mli,SIGNAL(triggered()),this,SLOT(handle_clear_all_()));
+
   multi_line_inactive->addTransition(new KeyEventTransition(Qt::Key_Left,DNG_ARROW_MODIFIERS,multiline_active_state_));
   multi_line_inactive->addTransition(new KeyEventTransition(Qt::Key_Right,DNG_ARROW_MODIFIERS,multiline_active_state_));
   multi_line_inactive->addTransition(new KeyEventTransition(Qt::Key_Up,DNG_ARROW_MODIFIERS,history_up));
@@ -395,6 +403,9 @@ void PythonShellWidget::setup_state_machine_()
   multiline_active_state_->addTransition(tr8);
   connect(tr8,SIGNAL(triggered()),this,SLOT(NewLineAtEnd()));
 
+  KeyEventTransition* clear_all_tr_mla=new KeyEventTransition(Qt::Key_D,Qt::ControlModifier,single_line);
+  multiline_active_state_->addTransition(clear_all_tr_mla);
+  connect(clear_all_tr_mla,SIGNAL(triggered()),this,SLOT(handle_clear_all_()));
   multiline_active_state_->addTransition(new KeyEventTransition(Qt::Key_Escape,Qt::NoModifier,multi_line_inactive));
   multiline_active_state_->addTransition(new KeyEventTransition(Qt::Key_Up,Qt::ControlModifier,history_up));
   multiline_active_state_->addTransition(new KeyEventTransition(Qt::Key_Down,Qt::ControlModifier,history_down));
@@ -747,6 +758,12 @@ void PythonShellWidget::handle_completion_()
 }
 
 
+void PythonShellWidget::handle_clear_all_()
+{
+  handle_select_all_rw_();
+  handle_delete_();
+}
+
 void PythonShellWidget::handle_delete_()
 {
   // BZDNG-238
@@ -761,13 +778,14 @@ void PythonShellWidget::handle_delete_()
   //
   // If we emulate the deletion of the text manually all is fine.
   QTextCursor cursor=this->textCursor();
+  int pos=block_edit_start_.position();
   if (cursor.hasSelection()) {
     cursor.removeSelectedText();
   } else {
     cursor.deleteChar();
   }
   QTextCursor tc=this->textCursor();
-  tc.setPosition(block_edit_start_.position());
+  tc.setPosition(pos);
   block_edit_start_=tc.block();
 }
 
@@ -776,6 +794,7 @@ void PythonShellWidget::handle_backspace_()
   // BZDNG-238
   // see above
   QTextCursor cursor=this->textCursor();
+  int pos=block_edit_start_.position();
   if (cursor.hasSelection()) {
     cursor.removeSelectedText();
   } else {
@@ -784,7 +803,7 @@ void PythonShellWidget::handle_backspace_()
     }
   }
   QTextCursor tc=this->textCursor();
-  tc.setPosition(block_edit_start_.position());
+  tc.setPosition(pos);
   block_edit_start_=tc.block();
 }
 
diff --git a/modules/gui/src/python_shell/python_shell_widget.hh b/modules/gui/src/python_shell/python_shell_widget.hh
index f7aba3400..f2b2dbced 100644
--- a/modules/gui/src/python_shell/python_shell_widget.hh
+++ b/modules/gui/src/python_shell/python_shell_widget.hh
@@ -93,6 +93,7 @@ protected slots:
   void handle_select_all_rw_();
   void handle_delete_();
   void handle_backspace_();
+  void handle_clear_all_();
 
 protected:
   virtual void resizeEvent(QResizeEvent* event);
-- 
GitLab