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.
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"
27 using lyx::support::internalLineEnding;
28 using lyx::support::externalLineEnding;
33 static char const * const mime_type = "application/x-lyx";
39 GuiClipboard::GuiClipboard()
41 connect(qApp->clipboard(), SIGNAL(dataChanged()),
42 this, SLOT(on_dataChanged()));
43 // initialize clipboard status.
48 string const GuiClipboard::getAsLyX() const
50 LYXERR(Debug::ACTION, "GuiClipboard::getAsLyX(): `");
51 // We don't convert encodings here since the encoding of the
52 // clipboard contents is specified in the data itself
53 QMimeData const * source =
54 qApp->clipboard()->mimeData(QClipboard::Clipboard);
56 LYXERR(Debug::ACTION, "' (no QMimeData)");
59 if (source->hasFormat(mime_type)) {
60 // data from ourself or some other LyX instance
61 QByteArray const ar = source->data(mime_type);
62 string const s(ar.data(), ar.count());
63 LYXERR(Debug::ACTION, s << "'");
66 LYXERR(Debug::ACTION, "'");
71 docstring const GuiClipboard::getAsText() const
73 // text data from other applications
74 QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
75 .normalized(QString::NormalizationForm_C);
76 LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << fromqstr(str) << "'");
80 return internalLineEnding(qstring_to_ucs4(str));
84 void GuiClipboard::put(string const & lyx, docstring const & text)
86 LYXERR(Debug::ACTION, "GuiClipboard::put(`" << lyx << "' `"
87 << to_utf8(text) << "')");
88 // We don't convert the encoding of lyx since the encoding of the
89 // clipboard contents is specified in the data itself
90 QMimeData * data = new QMimeData;
92 QByteArray const qlyx(lyx.c_str(), lyx.size());
93 data->setData(mime_type, qlyx);
95 // Don't test for text.empty() since we want to be able to clear the
97 QString const qtext = toqstr(text);
99 qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
103 bool GuiClipboard::hasLyXContents() const
105 QMimeData const * const source =
106 qApp->clipboard()->mimeData(QClipboard::Clipboard);
107 return source && source->hasFormat(mime_type);
111 bool GuiClipboard::isInternal() const
113 // ownsClipboard() is also true for stuff coming from dialogs, e.g.
114 // the preamble dialog
115 // FIXME: This does only work on X11, since ownsClipboard() is
116 // hardwired to return false on Windows and OS X.
117 return qApp->clipboard()->ownsClipboard() && hasLyXContents();
121 void GuiClipboard::on_dataChanged()
123 text_clipboard_empty_ = qApp->clipboard()->
124 text(QClipboard::Clipboard).isEmpty();
126 has_lyx_contents_ = hasLyXContents();
130 bool GuiClipboard::empty() const
132 // We need to check both the plaintext and the LyX version of the
133 // clipboard. The plaintext version is empty if the LyX version
134 // contains only one inset, and the LyX version is empty if the
135 // clipboard does not come from LyX.
136 if (!text_clipboard_empty_)
138 return !has_lyx_contents_;
141 } // namespace frontend
144 #include "GuiClipboard_moc.cpp"