]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiClipboard.C
Fix unreported bug related to 3246 by Richard Heck:
[lyx.git] / src / frontends / qt4 / GuiClipboard.C
1 // -*- C++ -*-
2 /**
3  * \file qt4/GuiClipboard.C
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author John Levon
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiClipboard.h"
16 #include "qt_helpers.h"
17
18 #include "debug.h"
19
20 #include <QApplication>
21 #include <QClipboard>
22 #include <QMimeData>
23 #include <QString>
24
25 #include "support/lstrings.h"
26 using lyx::support::internalLineEnding;
27 using lyx::support::externalLineEnding;
28
29 using std::endl;
30 using std::string;
31
32
33 namespace {
34
35 char const * const mime_type = "application/x-lyx";
36
37 }
38
39
40 namespace lyx {
41 namespace frontend {
42
43 string const GuiClipboard::getAsLyX() const
44 {
45         lyxerr[Debug::ACTION] << "GuiClipboard::getAsLyX(): `";
46         // We don't convert encodings here since the encoding of the
47         // clipboard contents is specified in the data itself
48         QMimeData const * source =
49                 qApp->clipboard()->mimeData(QClipboard::Clipboard);
50         if (!source) {
51                 lyxerr[Debug::ACTION] << "' (no QMimeData)" << endl;
52                 return string();
53         }
54         if (source->hasFormat(mime_type)) {
55                 // data from ourself or some other LyX instance
56                 QByteArray const ar = source->data(mime_type);
57                 string const s(ar.data(), ar.count());
58                 if (lyxerr.debugging(Debug::ACTION))
59                         lyxerr[Debug::ACTION] << s << "'" << endl;
60                 return s;
61         }
62         lyxerr[Debug::ACTION] << "'" << endl;
63         return string();
64 }
65
66
67 docstring const GuiClipboard::getAsText() const
68 {
69         // text data from other applications
70         QString const str = qApp->clipboard()->text(QClipboard::Clipboard);
71         if (lyxerr.debugging(Debug::ACTION))
72                 lyxerr[Debug::ACTION] << "GuiClipboard::getAsText(): `"
73                                       << fromqstr(str) << "'" << endl;
74         if (str.isNull())
75                 return docstring();
76
77         return internalLineEnding(qstring_to_ucs4(str));
78 }
79
80
81 void GuiClipboard::put(string const & lyx, docstring const & text)
82 {
83         if (lyxerr.debugging(Debug::ACTION))
84                 lyxerr[Debug::ACTION] << "GuiClipboard::put(`" << lyx << "' `"
85                                       << to_utf8(text) << "')" << endl;
86         // We don't convert the encoding of lyx since the encoding of the
87         // clipboard contents is specified in the data itself
88         QMimeData * data = new QMimeData;
89         if (!lyx.empty()) {
90                 QByteArray const qlyx(lyx.c_str(), lyx.size());
91                 data->setData(mime_type, qlyx);
92         }
93         // Don't test for text.empty() since we want to be able to clear the
94         // clipboard.
95         QString const qtext = toqstr(text);
96         data->setText(qtext);
97         qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
98 }
99
100
101 bool GuiClipboard::hasLyXContents() const
102 {
103         QMimeData const * const source =
104                 qApp->clipboard()->mimeData(QClipboard::Clipboard);
105         return source && source->hasFormat(mime_type);
106 }
107
108
109 bool GuiClipboard::isInternal() const
110 {
111         // ownsClipboard() is also true for stuff coming from dialogs, e.g.
112         // the preamble dialog
113         return qApp->clipboard()->ownsClipboard() && hasLyXContents();
114 }
115
116
117 bool GuiClipboard::empty() const
118 {
119         // We need to check both the plaintext and the LyX version of the
120         // clipboard. The plaintext version is empty if the LyX version
121         // contains only one inset, and the LyX version is empry if the
122         // clipboard does not come from LyX.
123         if (!qApp->clipboard()->text(QClipboard::Clipboard).isEmpty())
124                 return false;
125         return !hasLyXContents();
126 }
127
128 } // namespace frontend
129 } // namespace lyx