Commit 6defb83d authored by Francois Ferrand's avatar Francois Ferrand Committed by Leandro Melo

TextEditor: menu for pasting from clipboard history.

Add new action to show a menu of recent clipboard history. When an item
is selected, the item is pasted.

Change-Id: Id7e1a90730404fb63762bf1f20678484ec34cd2b
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
Reviewed-by: default avatarLeandro Melo <leandro.melo@nokia.com>
parent 80180ba7
......@@ -55,6 +55,7 @@
#include "texteditorsettings.h"
#include "texteditoroverlay.h"
#include "circularclipboard.h"
#include "circularclipboardassist.h"
#include <aggregation/aggregate.h>
#include <coreplugin/actionmanager/actionmanager.h>
......@@ -203,14 +204,6 @@ static void convertToPlainText(QString &txt)
}
}
static bool isModifierKey(int key)
{
return key == Qt::Key_Shift
|| key == Qt::Key_Control
|| key == Qt::Key_Alt
|| key == Qt::Key_Meta;
}
static const char kTextBlockMimeType[] = "application/vnd.nokia.qtcreator.blocktext";
static const char kVerticalTextBlockMimeType[] = "application/vnd.nokia.qtcreator.vblocktext";
......@@ -1554,11 +1547,6 @@ void BaseTextEditorWidget::keyPressEvent(QKeyEvent *e)
d->m_moveLineUndoHack = false;
d->clearVisibleFoldedBlock();
if (d->m_isCirculatingClipboard
&& !isModifierKey(e->key())) {
d->m_isCirculatingClipboard = false;
}
if (e->key() == Qt::Key_Alt
&& d->m_behaviorSettings.m_keyboardTooltips) {
d->m_maybeFakeTooltipEvent = true;
......@@ -2496,7 +2484,6 @@ BaseTextEditorWidgetPrivate::BaseTextEditorWidgetPrivate()
m_requestMarkEnabled(true),
m_lineSeparatorsAllowed(false),
m_maybeFakeTooltipEvent(false),
m_isCirculatingClipboard(false),
m_visibleWrapColumn(0),
m_linkPressed(false),
m_delayedUpdateTimer(0),
......@@ -2511,7 +2498,8 @@ BaseTextEditorWidgetPrivate::BaseTextEditorWidgetPrivate()
m_assistRelevantContentAdded(false),
m_cursorBlockNumber(-1),
m_autoCompleter(new AutoCompleter),
m_indenter(new Indenter)
m_indenter(new Indenter),
m_clipboardAssistProvider(new Internal::ClipboardAssistProvider)
{
}
......@@ -5821,26 +5809,19 @@ void BaseTextEditorWidget::paste()
void BaseTextEditorWidget::circularPaste()
{
const QMimeData *mimeData = CircularClipboard::instance()->next();
if (!mimeData)
return;
QTextCursor cursor = textCursor();
if (!d->m_isCirculatingClipboard) {
cursor.beginEditBlock();
d->m_isCirculatingClipboard = true;
} else {
cursor.joinPreviousEditBlock();
CircularClipboard *circularClipBoard = CircularClipboard::instance();
if (const QMimeData *clipboardData = QApplication::clipboard()->mimeData()) {
circularClipBoard->collect(duplicateMimeData(clipboardData));
circularClipBoard->toLastCollect();
}
const int selectionStart = qMin(cursor.position(), cursor.anchor());
insertFromMimeData(mimeData);
cursor.setPosition(selectionStart, QTextCursor::KeepAnchor);
cursor.endEditBlock();
setTextCursor(flippedCursor(cursor));
if (circularClipBoard->size() > 1)
return invokeAssist(QuickFix, d->m_clipboardAssistProvider.data());
// We want to latest pasted content to replace the system's current clipboard.
QPlainTextEdit::copy();
if (const QMimeData *mimeData = circularClipBoard->next().data()) {
QApplication::clipboard()->setMimeData(duplicateMimeData(mimeData));
paste();
}
}
void BaseTextEditorWidget::switchUtf8bom()
......
......@@ -250,6 +250,7 @@ public:
virtual IAssistInterface *createAssistInterface(AssistKind assistKind,
AssistReason assistReason) const;
QMimeData *duplicateMimeData(const QMimeData *source) const;
public slots:
void setDisplayName(const QString &title);
......@@ -347,7 +348,6 @@ protected:
QMimeData *createMimeDataFromSelection() const;
bool canInsertFromMimeData(const QMimeData *source) const;
void insertFromMimeData(const QMimeData *source);
QMimeData *duplicateMimeData(const QMimeData *source) const;
static QString msgTextTooLarge(quint64 size);
......
......@@ -54,6 +54,7 @@ class CodeAssistant;
namespace Internal {
class TextEditorOverlay;
class ClipboardAssistProvider;
class TEXTEDITOR_EXPORT BaseTextBlockSelection
{
......@@ -244,7 +245,6 @@ public:
uint autoParenthesisOverwriteBackup : 1;
uint surroundWithEnabledOverwriteBackup : 1;
uint m_maybeFakeTooltipEvent : 1;
uint m_isCirculatingClipboard: 1;
int m_visibleWrapColumn;
QTextCharFormat m_linkFormat;
......@@ -298,6 +298,8 @@ public:
QScopedPointer<AutoCompleter> m_autoCompleter;
QScopedPointer<Indenter> m_indenter;
QScopedPointer<Internal::ClipboardAssistProvider> m_clipboardAssistProvider;
};
} // namespace Internal
......
......@@ -42,7 +42,6 @@ CircularClipboard::CircularClipboard()
CircularClipboard::~CircularClipboard()
{
qDeleteAll(m_items);
}
CircularClipboard *CircularClipboard::instance()
......@@ -52,28 +51,29 @@ CircularClipboard *CircularClipboard::instance()
}
void CircularClipboard::collect(const QMimeData *mimeData)
{
collect(QSharedPointer<const QMimeData>(mimeData));
}
void CircularClipboard::collect(const QSharedPointer<const QMimeData> &mimeData)
{
//Avoid duplicates
const QString text = mimeData->text();
for (QList<const QMimeData *>::iterator i = m_items.begin(); i != m_items.end(); ++i) {
for (QList< QSharedPointer<const QMimeData> >::iterator i = m_items.begin(); i != m_items.end(); ++i) {
if (mimeData == *i || text == (*i)->text()) {
if (mimeData != *i)
delete *i;
m_items.erase(i);
break;
}
}
if (m_items.size() > kMaxSize) {
delete m_items.last();
if (m_items.size() >= kMaxSize)
m_items.removeLast();
}
m_items.prepend(mimeData);
}
const QMimeData *CircularClipboard::next() const
QSharedPointer<const QMimeData> CircularClipboard::next() const
{
if (m_items.isEmpty())
return 0;
return QSharedPointer<const QMimeData>();
if (m_current == m_items.length() - 1)
m_current = 0;
......@@ -87,3 +87,8 @@ void CircularClipboard::toLastCollect()
{
m_current = -1;
}
int CircularClipboard::size() const
{
return m_items.size();
}
......@@ -35,6 +35,7 @@
#include <QList>
#include <QMimeData>
#include <QSharedPointer>
namespace TextEditor {
namespace Internal {
......@@ -45,8 +46,10 @@ public:
static CircularClipboard *instance();
void collect(const QMimeData *mimeData);
const QMimeData *next() const;
void collect(const QSharedPointer<const QMimeData> &mimeData);
QSharedPointer<const QMimeData> next() const;
void toLastCollect();
int size() const;
private:
CircularClipboard();
......@@ -54,7 +57,7 @@ private:
CircularClipboard &operator=(const CircularClipboard &);
mutable int m_current;
QList<const QMimeData *> m_items;
QList< QSharedPointer<const QMimeData> > m_items;
};
} // namespace Internal
......
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "circularclipboardassist.h"
#include "codeassist/iassistprovider.h"
#include "codeassist/iassistinterface.h"
#include "codeassist/iassistprocessor.h"
#include "codeassist/iassistproposal.h"
#include "codeassist/basicproposalitem.h"
#include "codeassist/basicproposalitemlistmodel.h"
#include "codeassist/genericproposal.h"
#include "basetexteditor.h"
#include "circularclipboard.h"
#include <coreplugin/coreconstants.h>
#include <QApplication>
#include <QClipboard>
using namespace TextEditor;
using namespace TextEditor::Internal;
namespace TextEditor {
namespace Internal {
class ClipboardProposalItem: public BasicProposalItem {
public:
enum { maxLen = 80 };
ClipboardProposalItem(QSharedPointer<const QMimeData> mimeData): m_mimeData(mimeData)
{
QString text = mimeData->text().simplified();
if (text.length() > maxLen) {
text.truncate(maxLen);
text.append(QLatin1String("..."));
}
setText(text);
}
virtual void apply(BaseTextEditor *editor, int /*basePosition*/) const
{
BaseTextEditorWidget *editwidget = editor->editorWidget();
//Move to last in circular clipboard
if (CircularClipboard * clipboard = CircularClipboard::instance()) {
clipboard->collect(m_mimeData);
clipboard->toLastCollect();
}
//Copy the selected item
QApplication::clipboard()->setMimeData(editwidget->duplicateMimeData(m_mimeData.data()));
//Paste
editwidget->paste();
}
private:
QSharedPointer<const QMimeData> m_mimeData;
};
class ClipboardAssistProcessor: public IAssistProcessor
{
public:
IAssistProposal *perform(const IAssistInterface *interface)
{
if (!interface)
return 0;
QScopedPointer<const IAssistInterface> assistInterface(interface);
QIcon icon = QIcon::fromTheme(QLatin1String("edit-paste"), QIcon(QLatin1String(Core::Constants::ICON_PASTE))).pixmap(16);
CircularClipboard * clipboard = CircularClipboard::instance();
QList<BasicProposalItem *> items;
for (int i = 0; i < clipboard->size(); ++i) {
QSharedPointer<const QMimeData> data = clipboard->next();
BasicProposalItem *item = new ClipboardProposalItem(data);
item->setIcon(icon);
item->setOrder(clipboard->size() - 1 - i);
items.append(item);
}
return new GenericProposal(interface->position(), new BasicProposalItemListModel(items));
}
};
} // namespace Internal
} // namespace TextEditor
bool ClipboardAssistProvider::supportsEditor(const Core::Id &/*editorId*/) const
{
return true;
}
IAssistProcessor *ClipboardAssistProvider::createProcessor() const
{
return new ClipboardAssistProcessor;
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#ifndef CIRCULARCLIPBOARDASSIST_H
#define CIRCULARCLIPBOARDASSIST_H
#include "codeassist/iassistprovider.h"
namespace TextEditor {
namespace Internal {
class ClipboardAssistProvider: public IAssistProvider
{
public:
virtual bool supportsEditor(const Core::Id &editorId) const;
virtual IAssistProcessor *createProcessor() const;
};
} // namespace Internal
} // namespace TextEditor
#endif // CIRCULARCLIPBOARDASSIST_H
......@@ -115,6 +115,7 @@ SOURCES += texteditorplugin.cpp \
codestylepool.cpp \
codestyleeditor.cpp \
circularclipboard.cpp \
circularclipboardassist.cpp \
itextmark.cpp
HEADERS += texteditorplugin.h \
......@@ -233,6 +234,7 @@ HEADERS += texteditorplugin.h \
codestyleeditor.h \
basefilefind_p.h \
circularclipboard.h \
circularclipboardassist.h \
itextmark.h
FORMS += \
......
......@@ -50,6 +50,8 @@ QtcPlugin {
"behaviorsettingswidget.ui",
"circularclipboard.cpp",
"circularclipboard.h",
"circularclipboardassist.cpp",
"circularclipboardassist.h",
"codecselector.cpp",
"codecselector.h",
"codestyleeditor.cpp",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment