]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiClipboard.cpp
188eafbbcefe545b306b4bfa2e7a3c66c1b8a879
[lyx.git] / src / frontends / qt4 / GuiClipboard.cpp
1 // -*- C++ -*-
2 /**
3  * \file qt4/GuiClipboard.cpp
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 "support/debug.h"
19
20 #include <QApplication>
21 #include <QClipboard>
22 #include <QMimeData>
23 #include <QString>
24
25 #include "support/lstrings.h"
26
27 using namespace std;
28 using namespace lyx::support;
29
30 static char const * const mime_type = "application/x-lyx";
31
32 namespace lyx {
33
34 namespace frontend {
35
36 GuiClipboard::GuiClipboard()
37 {
38         connect(qApp->clipboard(), SIGNAL(dataChanged()),
39                 this, SLOT(on_dataChanged()));
40         // initialize clipboard status.
41         on_dataChanged();
42 }
43
44
45 string const GuiClipboard::getAsLyX() const
46 {
47         LYXERR(Debug::ACTION, "GuiClipboard::getAsLyX(): `");
48         // We don't convert encodings here since the encoding of the
49         // clipboard contents is specified in the data itself
50         QMimeData const * source =
51                 qApp->clipboard()->mimeData(QClipboard::Clipboard);
52         if (!source) {
53                 LYXERR(Debug::ACTION, "' (no QMimeData)");
54                 return string();
55         }
56         if (source->hasFormat(mime_type)) {
57                 // data from ourself or some other LyX instance
58                 QByteArray const ar = source->data(mime_type);
59                 string const s(ar.data(), ar.count());
60                 LYXERR(Debug::ACTION, s << "'");
61                 return s;
62         }
63         LYXERR(Debug::ACTION, "'");
64         return string();
65 }
66
67
68 docstring const GuiClipboard::getAsText() const
69 {
70         // text data from other applications
71         QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
72                                 .normalized(QString::NormalizationForm_C);
73         LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << fromqstr(str) << "'");
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         LYXERR(Debug::ACTION, "GuiClipboard::put(`" << lyx << "' `"
84                               << to_utf8(text) << "')");
85         // We don't convert the encoding of lyx since the encoding of the
86         // clipboard contents is specified in the data itself
87         QMimeData * data = new QMimeData;
88         if (!lyx.empty()) {
89                 QByteArray const qlyx(lyx.c_str(), lyx.size());
90                 data->setData(mime_type, qlyx);
91         }
92         // Don't test for text.empty() since we want to be able to clear the
93         // clipboard.
94         QString const qtext = toqstr(text);
95         data->setText(qtext);
96         qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
97 }
98
99
100 bool GuiClipboard::hasLyXContents() const
101 {
102         QMimeData const * const source =
103                 qApp->clipboard()->mimeData(QClipboard::Clipboard);
104         return source && source->hasFormat(mime_type);
105 }
106
107
108 bool GuiClipboard::isInternal() const
109 {
110         // ownsClipboard() is also true for stuff coming from dialogs, e.g.
111         // the preamble dialog
112         // FIXME: This does only work on X11, since ownsClipboard() is
113         // hardwired to return false on Windows and OS X.
114         return qApp->clipboard()->ownsClipboard() && hasLyXContents();
115 }
116
117
118 void GuiClipboard::on_dataChanged()
119 {
120         text_clipboard_empty_ = qApp->clipboard()->
121                 text(QClipboard::Clipboard).isEmpty();
122
123         has_lyx_contents_ = hasLyXContents();
124 }
125
126
127 bool GuiClipboard::empty() const
128 {
129         // We need to check both the plaintext and the LyX version of the
130         // clipboard. The plaintext version is empty if the LyX version
131         // contains only one inset, and the LyX version is empty if the
132         // clipboard does not come from LyX.
133         if (!text_clipboard_empty_)
134                 return false;
135         return !has_lyx_contents_;
136 }
137
138 } // namespace frontend
139 } // namespace lyx
140
141 #include "GuiClipboard_moc.cpp"