1
/***************************************************************************
2
* Copyright (C) 2004-2010 by Thomas Fischer *
3
* fischer@unix-ag.uni-kl.de *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19
***************************************************************************/
21
#include <QApplication>
24
#include <QMouseEvent>
29
#include <bibtexeditor.h>
30
#include <bibtexfilemodel.h>
31
#include <fileimporterbibtex.h>
32
#include <fileexporterbibtex.h>
34
#include "clipboard.h"
36
class Clipboard::ClipboardPrivate
42
BibTeXEditor *bibTeXEditor;
43
QPoint previousPosition;
45
ClipboardPrivate(BibTeXEditor *be, Clipboard *p)
46
: parent(p), bibTeXEditor(be) {
50
QString selectionToText() {
51
QModelIndexList mil = bibTeXEditor->selectionModel()->selectedRows();
52
File *file = new File();
53
for (QModelIndexList::ConstIterator it = mil.constBegin(); it != mil.constEnd(); ++it) {
54
file->append(bibTeXEditor->bibTeXModel()->element(bibTeXEditor->sortFilterProxyModel()->mapToSource(*it).row()));
57
FileExporterBibTeX exporter;
58
QBuffer buffer(bibTeXEditor);
59
buffer.open(QBuffer::WriteOnly);
60
exporter.save(&buffer, file);
63
buffer.open(QBuffer::ReadOnly);
64
QTextStream ts(&buffer);
65
QString text = ts.readAll();
71
bool insertText(const QString &text) {
72
/// use BibTeX importer to generate representation from plain text
73
FileImporterBibTeX importer;
74
File *file = importer.fromString(text);
76
BibTeXFileModel *bibTeXModel = bibTeXEditor->bibTeXModel();
77
QSortFilterProxyModel *sfpModel = bibTeXEditor->sortFilterProxyModel();
79
/// insert new elements one by one
80
int startRow = bibTeXModel->rowCount(); ///< memorize row where insertion started
81
for (File::Iterator it = file->begin(); it != file->end(); ++it)
82
bibTeXModel->insertRow(*it, bibTeXEditor->model()->rowCount());
83
int endRow = bibTeXModel->rowCount() - 1; ///< memorize row where insertion ended
85
// FIXME selection of new elements will not work if list is sorted!
87
/// select newly inserted elements
88
QItemSelectionModel *ism = bibTeXEditor->selectionModel();
90
/// highlight those rows in the editor which correspond to newly inserted elements
91
for (int i = startRow; i <= endRow;++i)
92
ism->select(sfpModel->mapFromSource(bibTeXModel->index(i, 0)), QItemSelectionModel::Rows | QItemSelectionModel::Select);
96
/// return true if at least one element was inserted
97
return startRow <= endRow;
101
Clipboard::Clipboard(BibTeXEditor *bibTeXEditor)
102
: QObject(bibTeXEditor), d(new ClipboardPrivate(bibTeXEditor, this))
104
connect(bibTeXEditor, SIGNAL(editorMouseEvent(QMouseEvent*)), this, SLOT(editorMouseEvent(QMouseEvent*)));
105
connect(bibTeXEditor, SIGNAL(editorDragEnterEvent(QDragEnterEvent*)), this, SLOT(editorDragEnterEvent(QDragEnterEvent*)));
106
connect(bibTeXEditor, SIGNAL(editorDragMoveEvent(QDragMoveEvent*)), this, SLOT(editorDragMoveEvent(QDragMoveEvent*)));
107
connect(bibTeXEditor, SIGNAL(editorDropEvent(QDropEvent*)), this, SLOT(editorDropEvent(QDropEvent*)));
108
bibTeXEditor->setAcceptDrops(true);
111
void Clipboard::cut()
114
d->bibTeXEditor->selectionDelete();
117
void Clipboard::copy()
119
QString text = d->selectionToText();
120
QClipboard *clipboard = QApplication::clipboard();
121
clipboard->setText(text);
124
void Clipboard::copyReferences()
126
QStringList references;
127
QModelIndexList mil = d->bibTeXEditor->selectionModel()->selectedRows();
128
for (QModelIndexList::ConstIterator it = mil.constBegin(); it != mil.constEnd(); ++it) {
129
Entry *entry = dynamic_cast<Entry*>(d->bibTeXEditor->bibTeXModel()->element(d->bibTeXEditor->sortFilterProxyModel()->mapToSource(*it).row()));
131
references << entry->id();
134
if (!references.isEmpty()) {
135
QClipboard *clipboard = QApplication::clipboard();
136
clipboard->setText(references.join(","));
140
void Clipboard::paste()
142
QClipboard *clipboard = QApplication::clipboard();
143
d->insertText(clipboard->text());
144
d->bibTeXEditor->externalModification();
148
void Clipboard::editorMouseEvent(QMouseEvent *event)
150
if (!(event->buttons()&Qt::LeftButton))
153
if (d->previousPosition.x() > -1 && (event->pos() - d->previousPosition).manhattanLength() >= QApplication::startDragDistance()) {
154
QString text = d->selectionToText();
156
QDrag *drag = new QDrag(d->bibTeXEditor);
157
QMimeData *mimeData = new QMimeData();
158
QByteArray data = text.toAscii();
159
mimeData->setData("text/plain", data);
160
drag->setMimeData(mimeData);
162
Qt::DropAction dropAction = drag->exec(Qt::CopyAction);
163
kDebug() << "dropAction = " << dropAction;
164
// Q_ASSERT_X(dropAction == Qt::CopyAction, "void Clipboard::editorMouseEvent(QMouseEvent *event)", "Drag'n'drop is not the expected copy operation");
167
d->previousPosition = event->pos();
170
void Clipboard::editorDragEnterEvent(QDragEnterEvent *event)
172
if (event->mimeData()->hasText())
173
event->acceptProposedAction();
176
void Clipboard::editorDragMoveEvent(QDragMoveEvent *event)
178
if (event->mimeData()->hasText())
179
event->acceptProposedAction();
182
void Clipboard::editorDropEvent(QDropEvent *event)
184
QString text = event->mimeData()->text();
186
if (!text.isEmpty()) {
188
d->bibTeXEditor->externalModification();