From 1074ae16b14bac25afd87b82b046e3c7d5b48639 Mon Sep 17 00:00:00 2001 From: Pritpal Bedi Date: Sat, 4 Feb 2012 01:29:53 +0000 Subject: [PATCH] 2012-02-03 17:26 UTC-0800 Pritpal Bedi (bedipritpal@hotmail.com) * contrib/hbqt/qtgui/hbqt_hbqplaintextedit.cpp * contrib/hbqt/qtgui/hbqt_hbqplaintextedit.h + Implemented: Drag & Drop of selected text. It confirms to the standard Windows behavior per drop protocol, and in addition, also confirms to the hbIDE's column-selection mode. --- harbour/ChangeLog | 7 + .../hbqt/qtgui/hbqt_hbqplaintextedit.cpp | 145 ++++++++++++++---- .../hbqt/qtgui/hbqt_hbqplaintextedit.h | 10 +- 3 files changed, 134 insertions(+), 28 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 8161ce0823..774a40a403 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -16,6 +16,13 @@ The license applies to all entries newer than 2009-04-28. */ +2012-02-03 17:26 UTC-0800 Pritpal Bedi (bedipritpal@hotmail.com) + * contrib/hbqt/qtgui/hbqt_hbqplaintextedit.cpp + * contrib/hbqt/qtgui/hbqt_hbqplaintextedit.h + + Implemented: Drag & Drop of selected text. + It confirms to the standard Windows behavior per drop protocol, + and in addition, also confirms to the hbIDE's column-selection mode. + 2012-02-02 10:32 UTC-0800 Pritpal Bedi (bedipritpal@hotmail.com) * contrib/gtwvg/wvgcrt.prg * contrib/gtwvg/wvgwnd.prg diff --git a/harbour/contrib/hbqt/qtgui/hbqt_hbqplaintextedit.cpp b/harbour/contrib/hbqt/qtgui/hbqt_hbqplaintextedit.cpp index 0115499f08..ca80e08876 100644 --- a/harbour/contrib/hbqt/qtgui/hbqt_hbqplaintextedit.cpp +++ b/harbour/contrib/hbqt/qtgui/hbqt_hbqplaintextedit.cpp @@ -128,7 +128,8 @@ HBQPlainTextEdit::HBQPlainTextEdit( QWidget * parent ) : QPlainTextEdit( parent isAliasCompleter = false; isCodeCompletionActive = true; isCompletionTipsActive = true; - + isInDrag = false; + #if 0 QTextFrameFormat format( this->document()->rootFrame()->frameFormat() ); format.setMargin( 0 ); @@ -184,6 +185,8 @@ HBQPlainTextEdit::HBQPlainTextEdit( QWidget * parent ) : QPlainTextEdit( parent highlighter = NULL; block = NULL; + + setAcceptDrops( true ); } /*----------------------------------------------------------------------*/ @@ -258,7 +261,7 @@ HBQPlainTextEdit::~HBQPlainTextEdit() disconnect( this, SIGNAL( cursorPositionChanged() ) ); delete lineNumberArea; - + if( block ) hb_itemRelease( block ); } @@ -794,6 +797,93 @@ void HBQPlainTextEdit::hbPaste() /*----------------------------------------------------------------------*/ +void HBQPlainTextEdit::dropEvent( QDropEvent *event ) +{ + if( event->dropAction() == Qt::CopyAction || event->dropAction() == Qt::MoveAction ) + { + if( event->source() == this ) + { + QTextCursor c = cursorForPosition( event->pos() ); + int row = c.blockNumber(); + + hbCopy(); + if( event->dropAction() != Qt::CopyAction ) + { + int linesBefore = blockCount(); + this->hbCut( Qt::Key_Delete ); + int linesAfter = blockCount(); + + QTextCursor c( textCursor() ); + c.movePosition( QTextCursor::Start ); + c.movePosition( QTextCursor::Down, QTextCursor::MoveAnchor, row + ( linesAfter - linesBefore ) ); + setTextCursor( c ); + } + else + { + setTextCursor( cursorForPosition( event->pos() ) ); + } + selectionState = 0; + hbClearSelection(); + + hbPaste(); + + QPlainTextEdit::dropEvent( event ); + c = textCursor(); + c.movePosition( QTextCursor::Left, QTextCursor::KeepAnchor, 1 ); + c.removeSelectedText(); + setTextCursor( c ); + return; + } + } + QPlainTextEdit::dropEvent( event ); +} + +/*----------------------------------------------------------------------*/ + +void HBQPlainTextEdit::dragMoveEvent( QDragMoveEvent *event ) +{ + if( event->mimeData()->hasText() ) + { + if( event->source() == this ) + { + event->accept(); + } + else + { + event->acceptProposedAction(); + } + } + else + { + event->ignore(); + } + QPlainTextEdit::dragMoveEvent( event ); +} + +/*----------------------------------------------------------------------*/ + +void HBQPlainTextEdit::dragEnterEvent( QDragEnterEvent *event ) +{ + if( event->mimeData()->hasText() ) + { + if( event->source() == this ) + { + event->accept(); + } + else + { + event->acceptProposedAction(); + } + } + else + { + event->ignore(); + } + QPlainTextEdit::dragEnterEvent( event ); +} + +/*----------------------------------------------------------------------*/ + void HBQPlainTextEdit::mouseDoubleClickEvent( QMouseEvent *event ) { QPlainTextEdit::mouseDoubleClickEvent( event ); @@ -860,15 +950,35 @@ void HBQPlainTextEdit::mousePressEvent( QMouseEvent *event ) { if( event->buttons() & Qt::LeftButton ) { - setCursorWidth( 1 ); - if( ! isSelectionPersistent ) + QTextCursor c( cursorForPosition( event->pos() ) ); + int row = c.blockNumber(); + + if( selectionState == 1 && row >= rowBegins && row <= rowEnds ) { - selectionState = 0; - hbClearSelection(); + event->ignore(); + QDrag * qDrag = new QDrag( this ); + QMimeData * qMimeData = new QMimeData(); + qMimeData->setText( " " ); + qDrag->setMimeData( qMimeData ); + qDrag->exec( Qt::MoveAction | Qt::CopyAction ); + delete qDrag; + emit selectionChanged(); + QTextCursor c( textCursor() ); + setTextCursor( c ); + return; } else - { - selectionState = 1; + { + setCursorWidth( 1 ); + if( ! isSelectionPersistent ) + { + selectionState = 0; + hbClearSelection(); + } + else + { + selectionState = 1; + } } } QPlainTextEdit::mousePressEvent( event ); @@ -1043,30 +1153,11 @@ bool HBQPlainTextEdit::hbKeyPressSelectionByApplication( QKeyEvent * event ) break; } case Qt::Key_Home: - { - QPlainTextEdit::keyPressEvent( event ); - columnEnds = textCursor().columnNumber(); - break; - } case Qt::Key_End: { QPlainTextEdit::keyPressEvent( event ); columnEnds = textCursor().columnNumber(); break; -#if 0 - QTextCursor c( textCursor() ); - c.movePosition( QTextCursor::EndOfLine, QTextCursor::MoveAnchor ); - if( c.columnNumber() <= columnEnds ) - { - QPlainTextEdit::keyPressEvent( event ); - columnEnds = textCursor().columnNumber();; - } - else - { - event->ignore(); - } - break; -#endif } case Qt::Key_Up: case Qt::Key_PageUp: diff --git a/harbour/contrib/hbqt/qtgui/hbqt_hbqplaintextedit.h b/harbour/contrib/hbqt/qtgui/hbqt_hbqplaintextedit.h index efbeff679a..dbacc072fc 100644 --- a/harbour/contrib/hbqt/qtgui/hbqt_hbqplaintextedit.h +++ b/harbour/contrib/hbqt/qtgui/hbqt_hbqplaintextedit.h @@ -59,7 +59,11 @@ #include #include +#include #include +#include +#include +#include #include #include #include @@ -168,7 +172,8 @@ private: bool isAliasCompleter; bool isCodeCompletionActive; bool isCompletionTipsActive; - + bool isInDrag; + protected: bool event( QEvent * event ); void resizeEvent( QResizeEvent * event ); @@ -179,6 +184,9 @@ protected: void focusInEvent( QFocusEvent * event ); void keyPressEvent( QKeyEvent * event ); void keyReleaseEvent( QKeyEvent * event ); + void dragEnterEvent( QDragEnterEvent * event ); + void dragMoveEvent( QDragMoveEvent * event ); + void dropEvent( QDropEvent * event ); public slots: QString hbTextAlias();