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.
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "GuiClipboard.h"
16 #include "qt_helpers.h"
20 #include <QApplication>
25 #include "support/lstrings.h"
26 using lyx::support::internalLineEnding;
27 using lyx::support::externalLineEnding;
35 char const * const mime_type = "application/x-lyx";
43 string const GuiClipboard::getAsLyX() const
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);
51 LYXERR(Debug::ACTION) << "' (no QMimeData)" << endl;
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 LYXERR(Debug::ACTION) << s << "'" << endl;
61 LYXERR(Debug::ACTION) << "'" << endl;
66 docstring const GuiClipboard::getAsText() const
68 // text data from other applications
69 QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
70 .normalized(QString::NormalizationForm_KC);
71 LYXERR(Debug::ACTION) << "GuiClipboard::getAsText(): `"
72 << fromqstr(str) << "'" << endl;
76 return internalLineEnding(qstring_to_ucs4(str));
80 void GuiClipboard::put(string const & lyx, docstring const & text)
82 LYXERR(Debug::ACTION) << "GuiClipboard::put(`" << lyx << "' `"
83 << to_utf8(text) << "')" << endl;
84 // We don't convert the encoding of lyx since the encoding of the
85 // clipboard contents is specified in the data itself
86 QMimeData * data = new QMimeData;
88 QByteArray const qlyx(lyx.c_str(), lyx.size());
89 data->setData(mime_type, qlyx);
91 // Don't test for text.empty() since we want to be able to clear the
93 QString const qtext = toqstr(text);
95 qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
99 bool GuiClipboard::hasLyXContents() const
101 QMimeData const * const source =
102 qApp->clipboard()->mimeData(QClipboard::Clipboard);
103 return source && source->hasFormat(mime_type);
107 bool GuiClipboard::isInternal() const
109 // ownsClipboard() is also true for stuff coming from dialogs, e.g.
110 // the preamble dialog
111 // FIXME: This does only work on X11, since ownsClipboard() is
112 // hardwired to return false on Windows and OS X.
113 return qApp->clipboard()->ownsClipboard() && hasLyXContents();
117 bool GuiClipboard::empty() const
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())
125 return !hasLyXContents();
128 } // namespace frontend