Skip to content

Commit

Permalink
corrected paste sometimes leaving selection on screen
Browse files Browse the repository at this point in the history
while the selection appeared on the screen, the selection was not
actually present and the document was correctly updated

the paste function was reimplemented with a clipboard mode argument,
which first clears the selection with a temporary text cursor, then the
clipboard text is inserted with the origin text cursor

for selection (middle-click) paste, the reimplemented paste function is
called with the selection clipboard mode
  • Loading branch information
thunder422 committed Mar 23, 2013
1 parent 32149da commit 0ef619b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 20 additions & 1 deletion editbox.cpp
Expand Up @@ -141,6 +141,25 @@ void EditBox::remove(void)
}


// function to paste text from the clipboard (and correctly update the screen)

void EditBox::paste(QClipboard::Mode mode)
{
QClipboard *clipboard = QApplication::clipboard();
QTextCursor cursor = textCursor();

// clear any selection on screen first with a temporary cursor
if (cursor.hasSelection())
{
QTextCursor tmpCursor = cursor;
tmpCursor.clearSelection();
setTextCursor(tmpCursor);
}

cursor.insertText(clipboard->text(mode));
}


// function to paste the current selection if supported into the program
//
// - return true on success, false if selection is not supported
Expand All @@ -154,7 +173,7 @@ bool EditBox::pasteSelection(const QPoint &pos)
{
setTextCursor(cursorForPosition(pos));
}
textCursor().insertText(clipboard->text(QClipboard::Selection));
paste(QClipboard::Selection);
return true;
}
return false;
Expand Down
2 changes: 2 additions & 0 deletions editbox.h
Expand Up @@ -25,6 +25,7 @@
#ifndef EDITBOX_H
#define EDITBOX_H

#include <QClipboard>
#include <QPlainTextEdit>
#include <QTextBlock>

Expand All @@ -37,6 +38,7 @@ class EditBox : public QPlainTextEdit
public:
explicit EditBox(QWidget *parent = 0);
void remove(void);
void paste(QClipboard::Mode mode = QClipboard::Clipboard);
void selectAll(void);
void resetModified(void);
void captureModifiedLine(void);
Expand Down

0 comments on commit 0ef619b

Please sign in to comment.