~ubuntu-branches/ubuntu/precise/kbibtex/precise

« back to all changes in this revision

Viewing changes to src/gui/bibtex/clipboard.cpp

  • Committer: Package Import Robot
  • Author(s): Michael Hanke
  • Date: 2011-07-18 09:29:48 UTC
  • mfrom: (1.1.6) (2.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20110718092948-ksxjmg7kdfamolmg
Tags: 0.3-1
* First upstream release for KDE4 (Closes: #634255). A number of search
  engines are still missing, in comparison to the 0.2 series.
* Bumped Standards-Version to 3.9.2, no changes necessary.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
*   Copyright (C) 2004-2010 by Thomas Fischer                             *
 
3
*   fischer@unix-ag.uni-kl.de                                             *
 
4
*                                                                         *
 
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.                                   *
 
9
*                                                                         *
 
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.                          *
 
14
*                                                                         *
 
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
***************************************************************************/
 
20
 
 
21
#include <QApplication>
 
22
#include <QClipboard>
 
23
#include <QBuffer>
 
24
#include <QMouseEvent>
 
25
#include <QDrag>
 
26
 
 
27
#include <KDebug>
 
28
 
 
29
#include <bibtexeditor.h>
 
30
#include <bibtexfilemodel.h>
 
31
#include <fileimporterbibtex.h>
 
32
#include <fileexporterbibtex.h>
 
33
#include <file.h>
 
34
#include "clipboard.h"
 
35
 
 
36
class Clipboard::ClipboardPrivate
 
37
{
 
38
private:
 
39
    Clipboard *parent;
 
40
 
 
41
public:
 
42
    BibTeXEditor *bibTeXEditor;
 
43
    QPoint previousPosition;
 
44
 
 
45
    ClipboardPrivate(BibTeXEditor *be, Clipboard *p)
 
46
            : parent(p), bibTeXEditor(be) {
 
47
        // TODO
 
48
    }
 
49
 
 
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()));
 
55
        }
 
56
 
 
57
        FileExporterBibTeX exporter;
 
58
        QBuffer buffer(bibTeXEditor);
 
59
        buffer.open(QBuffer::WriteOnly);
 
60
        exporter.save(&buffer, file);
 
61
        buffer.close();
 
62
 
 
63
        buffer.open(QBuffer::ReadOnly);
 
64
        QTextStream ts(&buffer);
 
65
        QString text = ts.readAll();
 
66
        buffer.close();
 
67
 
 
68
        return text;
 
69
    }
 
70
 
 
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);
 
75
 
 
76
        BibTeXFileModel *bibTeXModel = bibTeXEditor->bibTeXModel();
 
77
        QSortFilterProxyModel *sfpModel = bibTeXEditor->sortFilterProxyModel();
 
78
 
 
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
 
84
 
 
85
        // FIXME selection of new elements will not work if list is sorted!
 
86
 
 
87
        /// select newly inserted elements
 
88
        QItemSelectionModel *ism = bibTeXEditor->selectionModel();
 
89
        ism->clear();
 
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);
 
93
 
 
94
        /// clean up
 
95
        delete file;
 
96
        /// return true if at least one element was inserted
 
97
        return startRow <= endRow;
 
98
    }
 
99
};
 
100
 
 
101
Clipboard::Clipboard(BibTeXEditor *bibTeXEditor)
 
102
        : QObject(bibTeXEditor), d(new ClipboardPrivate(bibTeXEditor, this))
 
103
{
 
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);
 
109
}
 
110
 
 
111
void Clipboard::cut()
 
112
{
 
113
    copy();
 
114
    d->bibTeXEditor->selectionDelete();
 
115
}
 
116
 
 
117
void Clipboard::copy()
 
118
{
 
119
    QString text = d->selectionToText();
 
120
    QClipboard *clipboard = QApplication::clipboard();
 
121
    clipboard->setText(text);
 
122
}
 
123
 
 
124
void Clipboard::copyReferences()
 
125
{
 
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()));
 
130
        if (entry != NULL)
 
131
            references << entry->id();
 
132
    }
 
133
 
 
134
    if (!references.isEmpty()) {
 
135
        QClipboard *clipboard = QApplication::clipboard();
 
136
        clipboard->setText(references.join(","));
 
137
    }
 
138
}
 
139
 
 
140
void Clipboard::paste()
 
141
{
 
142
    QClipboard *clipboard = QApplication::clipboard();
 
143
    d->insertText(clipboard->text());
 
144
    d->bibTeXEditor->externalModification();
 
145
}
 
146
 
 
147
 
 
148
void Clipboard::editorMouseEvent(QMouseEvent *event)
 
149
{
 
150
    if (!(event->buttons()&Qt::LeftButton))
 
151
        return;
 
152
 
 
153
    if (d->previousPosition.x() > -1 && (event->pos() - d->previousPosition).manhattanLength() >= QApplication::startDragDistance()) {
 
154
        QString text = d->selectionToText();
 
155
 
 
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);
 
161
 
 
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");
 
165
    }
 
166
 
 
167
    d->previousPosition = event->pos();
 
168
}
 
169
 
 
170
void Clipboard::editorDragEnterEvent(QDragEnterEvent *event)
 
171
{
 
172
    if (event->mimeData()->hasText())
 
173
        event->acceptProposedAction();
 
174
}
 
175
 
 
176
void Clipboard::editorDragMoveEvent(QDragMoveEvent *event)
 
177
{
 
178
    if (event->mimeData()->hasText())
 
179
        event->acceptProposedAction();
 
180
}
 
181
 
 
182
void Clipboard::editorDropEvent(QDropEvent *event)
 
183
{
 
184
    QString text = event->mimeData()->text();
 
185
 
 
186
    if (!text.isEmpty()) {
 
187
        d->insertText(text);
 
188
        d->bibTeXEditor->externalModification();
 
189
    }
 
190
}